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 (
Price(..), Price(..),
fromDouble, fromDouble,
toDouble, toDouble,
decompose decompose,
compose
) where ) where
import Data.Int import Data.Int
@ -43,3 +44,6 @@ fromDouble d = Price { priceQuants = truncate (d * fromIntegral mega) }
decompose :: Price -> (Int64, Int32) decompose :: Price -> (Int64, Int32)
decompose Price{priceQuants = p} = (p `div` mega, (fromInteger . toInteger) $ p `mod` mega) 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 {
security :: !T.Text, security :: !T.Text,
datatype :: !DataType, datatype :: !DataType,
timestamp :: !UTCTime, timestamp :: !UTCTime,
value :: !Decimal, value :: !Price,
volume :: !Integer volume :: !Integer
} deriving (Show, Eq, Generic) } deriving (Show, Eq, Generic)
@ -106,8 +106,8 @@ serializeTickBody tick = toLazyByteString $ mconcat [
putWord64le $ fromIntegral . toSeconds' . timestamp $ tick, putWord64le $ fromIntegral . toSeconds' . timestamp $ tick,
putWord32le $ fromIntegral . fracSeconds . timestamp $ tick, putWord32le $ fromIntegral . fracSeconds . timestamp $ tick,
putWord32le $ fromIntegral . fromEnum . datatype $ tick, putWord32le $ fromIntegral . fromEnum . datatype $ tick,
putWord64le $ truncate . value $ tick, putWord64le $ fromInteger . toInteger . fst . decompose . value $ tick,
putWord32le $ truncate . (*. 1000000000) . fractionalPart $ value tick, putWord32le $ (* 1000) . fromInteger . toInteger . snd . decompose . value $ tick,
putWord32le $ fromIntegral $ volume tick ] putWord32le $ fromIntegral $ volume tick ]
where where
fractionalPart :: (RealFrac a) => a -> a fractionalPart :: (RealFrac a) => a -> a
@ -132,7 +132,7 @@ parseTick = do
return Tick { security = "", return Tick { security = "",
datatype = dt, datatype = dt,
timestamp = makeTimestamp tsec tusec, timestamp = makeTimestamp tsec tusec,
value = makeValue intpart nanopart, value = compose (intpart, nanopart `div` 1000),
volume = volume } volume = volume }
where where
makeTimestamp :: Word64 -> Word32 -> UTCTime makeTimestamp :: Word64 -> Word32 -> UTCTime

4
test/ArbitraryInstances.hs

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

Loading…
Cancel
Save