module HQuant.Technical where import Data.List --import Data.Time.Calendar (Day) import HQuant.History import Data.Maybe --import Control.Arrow --import Control.Monad --import Data.Graph.Inductive.Query.Monad -- http://www.cs.ou.edu/cs1323h/handouts/suppliedSoftware/NumericUtilities.lhs -- standardDeviation :: Doubleing num => [num] -> num -- standardDeviation samples = -- (sqrt . average . map deviationSquared) samples -- where -- mu = average samples -- deviationSquared x = abs(x - mu)^2 -- bollingerBand h l c = -- (up, mid, low) -- where -- tp = typicalPrice h l c -- mid = smaDouble 20 tp -- up = mid + standardDeviation -- low = mid + standardDeviation --pday :: Period -> Day --pday (PDay d) = d --typicalPrice h l c = (h+l+c)/3 assertForwardHistory :: [HistoryLine] -> [HistoryLine] assertForwardHistory hs = hs --assertForwardHistory hs = if --assert False x = error "assertion failed!" simpleMovingAverage :: Int -> [HistoryLine] -> [PlotPoint] simpleMovingAverage nperiods hs = reverse $ catMaybes $ mapPlot (hsSma nperiods) $ reverse hs hsSma :: Int -> [HistoryLine] -> Maybe PlotPoint hsSma n hs = if length hs >= n then Just $ averagePlotPoints n (hClosePlotN n hs) else Nothing historyPeriod :: HistoryLine -> Period historyPeriod h = PDay (HQuant.History.hdate h) mapPlot :: ([HistoryLine] -> Maybe PlotPoint) -> [HistoryLine] -> [Maybe PlotPoint] mapPlot _ [] = [] mapPlot f hs = f hs : mapPlot f (tail hs) mapPlot2 :: ([HistoryLine] -> Maybe PlotPoint) -> [HistoryLine] -> [Maybe PlotPoint] mapPlot2 _ [] = [] mapPlot2 f hs = f hs : mapPlot2 f (tail hs) foldPlot :: (Double -> Double -> Double) -> Double -> [PlotPoint] -> Double foldPlot _ z [] = z -- if the list is empty, the result is the initial value foldPlot f z (x:xs) = foldPlot f (f z (plotPrice x)) xs -- if not, we recurse immediately, making the new initial value the result averagePlotPoints :: Int -> [PlotPoint] -> PlotPoint averagePlotPoints n fs = PlotPoint (plotPeriod $ head fs) ((*) (1.0/(fromIntegral n)) ( foldPlot (+) 0 fs) ) hClosePlotN :: Int -> [HistoryLine] -> [PlotPoint] hClosePlotN n hs = map (\h -> PlotPoint (historyPeriod h) (close h)) $ take n hs