Skip to content
Browse files

Give instance declarations for Z.TimeStamp: Num, Real, Enum, Integral

  • Loading branch information...
1 parent 9187c2e commit fb17642c62f64cc1c610305797a64e7c3d0d104b @alang9 committed Oct 21, 2011
Showing with 28 additions and 1 deletion.
  1. +28 −1 Data/ZoomCache/Gnuplot.hs
View
29 Data/ZoomCache/Gnuplot.hs
@@ -19,6 +19,7 @@ module Data.ZoomCache.Gnuplot
, mavgPlot
) where
+import Control.Arrow ((***))
import Data.Maybe
import qualified Data.Iteratee as I
@@ -78,8 +79,33 @@ push a (AvgQueue m [] ys l)
queueAvg :: AvgQueue -> Double
queueAvg (AvgQueue m xs ys l) = realToFrac (sum xs + sum ys) / realToFrac l
+avgEmptyQueue :: Int -> AvgQueue
avgEmptyQueue m = AvgQueue m [] [] 0
+instance Num Z.TimeStamp where
+ a + b = Z.TS $ Z.unTS a + Z.unTS b
+ a - b = Z.TS $ Z.unTS a - Z.unTS b
+ a * b = Z.TS $ Z.unTS a * Z.unTS b
+ negate a = Z.TS . negate $ Z.unTS a
+ abs a = Z.TS . abs $ Z.unTS a
+ signum a = Z.TS . signum $ Z.unTS a
+ fromInteger i = Z.TS $ fromInteger i
+
+instance Real Z.TimeStamp where
+ toRational a = toRational $ Z.unTS a
+
+instance Enum Z.TimeStamp where
+ toEnum i = Z.TS $ toEnum i
+ fromEnum a = fromEnum $ Z.unTS a
+
+instance Integral Z.TimeStamp where
+ quotRem a b = Z.TS *** Z.TS $ Z.unTS a `quotRem` Z.unTS b
+ toInteger a = toInteger $ Z.unTS a
+
+totalTime :: [Z.Summary a] -> Maybe Z.TimeStamp
+totalTime [] = Nothing
+totalTime l = Just $ Z.summaryCloseTime (last l) - Z.summaryOpenTime (head l)
+
mavgPlot :: forall a. [Z.Stream a]
-> Int -> Plot.T Z.TimeStamp Double
mavgPlot streams lvl = Plot.list Graph.lines . snd $
@@ -94,6 +120,7 @@ mavgPlot streams lvl = Plot.list Graph.lines . snd $
(timeStamp, avg) = getSummaryAvgs s
newQueue = push avg queue
+
----------------------------------------------------------------------
-- plotSummaries :: Atom.C a => Int -> [Z.Stream a] -> [Attribute] -> IO ()
@@ -129,7 +156,7 @@ getSummaryCandleVals s = ( Z.summaryCloseTime s
))
getSummaryAvgs :: Z.Summary a -> (Z.TimeStamp, Double)
-getSummaryAvgs s = ( Z.summaryCloseTime s
+getSummaryAvgs s = ( ((Z.summaryCloseTime s) + (Z.summaryOpenTime s)) `div` 2
, Z.summaryAvg s
)

0 comments on commit fb17642

Please sign in to comment.
Something went wrong with that request. Please try again.