Browse Source

Changed tick representation

master
Denis Tereshkin 8 years ago
parent
commit
501003a2cf
  1. 6
      src/ATrade/Price.hs
  2. 8
      src/ATrade/Types.hs
  3. 4
      test/ArbitraryInstances.hs

6
src/ATrade/Price.hs

@ -4,7 +4,8 @@ module ATrade.Price ( @@ -4,7 +4,8 @@ module ATrade.Price (
Price(..),
fromDouble,
toDouble,
decompose
decompose,
compose
) where
import Data.Int
@ -43,3 +44,6 @@ fromDouble d = Price { priceQuants = truncate (d * fromIntegral mega) } @@ -43,3 +44,6 @@ fromDouble d = Price { priceQuants = truncate (d * fromIntegral mega) }
decompose :: Price -> (Int64, Int32)
decompose Price{priceQuants = p} = (p `div` mega, (fromInteger . toInteger) $ p `mod` mega)
compose :: (Int64, Int32) -> Price
compose (int, frac) = Price { priceQuants = int * mega + (fromInteger . toInteger) frac }

8
src/ATrade/Types.hs

@ -93,7 +93,7 @@ data Tick = Tick { @@ -93,7 +93,7 @@ data Tick = Tick {
security :: !T.Text,
datatype :: !DataType,
timestamp :: !UTCTime,
value :: !Decimal,
value :: !Price,
volume :: !Integer
} deriving (Show, Eq, Generic)
@ -106,8 +106,8 @@ serializeTickBody tick = toLazyByteString $ mconcat [ @@ -106,8 +106,8 @@ serializeTickBody tick = toLazyByteString $ mconcat [
putWord64le $ fromIntegral . toSeconds' . timestamp $ tick,
putWord32le $ fromIntegral . fracSeconds . timestamp $ tick,
putWord32le $ fromIntegral . fromEnum . datatype $ tick,
putWord64le $ truncate . value $ tick,
putWord32le $ truncate . (*. 1000000000) . fractionalPart $ value tick,
putWord64le $ fromInteger . toInteger . fst . decompose . value $ tick,
putWord32le $ (* 1000) . fromInteger . toInteger . snd . decompose . value $ tick,
putWord32le $ fromIntegral $ volume tick ]
where
fractionalPart :: (RealFrac a) => a -> a
@ -132,7 +132,7 @@ parseTick = do @@ -132,7 +132,7 @@ parseTick = do
return Tick { security = "",
datatype = dt,
timestamp = makeTimestamp tsec tusec,
value = makeValue intpart nanopart,
value = compose (intpart, nanopart `div` 1000),
volume = volume }
where
makeTimestamp :: Word64 -> Word32 -> UTCTime

4
test/ArbitraryInstances.hs

@ -20,14 +20,14 @@ import Data.Scientific @@ -20,14 +20,14 @@ import Data.Scientific
import Data.Time.Clock
import Data.Time.Calendar
notTooBig x = abs x < 1000000000000
notTooBig x = abs x < 100000000
instance Arbitrary Tick where
arbitrary = Tick <$>
arbitrary <*>
arbitrary <*>
arbitraryTimestamp <*>
(roundTo 9 <$> (arbitrary `suchThat` notTooBig)) <*>
arbitrary <*>
arbitrary
where
arbitraryTimestamp = do

Loading…
Cancel
Save