From 501003a2cf15a82f6eea5bf596af561bc1627f12 Mon Sep 17 00:00:00 2001 From: Denis Tereshkin Date: Sun, 15 Oct 2017 21:21:05 +0700 Subject: [PATCH] Changed tick representation --- src/ATrade/Price.hs | 6 +++++- src/ATrade/Types.hs | 8 ++++---- test/ArbitraryInstances.hs | 4 ++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/ATrade/Price.hs b/src/ATrade/Price.hs index a44515a..b03b686 100644 --- a/src/ATrade/Price.hs +++ b/src/ATrade/Price.hs @@ -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) } 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 } + diff --git a/src/ATrade/Types.hs b/src/ATrade/Types.hs index 8f07f37..6c9a0dc 100644 --- a/src/ATrade/Types.hs +++ b/src/ATrade/Types.hs @@ -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 [ 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 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 diff --git a/test/ArbitraryInstances.hs b/test/ArbitraryInstances.hs index 8af7684..16ee1ed 100644 --- a/test/ArbitraryInstances.hs +++ b/test/ArbitraryInstances.hs @@ -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