module HQuant.Chart where import Data.List --import Data.Time.Calendar (Day) import HQuant.History --import Data.Maybe import Graphics.Rendering.Chart import Graphics.Rendering.Chart.Gtk import Data.Colour import Data.Colour.Names import Data.Colour.SRGB import Data.Accessor import Data.Time.LocalTime lwidth :: Double lwidth = 0.4 lineStyle :: AlphaColour Double -> CairoLineStyle lineStyle color = line_width ^= 3 * lwidth $ line_color ^= color $ defaultPlotLines ^. plot_lines_style limitLineStyle :: Colour Double -> CairoLineStyle limitLineStyle color = line_width ^= lwidth $ line_color ^= opaque color -- $ line_dashes ^= [5,10] $ defaultPlotLines ^. plot_lines_style plotLines :: [(x, y)] -> String-> AlphaColour Double-> PlotLines x y plotLines p title color = plot_lines_style ^= lineStyle color $ plot_lines_values ^= [[ (d, v) | (d,v) <- p]] $ plot_lines_title ^= title $ defaultPlotLines --limits :: (Ord a, Ord a2) =>t -> [Either (Plot a1 (a -> a -> a)) (Plot a3 (a2 -> a2 -> a2))] --limits prices = [ Left $ hlinePlot "min/max" (limitLineStyle blue) min, -- Left $ hlinePlot "" (limitLineStyle blue) max, -- Right $ hlinePlot "min/max" (limitLineStyle green) min, -- Right $ hlinePlot "" (limitLineStyle green) max ] bg :: AlphaColour Double bg = opaque white -- $ sRGB 0 0 0.25 fg :: AlphaColour Double fg = opaque blue fg1 :: AlphaColour Double fg1 = opaque red-- $ sRGB 0.0 0.0 0.15 layoutStudy :: (PlotValue x, PlotValue y) => String -> [(x, y)] -> [(x, y)] -> Layout1 x y layoutStudy title prices study = layout1_title ^=title $ layout1_background ^= solidFillStyle bg $ updateAllAxesStyles (axis_grid_style ^= solidLine 1 fg1) $ layout1_left_axis ^: laxis_override ^= axisGridHide $ layout1_right_axis ^: laxis_override ^= axisGridHide $ layout1_bottom_axis ^: laxis_override ^= axisGridHide $ layout1_plots ^= ([ Left (toPlot $plotLines prices "Price" fg), Left (toPlot $plotLines study title fg1)] ) $ layout1_grid_last ^= False $ setLayout1Foreground fg $ defaultLayout1 layoutBuySells :: String -> [(LocalTime, Double)] -> [(LocalTime, Double)] -> [PlotPoint]-> [PlotPoint] -> Layout1 LocalTime Double layoutBuySells title prices study buys sells = layout1_title ^="SPY" $ layout1_background ^= solidFillStyle bg $ updateAllAxesStyles (axis_grid_style ^= solidLine 1 fg1) $ layout1_left_axis ^: laxis_override ^= axisGridHide $ layout1_right_axis ^: laxis_override ^= axisGridHide $ layout1_bottom_axis ^: laxis_override ^= axisGridHide $ layout1_plots ^= ([ Left (toPlot $plotLines prices "Price" fg), Left (toPlot $plotLines study title fg1)] ++ map (\p -> Left $ hlinePlot "b" (limitLineStyle green) (plotPrice p)) buys ++ map (\p -> Left $ vlinePlot "b" (limitLineStyle green) (fst$plotLocalTime p)) buys ++ map (\p -> Left $ hlinePlot "s" (limitLineStyle red) (plotPrice p)) sells ++ map (\p -> Left $ vlinePlot "s" (limitLineStyle red) (fst$plotLocalTime p)) sells )-- ++ limits prices) $ layout1_grid_last ^= False $ setLayout1Foreground fg $ defaultLayout1 chartStudy :: String -> [(LocalTime,Double)] -> [(LocalTime,Double)] -> Renderable () chartStudy title prices study = toRenderable $ layoutStudy title prices study chartBuySells :: String -> [(LocalTime,Double)] -> [(LocalTime,Double)] -> [PlotPoint]-> [PlotPoint] -> Renderable () chartBuySells title prices study buys sells = toRenderable $ layoutBuySells title prices study buys sells priceChartStudy :: String -> [(LocalTime,Double)] -> [(LocalTime,Double)] -> IO () priceChartStudy title pr st = renderableToWindow (chartStudy title pr st ) 640 480 priceChartBuySells :: String -> [(LocalTime,Double)] -> [(LocalTime,Double)] -> [PlotPoint]-> [PlotPoint] -> IO () priceChartBuySells title pr st bu se = renderableToWindow (chartBuySells title pr st bu se ) 640 480