Compare commits

..

46 Commits

Author SHA1 Message Date
Denis Tereshkin 1a2703fbb4 Fix build 5 months ago
Denis Tereshkin 45251d8968 Junction: print logs to stderr 5 months ago
Denis Tereshkin 5d7434c631 Junction: per-component configurable log levels 11 months ago
Denis Tereshkin 4d6cc07ee3 Libatrade version bump 11 months ago
Denis Tereshkin fefd0974d0 Update for new libatrade 11 months ago
Denis Tereshkin e873754aae Bump resolver 2 years ago
Denis Tereshkin 4d596709c1 Junction: log errors from BrokerClient 2 years ago
Denis Tereshkin 51d2bd199f Update resolver to lts-20.18 3 years ago
Denis Tereshkin 3e78fa99dd BarAggregator: update bar timestamp on close 3 years ago
Denis Tereshkin bcb7d91805 QuoteThread: Aggregate bars from ticks 3 years ago
Denis Tereshkin dc6702765f Test: build 3 years ago
Denis Tereshkin 1284aa8603 QuoteThread: log when started 3 years ago
Denis Tereshkin 37e8eecc9f Bump libatrade version 4 years ago
Denis Tereshkin d0e130e121 exitAtLimit: round exit price 4 years ago
Denis Tereshkin 764e02dd43 Backtest driver fixed 4 years ago
Denis Tereshkin 467dcb066e positions: new sizer 4 years ago
Denis Tereshkin acfaa3d51c junction: RemoteControl: replace Rep socket with Router 4 years ago
Denis Tereshkin d95a08e685 junction: RemoteControl: handle SET_STATE command 4 years ago
Denis Tereshkin b06b02a092 junction: RemoteControl: fix incoming message polling 4 years ago
Denis Tereshkin c4ffc84aef junction: reduce RemoteControl polling timeout 4 years ago
Denis Tereshkin b68874a1ff junction: RemoteControl: handle GET_STATE command 4 years ago
Denis Tereshkin 5924e3ef70 junction: RemoteControl: handle RELOAD_CONFIG command 4 years ago
Denis Tereshkin 2d146d8a16 junction: remote control: implement start robot command handler 4 years ago
Denis Tereshkin 035aa0da36 junction: format RobotDriverThread 4 years ago
Denis Tereshkin 5cee717b81 junction: some exception safety 4 years ago
Denis Tereshkin ac4785e5a0 junction: startRobot refactoring 4 years ago
Denis Tereshkin 1cf0c6d4ea junction: startRobot refactoring 4 years ago
Denis Tereshkin 4ac71e148c Junction: handle robot stop command 4 years ago
Denis Tereshkin a245a01a66 QuoteStream unsubscription 4 years ago
Denis Tereshkin 9ab4a9e9fa RemoteControl: implement basic request-response loop 4 years ago
Denis Tereshkin 748c1ded89 junction: locked logging 4 years ago
Denis Tereshkin 9d016b0d44 positions: fix calculateSizeFixedCashWith: correct sizing 4 years ago
Denis Tereshkin 1a1f81a351 junction: ignore zero ticks 4 years ago
Denis Tereshkin 595a194c05 junction: configurable broker identity 4 years ago
Denis Tereshkin 6eebfb54e6 junction driver: more logging 4 years ago
Denis Tereshkin 2123d63f66 Version bump 4 years ago
Denis Tereshkin 4271c12845 Various QoL enhancements 4 years ago
Denis Tereshkin 2e9b40c2fa Positions: two default sizers 4 years ago
Denis Tereshkin 91d8b7d2a8 Positions API: support for sizers 4 years ago
Denis Tereshkin 3cca37812f MonadRobot: new method: getTickerInfo 4 years ago
Denis Tereshkin 70b043ac3d Merge branch 'junction' 4 years ago
Denis Tereshkin 3d2c40e158 Revert "Real driver refactoring" 4 years ago
Denis Tereshkin bd5ddfc8b6 Merge branch 'stable' 4 years ago
Denis Tereshkin 7e72d1ae0a Adjust to libatrade-0.10 4 years ago
Denis Tereshkin b40c2966b7 Real driver refactoring 7 years ago
Denis Tereshkin 32a75e8c47 Switched from quickcheck to hedgehog 7 years ago
  1. 20
      robocom-zero.cabal
  2. 24
      src/ATrade/BarAggregator.hs
  3. 533
      src/ATrade/Driver/Backtest.hs
  4. 305
      src/ATrade/Driver/Junction.hs
  5. 22
      src/ATrade/Driver/Junction/BrokerService.hs
  6. 258
      src/ATrade/Driver/Junction/JunctionMonad.hs
  7. 39
      src/ATrade/Driver/Junction/ProgramConfiguration.hs
  8. 3
      src/ATrade/Driver/Junction/QuoteStream.hs
  9. 166
      src/ATrade/Driver/Junction/QuoteThread.hs
  10. 151
      src/ATrade/Driver/Junction/RemoteControl.hs
  11. 64
      src/ATrade/Driver/Junction/RobotDriverThread.hs
  12. 15
      src/ATrade/Driver/Junction/Types.hs
  13. 114
      src/ATrade/Driver/Real.hs
  14. 94
      src/ATrade/Driver/Real/BrokerClientThread.hs
  15. 3
      src/ATrade/Quotes/QTIS.hs
  16. 14
      src/ATrade/RoboCom/Monad.hs
  17. 258
      src/ATrade/RoboCom/Positions.hs
  18. 18
      src/ATrade/RoboCom/Types.hs
  19. 7
      stack.yaml
  20. 107
      test/Test/BarAggregator.hs
  21. 19
      test/Test/Driver/Junction/QuoteThread.hs
  22. 2
      test/Test/RoboCom/Indicators.hs
  23. 165
      test/Test/RoboCom/Positions.hs
  24. 4
      test/Test/RoboCom/Utils.hs

20
robocom-zero.cabal

@ -1,5 +1,5 @@
name: robocom-zero name: robocom-zero
version: 0.2.0.0 version: 0.2.1.0
-- synopsis: -- synopsis:
-- description: -- description:
homepage: https://github.com/asakul/robocom-zero#readme homepage: https://github.com/asakul/robocom-zero#readme
@ -26,7 +26,7 @@ library
, ATrade.Quotes , ATrade.Quotes
, ATrade.Quotes.QHP , ATrade.Quotes.QHP
, ATrade.Quotes.QTIS , ATrade.Quotes.QTIS
-- , ATrade.Driver.Backtest , ATrade.Driver.Backtest
, ATrade.Driver.Junction , ATrade.Driver.Junction
, ATrade.Driver.Junction.Types , ATrade.Driver.Junction.Types
, ATrade.Driver.Junction.QuoteThread , ATrade.Driver.Junction.QuoteThread
@ -39,8 +39,10 @@ library
, ATrade.Quotes.HistoryProvider , ATrade.Quotes.HistoryProvider
, ATrade.Quotes.TickerInfoProvider , ATrade.Quotes.TickerInfoProvider
other-modules: Paths_robocom_zero other-modules: Paths_robocom_zero
, ATrade.Driver.Junction.RemoteControl
, ATrade.Driver.Junction.JunctionMonad
build-depends: base >= 4.7 && < 5 build-depends: base >= 4.7 && < 5
, libatrade >= 0.12.0.0 && < 0.13.0.0 , libatrade >= 0.16.0.0 && < 0.17.0.0
, text , text
, text-icu , text-icu
, lens , lens
@ -73,6 +75,10 @@ library
, extra , extra
, co-log , co-log
, text-show , text-show
, unliftio
, conduit
, split
, cassava
default-language: Haskell2010 default-language: Haskell2010
other-modules: ATrade.Exceptions other-modules: ATrade.Exceptions
@ -87,12 +93,14 @@ test-suite robots-test
, libatrade , libatrade
, time , time
, text , text
, hedgehog
, tasty , tasty
, tasty-hunit , tasty-hunit
, tasty-golden , tasty-golden
, tasty-smallcheck , tasty-hedgehog
, tasty-quickcheck
, tasty-hspec , tasty-hspec
, tasty-quickcheck
, tasty-smallcheck
, quickcheck-text , quickcheck-text
, quickcheck-instances , quickcheck-instances
, containers , containers
@ -101,6 +109,8 @@ test-suite robots-test
, zeromq4-haskell-zap , zeromq4-haskell-zap
, BoundedChan , BoundedChan
, mtl , mtl
, co-log-core
, co-log
ghc-options: -threaded -rtsopts -with-rtsopts=-N ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010 default-language: Haskell2010
other-modules: Test.RoboCom.Indicators other-modules: Test.RoboCom.Indicators

24
src/ATrade/BarAggregator.hs

@ -72,25 +72,28 @@ handleTicks ticks aggregator = foldl f ([], aggregator) ticks
handleTick :: Tick -> BarAggregator -> (Maybe Bar, BarAggregator) handleTick :: Tick -> BarAggregator -> (Maybe Bar, BarAggregator)
handleTick tick = runState $ do handleTick tick = runState $ do
lLastTicks %= M.insert (security tick, datatype tick) tick lLastTicks %= M.insert (security tick, datatype tick) tick
tws <- gets tickTimeWindows timeWindows <- gets tickTimeWindows
mybars <- gets bars mybars <- gets bars
if (any (isInTimeInterval tick) tws) if any (isInTimeInterval tick) timeWindows
then then
case M.lookup (security tick) mybars of case M.lookup (security tick) mybars of
Just series -> case bsBars series of Just series -> case bsBars series of
(b:bs) -> do (b:bs) -> do
let currentBn = barNumber (barTimestamp b) (fromIntegral . unBarTimeframe $ bsTimeframe series) let timeframeInSeconds = fromIntegral . unBarTimeframe $ bsTimeframe series
let currentBn = barNumber (barTimestamp b) timeframeInSeconds
case datatype tick of case datatype tick of
LastTradePrice -> LastTradePrice ->
if volume tick > 0 if volume tick > 0
then then
if currentBn == barNumber (timestamp tick) (fromIntegral . unBarTimeframe $ bsTimeframe series) if currentBn == barNumber (timestamp tick) timeframeInSeconds
then do then do
lBars %= M.insert (security tick) series { bsBars = updateBar b tick : bs } lBars %= M.insert (security tick) series { bsBars = updateBar b tick : bs }
return Nothing return Nothing
else do else do
lBars %= M.insert (security tick) series { bsBars = barFromTick tick : b : bs } let barEndTimestamp = barEndTime b timeframeInSeconds
return . Just $ b let resultingBar = b { barTimestamp = barEndTimestamp }
lBars %= M.insert (security tick) series { bsBars = barFromTick tick : resultingBar : bs }
return . Just $ resultingBar
else else
return Nothing return Nothing
_ -> _ ->
@ -140,15 +143,16 @@ handleTick tick = runState $ do
updateTime :: Tick -> BarAggregator -> (Maybe Bar, BarAggregator) updateTime :: Tick -> BarAggregator -> (Maybe Bar, BarAggregator)
updateTime tick = runState $ do updateTime tick = runState $ do
lLastTicks %= M.insert (security tick, datatype tick) tick lLastTicks %= M.insert (security tick, datatype tick) tick
tws <- gets tickTimeWindows timeWindows <- gets tickTimeWindows
mybars <- gets bars mybars <- gets bars
if (any (isInTimeInterval tick) tws) if any (isInTimeInterval tick) timeWindows
then then
case M.lookup (security tick) mybars of case M.lookup (security tick) mybars of
Just series -> case bsBars series of Just series -> case bsBars series of
(b:bs) -> do (b:bs) -> do
let currentBn = barNumber (barTimestamp b) (fromIntegral . unBarTimeframe $ bsTimeframe series) let timeframeInSeconds = fromIntegral . unBarTimeframe $ bsTimeframe series
let thisBn = barNumber (timestamp tick) (fromIntegral . unBarTimeframe $ bsTimeframe series) let currentBn = barNumber (barTimestamp b) timeframeInSeconds
let thisBn = barNumber (timestamp tick) timeframeInSeconds
if if
| currentBn == thisBn -> do | currentBn == thisBn -> do
lBars %= M.insert (security tick) series { bsBars = updateBarTimestamp b tick : bs } lBars %= M.insert (security tick) series { bsBars = updateBarTimestamp b tick : bs }

533
src/ATrade/Driver/Backtest.hs

@ -13,59 +13,101 @@ module ATrade.Driver.Backtest (
backtestMain backtestMain
) where ) where
import ATrade.Driver.Types (InitializationCallback, import ATrade.Driver.Junction.Types (StrategyDescriptor (StrategyDescriptor),
StrategyInstanceParams (..)) StrategyDescriptorE (StrategyDescriptorE),
import ATrade.Exceptions TickerConfig, confStrategy,
import ATrade.Quotes confTickers, eventCallback,
import ATrade.Quotes.Finam as QF strategyBaseName, tickerId,
import ATrade.Quotes.QTIS timeframe)
import ATrade.RoboCom.Monad (Event (..), EventCallback, import ATrade.Exceptions (RoboComException (UnableToLoadConfig, UnableToLoadFeed))
MonadRobot (..), import ATrade.Logging (Message, Severity (Error, Trace),
StrategyEnvironment (..), fmtMessage, logWith)
appendToLog, seBars, seLastTimestamp) import ATrade.Quotes.QTIS (TickerInfo (tiLotSize, tiTickSize),
import ATrade.RoboCom.Positions qtisGetTickersInfo)
import ATrade.RoboCom.Types (BarSeries (..), Bars, InstrumentParameters (InstrumentParameters), import ATrade.RoboCom.ConfigStorage (ConfigStorage (loadConfig))
Ticker (..), Timeframe (..)) import ATrade.RoboCom.Monad (Event (..), MonadRobot (..),
import ATrade.Types StrategyEnvironment (..),
import Conduit (awaitForever, runConduit, yield, appendToLog, seLastTimestamp)
(.|)) import ATrade.RoboCom.Types (BarSeries (..),
import Control.Exception.Safe BarSeriesId (BarSeriesId), Bars,
import Control.Lens hiding (ix, (<|), (|>)) InstrumentParameters (InstrumentParameters),
import Control.Monad.ST (runST) Ticker (..))
import Control.Monad.State import ATrade.Types (Bar (Bar, barHigh, barLow, barOpen, barSecurity, barTimestamp),
import Data.Aeson (FromJSON (..), Value (..), decode) BarTimeframe (BarTimeframe),
import Data.Aeson.Types (parseMaybe) Operation (Buy),
import Data.ByteString.Lazy (readFile, toStrict) Order (orderAccountId, orderId, orderOperation, orderPrice, orderQuantity, orderSecurity, orderSignalId),
import Data.Default OrderId,
import Data.HashMap.Strict (lookup) OrderPrice (Limit, Market),
import Data.List (partition) OrderState (Cancelled, Executed, Submitted),
import Data.List.Split (splitOn) Price, TickerId, Trade (..),
import qualified Data.Map.Strict as M fromDouble)
import Data.Sequence (Seq (..), (<|), (|>)) import Colog (LogAction, (>$<))
import qualified Data.Sequence as Seq import Colog.Actions (logTextStdout)
import Data.STRef (newSTRef, readSTRef, writeSTRef) import Conduit (ConduitT, Void, awaitForever,
import qualified Data.Text as T runConduit, yield, (.|))
import Data.Text.IO (putStrLn) import Control.Exception.Safe (catchAny, throw)
import qualified Data.Text.Lazy as TL import Control.Lens (makeLenses, use, (%=), (+=),
import Data.Time.Calendar (fromGregorian) (.=), (^.))
import Data.Time.Clock (DiffTime, UTCTime (..)) import Control.Monad.ST (runST)
import Data.Vector ((!), (!?), (//)) import Control.Monad.State (MonadIO, MonadPlus (mzero),
import qualified Data.Vector as V MonadState, MonadTrans (lift),
import Options.Applicative hiding (Success) State, StateT (StateT),
import Prelude hiding (lookup, putStrLn, readFile) execState, forM_, gets, when)
import Safe (headMay) import Data.Aeson (FromJSON (..), Value (..),
import System.ZMQ4 hiding (Event) decode)
import Data.Aeson.Types (parseMaybe)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
import Data.ByteString.Lazy (readFile, toStrict)
import qualified Data.ByteString.Lazy as BL
import Data.Csv (FromField (parseField),
FromRecord (parseRecord),
HasHeader (HasHeader), (.!))
import qualified Data.Csv as Csv
import Data.Default (Default (def))
import Data.HashMap.Strict (lookup)
import Data.IORef (newIORef)
import Data.List (partition)
import qualified Data.List as L
import Data.List.NonEmpty (NonEmpty ((:|)))
import Data.List.Split (splitOn)
import qualified Data.Map.Strict as M
import Data.Sequence (Seq (..), (<|), (|>))
import qualified Data.Sequence as Seq
import Data.STRef (newSTRef, readSTRef, writeSTRef)
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8)
import Data.Text.IO (putStrLn)
import qualified Data.Text.Lazy as TL
import Data.Time (defaultTimeLocale, parseTimeM)
import Data.Time.Calendar (fromGregorian)
import Data.Time.Clock (UTCTime (..), addUTCTime)
import Data.Vector ((!), (!?), (//))
import qualified Data.Vector as V
import Dhall (FromDhall, auto, input)
import Options.Applicative (Alternative (some), Parser,
ReadM, eitherReader, execParser,
fullDesc, header, helper, info,
long, metavar, option, short,
strOption)
import Prelude hiding (log, lookup, putStrLn,
readFile)
import Safe (headMay)
import System.IO (IOMode (ReadMode), withFile)
import System.ZMQ4 (withContext)
data Feed = Feed TickerId FilePath data Feed = Feed TickerId FilePath
deriving (Show, Eq) deriving (Show, Eq)
data Params = Params { data Params = Params {
strategyBasename :: String,
strategyConfigFile :: FilePath, strategyConfigFile :: FilePath,
qtisEndpoint :: String, qtisEndpoint :: String,
paramsFeeds :: [Feed] paramsFeeds :: [Feed]
} deriving (Show, Eq) } deriving (Show, Eq)
data BacktestState c s = BacktestState { data BacktestState c s = BacktestState {
_descriptor :: StrategyDescriptor c s,
_cash :: Double, _cash :: Double,
_robotState :: s, _robotState :: s,
_robotParams :: c, _robotParams :: c,
@ -75,101 +117,135 @@ data BacktestState c s = BacktestState {
_tradesLog :: [Trade], _tradesLog :: [Trade],
_orderIdCounter :: Integer, _orderIdCounter :: Integer,
_pendingTimers :: [UTCTime], _pendingTimers :: [UTCTime],
_logs :: [T.Text] _logs :: [T.Text],
_barsMap :: M.Map BarSeriesId BarSeries,
_availableTickers :: NonEmpty BarSeriesId
} }
makeLenses ''BacktestState makeLenses ''BacktestState
data Row = Row {
rowTicker :: T.Text,
rowTimeframe :: Int,
rowTime :: UTCTime,
rowOpen :: Price,
rowHigh :: Price,
rowLow :: Price,
rowClose :: Price,
rowVolume :: Integer
} deriving (Show, Eq)
instance FromField Price where
parseField s = fromDouble <$> (parseField s :: Csv.Parser Double)
instance FromRecord Row where
parseRecord v
| length v == 9 = do
tkr <- v .! 0
tf <- v .! 1
date <- v .! 2
time <- v .! 3
dt <- addUTCTime (-3 * 3600) <$> parseDt date time
open <- v .! 4
high <- v .! 5
low <- v .! 6
close <- v .! 7
vol <- v .! 8
return $ Row tkr tf dt open high low close vol
| otherwise = mzero
where
parseDt :: B.ByteString -> B.ByteString -> Csv.Parser UTCTime
parseDt d t = case parseTimeM True defaultTimeLocale "%Y%m%d %H%M%S" $ B8.unpack d ++ " " ++ B8.unpack t of
Just dt -> return dt
Nothing -> fail "Unable to parse date/time"
parseQuotes :: B.ByteString -> Maybe [Row]
parseQuotes csvData = case Csv.decode HasHeader $ BL.fromStrict csvData of
Left _ -> Nothing
Right d -> Just $ V.toList d
paramsParser :: Parser Params paramsParser :: Parser Params
paramsParser = Params paramsParser = Params
<$> strOption ( <$> strOption (
long "config" <> short 'c' long "strategy-name" <> short 'n')
) <*> strOption (
long "config" <> short 'c')
<*> strOption <*> strOption
( long "qtis" <> short 'q' <> metavar "ENDPOINT/ID" ) ( long "qtis" <> short 'q' <> metavar "ENDPOINT/ID")
<*> some (option feedArgParser ( <*> some (option feedArgParser (
long "feed" <> short 'f' long "feed" <> short 'f'))
))
feedArgParser :: ReadM Feed feedArgParser :: ReadM Feed
feedArgParser = eitherReader (\s -> case splitOn ":" s of feedArgParser = eitherReader (\s -> case splitOn ":" s of
[tid, fpath] -> Right $ Feed (T.pack tid) fpath [tid, fpath] -> Right $ Feed (T.pack tid) fpath
_ -> Left $ "Unable to parse feed id: " ++ s) _ -> Left $ "Unable to parse feed id: " ++ s)
backtestMain :: (FromJSON c, StateHasPositions s) => DiffTime -> s -> EventCallback c s -> IO () logger :: (MonadIO m) => LogAction m Message
backtestMain _dataDownloadDelta defaultState callback = do logger = fmtMessage >$< logTextStdout
params <- execParser opts
(tickerList, config) <- loadStrategyConfig params
let instanceParams = StrategyInstanceParams { backtestMain :: M.Map T.Text StrategyDescriptorE -> IO ()
strategyInstanceId = "foo", backtestMain descriptors = do
strategyAccount = "foo", params <- execParser opts
strategyVolume = 1, let log = logWith logger
tickers = tickerList, let strategyName = T.pack $ strategyBasename params
strategyQTISEp = Nothing }
feeds <- loadFeeds (paramsFeeds params) feeds <- loadFeeds (paramsFeeds params)
bars <- makeBars (T.pack $ qtisEndpoint params) tickerList case M.lookup strategyName descriptors of
Just (StrategyDescriptorE desc) -> flip catchAny (\e -> log Error "Backtest" $ "Exception: " <> (T.pack . show $ e)) $
runBacktestDriver feeds config bars runBacktestDriver desc feeds params
Nothing -> log Error "Backtest" $ "Can't find strategy: " <> strategyName
where where
opts = info (helper <*> paramsParser) opts = info (helper <*> paramsParser)
( fullDesc <> header "ATrade strategy backtesting framework" ) ( fullDesc <> header "ATrade strategy backtesting framework" )
makeBars :: T.Text -> [Ticker] -> IO (M.Map TickerId BarSeries) makeBars :: T.Text -> [TickerConfig] -> IO (M.Map BarSeriesId BarSeries)
makeBars qtisEp tickersList = makeBars qtisEp confs =
withContext $ \ctx -> withContext $ \ctx ->
M.fromList <$> mapM (mkBarEntry ctx qtisEp) tickersList M.fromList <$> mapM (mkBarEntry ctx qtisEp) confs
mkBarEntry ctx qtisEp tickerEntry = do mkBarEntry ctx qtisEp conf = do
info <- qtisGetTickersInfo ctx qtisEp (code tickerEntry) info <- qtisGetTickersInfo ctx qtisEp (tickerId conf)
return (code tickerEntry, BarSeries (code tickerEntry) (Timeframe (timeframeSeconds tickerEntry)) [] (InstrumentParameters (fromInteger $ tiLotSize info) (tiTickSize info))) return (BarSeriesId (tickerId conf) (timeframe conf),
BarSeries
(tickerId conf)
(timeframe conf)
runBacktestDriver feeds params tickerList = do []
let s = runConduit $ barStreamFromFeeds feeds .| backtestLoop (InstrumentParameters (tickerId conf) (fromInteger $ tiLotSize info) (tiTickSize info)))
let finalState = execState (unBacktestingMonad s) $ defaultBacktestState defaultState params tickerList
print $ finalState ^. cash runBacktestDriver desc feeds params = do
print $ finalState ^. tradesLog bigConf <- loadConfig (T.pack $ strategyConfigFile params)
forM_ (reverse $ finalState ^. logs) putStrLn case confTickers bigConf of
tickerList@(firstTicker:restTickers) -> do
loadStrategyConfig :: (FromJSON c) => Params -> IO ([Ticker], c) bars <- makeBars (T.pack $ qtisEndpoint params) tickerList
loadStrategyConfig params = do let s = runConduit $ barStreamFromFeeds feeds .| backtestLoop desc
content <- readFile (strategyConfigFile params) let finalState =
case loadStrategyConfig' content of execState (unBacktestingMonad s) $ defaultBacktestState def (confStrategy bigConf) desc bars (fmap toBarSeriesId (firstTicker :| restTickers))
Just (tickersList, config) -> return (tickersList, config) print $ finalState ^. cash
_ -> throw $ UnableToLoadConfig (T.pack . strategyConfigFile $ params) print $ finalState ^. tradesLog
forM_ (reverse $ finalState ^. logs) putStrLn
loadStrategyConfig' content = do _ -> return ()
v <- decode content
case v of toBarSeriesId conf = BarSeriesId (tickerId conf) (timeframe conf)
Object o -> do
mbTickers <- "tickers" `lookup` o
mbParams <- "params" `lookup` o
tickers' <- parseMaybe parseJSON mbTickers
params <- parseMaybe parseJSON mbParams
return (tickers', params)
_ -> Nothing
barStreamFromFeeds :: (Monad m) => V.Vector (BarTimeframe, [Bar]) -> ConduitT () (BarSeriesId, Bar) m ()
barStreamFromFeeds feeds = case nextBar feeds of barStreamFromFeeds feeds = case nextBar feeds of
Just (bar, feeds') -> yield bar >> barStreamFromFeeds feeds' Just (tf, bar, feeds') -> yield (BarSeriesId (barSecurity bar) tf, bar) >> barStreamFromFeeds feeds'
_ -> return () _ -> return ()
nextBar :: V.Vector [Bar] -> Maybe (Bar, V.Vector [Bar]) nextBar :: V.Vector (BarTimeframe, [Bar]) -> Maybe (BarTimeframe, Bar, V.Vector (BarTimeframe, [Bar]))
nextBar feeds = case indexOfNextFeed feeds of nextBar feeds = case indexOfNextFeed feeds of
Just ix -> do Just ix -> do
f <- feeds !? ix (tf, f) <- feeds !? ix
h <- headMay f h <- headMay f
return (h, feeds // [(ix, tail f)]) return (tf, h, feeds // [(ix, (tf, tail f))])
_ -> Nothing _ -> Nothing
indexOfNextFeed feeds = runST $ do indexOfNextFeed feeds = runST $ do
minTs <- newSTRef Nothing minTs <- newSTRef Nothing
minIx <- newSTRef Nothing minIx <- newSTRef Nothing
forM_ [0..(V.length feeds-1)] (\ix -> do forM_ [0..(V.length feeds-1)] (\ix -> do
let feed = feeds ! ix let (_, feed) = feeds ! ix
curTs <- readSTRef minTs curTs <- readSTRef minTs
case feed of case feed of
x:_ -> case curTs of x:_ -> case curTs of
@ -182,126 +258,124 @@ backtestMain _dataDownloadDelta defaultState callback = do
_ -> return ()) _ -> return ())
readSTRef minIx readSTRef minIx
backtestLoop = awaitForever (\bar -> do backtestLoop :: StrategyDescriptor c s -> ConduitT (BarSeriesId, Bar) Void (BacktestingMonad c s) ()
backtestLoop desc =
awaitForever (\(bsId, bar) -> do
_curState <- use robotState _curState <- use robotState
_env <- gets _strategyEnvironment _env <- gets _strategyEnvironment
let newTimestamp = barTimestamp bar let newTimestamp = barTimestamp bar
strategyEnvironment . seBars %= (flip updateBars bar) barsMap %= updateBars bsId bar
strategyEnvironment . seLastTimestamp .= newTimestamp strategyEnvironment . seLastTimestamp .= newTimestamp
enqueueEvent (NewBar bar) enqueueEvent (NewBar (bsIdTf bsId, bar))
lift handleEvents) lift (handleEvents desc))
handleEvents = do bsIdTf (BarSeriesId _ tf) = tf
events <- use pendingEvents
case events of
x :<| xs -> do handleEvents :: StrategyDescriptor c s -> BacktestingMonad c s ()
pendingEvents .= xs handleEvents desc = do
handleEvent x events <- use pendingEvents
handleEvents case events of
_ -> return () x :<| xs -> do
pendingEvents .= xs
executePendingOrders bar = do handleEvent desc x
executeMarketOrders bar handleEvents desc
executeLimitOrders bar _ -> return ()
executeLimitOrders bar = do executePendingOrders bar = do
(limitOrders, otherOrders'') <- partition executeMarketOrders bar
(\o -> case orderPrice o of executeLimitOrders bar
Limit _ -> True
_ -> False) <$> use pendingOrders executeLimitOrders bar = do
let (executableOrders, otherOrders') = partition (isExecutable bar) limitOrders (limitOrders, otherOrders'') <- partition
pendingOrders .= otherOrders' ++ otherOrders'' (\o -> case orderPrice o of
forM_ executableOrders $ \order -> order `executeAtPrice` priceForLimitOrder order bar Limit _ -> True
_ -> False) <$> use pendingOrders
isExecutable bar order = case orderPrice order of let (executableOrders, otherOrders') = partition (isExecutable bar) limitOrders
Limit price -> if orderOperation order == Buy pendingOrders .= otherOrders' ++ otherOrders''
then price >= barLow bar forM_ executableOrders $ \order -> order `executeAtPrice` priceForLimitOrder order bar
else price <= barHigh bar
_ -> True isExecutable bar order = case orderPrice order of
Limit price -> if orderOperation order == Buy
priceForLimitOrder order bar = case orderPrice order of then price >= barLow bar
Limit price -> if orderOperation order == Buy else price <= barHigh bar
then if price >= barOpen bar _ -> True
then barOpen bar
else price priceForLimitOrder order bar = case orderPrice order of
else if price <= barOpen bar Limit price -> if orderOperation order == Buy
then barOpen bar then if price >= barOpen bar
else price then barOpen bar
_ -> error "Should've been limit order" else price
else if price <= barOpen bar
executeMarketOrders bar = do then barOpen bar
(marketOrders, otherOrders) <- partition (\o -> orderPrice o == Market) <$> use pendingOrders else price
pendingOrders .= otherOrders _ -> error "Should've been limit order"
forM_ marketOrders $ \order ->
order `executeAtPrice` barOpen bar executeMarketOrders bar = do
(marketOrders, otherOrders) <- partition (\o -> orderPrice o == Market) <$> use pendingOrders
executeAtPrice order price = do pendingOrders .= otherOrders
ts <- use $ strategyEnvironment . seLastTimestamp forM_ marketOrders $ \order ->
let thisTrade = mkTrade order price ts order `executeAtPrice` barOpen bar
tradesLog %= (\log' -> thisTrade : log')
pendingEvents %= (\s -> (OrderUpdate (orderId order) Executed) <| s) executeAtPrice order price = do
pendingEvents %= (\s -> (NewTrade thisTrade) <| s) ts <- use $ strategyEnvironment . seLastTimestamp
let thisTrade = mkTrade order price ts
mkTrade :: Order -> Price -> UTCTime -> Trade tradesLog %= (thisTrade :)
mkTrade order price ts = Trade { pendingEvents %= (\s -> OrderUpdate (orderId order) Executed <| s)
tradeOrderId = orderId order, pendingEvents %= (\s -> NewTrade thisTrade <| s)
tradePrice = price,
tradeQuantity = orderQuantity order, mkTrade :: Order -> Price -> UTCTime -> Trade
tradeVolume = (fromIntegral . orderQuantity $ order) * price, mkTrade order price ts = Trade {
tradeVolumeCurrency = "pt", tradeOrderId = orderId order,
tradeOperation = orderOperation order, tradePrice = price,
tradeAccount = orderAccountId order, tradeQuantity = orderQuantity order,
tradeSecurity = orderSecurity order, tradeVolume = (fromIntegral . orderQuantity $ order) * price,
tradeTimestamp = ts, tradeVolumeCurrency = "pt",
tradeCommission = 0, tradeOperation = orderOperation order,
tradeSignalId = orderSignalId order tradeAccount = orderAccountId order,
} tradeSecurity = orderSecurity order,
tradeTimestamp = ts,
handleEvent event@(NewBar bar) = do tradeCommission = 0,
executePendingOrders bar tradeSignalId = orderSignalId order
handleEvents -- This should pass OrderUpdate events to the callback before NewBar events }
firedTimers <- fireTimers (barTimestamp bar)
mapM_ (\x -> enqueueEvent (TimerFired x)) firedTimers handleEvent :: StrategyDescriptor c s -> Event -> BacktestingMonad c s ()
handleEvent' event handleEvent desc event@(NewBar (_, bar)) = do
return () executePendingOrders bar
handleEvents desc -- This should pass OrderUpdate events to the callback before NewBar events
handleEvent event = handleEvent' event firedTimers <- fireTimers (barTimestamp bar)
mapM_ (enqueueEvent . TimerFired) firedTimers
handleEvent' event = callback event handleEvent' desc event
return ()
updateBars barMap newbar = M.alter (\case
Nothing -> Just BarSeries { bsTickerId = barSecurity newbar, handleEvent desc event = handleEvent' desc event
bsTimeframe = Timeframe 60,
bsBars = [newbar, newbar] } handleEvent' desc event = eventCallback desc event
Just bs -> Just bs { bsBars = updateBarList newbar (bsBars bs) }) (barSecurity newbar) barMap
updateBars bsId newbar barMap = M.adjust (\bs -> bs { bsBars = newbar : bsBars bs }) bsId barMap
updateBarList newbar (_:bs) = newbar:newbar:bs
updateBarList newbar _ = newbar:[newbar] fireTimers ts = do
(firedTimers, otherTimers) <- partition (< ts) <$> use pendingTimers
fireTimers ts = do pendingTimers .= otherTimers
(firedTimers, otherTimers) <- partition (< ts) <$> use pendingTimers return firedTimers
pendingTimers .= otherTimers
return firedTimers loadFeeds :: [Feed] -> IO (V.Vector (BarTimeframe, [Bar]))
loadFeeds feeds = V.fromList <$> mapM loadFeed feeds
loadFeeds :: [Feed] -> IO (V.Vector [Bar]) loadFeed (Feed tid path) = do
loadFeeds feeds = V.fromList <$> mapM loadFeed feeds content <- readFile path
loadFeed (Feed tid path) = do case parseQuotes $ toStrict content of
content <- readFile path Just quotes -> case headMay quotes of
case QF.parseQuotes $ toStrict content of Just first -> return (BarTimeframe (rowTimeframe first), fmap (rowToBar tid) quotes)
Just quotes -> return $ fmap (rowToBar tid) quotes Nothing -> throw $ UnableToLoadFeed (T.pack path)
_ -> throw $ UnableToLoadFeed (T.pack path) _ -> throw $ UnableToLoadFeed (T.pack path)
rowToBar tid r = Bar tid (rowTime r) (rowOpen r) (rowHigh r) (rowLow r) (rowClose r) (rowVolume r) rowToBar tid r = Bar tid (rowTime r) (rowOpen r) (rowHigh r) (rowLow r) (rowClose r) (rowVolume r)
enqueueEvent :: MonadState (BacktestState c s) m => Event -> m ()
enqueueEvent event = pendingEvents %= (\s -> s |> event) enqueueEvent event = pendingEvents %= (|> event)
instance (Default c, Default s) => Default (BacktestState c s) defaultBacktestState :: s -> c -> StrategyDescriptor c s -> M.Map BarSeriesId BarSeries -> NonEmpty BarSeriesId -> BacktestState c s
where defaultBacktestState s c desc = BacktestState desc 0 s c (StrategyEnvironment "" "" 1 (UTCTime (fromGregorian 1970 1 1) 0)) [] Seq.empty [] 1 [] []
def = defaultBacktestState def def def
defaultBacktestState :: s -> c -> Bars -> BacktestState c s
defaultBacktestState s c bars = BacktestState 0 s c (StrategyEnvironment "" "" 1 bars (UTCTime (fromGregorian 1970 1 1) 0)) [] Seq.empty [] 1 [] []
newtype BacktestingMonad s c a = BacktestingMonad { unBacktestingMonad :: State (BacktestState s c) a } newtype BacktestingMonad s c a = BacktestingMonad { unBacktestingMonad :: State (BacktestState s c) a }
deriving (Functor, Applicative, Monad, MonadState (BacktestState s c)) deriving (Functor, Applicative, Monad, MonadState (BacktestState s c))
@ -315,21 +389,38 @@ instance MonadRobot (BacktestingMonad c s) c s where
submitOrder order = do submitOrder order = do
oid <- nextOrderId oid <- nextOrderId
let orderWithId = order { orderId = oid } let orderWithId = order { orderId = oid }
pendingOrders %= ((:) orderWithId) pendingOrders %= (orderWithId :)
pendingEvents %= (\s -> s |> (OrderSubmitted orderWithId)) pendingEvents %= (\s -> s |> OrderUpdate oid Submitted)
return oid
cancelOrder oid = do cancelOrder oid = do
orders <- use pendingOrders orders <- use pendingOrders
let (matchingOrders, otherOrders) = partition (\o -> orderId o == oid) orders let (matchingOrders, otherOrders) = partition (\o -> orderId o == oid) orders
case matchingOrders of case matchingOrders of
[] -> return () [] -> return ()
xs -> do xs -> do
mapM_ (\o -> pendingEvents %= (\s -> s |> (OrderUpdate (orderId o) Cancelled))) xs mapM_ (\o -> pendingEvents %= (\s -> s |> OrderUpdate (orderId o) Cancelled)) xs
pendingOrders .= otherOrders pendingOrders .= otherOrders
appendToLog txt = logs %= ((:) (TL.toStrict txt)) appendToLog _ txt = logs %= ((TL.toStrict txt) :)
setupTimer time = pendingTimers %= ((:) time) setupTimer time = pendingTimers %= (time :)
enqueueIOAction _actionId _action = error "Backtesting io actions is not supported" enqueueIOAction _actionId _action = error "Backtesting io actions is not supported"
getConfig = use robotParams getConfig = use robotParams
getState = use robotState getState = use robotState
setState s = robotState .= s setState s = robotState .= s
getEnvironment = use strategyEnvironment getEnvironment = use strategyEnvironment
getTicker tid tf = do
m <- gets _barsMap
return $ M.lookup (BarSeriesId tid tf) m
getTickerInfo tid = do
tickers <- getAvailableTickers
case L.find (\(BarSeriesId t _) -> t == tid) tickers of
Just (BarSeriesId t tf) -> do
ticker <- getTicker t tf
return (bsParams <$> ticker)
Nothing -> return Nothing
getAvailableTickers = use availableTickers
instance ConfigStorage IO where
loadConfig filepath = do
cfg <- B.readFile $ T.unpack filepath
input auto (decodeUtf8 cfg)

305
src/ATrade/Driver/Junction.hs

@ -1,99 +1,61 @@
{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE RankNTypes #-}
{-# LANGUAGE OverloadedStrings #-}
module ATrade.Driver.Junction module ATrade.Driver.Junction
( (
junctionMain junctionMain
) where ) where
import ATrade.Broker.Client (BrokerClientHandle, import ATrade.Broker.Client (startBrokerClient,
startBrokerClient,
stopBrokerClient) stopBrokerClient)
import ATrade.Broker.Protocol (Notification (OrderNotification, TradeNotification), import ATrade.Broker.Protocol (Notification (OrderNotification, TradeNotification),
NotificationSqnum (unNotificationSqnum), NotificationSqnum (unNotificationSqnum),
getNotificationSqnum) getNotificationSqnum)
import ATrade.Driver.Junction.BrokerService (BrokerService, import ATrade.Driver.Junction.BrokerService (getNotifications,
getNotifications,
mkBrokerService) mkBrokerService)
import ATrade.Driver.Junction.JunctionMonad (JunctionEnv (..),
JunctionM (..),
saveRobots,
startRobot)
import ATrade.Driver.Junction.ProgramConfiguration (ProgramConfiguration (..), import ATrade.Driver.Junction.ProgramConfiguration (ProgramConfiguration (..),
ProgramOptions (ProgramOptions, configPath)) ProgramOptions (ProgramOptions, configPath))
import ATrade.Driver.Junction.QuoteStream (QuoteStream (addSubscription, removeSubscription),
QuoteSubscription (QuoteSubscription),
SubscriptionId (SubscriptionId))
import ATrade.Driver.Junction.QuoteThread (DownloaderEnv (DownloaderEnv), import ATrade.Driver.Junction.QuoteThread (DownloaderEnv (DownloaderEnv),
QuoteThreadHandle,
withQThread) withQThread)
import qualified ATrade.Driver.Junction.QuoteThread as QT import ATrade.Driver.Junction.RemoteControl (handleRemoteControl)
import ATrade.Driver.Junction.RobotDriverThread (RobotDriverHandle, RobotEnv (RobotEnv), import ATrade.Driver.Junction.RobotDriverThread (RobotDriverHandle, postNotificationEvent)
RobotM (..), import ATrade.Driver.Junction.Types (StrategyDescriptorE)
createRobotDriverThread, import ATrade.Logging (Message (..), Severity (Debug, Info, Trace, Warning),
onStrategyInstance,
postNotificationEvent)
import ATrade.Driver.Junction.Types (StrategyDescriptorE (StrategyDescriptorE),
StrategyInstance (strategyInstanceId),
StrategyInstanceDescriptor (..),
confStrategy,
confTickers,
strategyState,
strategyTimers,
tickerId,
timeframe)
import ATrade.Logging (Message, Severity (Debug, Error, Info, Trace, Warning),
fmtMessage, fmtMessage,
logWarning,
logWith) logWith)
import ATrade.Quotes.QHP (mkQHPHandle) import ATrade.Quotes.QHP (mkQHPHandle)
import ATrade.RoboCom.ConfigStorage (ConfigStorage (loadConfig)) import ATrade.Types (OrderId, Trade (tradeOrderId))
import ATrade.RoboCom.Monad (StrategyEnvironment (..)) import Colog (LogAction (LogAction),
import ATrade.RoboCom.Persistence (MonadPersistence (loadState, saveState)) cfilter,
import ATrade.RoboCom.Types (BarSeriesId (BarSeriesId), hoistLogAction,
Bars) logTextStderr,
import ATrade.Types (ClientSecurityParams (ClientSecurityParams), (<&), (>$<))
OrderId,
Trade (tradeOrderId))
import Colog (HasLog (getLogAction, setLogAction),
LogAction,
logTextStdout,
(>$<))
import Colog.Actions (logTextHandle) import Colog.Actions (logTextHandle)
import Control.Concurrent (threadDelay) import Control.Concurrent.QSem (newQSem)
import Control.Exception.Safe (MonadThrow,
bracket)
import Control.Monad (forM_, forever) import Control.Monad (forM_, forever)
import Control.Monad.Extra (whenM) import Control.Monad.Extra (whenM)
import Control.Monad.IO.Class (MonadIO (liftIO)) import Control.Monad.IO.Class (MonadIO (liftIO))
import Control.Monad.Reader (MonadReader, ReaderT (runReaderT), import Control.Monad.Reader (ReaderT (runReaderT))
asks)
import Data.Aeson (eitherDecode,
encode)
import qualified Data.ByteString.Lazy as BL
import Data.Default (Default (def))
import Data.Foldable (traverse_)
import Data.IORef (IORef, import Data.IORef (IORef,
atomicModifyIORef', atomicModifyIORef',
newIORef, newIORef,
readIORef) readIORef)
import Data.List.NonEmpty (NonEmpty ((:|)))
import qualified Data.Map.Strict as M import qualified Data.Map.Strict as M
import Data.Set (notMember) import Data.Set (notMember)
import qualified Data.Set as S import qualified Data.Set as S
import qualified Data.Text as T import qualified Data.Text as T
import Data.Text.Encoding (encodeUtf8)
import Data.Text.IO (readFile) import Data.Text.IO (readFile)
import Data.Time (getCurrentTime) import Database.Redis (ConnectInfo (..), PortID (UnixSocket),
import Data.Time.Clock.POSIX (getPOSIXTime)
import Database.Redis (ConnectInfo (..),
Connection,
PortID (UnixSocket),
checkedConnect, checkedConnect,
defaultConnectInfo, defaultConnectInfo)
get, mset,
runRedis)
import Dhall (auto, input) import Dhall (auto, input)
import Options.Applicative (Parser, import Options.Applicative (Parser,
execParser, execParser,
@ -109,79 +71,31 @@ import System.IO (BufferMode (LineBu
Handle, Handle,
IOMode (AppendMode), IOMode (AppendMode),
hSetBuffering, hSetBuffering,
openFile,
withFile) withFile)
import System.ZMQ4 (withContext) import System.ZMQ4 (Router (Router),
import System.ZMQ4.ZAP (loadCertificateFromFile) bind, withContext,
withSocket)
import UnliftIO (MonadUnliftIO)
import UnliftIO.Exception (bracket)
import UnliftIO.QSem (QSem, withQSem)
data JunctionEnv =
JunctionEnv
{
peRedisSocket :: Connection,
peConfigPath :: FilePath,
peQuoteThread :: QuoteThreadHandle,
peBroker :: BrokerClientHandle,
peRobots :: IORef (M.Map T.Text RobotDriverHandle),
peLogAction :: LogAction JunctionM Message
}
newtype JunctionM a = JunctionM { unJunctionM :: ReaderT JunctionEnv IO a } locked :: (MonadIO m, MonadUnliftIO m) => QSem -> LogAction m a -> LogAction m a
deriving (Functor, Applicative, Monad, MonadReader JunctionEnv, MonadIO, MonadThrow) locked sem action = LogAction (\m -> withQSem sem (action <& m))
instance HasLog JunctionEnv Message JunctionM where logger :: (MonadIO m) => M.Map T.Text Severity -> Handle -> LogAction m Message
getLogAction = peLogAction logger loglevels h = cfilter checkLoglevel (fmtMessage >$< (logTextStderr <> logTextHandle h))
setLogAction a e = e { peLogAction = a } where
checkLoglevel msg =
instance ConfigStorage JunctionM where case M.lookup (msgComponent msg) loglevels of
loadConfig key = do Just level -> msgSeverity msg >= level
basePath <- asks peConfigPath Nothing -> True
let path = basePath <> "/" <> T.unpack key -- TODO fix path construction
liftIO $ readFile path >>= input auto
instance MonadPersistence JunctionM where
saveState newState key = do
conn <- asks peRedisSocket
now <- liftIO getPOSIXTime
res <- liftIO $ runRedis conn $ mset [(encodeUtf8 key, BL.toStrict $ encode newState),
(encodeUtf8 (key <> ":last_store") , encodeUtf8 . T.pack . show $ now)]
case res of
Left _ -> logWarning "Junction " "Unable to save state"
Right _ -> return ()
loadState key = do
conn <- asks peRedisSocket
res <- liftIO $ runRedis conn $ get (encodeUtf8 key)
-- TODO: just chain eithers
case res of
Left _ -> do
logWarning "Junction" "Unable to load state"
return def
Right maybeRawState ->
case maybeRawState of
Just rawState -> case eitherDecode $ BL.fromStrict rawState of
Left _ -> do
logWarning "Junction" "Unable to decode state"
return def
Right decodedState -> return decodedState
Nothing -> do
logWarning "Junction" "Unable to decode state"
return def
instance QuoteStream JunctionM where
addSubscription (QuoteSubscription ticker timeframe) chan = do
qt <- asks peQuoteThread
QT.addSubscription qt ticker timeframe chan
return (SubscriptionId 0) -- TODO subscription Ids
removeSubscription _ = undefined
logger :: (MonadIO m) => Handle -> LogAction m Message
logger h = fmtMessage >$< (logTextStdout <> logTextHandle h)
junctionMain :: M.Map T.Text StrategyDescriptorE -> IO () junctionMain :: M.Map T.Text StrategyDescriptorE -> IO ()
junctionMain descriptors = do junctionMain descriptors = do
opts <- parseOptions opts <- parseOptions
let initialLogger = fmtMessage >$< logTextStdout let initialLogger = fmtMessage >$< logTextStderr
logWith initialLogger Info "Junction" $ "Reading config from: " <> (T.pack . show) (configPath opts) logWith initialLogger Info "Junction" $ "Reading config from: " <> (T.pack . show) (configPath opts)
@ -189,83 +103,57 @@ junctionMain descriptors = do
withFile (logBasePath cfg <> "/all.log") AppendMode $ \h -> do withFile (logBasePath cfg <> "/all.log") AppendMode $ \h -> do
let log = logWith (logger h) hSetBuffering h LineBuffering
locksem <- newQSem 1
let globalLogger = locked locksem (logger (M.fromList $ logLevels cfg) h)
let log = logWith globalLogger
barsMap <- newIORef M.empty barsMap <- newIORef M.empty
tickerInfoMap <- newIORef M.empty
log Info "Junction" $ "Connecting to redis: " <> redisSocket cfg log Info "Junction" $ "Connecting to redis: " <> redisSocket cfg
redis <- checkedConnect (defaultConnectInfo { connectPort = UnixSocket (T.unpack $ redisSocket cfg) }) redis <- checkedConnect (defaultConnectInfo { connectPort = UnixSocket (T.unpack $ redisSocket cfg) })
log Info "Junction" "redis: connected" log Info "Junction" "redis: connected"
withContext $ \ctx -> do withContext $ \ctx -> do
log Debug "Junction" "0mq context created" log Debug "Junction" "0mq context created"
let downloaderLogAction = logger h let downloaderEnv = DownloaderEnv (mkQHPHandle ctx (qhpEndpoint cfg)) ctx (qtisEndpoint cfg) (hoistLogAction liftIO globalLogger)
let downloaderEnv = DownloaderEnv (mkQHPHandle ctx (qhpEndpoint cfg)) ctx (qtisEndpoint cfg) downloaderLogAction
robotsMap <- newIORef M.empty robotsMap <- newIORef M.empty
ordersMap <- newIORef M.empty ordersMap <- newIORef M.empty
handledNotifications <- newIORef S.empty handledNotifications <- newIORef S.empty
withBroker cfg ctx robotsMap ordersMap handledNotifications (logger h) $ \bro -> withBroker cfg robotsMap ordersMap handledNotifications globalLogger $ \bro ->
withQThread downloaderEnv barsMap cfg ctx (logger h) $ \qt -> do withQThread downloaderEnv barsMap tickerInfoMap cfg ctx globalLogger $ \qt ->
broService <- mkBrokerService bro ordersMap withSocket ctx Router $ \rcSocket -> do
let junctionLogAction = logger h liftIO $ bind rcSocket (T.unpack . remoteControlEndpoint $ cfg)
let env = broService <- mkBrokerService bro ordersMap
JunctionEnv let junctionLogAction = hoistLogAction liftIO globalLogger
{ let env =
peRedisSocket = redis, JunctionEnv
peConfigPath = robotsConfigsPath cfg, {
peQuoteThread = qt, peRedisSocket = redis,
peBroker = bro, peConfigPath = robotsConfigsPath cfg,
peRobots = robotsMap, peQuoteThread = qt,
peLogAction = junctionLogAction peBroker = bro,
} peRobots = robotsMap,
withJunction env $ do peRemoteControlSocket = rcSocket,
startRobots h cfg barsMap broService peLogAction = junctionLogAction,
forever $ do peIoLogAction = globalLogger,
notifications <- liftIO $ getNotifications broService peProgramConfiguration = cfg,
forM_ notifications (liftIO . handleBrokerNotification robotsMap ordersMap handledNotifications (logger h)) peBarsMap = barsMap,
saveRobots peTickerInfoMap = tickerInfoMap,
liftIO $ threadDelay 1000000 peBrokerService = broService,
peDescriptors = descriptors
}
withJunction env $ do
startRobots cfg
forever $ do
notifications <- getNotifications broService
forM_ notifications (liftIO . handleBrokerNotification robotsMap ordersMap handledNotifications globalLogger)
saveRobots
handleRemoteControl 1000
where where
saveRobots :: JunctionM () startRobots :: ProgramConfiguration -> JunctionM ()
saveRobots = do startRobots cfg = forM_ (instances cfg) startRobot
robotsMap <- asks peRobots >>= (liftIO . readIORef)
traverse_ saveRobotState robotsMap
saveRobotState :: RobotDriverHandle -> JunctionM ()
saveRobotState handle = onStrategyInstance handle $ \inst -> do
currentState <- liftIO $ readIORef (strategyState inst)
saveState currentState (strategyInstanceId inst)
currentTimers <- liftIO $ readIORef (strategyTimers inst)
saveState currentTimers (strategyInstanceId inst <> ":timers")
startRobots :: Handle -> ProgramConfiguration -> IORef Bars -> BrokerService -> JunctionM ()
startRobots logHandle cfg barsMap broService = forM_ (instances cfg) $ \inst -> do
now <- liftIO getCurrentTime
case M.lookup (strategyBaseName inst) descriptors of
Just (StrategyDescriptorE desc) -> do
bigConf <- loadConfig (configKey inst)
case confTickers bigConf of
(firstTicker:restTickers) -> do
rConf <- liftIO $ newIORef (confStrategy bigConf)
rState <- loadState (stateKey inst) >>= liftIO . newIORef
rTimers <- loadState (stateKey inst <> ":timers") >>= liftIO . newIORef
localH <- liftIO $ openFile (logBasePath cfg <> "/" <> T.unpack (strategyId inst) <> ".log") AppendMode
liftIO $ hSetBuffering localH LineBuffering
let robotLogAction = logger logHandle <> (fmtMessage >$< logTextHandle localH)
stratEnv <- liftIO $ newIORef StrategyEnvironment
{
_seInstanceId = strategyId inst,
_seAccount = accountId inst,
_seVolume = 1,
_seLastTimestamp = now
}
let robotEnv = RobotEnv rState rConf rTimers barsMap stratEnv robotLogAction broService (toBarSeriesId <$> (firstTicker :| restTickers))
robot <- createRobotDriverThread inst desc (flip runReaderT robotEnv . unRobotM) bigConf rConf rState rTimers
robotsMap' <- asks peRobots
liftIO $ atomicModifyIORef' robotsMap' (\s -> (M.insert (strategyId inst) robot s, ()))
_ -> logWith (logger logHandle) Error (strategyId inst) $ "No tickers configured !!!"
Nothing -> error "Unknown strategy"
toBarSeriesId t = BarSeriesId (tickerId t) (timeframe t)
withJunction :: JunctionEnv -> JunctionM () -> IO () withJunction :: JunctionEnv -> JunctionM () -> IO ()
withJunction env = (`runReaderT` env) . unJunctionM withJunction env = (`runReaderT` env) . unJunctionM
@ -276,8 +164,8 @@ junctionMain descriptors = do
LogAction IO Message -> LogAction IO Message ->
Notification -> Notification ->
IO () IO ()
handleBrokerNotification robotsRef ordersMapRef handled logger notification= do handleBrokerNotification robotsRef ordersMapRef handled logger' notification= do
logWith logger Trace "Junction" $ "Incoming notification: " <> (T.pack . show . unNotificationSqnum . getNotificationSqnum) notification logWith logger' Trace "Junction" $ "Incoming notification: " <> (T.pack . show . unNotificationSqnum . getNotificationSqnum) notification
whenM (notMember (getNotificationSqnum notification) <$> readIORef handled) $ do whenM (notMember (getNotificationSqnum notification) <$> readIORef handled) $ do
robotsMap <- readIORef robotsRef robotsMap <- readIORef robotsRef
ordersMap <- readIORef ordersMapRef ordersMap <- readIORef ordersMapRef
@ -285,8 +173,8 @@ junctionMain descriptors = do
case getNotificationTarget robotsMap ordersMap notification of case getNotificationTarget robotsMap ordersMap notification of
Just robot -> postNotificationEvent robot notification Just robot -> postNotificationEvent robot notification
Nothing -> do Nothing -> do
logWith logger Warning "Junction" $ "Unknown order: " <> (T.pack . show) (notificationOrderId notification) logWith logger' Warning "Junction" $ "Unknown order: " <> (T.pack . show) (notificationOrderId notification)
logWith logger Debug "Junction" $ "Ordermap: " <> (T.pack . show) (M.toList ordersMap) logWith logger' Debug "Junction" $ "Ordermap: " <> (T.pack . show) (M.toList ordersMap)
atomicModifyIORef' handled (\s -> (S.insert (getNotificationSqnum notification) s, ())) atomicModifyIORef' handled (\s -> (S.insert (getNotificationSqnum notification) s, ()))
@ -298,30 +186,15 @@ junctionMain descriptors = do
notificationOrderId (OrderNotification _ oid _) = oid notificationOrderId (OrderNotification _ oid _) = oid
notificationOrderId (TradeNotification _ trade) = tradeOrderId trade notificationOrderId (TradeNotification _ trade) = tradeOrderId trade
withBroker cfg ctx robotsMap ordersMap handled logger f = do withBroker cfg robotsMap ordersMap handled logger' f = do
securityParameters <- loadBrokerSecurityParameters cfg
bracket bracket
(startBrokerClient (startBrokerClient
"broker" (brokerIdentity cfg)
ctx
(brokerEndpoint cfg) (brokerEndpoint cfg)
(brokerNotificationEndpoint cfg) [handleBrokerNotification robotsMap ordersMap handled logger']
[handleBrokerNotification robotsMap ordersMap handled logger] logger')
securityParameters
logger)
stopBrokerClient f stopBrokerClient f
loadBrokerSecurityParameters cfg =
case (brokerClientCert cfg, brokerServerCert cfg) of
(Just clientCertPath, Just serverCertPath) -> do
eClientCert <- loadCertificateFromFile clientCertPath
eServerCert <- loadCertificateFromFile serverCertPath
case (eClientCert, eServerCert) of
(Right clientCert, Right serverCert) -> return $ ClientSecurityParams (Just clientCert) (Just serverCert)
(_, _) -> return $ ClientSecurityParams Nothing Nothing
_ -> return $ ClientSecurityParams Nothing Nothing
parseOptions = execParser options parseOptions = execParser options
options = info (optionsParser <**> helper) options = info (optionsParser <**> helper)
(fullDesc <> (fullDesc <>

22
src/ATrade/Driver/Junction/BrokerService.hs

@ -12,7 +12,7 @@ module ATrade.Driver.Junction.BrokerService
import qualified ATrade.Broker.Client as Bro import qualified ATrade.Broker.Client as Bro
import ATrade.Broker.Protocol (Notification (..)) import ATrade.Broker.Protocol (Notification (..))
import ATrade.Logging (Message, logDebug) import ATrade.Logging (Message, logDebug, logWarning)
import ATrade.Types (Order (..), OrderId) import ATrade.Types (Order (..), OrderId)
import Colog (WithLog) import Colog (WithLog)
import Control.Monad.IO.Class (MonadIO (liftIO)) import Control.Monad.IO.Class (MonadIO (liftIO))
@ -38,19 +38,27 @@ submitOrder service identity order = do
oid <- nextOrderId service oid <- nextOrderId service
logDebug "BrokerService" $ "New order, id: " <> (T.pack . show) oid logDebug "BrokerService" $ "New order, id: " <> (T.pack . show) oid
liftIO $ atomicModifyIORef' (orderMap service) (\s -> (M.insert oid identity s, ())) liftIO $ atomicModifyIORef' (orderMap service) (\s -> (M.insert oid identity s, ()))
_ <- liftIO $ Bro.submitOrder (broker service) order { orderId = oid } r <- liftIO $ Bro.submitOrder (broker service) order { orderId = oid }
case r of
Left err -> logWarning "BrokerService" $ "Submit order error: " <> err
_ -> return ()
return oid return oid
where where
nextOrderId srv = liftIO $ atomicModifyIORef' (orderIdCounter srv) (\s -> (s + 1, s)) nextOrderId srv = liftIO $ atomicModifyIORef' (orderIdCounter srv) (\s -> (s + 1, s))
cancelOrder :: BrokerService -> OrderId -> IO () cancelOrder :: (MonadIO m, WithLog env Message m) => BrokerService -> OrderId -> m ()
cancelOrder service oid = do cancelOrder service oid = do
_ <- Bro.cancelOrder (broker service) oid r <- liftIO $ Bro.cancelOrder (broker service) oid
case r of
Left err -> logWarning "BrokerServer" $ "Cancel order error: " <> err
_ -> return ()
return () return ()
getNotifications :: BrokerService -> IO [Notification] getNotifications :: (MonadIO m, WithLog env Message m) => BrokerService -> m [Notification]
getNotifications service = do getNotifications service = do
v <- Bro.getNotifications (broker service) v <- liftIO $ Bro.getNotifications (broker service)
case v of case v of
Left _ -> return [] Left err -> do
logWarning "BrokerServer" $ "Get notifications order error: " <> err
return []
Right n -> return n Right n -> return n

258
src/ATrade/Driver/Junction/JunctionMonad.hs

@ -0,0 +1,258 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
module ATrade.Driver.Junction.JunctionMonad
(
JunctionEnv(..),
JunctionM(..),
startRobot,
saveRobots,
reloadConfig,
getState,
setState
) where
import ATrade.Broker.Client (BrokerClientHandle)
import ATrade.Driver.Junction.BrokerService (BrokerService)
import ATrade.Driver.Junction.ProgramConfiguration (ProgramConfiguration (logBasePath))
import ATrade.Driver.Junction.QuoteStream (QuoteStream (addSubscription, removeSubscription),
QuoteSubscription (QuoteSubscription))
import ATrade.Driver.Junction.QuoteThread (QuoteThreadHandle)
import qualified ATrade.Driver.Junction.QuoteThread as QT
import ATrade.Driver.Junction.RobotDriverThread (RobotDriverHandle, RobotEnv (RobotEnv),
RobotM (unRobotM),
createRobotDriverThread,
getInstanceDescriptor,
onStrategyInstance,
onStrategyInstanceM)
import ATrade.Driver.Junction.Types (StrategyDescriptorE (StrategyDescriptorE),
StrategyInstanceDescriptor,
accountId,
confStrategy,
confTickers,
configKey,
stateKey,
strategyBaseName,
strategyConfig,
strategyId,
strategyInstanceId,
strategyState,
strategyTimers,
tickerId,
timeframe)
import ATrade.Logging (Message, Severity (Error, Info),
fmtMessage,
logWarning,
logWith)
import ATrade.RoboCom.ConfigStorage (ConfigStorage (loadConfig))
import ATrade.RoboCom.Monad (StrategyEnvironment (..))
import ATrade.RoboCom.Persistence (MonadPersistence (loadState, saveState))
import ATrade.RoboCom.Types (BarSeriesId (BarSeriesId),
Bars,
TickerInfoMap)
import Colog (HasLog (getLogAction, setLogAction),
LogAction,
hoistLogAction,
logTextHandle,
(>$<))
import Control.Exception.Safe (finally)
import Control.Monad.Reader (MonadIO (liftIO),
MonadReader,
ReaderT (runReaderT),
asks)
import Data.Aeson (decode,
eitherDecode,
encode)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import Data.Default (Default (def))
import Data.Foldable (traverse_)
import Data.IORef (IORef,
atomicModifyIORef',
newIORef,
readIORef,
writeIORef)
import Data.List.NonEmpty (NonEmpty ((:|)))
import qualified Data.Map.Strict as M
import qualified Data.Text as T
import Data.Text.Encoding (encodeUtf8)
import Data.Text.IO (readFile)
import Data.Time (getCurrentTime)
import Data.Time.Clock.POSIX (getPOSIXTime)
import Database.Redis (Connection, get,
mset, runRedis)
import Dhall (auto, input)
import Prelude hiding (log,
readFile)
import System.IO (BufferMode (LineBuffering),
IOMode (AppendMode),
hClose,
hSetBuffering,
openFile)
import System.ZMQ4 (Router, Socket)
import UnliftIO (MonadUnliftIO)
import UnliftIO.Exception (catchAny,
onException)
data JunctionEnv =
JunctionEnv
{
peRedisSocket :: Connection,
peConfigPath :: FilePath,
peQuoteThread :: QuoteThreadHandle,
peBroker :: BrokerClientHandle,
peRobots :: IORef (M.Map T.Text RobotDriverHandle),
peRemoteControlSocket :: Socket Router,
peLogAction :: LogAction JunctionM Message,
peIoLogAction :: LogAction IO Message,
peProgramConfiguration :: ProgramConfiguration,
peBarsMap :: IORef Bars,
peTickerInfoMap :: IORef TickerInfoMap,
peBrokerService :: BrokerService,
peDescriptors :: M.Map T.Text StrategyDescriptorE
}
newtype JunctionM a = JunctionM { unJunctionM :: ReaderT JunctionEnv IO a }
deriving (Functor, Applicative, Monad, MonadReader JunctionEnv, MonadIO, MonadUnliftIO)
instance HasLog JunctionEnv Message JunctionM where
getLogAction = peLogAction
setLogAction a e = e { peLogAction = a }
instance ConfigStorage JunctionM where
loadConfig key = do
basePath <- asks peConfigPath
let path = basePath <> "/" <> T.unpack key -- TODO fix path construction
liftIO $ readFile path >>= input auto
instance MonadPersistence JunctionM where
saveState newState key = do
conn <- asks peRedisSocket
now <- liftIO getPOSIXTime
res <- liftIO $ runRedis conn $ mset [(encodeUtf8 key, BL.toStrict $ encode newState),
(encodeUtf8 (key <> ":last_store") , encodeUtf8 . T.pack . show $ now)]
case res of
Left _ -> logWarning "Junction " "Unable to save state"
Right _ -> return ()
loadState key = do
conn <- asks peRedisSocket
res <- liftIO $ runRedis conn $ get (encodeUtf8 key)
-- TODO: just chain eithers
case res of
Left _ -> do
logWarning "Junction" "Unable to load state"
return def
Right maybeRawState ->
case maybeRawState of
Just rawState -> case eitherDecode $ BL.fromStrict rawState of
Left _ -> do
logWarning "Junction" "Unable to decode state"
return def
Right decodedState -> return decodedState
Nothing -> do
logWarning "Junction" "Unable to decode state"
return def
instance QuoteStream JunctionM where
addSubscription (QuoteSubscription ticker tf) chan = do
qt <- asks peQuoteThread
QT.addSubscription qt ticker tf chan
removeSubscription subId = do
qt <- asks peQuoteThread
QT.removeSubscription qt subId
startRobot :: StrategyInstanceDescriptor -> JunctionM ()
startRobot inst = do
ioLogger <- asks peIoLogAction
descriptors <- asks peDescriptors
cfg <- asks peProgramConfiguration
barsMap <- asks peBarsMap
tickerInfoMap <- asks peTickerInfoMap
broService <- asks peBrokerService
now <- liftIO getCurrentTime
let lLogger = hoistLogAction liftIO ioLogger
logWith lLogger Info "Junction" $ "Starting strategy: " <> strategyBaseName inst
case M.lookup (strategyBaseName inst) descriptors of
Just (StrategyDescriptorE desc) -> flip catchAny (\e -> logWith lLogger Error "Junction" $ "Exception: " <> (T.pack . show $ e)) $ do
bigConf <- loadConfig (configKey inst)
case confTickers bigConf of
(firstTicker:restTickers) -> do
rConf <- liftIO $ newIORef (confStrategy bigConf)
rState <- loadState (stateKey inst) >>= liftIO . newIORef
rTimers <- loadState (stateKey inst <> ":timers") >>= liftIO . newIORef
localH <- liftIO $ openFile (logBasePath cfg <> "/" <> T.unpack (strategyId inst) <> ".log") AppendMode
liftIO $ hSetBuffering localH LineBuffering
let robotLogAction = hoistLogAction liftIO ioLogger <> (fmtMessage >$< logTextHandle localH)
stratEnv <- liftIO $ newIORef StrategyEnvironment
{
_seInstanceId = strategyId inst,
_seAccount = accountId inst,
_seVolume = 1,
_seLastTimestamp = now
}
let robotEnv =
RobotEnv rState rConf rTimers barsMap tickerInfoMap stratEnv robotLogAction broService (toBarSeriesId <$> (firstTicker :| restTickers))
robot <- createRobotDriverThread inst desc (\a -> (flip runReaderT robotEnv . unRobotM) a `finally` hClose localH) bigConf rConf rState rTimers
robotsMap' <- asks peRobots
liftIO $ atomicModifyIORef' robotsMap' (\s -> (M.insert (strategyId inst) robot s, ()))
_ -> logWith lLogger Error (strategyId inst) "No tickers configured !!!"
Nothing -> logWith lLogger Error "Junction" $ "Unknown strategy: " <> strategyBaseName inst
where
toBarSeriesId t = BarSeriesId (tickerId t) (timeframe t)
saveRobots :: JunctionM ()
saveRobots = do
robotsMap <- asks peRobots >>= (liftIO . readIORef)
traverse_ saveRobotState robotsMap
saveRobotState :: RobotDriverHandle -> JunctionM ()
saveRobotState handle = onStrategyInstance handle $ \inst -> do
currentState <- liftIO $ readIORef (strategyState inst)
saveState currentState (strategyInstanceId inst)
currentTimers <- liftIO $ readIORef (strategyTimers inst)
saveState currentTimers (strategyInstanceId inst <> ":timers")
reloadConfig :: T.Text -> JunctionM (Either T.Text ())
reloadConfig instId = flip catchAny (\_ -> return $ Left "Exception") $ do
robotsMap' <- asks peRobots
robots <- liftIO $ readIORef robotsMap'
case M.lookup instId robots of
Just robot -> do
onStrategyInstanceM robot
(\inst -> do
let instDesc = getInstanceDescriptor robot
bigConf <- loadConfig (configKey instDesc)
liftIO $ writeIORef (strategyConfig inst) (confStrategy bigConf))
return $ Right ()
Nothing -> return $ Left "Unable to load config"
getState :: T.Text -> JunctionM (Either T.Text B.ByteString)
getState instId = do
robotsMap' <- asks peRobots
robots <- liftIO $ readIORef robotsMap'
case M.lookup instId robots of
Just robot -> do
Right <$> onStrategyInstanceM robot
(\inst -> do
v <- liftIO $ readIORef (strategyState inst)
return $ BL.toStrict $ encode v)
Nothing -> return $ Left $ "Unknown robot: " <> instId
setState :: T.Text -> B.ByteString -> JunctionM (Either T.Text ())
setState instId newState = do
robotsMap' <- asks peRobots
robots <- liftIO $ readIORef robotsMap'
case M.lookup instId robots of
Just robot -> do
onStrategyInstanceM robot
(\inst -> do
case decode . BL.fromStrict $ newState of
Just newS -> do
liftIO $ writeIORef (strategyState inst) newS
return $ Right ()
Nothing -> return $ Left $ "Unable to decode state for " <> instId)
Nothing -> return $ Left $ "Unknown robot: " <> instId

39
src/ATrade/Driver/Junction/ProgramConfiguration.hs

@ -1,4 +1,7 @@
{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module ATrade.Driver.Junction.ProgramConfiguration module ATrade.Driver.Junction.ProgramConfiguration
( (
@ -6,8 +9,12 @@ module ATrade.Driver.Junction.ProgramConfiguration
ProgramConfiguration(..) ProgramConfiguration(..)
) where ) where
import ATrade.Driver.Junction.Types (StrategyInstanceDescriptor) import ATrade.Driver.Junction.Types (StrategyInstanceDescriptor)
import ATrade.Logging (Severity (..))
import qualified Data.Text as T import qualified Data.Text as T
import Dhall (FromDhall) import Dhall (FromDhall, autoWith)
import Dhall.Core (Expr (..), FieldSelection (..))
import qualified Dhall.Map
import Dhall.Marshal.Decode (Decoder (..), typeError)
import GHC.Generics (Generic) import GHC.Generics (Generic)
newtype ProgramOptions = newtype ProgramOptions =
@ -23,15 +30,43 @@ data ProgramConfiguration =
brokerNotificationEndpoint :: T.Text, brokerNotificationEndpoint :: T.Text,
brokerServerCert :: Maybe FilePath, brokerServerCert :: Maybe FilePath,
brokerClientCert :: Maybe FilePath, brokerClientCert :: Maybe FilePath,
brokerIdentity :: T.Text,
quotesourceEndpoint :: T.Text, quotesourceEndpoint :: T.Text,
quotesourceServerCert :: Maybe FilePath, quotesourceServerCert :: Maybe FilePath,
quotesourceClientCert :: Maybe FilePath, quotesourceClientCert :: Maybe FilePath,
qhpEndpoint :: T.Text, qhpEndpoint :: T.Text,
qtisEndpoint :: T.Text, qtisEndpoint :: T.Text,
remoteControlEndpoint :: T.Text,
redisSocket :: T.Text, redisSocket :: T.Text,
robotsConfigsPath :: FilePath, robotsConfigsPath :: FilePath,
logBasePath :: FilePath, logBasePath :: FilePath,
logLevels :: [(T.Text, Severity)],
instances :: [StrategyInstanceDescriptor] instances :: [StrategyInstanceDescriptor]
} deriving (Generic, Show) } deriving (Generic, Show)
instance FromDhall Severity where
autoWith _ = Decoder {..}
where
extract expr@(Field _ FieldSelection{ fieldSelectionLabel }) =
case fieldSelectionLabel of
"Trace" -> pure Trace
"Debug" -> pure Debug
"Info" -> pure Info
"Warning" -> pure Warning
"Error" -> pure Error
_ -> typeError expected expr
extract expr = typeError expected expr
expected = pure
(Union
(Dhall.Map.fromList
[ ("Trace", Nothing)
, ("Debug", Nothing)
, ("Info", Nothing)
, ("Warning", Nothing)
, ("Error", Nothing)
]
)
)
instance FromDhall ProgramConfiguration instance FromDhall ProgramConfiguration

3
src/ATrade/Driver/Junction/QuoteStream.hs

@ -21,6 +21,9 @@ instance Hashable BarTimeframe
instance Hashable QuoteSubscription instance Hashable QuoteSubscription
newtype SubscriptionId = SubscriptionId { unSubscriptionId :: Int } newtype SubscriptionId = SubscriptionId { unSubscriptionId :: Int }
deriving (Show, Eq, Generic)
instance Hashable SubscriptionId
class (Monad m) => QuoteStream m where class (Monad m) => QuoteStream m where
addSubscription :: QuoteSubscription -> BoundedChan QuoteData -> m SubscriptionId addSubscription :: QuoteSubscription -> BoundedChan QuoteData -> m SubscriptionId

166
src/ATrade/Driver/Junction/QuoteThread.hs

@ -4,6 +4,7 @@
{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-} {-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeSynonymInstances #-} {-# LANGUAGE TypeSynonymInstances #-}
@ -13,15 +14,20 @@ module ATrade.Driver.Junction.QuoteThread
startQuoteThread, startQuoteThread,
stopQuoteThread, stopQuoteThread,
addSubscription, addSubscription,
removeSubscription,
DownloaderM, DownloaderM,
DownloaderEnv(..), DownloaderEnv(..),
runDownloaderM, runDownloaderM,
withQThread withQThread
) where ) where
import qualified ATrade.BarAggregator as BA
import ATrade.Driver.Junction.ProgramConfiguration (ProgramConfiguration (..)) import ATrade.Driver.Junction.ProgramConfiguration (ProgramConfiguration (..))
import ATrade.Driver.Junction.QuoteStream (QuoteSubscription (..)) import ATrade.Driver.Junction.QuoteStream (QuoteSubscription (..),
import ATrade.Logging (Message) SubscriptionId (SubscriptionId))
import ATrade.Logging (Message, logDebug,
logInfo,
logWarning)
import ATrade.Quotes.HistoryProvider (HistoryProvider (..)) import ATrade.Quotes.HistoryProvider (HistoryProvider (..))
import ATrade.Quotes.QHP (QHPHandle, requestHistoryFromQHP) import ATrade.Quotes.QHP (QHPHandle, requestHistoryFromQHP)
import ATrade.Quotes.QTIS (TickerInfo (tiLotSize, tiTickSize, tiTicker), import ATrade.Quotes.QTIS (TickerInfo (tiLotSize, tiTickSize, tiTicker),
@ -36,7 +42,8 @@ import ATrade.RoboCom.Types (Bar (barSecurity),
BarSeries (..), BarSeries (..),
BarSeriesId (BarSeriesId), BarSeriesId (BarSeriesId),
Bars, Bars,
InstrumentParameters (InstrumentParameters)) InstrumentParameters (InstrumentParameters),
TickerInfoMap)
import ATrade.Types (BarTimeframe (BarTimeframe), import ATrade.Types (BarTimeframe (BarTimeframe),
ClientSecurityParams (ClientSecurityParams), ClientSecurityParams (ClientSecurityParams),
Tick (security), Tick (security),
@ -49,11 +56,13 @@ import Control.Concurrent (ThreadId, forkIO,
import Control.Concurrent.BoundedChan (BoundedChan, import Control.Concurrent.BoundedChan (BoundedChan,
newBoundedChan, newBoundedChan,
readChan, readChan,
tryWriteChan,
writeChan) writeChan)
import Control.Exception.Safe (MonadMask, import Control.Exception.Safe (MonadMask,
MonadThrow, MonadThrow,
bracket) bracket)
import Control.Monad (forM, forever) import Control.Monad (forM, forM_,
forever)
import Control.Monad.Reader (MonadIO (liftIO), ReaderT (runReaderT), import Control.Monad.Reader (MonadIO (liftIO), ReaderT (runReaderT),
lift) lift)
import Control.Monad.Reader.Class (MonadReader, asks) import Control.Monad.Reader.Class (MonadReader, asks)
@ -76,54 +85,84 @@ data QuoteThreadEnv =
QuoteThreadEnv QuoteThreadEnv
{ {
bars :: IORef Bars, bars :: IORef Bars,
endpoints :: IORef (HM.HashMap QuoteSubscription [BoundedChan QuoteData]), endpoints :: IORef (HM.HashMap QuoteSubscription [(SubscriptionId, BoundedChan QuoteData)]),
qsclient :: QuoteSourceClientHandle, qsclient :: QuoteSourceClientHandle,
paramsCache :: IORef (M.Map TickerId InstrumentParameters), paramsCache :: IORef TickerInfoMap,
downloaderChan :: BoundedChan QuoteSubscription downloaderChan :: BoundedChan QuoteSubscription,
subscriptionIdCounter :: IORef Int,
subscriptions :: IORef (HM.HashMap SubscriptionId QuoteSubscription),
aggregators :: IORef (HM.HashMap (TickerId, BarTimeframe) BA.BarAggregator)
} }
startQuoteThread :: (MonadIO m, startQuoteThread :: (MonadIO m,
MonadIO m1, MonadIO m1,
WithLog DownloaderEnv Message m1, WithLog env Message m1,
HistoryProvider m1, HistoryProvider m1,
TickerInfoProvider m1) => TickerInfoProvider m1) =>
IORef Bars -> IORef Bars ->
IORef TickerInfoMap ->
Context -> Context ->
T.Text -> T.Text ->
ClientSecurityParams -> ClientSecurityParams ->
(m1 () -> IO ()) -> (m1 () -> IO ()) ->
LogAction IO Message -> LogAction IO Message ->
m QuoteThreadHandle m QuoteThreadHandle
startQuoteThread barsRef ctx ep secparams downloadThreadRunner logger = do startQuoteThread barsRef tiRef ctx ep secparams downloadThreadRunner logger = do
chan <- liftIO $ newBoundedChan 2000 chan <- liftIO $ newBoundedChan 2000
dChan <- liftIO $ newBoundedChan 2000 dChan <- liftIO $ newBoundedChan 2000
qsc <- liftIO $ startQuoteSourceClient chan [] ctx ep secparams logger qsc <- liftIO $ startQuoteSourceClient chan [] ctx ep secparams logger
env <- liftIO $ QuoteThreadEnv barsRef <$> newIORef HM.empty <*> pure qsc <*> newIORef M.empty <*> pure dChan env <- liftIO $ QuoteThreadEnv barsRef <$> newIORef HM.empty <*> pure qsc <*> pure tiRef <*> pure dChan <*> newIORef 0 <*> newIORef HM.empty <*> newIORef HM.empty
tid <- liftIO . forkIO $ quoteThread env chan tid <- liftIO . forkIO $ quoteThread env chan
downloaderTid <- liftIO . forkIO $ downloadThreadRunner (downloaderThread env dChan) downloaderTid <- liftIO . forkIO $ downloadThreadRunner (downloaderThread env dChan)
return $ QuoteThreadHandle tid downloaderTid env return $ QuoteThreadHandle tid downloaderTid env
where where
downloaderThread env chan = forever $ do downloaderThread env chan = do
QuoteSubscription tickerid tf <- liftIO $ readChan chan logInfo "QuoteThread" "Started"
paramsMap <- liftIO $ readIORef $ paramsCache env forever $ do
mbParams <- case M.lookup tickerid paramsMap of QuoteSubscription tickerid tf <- liftIO $ readChan chan
Nothing -> do logInfo "QuoteThread" $ "Subscription: " <> tickerid
paramsList <- getInstrumentParameters [tickerid] paramsMap <- liftIO $ readIORef $ paramsCache env
case paramsList of mbParams <- case M.lookup tickerid paramsMap of
(params:_) -> liftIO $ atomicModifyIORef' (paramsCache env) (\m -> (M.insert tickerid params m, Just params)) Nothing -> do
_ -> return Nothing paramsList <- getInstrumentParameters [tickerid]
Just params -> return $ Just params case paramsList of
barsMap <- liftIO $ readIORef (bars env) (params:_) -> liftIO $ atomicModifyIORef' (paramsCache env) (\m -> (M.insert tickerid params m, Just params))
case M.lookup (BarSeriesId tickerid tf) barsMap of _ -> return Nothing
Just _ -> return () -- already downloaded Just params -> return $ Just params
Nothing -> case mbParams of logDebug "QuoteThread" $ "Got info params: " <> (T.pack . show $ mbParams)
Just params -> do barsMap <- liftIO $ readIORef (bars env)
now <- liftIO getCurrentTime case M.lookup (BarSeriesId tickerid tf) barsMap of
barsData <- getHistory tickerid tf ((-86400 * 60) `addUTCTime` now) now Just _ -> return () -- already downloaded
let barSeries = BarSeries tickerid tf barsData params Nothing -> case mbParams of
liftIO $ atomicModifyIORef' (bars env) (\m -> (M.insert (BarSeriesId tickerid tf) barSeries m, ())) Just params -> do
_ -> return () -- TODO log now <- liftIO getCurrentTime
-- Load data in interval [today - 60days; today + 1day]. +1 day guarantees that we will download data up until current time.
-- If we don't make this adjustment it is possible that we will get data only up to beginning of current day.
barsData <- getHistory tickerid tf ((-86400 * 60) `addUTCTime` now) (86400 `addUTCTime` now)
let barSeries = BarSeries tickerid tf barsData params
liftIO $ atomicModifyIORef' (bars env) (\m -> (M.insert (BarSeriesId tickerid tf) barSeries m, ()))
_ -> logWarning "QuoteThread" $ "Unable to find parameters for: " <> (T.pack . show $ BarSeriesId tickerid tf)
pushToBarAggregators tick = forM_ (BarTimeframe <$> [60, 300, 900, 3600]) (pushTickToAggregator tick)
pushTickToAggregator tick tf = do
aggsRef <- asks aggregators
aggs <- liftIO . readIORef $ aggsRef
let key = (security tick, tf)
case HM.lookup key aggs of
Just agg -> do
let (mbar, agg') = BA.handleTick tick agg
liftIO $ atomicModifyIORef' aggsRef (\m -> (HM.insert key agg' m, ()))
barsRef' <- asks bars
case mbar of
Just bar -> do
liftIO $ atomicModifyIORef' barsRef' (\x -> (updateBarsMap x bar tf, ()))
writeBarData bar tf (QDBar (tf, bar))
_ -> do
pure ()
_ -> do
let agg = BA.mkAggregatorFromBars (M.singleton (security tick) (BarSeries (security tick) tf [] (InstrumentParameters (security tick) 1 1))) [(0, 86400)]
liftIO $ atomicModifyIORef' aggsRef (\m -> (HM.insert key agg m, ()))
quoteThread env chan = flip runReaderT env $ forever $ do quoteThread env chan = flip runReaderT env $ forever $ do
qssData <- lift $ readChan chan qssData <- lift $ readChan chan
@ -131,14 +170,25 @@ startQuoteThread barsRef ctx ep secparams downloadThreadRunner logger = do
QDBar (tf, bar) -> do QDBar (tf, bar) -> do
barsRef' <- asks bars barsRef' <- asks bars
lift $ atomicModifyIORef' barsRef' (\x -> (updateBarsMap x bar tf, ())) lift $ atomicModifyIORef' barsRef' (\x -> (updateBarsMap x bar tf, ()))
_ -> return () -- TODO pass to bar aggregator writeBarData bar tf qssData
let key = case qssData of QDTick tick -> do
QDTick tick -> QuoteSubscription (security tick) (BarTimeframe 0) pushToBarAggregators tick
QDBar (tf, bar) -> QuoteSubscription (barSecurity bar) tf writeTickData tick qssData
writeTickData tick qssData = do
let key = QuoteSubscription (security tick) (BarTimeframe 0)
subs <- asks endpoints >>= (lift . readIORef)
case HM.lookup key subs of
Just clientChannels -> do
lift $ mapM_ (\(_, chan') -> tryWriteChan chan' qssData) clientChannels
Nothing -> return ()
writeBarData bar tf qssData = do
let key = QuoteSubscription (barSecurity bar) tf
subs <- asks endpoints >>= (lift . readIORef) subs <- asks endpoints >>= (lift . readIORef)
case HM.lookup key subs of case HM.lookup key subs of
Just clientChannels -> do Just clientChannels -> do
lift $ mapM_ (`writeChan` qssData) clientChannels lift $ mapM_ (\(_, chan') -> tryWriteChan chan' qssData) clientChannels
Nothing -> return () Nothing -> return ()
stopQuoteThread :: (MonadIO m) => QuoteThreadHandle -> m () stopQuoteThread :: (MonadIO m) => QuoteThreadHandle -> m ()
@ -147,19 +197,36 @@ stopQuoteThread (QuoteThreadHandle tid dtid env) = liftIO $ do
killThread dtid killThread dtid
stopQuoteSourceClient (qsclient env) stopQuoteSourceClient (qsclient env)
addSubscription :: (MonadIO m) => QuoteThreadHandle -> TickerId -> BarTimeframe -> BoundedChan QuoteData -> m () addSubscription :: (MonadIO m) => QuoteThreadHandle -> TickerId -> BarTimeframe -> BoundedChan QuoteData -> m SubscriptionId
addSubscription (QuoteThreadHandle _ _ env) tid tf chan = liftIO $ do addSubscription (QuoteThreadHandle _ _ env) tid tf chan = liftIO $ do
writeChan (downloaderChan env) (QuoteSubscription tid tf) cnt <- atomicModifyIORef' (subscriptionIdCounter env) (\c -> (c + 1, c))
atomicModifyIORef' (endpoints env) (\m -> (doAddSubscription m tid, ())) let subscription = QuoteSubscription tid tf
let subid = SubscriptionId cnt
writeChan (downloaderChan env) subscription
atomicModifyIORef' (endpoints env) (\m -> (doAddSubscription m subid tid, ()))
atomicModifyIORef' (subscriptions env) (\m -> (HM.insert subid subscription m, ()))
quoteSourceClientSubscribe (qsclient env) [(tid, BarTimeframe 0)] quoteSourceClientSubscribe (qsclient env) [(tid, BarTimeframe 0)]
return subid
where where
doAddSubscription m tickerid = doAddSubscription m subid tickerid =
let m1 = HM.alter (\case let m1 = HM.alter (\case
Just chans -> Just (chan : chans) Just chans -> Just ((subid, chan) : chans)
_ -> Just [chan]) (QuoteSubscription tickerid tf) m in _ -> Just [(subid, chan)]) (QuoteSubscription tickerid tf) m in
HM.alter (\case HM.alter (\case
Just chans -> Just (chan : chans) Just chans -> Just ((subid, chan) : chans)
_ -> Just [chan]) (QuoteSubscription tickerid (BarTimeframe 0)) m1 _ -> Just [(subid, chan)]) (QuoteSubscription tickerid (BarTimeframe 0)) m1
removeSubscription :: (MonadIO m) => QuoteThreadHandle -> SubscriptionId -> m ()
removeSubscription (QuoteThreadHandle _ _ env) subId = liftIO $ do
subs <- readIORef (subscriptions env)
case HM.lookup subId subs of
Just sub -> atomicModifyIORef' (endpoints env) (\m -> (doRemoveSubscription m sub, ()))
Nothing -> return ()
where
doRemoveSubscription m sub =
let m1 = HM.adjust (filter (\(subId', _) -> subId' == subId)) sub m in
HM.adjust (filter (\(subId', _) -> subId' == subId)) (sub0 sub) m1
sub0 sub = let QuoteSubscription tid _ = sub in QuoteSubscription tid (BarTimeframe 0)
updateBarsMap :: Bars -> Bar -> BarTimeframe -> Bars updateBarsMap :: Bars -> Bar -> BarTimeframe -> Bars
updateBarsMap barsMap bar tf = M.adjust (addToSeries bar) (BarSeriesId (barSecurity bar) tf) barsMap updateBarsMap barsMap bar tf = M.adjust (addToSeries bar) (BarSeriesId (barSecurity bar) tf) barsMap
@ -200,12 +267,21 @@ instance TickerInfoProvider DownloaderM where
(fromInteger $ tiLotSize ti) (fromInteger $ tiLotSize ti)
(tiTickSize ti) (tiTickSize ti)
withQThread :: DownloaderEnv -> IORef Bars -> ProgramConfiguration -> Context -> LogAction IO Message -> (QuoteThreadHandle -> IO ()) -> IO () withQThread ::
withQThread env barsMap cfg ctx logger f = do DownloaderEnv
-> IORef Bars
-> IORef TickerInfoMap
-> ProgramConfiguration
-> Context
-> LogAction IO Message
-> (QuoteThreadHandle -> IO ())
-> IO ()
withQThread env barsMap tiMap cfg ctx logger f = do
securityParameters <- loadSecurityParameters securityParameters <- loadSecurityParameters
bracket bracket
(startQuoteThread (startQuoteThread
barsMap barsMap
tiMap
ctx ctx
(quotesourceEndpoint cfg) (quotesourceEndpoint cfg)
securityParameters securityParameters

151
src/ATrade/Driver/Junction/RemoteControl.hs

@ -0,0 +1,151 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
module ATrade.Driver.Junction.RemoteControl
(
handleRemoteControl
) where
import ATrade.Driver.Junction.JunctionMonad (JunctionEnv (peLogAction, peRemoteControlSocket, peRobots),
JunctionM, getState,
reloadConfig,
setState, startRobot)
import ATrade.Driver.Junction.RobotDriverThread (stopRobot)
import ATrade.Driver.Junction.Types (StrategyInstanceDescriptor)
import ATrade.Logging (Severity (Info),
logErrorWith,
logWith)
import Control.Monad (unless)
import Control.Monad.Reader (asks)
import Data.Aeson (decode)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import Data.List.NonEmpty (NonEmpty ((:|)))
import qualified Data.Map.Strict as M
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8',
encodeUtf8)
import System.ZMQ4 (Event (In),
Poll (Sock), poll,
receiveMulti,
sendMulti)
import UnliftIO (MonadIO (liftIO),
atomicModifyIORef',
readIORef)
data RemoteControlResponse =
ResponseOk
| ResponseError T.Text
| ResponseData B.ByteString
deriving (Show, Eq)
data RemoteControlRequest =
StartRobot StrategyInstanceDescriptor
| StopRobot T.Text
| ReloadConfig T.Text
| GetState T.Text
| SetState T.Text B.ByteString
| Ping
deriving (Show)
data ParseError =
UnknownCmd
| UtfDecodeError
| JsonDecodeError
deriving (Show, Eq)
parseRemoteControlRequest :: B.ByteString -> Either ParseError RemoteControlRequest
parseRemoteControlRequest bs =
if
| cmd == "START" -> parseStart
| cmd == "STOP" -> parseStop
| cmd == "RELOAD_CONFIG" -> parseReloadConfig
| cmd == "GET_STATE" -> parseGetState
| cmd == "SET_STATE" -> parseSetState
| cmd == "PING" -> Right Ping
| otherwise -> Left UnknownCmd
where
cmd = B.takeWhile (/= 0x20) bs
rest = B.dropWhile (== 0x20) . B.dropWhile (/= 0x20) $ bs
parseStart = case decode . BL.fromStrict $ rest of
Just inst -> Right (StartRobot inst)
Nothing -> Left JsonDecodeError
parseStop = case decodeUtf8' rest of
Left _ -> Left UtfDecodeError
Right r -> Right (StopRobot (T.strip r))
parseReloadConfig = case decodeUtf8' rest of
Left _ -> Left UtfDecodeError
Right r -> Right (ReloadConfig (T.strip r))
parseGetState = case decodeUtf8' (B.takeWhile (/= 0x20) rest) of
Left _ -> Left UtfDecodeError
Right r -> Right (GetState r)
parseSetState = case decodeUtf8' (B.takeWhile (/= 0x20) rest) of
Left _ -> Left UtfDecodeError
Right r -> Right (SetState r (B.dropWhile (== 0x20) . B.dropWhile (/= 0x20) $ rest))
makeRemoteControlResponse :: RemoteControlResponse -> B.ByteString
makeRemoteControlResponse ResponseOk = "OK"
makeRemoteControlResponse (ResponseError msg) = "ERROR " <> encodeUtf8 msg
makeRemoteControlResponse (ResponseData d) = "DATA\n" <> d
handleRemoteControl :: Int -> JunctionM ()
handleRemoteControl timeout = do
sock <- asks peRemoteControlSocket
logger <- asks peLogAction
evs <- poll (fromIntegral timeout) [Sock sock [In] Nothing]
case evs of
(x:_) -> unless (null x) $ do
frames <- liftIO $ receiveMulti sock
case frames of
[peerId, _, rawRequest] -> do
case parseRemoteControlRequest rawRequest of
Left err -> logErrorWith logger "RemoteControl" ("Unable to parse request: " <> (T.pack . show) err)
Right request -> do
response <- handleRequest request
liftIO $ sendMulti sock $ peerId :| [B.empty, makeRemoteControlResponse response]
_ -> logErrorWith logger "RemoteControl" "Invalid incoming request"
_ -> return ()
where
handleRequest (StartRobot inst) = do
startRobot inst
return ResponseOk
handleRequest (StopRobot instId) = do
robotsRef <- asks peRobots
robots <- readIORef robotsRef
case M.lookup instId robots of
Just robot -> do
logger <- asks peLogAction
logWith logger Info "RemoteControl" $ "Stopping robot: " <> instId
stopRobot robot
liftIO $ atomicModifyIORef' robotsRef (\r -> (M.delete instId r, ()))
return ResponseOk
Nothing -> return $ ResponseError $ "Not started: " <> instId
handleRequest (ReloadConfig instId) = do
res <- reloadConfig instId
case res of
Left errmsg -> return $ ResponseError errmsg
Right () -> return ResponseOk
handleRequest (GetState instId) = do
res <- getState instId
case res of
Left errmsg -> return $ ResponseError errmsg
Right d -> return $ ResponseData d
handleRequest (SetState instId rawState) = do
res <- setState instId rawState
case res of
Left errmsg -> return $ ResponseError errmsg
Right () -> return ResponseOk
handleRequest Ping = return ResponseOk

64
src/ATrade/Driver/Junction/RobotDriverThread.hs

@ -13,12 +13,17 @@ module ATrade.Driver.Junction.RobotDriverThread
RobotM(..), RobotM(..),
RobotDriverHandle, RobotDriverHandle,
onStrategyInstance, onStrategyInstance,
postNotificationEvent) where onStrategyInstanceM,
postNotificationEvent,
stopRobot,
getInstanceDescriptor
) where
import ATrade.Broker.Protocol (Notification (OrderNotification, TradeNotification)) import ATrade.Broker.Protocol (Notification (OrderNotification, TradeNotification))
import qualified ATrade.Driver.Junction.BrokerService as Bro import qualified ATrade.Driver.Junction.BrokerService as Bro
import ATrade.Driver.Junction.QuoteStream (QuoteStream (addSubscription), import ATrade.Driver.Junction.QuoteStream (QuoteStream (addSubscription, removeSubscription),
QuoteSubscription (QuoteSubscription)) QuoteSubscription (QuoteSubscription),
SubscriptionId)
import ATrade.Driver.Junction.Types (BigConfig, import ATrade.Driver.Junction.Types (BigConfig,
StrategyDescriptor, StrategyDescriptor,
StrategyInstance (StrategyInstance, strategyEventCallback), StrategyInstance (StrategyInstance, strategyEventCallback),
@ -28,31 +33,32 @@ import ATrade.Driver.Junction.Types (BigConfig,
eventCallback, stateKey, eventCallback, stateKey,
strategyId, tickerId, strategyId, tickerId,
timeframe) timeframe)
import ATrade.Logging (Message, log, logDebug, import ATrade.Logging (Message, log)
logInfo, logWarning)
import ATrade.QuoteSource.Client (QuoteData (..)) import ATrade.QuoteSource.Client (QuoteData (..))
import ATrade.RoboCom.ConfigStorage (ConfigStorage) import ATrade.RoboCom.ConfigStorage (ConfigStorage)
import ATrade.RoboCom.Monad (Event (NewBar, NewTick, NewTrade, OrderSubmitted, OrderUpdate), import ATrade.RoboCom.Monad (Event (NewBar, NewTick, NewTrade, OrderUpdate),
MonadRobot (..), MonadRobot (..),
StrategyEnvironment (StrategyEnvironment, _seInstanceId, _seLastTimestamp)) StrategyEnvironment (..))
import ATrade.RoboCom.Persistence (MonadPersistence) import ATrade.RoboCom.Persistence (MonadPersistence)
import ATrade.RoboCom.Types (BarSeriesId (BarSeriesId), import ATrade.RoboCom.Types (BarSeriesId (BarSeriesId),
Bars) Bars, TickerInfoMap)
import ATrade.Types (Order (orderId), OrderId, import ATrade.Types (OrderId, OrderState,
OrderState, Trade) Tick (value), Trade)
import Colog (HasLog (getLogAction, setLogAction), import Colog (HasLog (getLogAction, setLogAction),
LogAction) LogAction)
import Control.Concurrent (ThreadId, forkIO) import Control.Concurrent (ThreadId, forkIO,
killThread)
import Control.Concurrent.BoundedChan (BoundedChan, import Control.Concurrent.BoundedChan (BoundedChan,
newBoundedChan, readChan, newBoundedChan, readChan,
writeChan) writeChan)
import Control.Exception.Safe (MonadThrow) import Control.Exception.Safe (MonadThrow)
import Control.Monad (forM_, forever, void) import Control.Monad (forM, forM_, forever,
void, when)
import Control.Monad.IO.Class (MonadIO, liftIO) import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.Reader (MonadReader (local), import Control.Monad.Reader (MonadReader (local),
ReaderT, asks) ReaderT, asks)
import Data.Aeson (FromJSON, ToJSON) import Data.Aeson (FromJSON, ToJSON)
import Data.Default import Data.Default (Default)
import Data.IORef (IORef, import Data.IORef (IORef,
atomicModifyIORef', atomicModifyIORef',
readIORef, writeIORef) readIORef, writeIORef)
@ -64,7 +70,7 @@ import Dhall (FromDhall)
import Prelude hiding (log) import Prelude hiding (log)
data RobotDriverHandle = forall c s. (FromDhall c, Default s, FromJSON s, ToJSON s) => data RobotDriverHandle = forall c s. (FromDhall c, Default s, FromJSON s, ToJSON s) =>
RobotDriverHandle (StrategyInstance c s) ThreadId ThreadId (BoundedChan RobotDriverEvent) RobotDriverHandle StrategyInstanceDescriptor (StrategyInstance c s) ThreadId ThreadId (BoundedChan RobotDriverEvent) [SubscriptionId]
data RobotDriverRequest data RobotDriverRequest
@ -87,7 +93,7 @@ robotDriverThread inst eventQueue =
handleEvent (EventRequest _) = return () handleEvent (EventRequest _) = return ()
handleEvent (QuoteEvent d) = handleEvent (QuoteEvent d) =
case d of case d of
QDTick tick -> strategyEventCallback inst (NewTick tick) QDTick tick -> when (value tick /= 0) $ strategyEventCallback inst (NewTick tick)
QDBar (tf, bar) -> strategyEventCallback inst (NewBar (tf, bar)) QDBar (tf, bar) -> strategyEventCallback inst (NewBar (tf, bar))
handleEvent (NewTradeEvent trade) = strategyEventCallback inst (NewTrade trade) handleEvent (NewTradeEvent trade) = strategyEventCallback inst (NewTrade trade)
handleEvent (OrderEvent oid newState) = strategyEventCallback inst (OrderUpdate oid newState) handleEvent (OrderEvent oid newState) = strategyEventCallback inst (OrderUpdate oid newState)
@ -118,19 +124,29 @@ createRobotDriverThread instDesc strDesc runner bigConf rConf rState rTimers = d
let inst = StrategyInstance (strategyId instDesc) (eventCallback strDesc) rState rConf rTimers let inst = StrategyInstance (strategyId instDesc) (eventCallback strDesc) rState rConf rTimers
quoteQueue <- liftIO $ newBoundedChan 2000 quoteQueue <- liftIO $ newBoundedChan 2000
forM_ (confTickers bigConf) (\x -> addSubscription (QuoteSubscription (tickerId x) (timeframe x)) quoteQueue) subIds <- forM (confTickers bigConf) (\x -> addSubscription (QuoteSubscription (tickerId x) (timeframe x)) quoteQueue)
qthread <- liftIO . forkIO $ forever $ passQuoteEvents eventQueue quoteQueue qthread <- liftIO . forkIO $ forever $ passQuoteEvents eventQueue quoteQueue
driver <- liftIO . forkIO $ runner $ robotDriverThread inst eventQueue driver <- liftIO . forkIO $ runner $ robotDriverThread inst eventQueue
return $ RobotDriverHandle inst driver qthread eventQueue return $ RobotDriverHandle instDesc inst driver qthread eventQueue subIds
where where
passQuoteEvents eventQueue quoteQueue = do passQuoteEvents eventQueue quoteQueue = do
v <- readChan quoteQueue v <- readChan quoteQueue
writeChan eventQueue (QuoteEvent v) writeChan eventQueue (QuoteEvent v)
stopRobot :: (MonadIO m, QuoteStream m) => RobotDriverHandle -> m ()
stopRobot (RobotDriverHandle _ _ driver qthread _ subIds) = do
forM_ subIds removeSubscription
liftIO $ killThread driver
liftIO $ killThread qthread
onStrategyInstance :: RobotDriverHandle -> forall r. (forall c s. (FromDhall c, Default s, FromJSON s, ToJSON s) => StrategyInstance c s -> r) -> r onStrategyInstance :: RobotDriverHandle -> forall r. (forall c s. (FromDhall c, Default s, FromJSON s, ToJSON s) => StrategyInstance c s -> r) -> r
onStrategyInstance (RobotDriverHandle inst _ _ _) f = f inst onStrategyInstance (RobotDriverHandle _ inst _ _ _ _) f = f inst
onStrategyInstanceM :: (MonadIO m) => RobotDriverHandle ->
(forall c s. (FromDhall c, Default s, FromJSON s, ToJSON s) => StrategyInstance c s -> m r) -> m r
onStrategyInstanceM (RobotDriverHandle _ inst _ _ _ _) f = f inst
data RobotEnv c s = data RobotEnv c s =
RobotEnv RobotEnv
@ -139,6 +155,7 @@ data RobotEnv c s =
configRef :: IORef c, configRef :: IORef c,
timersRef :: IORef [UTCTime], timersRef :: IORef [UTCTime],
bars :: IORef Bars, bars :: IORef Bars,
tickerInfoMap :: IORef TickerInfoMap,
env :: IORef StrategyEnvironment, env :: IORef StrategyEnvironment,
logAction :: LogAction (RobotM c s) Message, logAction :: LogAction (RobotM c s) Message,
brokerService :: Bro.BrokerService, brokerService :: Bro.BrokerService,
@ -160,7 +177,7 @@ instance MonadRobot (RobotM c s) c s where
cancelOrder oid = do cancelOrder oid = do
bro <- asks brokerService bro <- asks brokerService
liftIO . void $ Bro.cancelOrder bro oid Bro.cancelOrder bro oid
appendToLog s t = do appendToLog s t = do
instId <- _seInstanceId <$> (asks env >>= liftIO . readIORef) instId <- _seInstanceId <$> (asks env >>= liftIO . readIORef)
@ -183,12 +200,17 @@ instance MonadRobot (RobotM c s) c s where
b <- asks bars >>= liftIO . readIORef b <- asks bars >>= liftIO . readIORef
return $ M.lookup (BarSeriesId tid tf) b return $ M.lookup (BarSeriesId tid tf) b
getTickerInfo tid = do
b <- asks tickerInfoMap >>= liftIO . readIORef
return $ M.lookup tid b
getAvailableTickers = asks tickers getAvailableTickers = asks tickers
postNotificationEvent :: (MonadIO m) => RobotDriverHandle -> Notification -> m () postNotificationEvent :: (MonadIO m) => RobotDriverHandle -> Notification -> m ()
postNotificationEvent (RobotDriverHandle _ _ _ eventQueue) notification = liftIO $ postNotificationEvent (RobotDriverHandle _ _ _ _ eventQueue _) notification = liftIO $
case notification of case notification of
OrderNotification _ oid state -> writeChan eventQueue (OrderEvent oid state) OrderNotification _ oid state -> writeChan eventQueue (OrderEvent oid state)
TradeNotification _ trade -> writeChan eventQueue (NewTradeEvent trade) TradeNotification _ trade -> writeChan eventQueue (NewTradeEvent trade)
getInstanceDescriptor :: RobotDriverHandle -> StrategyInstanceDescriptor
getInstanceDescriptor (RobotDriverHandle instDesc _ _ _ _ _) = instDesc

15
src/ATrade/Driver/Junction/Types.hs

@ -1,6 +1,7 @@
{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE ExistentialQuantification #-} {-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-} {-# LANGUAGE RankNTypes #-}
module ATrade.Driver.Junction.Types module ATrade.Driver.Junction.Types
@ -16,7 +17,8 @@ module ATrade.Driver.Junction.Types
import ATrade.RoboCom.Monad (EventCallback) import ATrade.RoboCom.Monad (EventCallback)
import ATrade.Types (BarTimeframe (..), TickerId) import ATrade.Types (BarTimeframe (..), TickerId)
import Data.Aeson (FromJSON (..), ToJSON (..)) import Data.Aeson (FromJSON (..), ToJSON (..), withObject,
(.:))
import Data.Default (Default) import Data.Default (Default)
import Data.IORef (IORef) import Data.IORef (IORef)
import qualified Data.Text as T import qualified Data.Text as T
@ -66,6 +68,17 @@ data StrategyInstanceDescriptor =
instance FromDhall StrategyInstanceDescriptor instance FromDhall StrategyInstanceDescriptor
instance FromJSON StrategyInstanceDescriptor where
parseJSON = withObject "StrategyInstanceDescriptor" $ \obj ->
StrategyInstanceDescriptor <$>
obj .: "account_id" <*>
obj .: "strategy_id" <*>
obj .: "strategy_base_name" <*>
obj .: "config_key" <*>
obj .: "state_key" <*>
obj .: "log_path"
data StrategyInstance c s = data StrategyInstance c s =
StrategyInstance StrategyInstance
{ {

114
src/ATrade/Driver/Real.hs

@ -1,11 +1,8 @@
{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE CPP #-}
{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE RankNTypes #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
module ATrade.Driver.Real ( module ATrade.Driver.Real (
StrategyInstanceParams(..), StrategyInstanceParams(..),
@ -14,56 +11,45 @@ module ATrade.Driver.Real (
barStrategyDriver barStrategyDriver
) where ) where
import ATrade.BarAggregator import Options.Applicative
import ATrade.Driver.Real.BrokerClientThread import System.IO
import ATrade.Driver.Real.QuoteSourceThread import System.Signal
import ATrade.Driver.Types (InitializationCallback, StrategyInstanceParams (..)) import System.Exit
import ATrade.Exceptions import System.Random
import ATrade.Quotes (MonadHistory (..), MonadInstrumentParametersSource (..)) import System.Log.Logger
import ATrade.Quotes.QHP as QQ import System.Log.Handler.Simple
import ATrade.Quotes.QTIS (TickerInfo (..), import System.Log.Handler (setFormatter)
qtisGetTickersInfo) import System.Log.Formatter
import ATrade.RoboCom.Monad (Event (..), import Control.Monad
EventCallback, import Control.Concurrent hiding (writeChan, readChan, writeList2Chan, yield)
MonadRobot (..), import Control.Concurrent.BoundedChan as BC
StrategyEnvironment (..), import Control.Exception
seBars, seLastTimestamp) import qualified Data.ByteString as BS
import ATrade.RoboCom.Types (BarSeries (..), InstrumentParameters (..), import qualified Data.ByteString.Lazy as BL
Ticker (..), import qualified Data.List as L
Timeframe (..)) import qualified Data.Map as M
import ATrade.RoboCom.Utils (fromHMS) import qualified Data.Text as T
import ATrade.Types import Data.Text.Encoding
import Control.Concurrent hiding (readChan, import Data.Aeson
writeChan, import Data.IORef
writeList2Chan, yield) import Data.Time.Calendar
import Control.Concurrent.BoundedChan as BC import Data.Time.Clock
import Control.Exception.Safe import Data.Time.Clock.POSIX
import Control.Lens hiding (Context, (.=)) import Data.Maybe
import Control.Monad import Data.Monoid
import Control.Monad.Reader import Database.Redis hiding (info, decode)
import Data.Aeson import ATrade.Types
import qualified Data.ByteString as BS import ATrade.RoboCom.Monad (StrategyMonad, StrategyAction(..), EventCallback, Event(..), runStrategyElement, StrategyEnvironment(..), Event(..))
import qualified Data.ByteString.Lazy as BL import ATrade.BarAggregator
import Data.IORef import ATrade.Driver.Real.BrokerClientThread
import qualified Data.Map as M import ATrade.Driver.Real.QuoteSourceThread
import Data.Maybe import ATrade.Driver.Real.Types (Strategy(..), StrategyInstanceParams(..), InitializationCallback)
import qualified Data.Text as T import ATrade.RoboCom.Types (BarSeries(..), Ticker(..), Timeframe(..))
import Data.Text.Encoding import ATrade.Exceptions
import qualified Data.Text.Lazy as TL import ATrade.Quotes.Finam as QF
import Data.Time.Calendar import ATrade.Quotes.QHP as QQ
import Data.Time.Clock import ATrade.Quotes.HAP as QH
import Data.Time.Clock.POSIX import System.ZMQ4 hiding (Event(..))
import Database.Redis hiding (decode, info)
import GHC.Generics
import Options.Applicative
import System.Exit
import System.IO
import System.Log.Formatter
import System.Log.Handler (setFormatter)
import System.Log.Handler.Simple
import System.Log.Logger
import System.Signal
import System.ZMQ4 hiding (Event (..))
data Params = Params { data Params = Params {
instanceId :: String, instanceId :: String,
@ -422,6 +408,18 @@ barStrategyDriver downloadDelta instanceParams callback shutdownVar = do
nowRef <- asks envLastTimestamp nowRef <- asks envLastTimestamp
lift $ writeIORef nowRef newTimestamp lift $ writeIORef nowRef newTimestamp
newTimers <- catMaybes <$> (readIORef timersRef >>= mapM (checkTimer eventChan newTimestamp))
atomicWriteIORef timersRef newTimers
let !newenv = env { seBars = currentBars, seLastTimestamp = newTimestamp }
let (!newState, !actions, _) = runStrategyElement params curState newenv $ (eventCallback strategy) event
writeIORef stateRef newState
writeIORef timersRef newTimers
newTimers' <- catMaybes <$> mapM handleTimerActions actions
mapM_ (handleActions ordersChan) actions
readAndHandleEvents agg ordersChan eventChan (strategy' { currentState = newState, strategyTimers = newTimers ++ newTimers' }) newenv
else debugM "Strategy" "Shutdown requested"
timersRef <- asks envTimers timersRef <- asks envTimers
oldTimers <- lift $ readIORef timersRef oldTimers <- lift $ readIORef timersRef
newTimers <- catMaybes <$> mapM (checkTimer eventChan newTimestamp) oldTimers newTimers <- catMaybes <$> mapM (checkTimer eventChan newTimestamp) oldTimers

94
src/ATrade/Driver/Real/BrokerClientThread.hs

@ -1,4 +1,5 @@
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
module ATrade.Driver.Real.BrokerClientThread ( module ATrade.Driver.Real.BrokerClientThread (
startBrokerClientThread, startBrokerClientThread,
@ -27,47 +28,58 @@ import Data.Time.Clock
import System.Log.Logger import System.Log.Logger
import System.ZMQ4 hiding (Event) import System.ZMQ4 hiding (Event)
data BrokerCommand = BrokerSubmitOrder Order | BrokerCancelOrder Integer | BrokerRequestNotifications data BrokerCommand = BrokerSubmitOrder Order | BrokerCancelOrder Integer | BrokerRequestNotifications | BrokerHandleNotification Notification
startBrokerClientThread :: T.Text -> Context -> T.Text -> T.Text -> BoundedChan BrokerCommand -> BoundedChan Event -> MVar a -> IO ThreadId
startBrokerClientThread :: T.Text -> Context -> T.Text -> BoundedChan BrokerCommand -> BoundedChan Event -> MVar a -> IO ThreadId startBrokerClientThread instId ctx brEp notifEp ordersChan eventChan shutdownVar = do
startBrokerClientThread instId ctx brEp ordersChan eventChan shutdownVar = forkIO $ whileM_ (isNothing <$> tryReadMVar shutdownVar) $ let callback = writeChan ordersChan . BrokerHandleNotification
bracket (startBrokerClient (encodeUtf8 instId) ctx brEp defaultClientSecurityParams) forkIO $ whileM_ (isNothing <$> tryReadMVar shutdownVar) $
(\bro -> do bracket (startBrokerClient (encodeUtf8 instId) ctx brEp notifEp [callback] defaultClientSecurityParams)
stopBrokerClient bro (\bro -> do
debugM "Strategy" "Broker client: stop") stopBrokerClient bro
(\bs -> handle (\e -> do debugM "Strategy" "Broker client: stop")
warningM "Strategy" $ "Broker client: exception: " ++ show (e :: SomeException) (\bs -> handle (\e -> do
throwIO e) $ do warningM "Strategy" $ "Broker client: exception: " ++ show (e :: SomeException)
now <- getCurrentTime throwIO e) $ do
lastNotificationTime <- newIORef now now <- getCurrentTime
whileM_ (andM [notTimeout lastNotificationTime, isNothing <$> tryReadMVar shutdownVar]) $ do lastNotificationTime <- newIORef now
brokerCommand <- readChan ordersChan lastKnownSqnum <- newIORef 0
case brokerCommand of whileM_ (andM [notTimeout lastNotificationTime, isNothing <$> tryReadMVar shutdownVar]) $ do
BrokerSubmitOrder order -> do brokerCommand <- readChan ordersChan
debugM "Strategy" $ "Submitting order: " ++ show order case brokerCommand of
maybeOid <- submitOrder bs order BrokerSubmitOrder order -> do
debugM "Strategy" "Order submitted" debugM "Strategy" $ "Submitting order: " ++ show order
case maybeOid of result <- submitOrder bs order
Right oid -> writeChan eventChan (OrderSubmitted order { orderId = oid }) debugM "Strategy" "Order submitted"
Left errmsg -> debugM "Strategy" $ T.unpack $ "Error: " `T.append` errmsg case result of
BrokerCancelOrder oid -> do Right _ -> debugM "Strategy" $ "Order submitted: " ++ show (orderId order)
debugM "Strategy" $ "Cancelling order: " ++ show oid
_ <- cancelOrder bs oid
debugM "Strategy" "Order cancelled"
BrokerRequestNotifications -> do
t <- getCurrentTime
nt <- readIORef lastNotificationTime
when (t `diffUTCTime` nt > 1) $ do
maybeNs <- getNotifications bs
case maybeNs of
Left errmsg -> debugM "Strategy" $ T.unpack $ "Error: " `T.append` errmsg Left errmsg -> debugM "Strategy" $ T.unpack $ "Error: " `T.append` errmsg
Right ns -> do BrokerCancelOrder oid -> do
mapM_ (sendNotification eventChan) ns debugM "Strategy" $ "Cancelling order: " ++ show oid
getCurrentTime >>= (writeIORef lastNotificationTime) _ <- cancelOrder bs oid
nTimeout <- notTimeout lastNotificationTime debugM "Strategy" "Order cancelled"
shouldShutdown <- isNothing <$> tryReadMVar shutdownVar BrokerRequestNotifications -> do
debugM "Strategy" $ "Broker loop end: " ++ show nTimeout ++ "/" ++ show shouldShutdown) t <- getCurrentTime
nt <- readIORef lastNotificationTime
when (t `diffUTCTime` nt > 1) $ do
maybeNs <- getNotifications bs
case maybeNs of
Left errmsg -> debugM "Strategy" $ T.unpack $ "Error: " `T.append` errmsg
Right ns -> do
mapM_ (\n -> do
prevSqnum <- atomicModifyIORef lastKnownSqnum (\s -> (getNotificationSqnum n, s))
when (prevSqnum + 1 < getNotificationSqnum n) $
warningM "Strategy" $ "Sqnum jump: " ++ show prevSqnum ++ "->" ++ show (getNotificationSqnum n)
sendNotification eventChan n) ns
getCurrentTime >>= writeIORef lastNotificationTime
BrokerHandleNotification notification -> do
sendNotification eventChan n
prevSqnum <- atomicModifyIORef lastKnownSqnum (\s -> (getNotificationSqnum n, s))
undefined
nTimeout <- notTimeout lastNotificationTime
shouldShutdown <- isNothing <$> tryReadMVar shutdownVar
debugM "Strategy" $ "Broker loop end: " ++ show nTimeout ++ "/" ++ show shouldShutdown)
notTimeout :: IORef UTCTime -> IO Bool notTimeout :: IORef UTCTime -> IO Bool
notTimeout ts = do notTimeout ts = do
@ -78,5 +90,5 @@ notTimeout ts = do
sendNotification :: BoundedChan Event -> Notification -> IO () sendNotification :: BoundedChan Event -> Notification -> IO ()
sendNotification eventChan notification = sendNotification eventChan notification =
writeChan eventChan $ case notification of writeChan eventChan $ case notification of
OrderNotification oid state -> OrderUpdate oid state OrderNotification sqnum oid state -> OrderUpdate oid state
TradeNotification trade -> NewTrade trade TradeNotification sqnum trade -> NewTrade trade

3
src/ATrade/Quotes/QTIS.hs

@ -37,9 +37,8 @@ instance ToJSON TickerInfo where
"lot_size" .= tiLotSize ti, "lot_size" .= tiLotSize ti,
"tick_size" .= tiTickSize ti ] "tick_size" .= tiTickSize ti ]
qtisGetTickersInfo :: (WithLog env Message m, MonadIO m) => Context -> T.Text -> TickerId -> m TickerInfo qtisGetTickersInfo :: (MonadIO m) => Context -> T.Text -> TickerId -> m TickerInfo
qtisGetTickersInfo ctx endpoint tickerId = do qtisGetTickersInfo ctx endpoint tickerId = do
logInfo "QTIS" $ "Requesting ticker: " <> tickerId <> " from " <> endpoint
liftIO $ withSocket ctx Req $ \sock -> do liftIO $ withSocket ctx Req $ \sock -> do
connect sock $ T.unpack endpoint connect sock $ T.unpack endpoint
send sock [] $ BL.toStrict tickerRequest send sock [] $ BL.toStrict tickerRequest

14
src/ATrade/RoboCom/Monad.hs

@ -20,7 +20,9 @@ module ATrade.RoboCom.Monad (
also, also,
t, t,
st, st,
getFirstTickerId) where getFirstTickerId,
getTickerAnyTimeframe
) where
import ATrade.RoboCom.Types import ATrade.RoboCom.Types
import ATrade.Types import ATrade.Types
@ -30,6 +32,7 @@ import Data.Aeson.Types
import qualified Data.Text as T import qualified Data.Text as T
import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy as TL
import Data.Time.Clock import Data.Time.Clock
import qualified Data.List as L
import Language.Haskell.Printf import Language.Haskell.Printf
import Language.Haskell.TH.Quote (QuasiQuoter) import Language.Haskell.TH.Quote (QuasiQuoter)
import ATrade.Logging (Severity) import ATrade.Logging (Severity)
@ -51,11 +54,20 @@ class (Monad m) => MonadRobot m c s | m -> c, m -> s where
setState (f oldState) setState (f oldState)
getEnvironment :: m StrategyEnvironment getEnvironment :: m StrategyEnvironment
getTicker :: TickerId -> BarTimeframe -> m (Maybe BarSeries) getTicker :: TickerId -> BarTimeframe -> m (Maybe BarSeries)
getTickerInfo :: TickerId -> m (Maybe InstrumentParameters)
getAvailableTickers :: m (NonEmpty BarSeriesId) getAvailableTickers :: m (NonEmpty BarSeriesId)
getFirstTickerId :: forall c s m. (Monad m, MonadRobot m c s) => m BarSeriesId getFirstTickerId :: forall c s m. (Monad m, MonadRobot m c s) => m BarSeriesId
getFirstTickerId = NE.head <$> getAvailableTickers getFirstTickerId = NE.head <$> getAvailableTickers
getTickerAnyTimeframe :: forall c s m. (Monad m, MonadRobot m c s) => TickerId -> m (Maybe BarSeries)
getTickerAnyTimeframe requestedTickerId = do
tickers <- getAvailableTickers
case L.find (\(BarSeriesId tid _) -> tid == requestedTickerId) tickers of
Just (BarSeriesId tid tf) -> getTicker tid tf
Nothing -> return Nothing
st :: QuasiQuoter st :: QuasiQuoter
st = t st = t

258
src/ATrade/RoboCom/Positions.hs

@ -1,3 +1,4 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiWayIf #-} {-# LANGUAGE MultiWayIf #-}
@ -8,18 +9,20 @@
{-| {-|
- Module : ATrade.RoboCom.Combinators - Module : ATrade.RoboCom.Combinators
- Description : Reusable behavioural components of strategies - Description : Reusable behavioural components of strategies
- Copyright : (c) Denis Tereshkin 2016 - Copyright : (c) Denis Tereshkin 2021
- License : Proprietary - License : BSD 3-clause
- Maintainer : denis@kasan.ws - Maintainer : denis@kasan.ws
- Stability : experimental - Stability : experimental
- Portability : POSIX - Portability : POSIX
- -
- A lot of behaviour is common for most of the strategies. This module contains those common blocks which can be composed to avoid boilerplate in main strategy code. - A lot of behaviour is common for most of the strategies.
- This module contains those common blocks which can be composed to avoid boilerplate in main strategy code.
-} -}
module ATrade.RoboCom.Positions module ATrade.RoboCom.Positions
( (
StateHasPositions(..), StateHasPositions(..),
ParamsSize(..),
PositionState(..), PositionState(..),
Position(..), Position(..),
posIsOpen, posIsOpen,
@ -45,12 +48,10 @@ module ATrade.RoboCom.Positions
onTradeEvent, onTradeEvent,
onActionCompletedEvent, onActionCompletedEvent,
enterAtMarket, enterAtMarket,
enterAtMarketForTicker,
enterAtMarketWithParams, enterAtMarketWithParams,
enterAtLimit, enterAtLimit,
enterAtLimitWithVolume,
enterAtLimitWithParams,
enterAtLimitForTicker, enterAtLimitForTicker,
enterAtLimitForTickerWithVolume,
enterAtLimitForTickerWithParams, enterAtLimitForTickerWithParams,
enterLongAtMarket, enterLongAtMarket,
enterShortAtMarket, enterShortAtMarket,
@ -65,8 +66,14 @@ module ATrade.RoboCom.Positions
setLimitStopLoss, setLimitStopLoss,
setTakeProfit, setTakeProfit,
setStopLossAndTakeProfit, setStopLossAndTakeProfit,
handlePositions
) where handlePositions,
calculateSizeIVS,
calculateSizeIVSWith,
calculateSizeFixed,
calculateSizeFixedCash,
calculateSizeFixedCashWith,
calculateSizeIVSWithMinimum) where
import GHC.Generics import GHC.Generics
@ -74,17 +81,18 @@ import ATrade.RoboCom.Monad
import ATrade.RoboCom.Types import ATrade.RoboCom.Types
import ATrade.Types import ATrade.Types
import Control.Lens import Control.Lens hiding (op)
import Control.Monad import Control.Monad
import ATrade.Logging (Severity (Trace, Warning)) import ATrade.Logging (Severity (Trace, Warning))
import ATrade.RoboCom.Monad (MonadRobot (getAvailableTickers)) import qualified ATrade.RoboCom.Indicators as I
import Data.Aeson import Data.Aeson
import qualified Data.List as L import qualified Data.List as L
import qualified Data.List.NonEmpty as NE import qualified Data.List.NonEmpty as NE
import qualified Data.Text as T import qualified Data.Text as T
import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy as TL
import Data.Time.Clock import Data.Time.Clock
import GHC.Records (HasField (..))
data PositionState = PositionWaitingOpenSubmission Order data PositionState = PositionWaitingOpenSubmission Order
| PositionWaitingOpen | PositionWaitingOpen
@ -146,8 +154,44 @@ modifyPositions f = do
pos <- getPositions <$> getState pos <- getPositions <$> getState
modifyState (\s -> setPositions s (f pos)) modifyState (\s -> setPositions s (f pos))
class ParamsHasMainTicker a where class ParamsSize a where
mainTicker :: a -> (BarTimeframe, TickerId) getPositionSize :: a -> BarSeries -> Operation -> Int
calculateSizeIVS :: (HasField "riskSize" a Double,
HasField "stopSize" a Double,
HasField "atrPeriod" a Int) =>
a -> BarSeries -> Operation -> Int
calculateSizeIVS cfg = calculateSizeIVSWith (getField @"atrPeriod" cfg) (getField @"riskSize" cfg) (getField @"stopSize" cfg) cfg
calculateSizeIVSWithMinimum :: (HasField "riskSize" a Double,
HasField "stopSize" a Double,
HasField "atrPeriod" a Int) =>
Int -> a -> BarSeries -> Operation -> Int
calculateSizeIVSWithMinimum minVolume cfg series op = max (calculateSizeIVS cfg series op) minVolume
calculateSizeIVSWith :: Int -> Double -> Double -> a -> BarSeries -> Operation -> Int
calculateSizeIVSWith atrPeriod riskSize stopSize _ series _ =
let atr = I.atr atrPeriod (bsBars series) in
truncate (riskSize / (atr * stopSize))
calculateSizeFixed :: (HasField "positionSize" a Int) =>
a -> BarSeries -> Operation -> Int
calculateSizeFixed cfg _ _ = getField @"positionSize" cfg
calculateSizeFixedCash :: ( HasField "totalCash" a Double,
HasField "maxPositions" a Int) =>
a -> BarSeries -> Operation -> Int
calculateSizeFixedCash cfg = calculateSizeFixedCashWith (getField @"totalCash" cfg) (getField @"maxPositions" cfg) cfg
calculateSizeFixedCashWith :: Double -> Int -> a -> BarSeries -> Operation -> Int
calculateSizeFixedCashWith totalCash maxPositions cfg series _ =
case bsBars $ series of
(lastBar:_) ->
let cashPerPosition = totalCash / fromIntegral maxPositions in
truncate (cashPerPosition / ((toDouble $ barClose lastBar) * (fromIntegral $ ipLotSize . bsParams $ series)))
_ -> 0
-- | Helper function. Finds first element in list which satisfies predicate 'p' and if found, applies 'm' to it, leaving other elements intact. -- | Helper function. Finds first element in list which satisfies predicate 'p' and if found, applies 'm' to it, leaving other elements intact.
findAndModify :: (a -> Bool) -> (a -> a) -> [a] -> [a] findAndModify :: (a -> Bool) -> (a -> a) -> [a] -> [a]
@ -179,20 +223,23 @@ orderDeadline maybeDeadline lastTs =
dispatchPosition :: (StateHasPositions s, MonadRobot m c s) => Event -> Position -> m Position dispatchPosition :: (StateHasPositions s, MonadRobot m c s) => Event -> Position -> m Position
dispatchPosition event pos = case posState pos of dispatchPosition event pos =
PositionWaitingOpenSubmission pendingOrder -> handlePositionWaitingOpenSubmission pendingOrder case posState pos of
PositionWaitingOpen -> handlePositionWaitingOpen PositionWaitingOpenSubmission pendingOrder -> handlePositionWaitingOpenSubmission pendingOrder
PositionOpen -> handlePositionOpen PositionWaitingOpen -> handlePositionWaitingOpen
PositionWaitingPendingCancellation -> handlePositionWaitingPendingCancellation PositionOpen -> handlePositionOpen
PositionWaitingCloseSubmission pendingOrder -> handlePositionWaitingCloseSubmission pendingOrder PositionWaitingPendingCancellation -> handlePositionWaitingPendingCancellation
PositionWaitingClose -> handlePositionWaitingClose PositionWaitingCloseSubmission pendingOrder -> handlePositionWaitingCloseSubmission pendingOrder
PositionClosed -> handlePositionClosed pos PositionWaitingClose -> handlePositionWaitingClose
PositionCancelled -> handlePositionCancelled pos PositionClosed -> handlePositionClosed pos
PositionCancelled -> handlePositionCancelled pos
where where
handlePositionWaitingOpenSubmission pendingOrder = do handlePositionWaitingOpenSubmission pendingOrder = do
lastTs <- view seLastTimestamp <$> getEnvironment lastTs <- view seLastTimestamp <$> getEnvironment
if orderDeadline (posSubmissionDeadline pos) lastTs if orderDeadline (posSubmissionDeadline pos) lastTs
then return $ pos { posState = PositionCancelled } -- TODO call TimeoutHandler if present then do
appendToLog Warning $ [t|Submission deadline: %?, %?|] lastTs (posSubmissionDeadline pos)
return $ pos { posState = PositionCancelled } -- TODO call TimeoutHandler if present
else case event of else case event of
OrderUpdate oid Submitted -> do OrderUpdate oid Submitted -> do
return $ if orderId pendingOrder == oid return $ if orderId pendingOrder == oid
@ -374,6 +421,25 @@ newPosition order account tickerId operation quantity submissionDeadline = do
modifyPositions (\p -> position : p) modifyPositions (\p -> position : p)
return position return position
rejectedPosition :: (StateHasPositions s, MonadRobot m c s) => m Position
rejectedPosition =
return Position {
posId = "Rejected",
posAccount = "",
posTicker = "",
posBalance = 0,
posState = PositionCancelled,
posNextState = Nothing,
posStopPrice = Nothing,
posStopLimitPrice = Nothing,
posTakeProfitPrice = Nothing,
posCurrentOrder = Nothing,
posSubmissionDeadline = Nothing,
posExecutionDeadline = Nothing,
posEntryTime = Nothing,
posExitTime = Nothing
}
reapDeadPositions :: (StateHasPositions s) => EventCallback c s reapDeadPositions :: (StateHasPositions s) => EventCallback c s
reapDeadPositions _ = modifyPositions (L.filter (not . posIsDead)) reapDeadPositions _ = modifyPositions (L.filter (not . posIsDead))
@ -461,16 +527,31 @@ onActionCompletedEvent event f = case event of
ActionCompleted tag v -> f tag v ActionCompleted tag v -> f tag v
_ -> doNothing _ -> doNothing
enterAtMarket :: (StateHasPositions s, MonadRobot m c s) => T.Text -> Operation -> m Position roundTo :: Price -> Price -> Price
roundTo quant v = quant * (fromIntegral . floor . toDouble) (v / quant)
enterAtMarket :: (StateHasPositions s, ParamsSize c, MonadRobot m c s) => T.Text -> Operation -> m Position
enterAtMarket operationSignalName operation = do enterAtMarket operationSignalName operation = do
env <- getEnvironment bsId <- getFirstTickerId
enterAtMarketWithParams (env ^. seAccount) (env ^. seVolume) (SignalId (env ^. seInstanceId) operationSignalName "") operation enterAtMarketForTicker operationSignalName bsId operation
enterAtMarketForTicker :: (StateHasPositions s, ParamsSize c, MonadRobot m c s) => T.Text -> BarSeriesId -> Operation -> m Position
enterAtMarketForTicker operationSignalName (BarSeriesId tid tf) operation = do
maybeSeries <- getTicker tid tf
case maybeSeries of
Just series -> do
env <- getEnvironment
cfg <- getConfig
let quantity = getPositionSize cfg series operation
enterAtMarketWithParams (env ^. seAccount) tid quantity (SignalId (env ^. seInstanceId) operationSignalName "") operation
Nothing -> do
appendToLog Warning $ "Unable to get ticker series: " <> TL.fromStrict tid
rejectedPosition
enterAtMarketWithParams :: (StateHasPositions s, MonadRobot m c s) => T.Text -> Int -> SignalId -> Operation -> m Position enterAtMarketWithParams :: (StateHasPositions s, MonadRobot m c s) => T.Text -> TickerId -> Int -> SignalId -> Operation -> m Position
enterAtMarketWithParams account quantity signalId operation = do enterAtMarketWithParams account tid quantity signalId operation = do
BarSeriesId tickerId _ <- getFirstTickerId oid <- submitOrder $ order tid
oid <- submitOrder $ order tickerId newPosition ((order tid) { orderId = oid }) account tid operation quantity 20
newPosition ((order tickerId) { orderId = oid }) account tickerId operation quantity 20
where where
order tickerId = mkOrder { order tickerId = mkOrder {
orderAccountId = account, orderAccountId = account,
@ -481,36 +562,36 @@ enterAtMarketWithParams account quantity signalId operation = do
orderSignalId = signalId orderSignalId = signalId
} }
enterAtLimit :: (StateHasPositions s, MonadRobot m c s) => NominalDiffTime -> T.Text -> Price -> Operation -> m Position enterAtLimit :: (StateHasPositions s, ParamsSize c, MonadRobot m c s) => T.Text -> Price -> Operation -> m Position
enterAtLimit timeToCancel operationSignalName price operation = do enterAtLimit operationSignalName price operation = do
bsId <- getFirstTickerId
env <- getEnvironment env <- getEnvironment
enterAtLimitWithParams timeToCancel (env ^. seAccount) (env ^. seVolume) (SignalId (env ^. seInstanceId) operationSignalName "") price operation enterAtLimitForTicker bsId operationSignalName price operation
enterAtLimitWithVolume :: (StateHasPositions s, MonadRobot m c s) => NominalDiffTime -> T.Text -> Price -> Int -> Operation -> m Position
enterAtLimitWithVolume timeToCancel operationSignalName price vol operation = do
acc <- view seAccount <$> getEnvironment
inst <- view seInstanceId <$> getEnvironment
enterAtLimitWithParams timeToCancel acc vol (SignalId inst operationSignalName "") price operation
enterAtLimitWithParams :: (StateHasPositions s, MonadRobot m c s) => NominalDiffTime -> T.Text -> Int -> SignalId -> Price -> Operation -> m Position
enterAtLimitWithParams timeToCancel account quantity signalId price operation = do
BarSeriesId tickerId _ <- getFirstTickerId
enterAtLimitForTickerWithParams tickerId timeToCancel account quantity signalId price operation
enterAtLimitForTickerWithVolume :: (StateHasPositions s, MonadRobot m c s) => TickerId -> NominalDiffTime -> T.Text -> Price -> Int -> Operation -> m Position enterAtLimitForTicker :: (StateHasPositions s, ParamsSize c, MonadRobot m c s) => BarSeriesId -> T.Text -> Price -> Operation -> m Position
enterAtLimitForTickerWithVolume tickerId timeToCancel operationSignalName price vol operation = do enterAtLimitForTicker (BarSeriesId tid tf) operationSignalName price operation = do
acc <- view seAccount <$> getEnvironment acc <- view seAccount <$> getEnvironment
inst <- view seInstanceId <$> getEnvironment inst <- view seInstanceId <$> getEnvironment
enterAtLimitForTickerWithParams tickerId timeToCancel acc vol (SignalId inst operationSignalName "") price operation maybeSeries <- getTicker tid tf
case maybeSeries of
enterAtLimitForTicker :: (StateHasPositions s, MonadRobot m c s) => TickerId -> NominalDiffTime -> T.Text -> Price -> Operation -> m Position Just series -> do
enterAtLimitForTicker tickerId timeToCancel operationSignalName price operation = do cfg <- getConfig
acc <- view seAccount <$> getEnvironment let quantity = getPositionSize cfg series operation
inst <- view seInstanceId <$> getEnvironment let roundedPrice = roundTo (ipTickSize . bsParams $ series) price
vol <- view seVolume <$> getEnvironment enterAtLimitForTickerWithParams tid (fromIntegral $ unBarTimeframe tf) acc quantity (SignalId inst operationSignalName "") roundedPrice operation
enterAtLimitForTickerWithParams tickerId timeToCancel acc vol (SignalId inst operationSignalName "") price operation Nothing -> rejectedPosition
enterAtLimitForTickerWithParams :: (StateHasPositions s, MonadRobot m c s) => TickerId -> NominalDiffTime -> T.Text -> Int -> SignalId -> Price -> Operation -> m Position enterAtLimitForTickerWithParams ::
(StateHasPositions s,
MonadRobot m c s) =>
TickerId
-> NominalDiffTime
-> T.Text
-> Int
-> SignalId
-> Price
-> Operation
-> m Position
enterAtLimitForTickerWithParams tickerId timeToCancel account quantity signalId price operation = do enterAtLimitForTickerWithParams tickerId timeToCancel account quantity signalId price operation = do
lastTs <- view seLastTimestamp <$> getEnvironment lastTs <- view seLastTimestamp <$> getEnvironment
oid <- submitOrder order oid <- submitOrder order
@ -527,23 +608,23 @@ enterAtLimitForTickerWithParams tickerId timeToCancel account quantity signalId
orderSignalId = signalId orderSignalId = signalId
} }
enterLongAtMarket :: (StateHasPositions s, MonadRobot m c s) => T.Text -> m Position enterLongAtMarket :: (StateHasPositions s, ParamsSize c, MonadRobot m c s) => T.Text -> m Position
enterLongAtMarket operationSignalName = enterAtMarket operationSignalName Buy enterLongAtMarket operationSignalName = enterAtMarket operationSignalName Buy
enterShortAtMarket :: (StateHasPositions s, MonadRobot m c s) => T.Text -> m Position enterShortAtMarket :: (StateHasPositions s, ParamsSize c, MonadRobot m c s) => T.Text -> m Position
enterShortAtMarket operationSignalName = enterAtMarket operationSignalName Sell enterShortAtMarket operationSignalName = enterAtMarket operationSignalName Sell
enterLongAtLimit :: (StateHasPositions s, MonadRobot m c s) => NominalDiffTime -> Price -> T.Text -> m Position enterLongAtLimit :: (StateHasPositions s, ParamsSize c, MonadRobot m c s) => Price -> T.Text -> m Position
enterLongAtLimit timeToCancel price operationSignalName = enterAtLimit timeToCancel operationSignalName price Buy enterLongAtLimit price operationSignalName = enterAtLimit operationSignalName price Buy
enterLongAtLimitForTicker :: (StateHasPositions s, MonadRobot m c s) => TickerId -> NominalDiffTime -> Price -> T.Text -> m Position enterLongAtLimitForTicker :: (StateHasPositions s, ParamsSize c, MonadRobot m c s) => BarSeriesId -> Price -> T.Text -> m Position
enterLongAtLimitForTicker tickerId timeToCancel price operationSignalName = enterAtLimitForTicker tickerId timeToCancel operationSignalName price Buy enterLongAtLimitForTicker tickerId price operationSignalName = enterAtLimitForTicker tickerId operationSignalName price Buy
enterShortAtLimit :: (StateHasPositions s, MonadRobot m c s) => NominalDiffTime -> Price -> T.Text -> m Position enterShortAtLimit :: (StateHasPositions s, ParamsSize c, MonadRobot m c s) => Price -> T.Text -> m Position
enterShortAtLimit timeToCancel price operationSignalName = enterAtLimit timeToCancel operationSignalName price Sell enterShortAtLimit price operationSignalName = enterAtLimit operationSignalName price Sell
enterShortAtLimitForTicker :: (StateHasPositions s, MonadRobot m c s) => TickerId -> NominalDiffTime -> Price -> T.Text -> m Position enterShortAtLimitForTicker :: (StateHasPositions s, ParamsSize c, MonadRobot m c s) => BarSeriesId -> Price -> T.Text -> m Position
enterShortAtLimitForTicker tickerId timeToCancel price operationSignalName = enterAtLimitForTicker tickerId timeToCancel operationSignalName price Sell enterShortAtLimitForTicker tickerId price operationSignalName = enterAtLimitForTicker tickerId operationSignalName price Sell
exitAtMarket :: (StateHasPositions s, MonadRobot m c s) => Position -> T.Text -> m Position exitAtMarket :: (StateHasPositions s, MonadRobot m c s) => Position -> T.Text -> m Position
exitAtMarket position operationSignalName = do exitAtMarket position operationSignalName = do
@ -580,23 +661,32 @@ exitAtLimit :: (StateHasPositions s, MonadRobot m c s) => NominalDiffTime -> Pri
exitAtLimit timeToCancel price position operationSignalName = do exitAtLimit timeToCancel price position operationSignalName = do
lastTs <- view seLastTimestamp <$> getEnvironment lastTs <- view seLastTimestamp <$> getEnvironment
inst <- view seInstanceId <$> getEnvironment inst <- view seInstanceId <$> getEnvironment
case posCurrentOrder position of cfg <- getConfig
Just order -> cancelOrder (orderId order) (BarSeriesId tid tf) <- getFirstTickerId
Nothing -> doNothing maybeSeries <- getTicker tid tf
oid <- submitOrder (closeOrder inst) case maybeSeries of
appendToLog Trace $ [t|exitAtLimit: %?, deadline: %?|] (posTicker position) (timeToCancel `addUTCTime` lastTs) Just series -> do
modifyPosition (\pos -> let roundedPrice = roundTo (ipTickSize . bsParams $ series) price
pos { posCurrentOrder = Nothing, case posCurrentOrder position of
posState = PositionWaitingCloseSubmission (closeOrder inst) { orderId = oid }, Just order -> cancelOrder (orderId order)
posNextState = Just PositionClosed, Nothing -> doNothing
posSubmissionDeadline = Just $ 10 `addUTCTime` lastTs, oid <- submitOrder (closeOrder inst roundedPrice)
posExecutionDeadline = Just $ timeToCancel `addUTCTime` lastTs }) position appendToLog Trace $ [t|exitAtLimit: %?, deadline: %?|] (posTicker position) (timeToCancel `addUTCTime` lastTs)
modifyPosition (\pos ->
pos { posCurrentOrder = Nothing,
posState = PositionWaitingCloseSubmission (closeOrder inst roundedPrice) { orderId = oid },
posNextState = Just PositionClosed,
posSubmissionDeadline = Just $ 10 `addUTCTime` lastTs,
posExecutionDeadline = Just $ timeToCancel `addUTCTime` lastTs }) position
Nothing -> do
appendToLog Warning $ "Unable to locate first bar series"
return position
where where
closeOrder inst = mkOrder { closeOrder inst roundedPrice = mkOrder {
orderAccountId = posAccount position, orderAccountId = posAccount position,
orderSecurity = posTicker position, orderSecurity = posTicker position,
orderQuantity = (abs . posBalance) position, orderQuantity = (abs . posBalance) position,
orderPrice = Limit price, orderPrice = Limit roundedPrice,
orderOperation = if posBalance position > 0 then Sell else Buy, orderOperation = if posBalance position > 0 then Sell else Buy,
orderSignalId = SignalId inst operationSignalName "" orderSignalId = SignalId inst operationSignalName ""
} }

18
src/ATrade/RoboCom/Types.hs

@ -11,14 +11,19 @@ module ATrade.RoboCom.Types (
BarSeries(..), BarSeries(..),
Ticker(..), Ticker(..),
Bars, Bars,
TickerInfoMap,
InstrumentParameters(..), InstrumentParameters(..),
bsidTickerId bsidTickerId,
barSeriesId
) where ) where
import ATrade.Types import ATrade.Types
import Control.Lens.Setter (over)
import Control.Lens.Tuple (_1)
import Data.Aeson import Data.Aeson
import Data.Aeson.Key (fromText, toText)
import Data.Aeson.KeyMap as KM
import Data.Aeson.Types import Data.Aeson.Types
import qualified Data.HashMap.Strict as HM
import qualified Data.Map.Strict as M import qualified Data.Map.Strict as M
import qualified Data.Text as T import qualified Data.Text as T
import GHC.Generics (Generic) import GHC.Generics (Generic)
@ -31,6 +36,8 @@ data InstrumentParameters =
ipTickSize :: Price ipTickSize :: Price
} deriving (Show, Eq) } deriving (Show, Eq)
type TickerInfoMap = M.Map TickerId InstrumentParameters
data BarSeries = data BarSeries =
BarSeries { BarSeries {
bsTickerId :: TickerId, bsTickerId :: TickerId,
@ -39,6 +46,9 @@ data BarSeries =
bsParams :: InstrumentParameters bsParams :: InstrumentParameters
} deriving (Show, Eq) } deriving (Show, Eq)
barSeriesId :: BarSeries -> BarSeriesId
barSeriesId s = BarSeriesId (bsTickerId s) (bsTimeframe s)
-- | Ticker description record -- | Ticker description record
data Ticker = Ticker { data Ticker = Ticker {
code :: T.Text, -- ^ Main ticker code, which is used to make orders and tick parsing code :: T.Text, -- ^ Main ticker code, which is used to make orders and tick parsing
@ -57,14 +67,14 @@ instance FromJSON Ticker where
return $ Ticker nm als' tf) return $ Ticker nm als' tf)
where where
parseAliases :: Value -> Parser [(String, String)] parseAliases :: Value -> Parser [(String, String)]
parseAliases = withObject "object1" (mapM parseAlias . HM.toList) parseAliases = withObject "object1" (mapM (parseAlias . over _1 toText) . KM.toList)
parseAlias :: (T.Text, Value) -> Parser (String, String) parseAlias :: (T.Text, Value) -> Parser (String, String)
parseAlias (k, v) = withText "string1" (\s -> return (T.unpack k, T.unpack s)) v parseAlias (k, v) = withText "string1" (\s -> return (T.unpack k, T.unpack s)) v
instance ToJSON Ticker where instance ToJSON Ticker where
toJSON t = object [ "name" .= code t, toJSON t = object [ "name" .= code t,
"timeframe" .= timeframeSeconds t, "timeframe" .= timeframeSeconds t,
"aliases" .= Object (HM.fromList $ fmap (\(x, y) -> (T.pack x, String $ T.pack y)) $ aliases t) ] "aliases" .= Object (KM.fromList $ fmap (\(x, y) -> (fromText . T.pack $ x, String $ T.pack y)) $ aliases t) ]
data BarSeriesId = BarSeriesId TickerId BarTimeframe data BarSeriesId = BarSeriesId TickerId BarTimeframe
deriving (Show, Eq, Generic, Ord) deriving (Show, Eq, Generic, Ord)

7
stack.yaml

@ -18,7 +18,7 @@
# #
# resolver: ./custom-snapshot.yaml # resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.yaml # resolver: https://example.com/snapshots/2018-01-01.yaml
resolver: lts-18.18 resolver: lts-20.26
# User packages to be built. # User packages to be built.
# Various formats can be used as shown in the example below. # Various formats can be used as shown in the example below.
@ -48,8 +48,9 @@ extra-deps:
- binary-ieee754-0.1.0.0 - binary-ieee754-0.1.0.0
- th-printf-0.7 - th-printf-0.7
- normaldistribution-1.1.0.3 - normaldistribution-1.1.0.3
- co-log-0.4.0.1@sha256:3d4c17f37693c80d1aa2c41669bc3438fac3e89dc5f479e57d79bc3ddc4dfcc5,5087 - co-log-0.5.0.0
- ansi-terminal-0.10.3@sha256:e2fbcef5f980dc234c7ad8e2fa433b0e8109132c9e643bc40ea5608cd5697797,3226 - chronos-1.1.5@sha256:ca35be5fdbbb384414226b4467c6d1c8b44defe59a9c8a3af32c1c5fb250c781,3830
- typerep-map-0.5.0.0@sha256:34f1ba9b268a6d52e26ae460011a5571e8099b50a3f4a7c8db25dd8efe3be8ee,4667
# Override default flag values for local packages and extra-deps # Override default flag values for local packages and extra-deps
# flags: {} # flags: {}

107
test/Test/BarAggregator.hs

@ -10,18 +10,21 @@ import ATrade.BarAggregator
import ATrade.RoboCom.Types import ATrade.RoboCom.Types
import ATrade.Types import ATrade.Types
import Data.List import Data.List
import qualified Data.Map.Strict as M import Data.List.NonEmpty (NonEmpty (..))
import qualified Data.Text as T import qualified Data.List.NonEmpty as NE
import qualified Data.Map.Strict as M
import qualified Data.Text as T
import Data.Time.Calendar import Data.Time.Calendar
import Data.Time.Clock import Data.Time.Clock
import Safe import Safe
import Hedgehog as HH
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import Test.Tasty import Test.Tasty
import Test.Tasty.Hedgehog
import Test.Tasty.HUnit import Test.Tasty.HUnit
import Test.Tasty.QuickCheck as QC
import Test.Tasty.SmallCheck as SC
import ArbitraryInstances
unitTests = testGroup "BarAggregator" [ unitTests = testGroup "BarAggregator" [
@ -33,9 +36,36 @@ unitTests = testGroup "BarAggregator" [
properties = testGroup "BarAggregator" [ properties = testGroup "BarAggregator" [
prop_allTicksInOneBar prop_allTicksInOneBar
, prop_threeBars
] ]
secParams = InstrumentParameters 1 0.01 secParams = InstrumentParameters "TEST_TICKER" 1 0.01
genTick :: T.Text -> UTCTime -> Int -> Gen Tick
genTick tickerId baseTime timeframe = do
ts <- generateTimestampInsideBar baseTime timeframe
val <- fromIntegral <$> Gen.int (Range.linear 1 1000000)
vol <- Gen.integral (Range.linear 1 1000000)
return $ Tick tickerId LastTradePrice ts (fromDouble $ val / 1000) vol
where
generateTimestampInsideBar base timeframe =
flip addUTCTime base .
fromRational .
toRational .
picosecondsToDiffTime <$> Gen.integral (Range.linear 0 (truncate 1e12 * fromIntegral timeframe))
mkAggregator :: TickerId -> Int -> BarAggregator
mkAggregator tickerId tf = mkAggregatorFromBars (M.singleton tickerId (BarSeries tickerId (BarTimeframe tf) [] secParams)) [(0, 86400)]
assertBarCorrespondence :: (MonadTest m) => Bar -> NE.NonEmpty Tick -> m ()
assertBarCorrespondence bar ticks = do
barHigh bar === maximum (value <$> sortedTicks)
barLow bar === minimum (value <$> sortedTicks)
barOpen bar === value (NE.head sortedTicks)
barClose bar === value (NE.last sortedTicks)
barVolume bar === sum (volume <$> sortedTicks)
where
sortedTicks = NE.fromList . sortOn timestamp . NE.toList $ ticks
testUnknownBarSeries :: TestTree testUnknownBarSeries :: TestTree
testUnknownBarSeries = testCase "Tick with unknown ticker id" $ do testUnknownBarSeries = testCase "Tick with unknown ticker id" $ do
@ -94,11 +124,13 @@ testTwoTicksInDifferentBars = testCase "Two ticks - different bar" $ do
let (mbar, newagg) = handleTick (tick testTimestamp1 12.00) agg let (mbar, newagg) = handleTick (tick testTimestamp1 12.00) agg
mbar @?= Nothing mbar @?= Nothing
let (mbar', newagg') = handleTick (tick testTimestamp2 14.00) newagg let (mbar', newagg') = handleTick (tick testTimestamp2 14.00) newagg
mbar' @?= Just (Bar "TEST_TICKER" testTimestamp1 12.00 12.00 12.00 12.00 1) mbar' @?= Just (Bar "TEST_TICKER" barEndTime 12.00 12.00 12.00 12.00 1)
(bsBars <$> (M.lookup "TEST_TICKER" $ bars newagg')) @?= Just [Bar "TEST_TICKER" testTimestamp2 14.00 14.00 14.00 14.00 1, Bar "TEST_TICKER" testTimestamp1 12.00 12.00 12.00 12.00 1]
(bsBars <$> (M.lookup "TEST_TICKER" $ bars newagg')) @?= Just [Bar "TEST_TICKER" testTimestamp2 14.00 14.00 14.00 14.00 1, Bar "TEST_TICKER" barEndTime 12.00 12.00 12.00 12.00 1]
where where
testTimestamp1 = (UTCTime (fromGregorian 1970 1 1) 58) testTimestamp1 = UTCTime (fromGregorian 1970 1 1) 58
testTimestamp2 = (UTCTime (fromGregorian 1970 1 1) 61) barEndTime = UTCTime (fromGregorian 1970 1 1) 60
testTimestamp2 = UTCTime (fromGregorian 1970 1 1) 61
tick ts val = Tick { tick ts val = Tick {
security = "TEST_TICKER", security = "TEST_TICKER",
datatype = LastTradePrice, datatype = LastTradePrice,
@ -106,27 +138,42 @@ testTwoTicksInDifferentBars = testCase "Two ticks - different bar" $ do
value = fromDouble val, value = fromDouble val,
volume = 1 } volume = 1 }
prop_allTicksInOneBar :: TestTree prop_allTicksInOneBar :: TestTree
prop_allTicksInOneBar = QC.testProperty "All ticks in one bar" $ QC.forAll (QC.choose (1, 86400)) $ \timeframe -> prop_allTicksInOneBar = testProperty "All ticks in one bar" $ property $ do
QC.forAll (QC.listOf1 (genTick "TEST_TICKER" baseTime timeframe)) $ \ticks -> tf <- forAll $ Gen.integral (Range.constant 1 86400)
let ticks' = sortOn timestamp ticks in ticks <- forAll $ Gen.list (Range.linear 1 100) (genTick "TEST_TICKER" baseTime tf)
let (newbars, agg) = handleTicks ticks' (mkAggregator "TEST_TICKER" timeframe) in let ticks' = sortOn timestamp ticks
null newbars && let (newbars, agg) = handleTicks ticks' (mkAggregator "TEST_TICKER" tf)
((barHigh <$> currentBar "TEST_TICKER" agg) == Just (maximum $ value <$> ticks)) && let (Just lastBar) = currentBar "TEST_TICKER" agg
((barLow <$> currentBar "TEST_TICKER" agg) == Just (minimum $ value <$> ticks)) && HH.assert $ null newbars
((barOpen <$> currentBar "TEST_TICKER" agg) == (value <$> headMay ticks')) && assertBarCorrespondence lastBar $ NE.fromList ticks
((barClose <$> currentBar "TEST_TICKER" agg) == (value <$> lastMay ticks')) &&
((barVolume <$> currentBar "TEST_TICKER" agg) == Just (sum $ volume <$> ticks))
where where
genTick :: T.Text -> UTCTime -> Int -> Gen Tick
genTick tickerId base tf = do
difftime <- fromRational . toRational . picosecondsToDiffTime <$> choose (0, truncate 1e12 * fromIntegral tf)
val <- arbitrary
vol <- arbitrary `suchThat` (> 0)
return $ Tick tickerId LastTradePrice (difftime `addUTCTime` baseTime) val vol
mkAggregator tickerId tf = mkAggregatorFromBars (M.singleton tickerId (BarSeries tickerId (BarTimeframe tf) [] secParams)) [(0, 86400)]
currentBar tickerId agg = headMay =<< (bsBars <$> M.lookup tickerId (bars agg)) currentBar tickerId agg = headMay =<< (bsBars <$> M.lookup tickerId (bars agg))
baseTime = UTCTime (fromGregorian 1970 1 1) 0 baseTime = UTCTime (fromGregorian 1970 1 1) 0
prop_threeBars :: TestTree
prop_threeBars = testProperty "Three bars" $ property $ do
tf <- forAll $ Gen.integral (Range.constant 1 86400)
ticks1 <- forAll $ Gen.list (Range.linear 1 100) (genTick "TEST_TICKER" baseTime tf)
let secondBarBaseTime = addUTCTime (fromIntegral tf) baseTime
ticks2 <- forAll $ Gen.list (Range.linear 1 100) (genTick "TEST_TICKER" secondBarBaseTime tf)
let thirdBarBaseTime = addUTCTime (fromIntegral $ 2 * tf) baseTime
ticks3 <- forAll $ Gen.list (Range.linear 1 100) (genTick "TEST_TICKER" thirdBarBaseTime tf)
let ticks' = sortOn timestamp $ ticks1 <> ticks2 <> ticks3
let ([secondBar, firstBar], agg) = handleTicks ticks' (mkAggregator "TEST_TICKER" tf)
assertBarCorrespondence firstBar (NE.fromList ticks1)
assertBarCorrespondence secondBar (NE.fromList ticks2)
barTimestamp firstBar === secondBarBaseTime
barTimestamp secondBar === thirdBarBaseTime
let (Just lastBar) = currentBar "TEST_TICKER" agg
assertBarCorrespondence lastBar (NE.fromList ticks3)
where
currentBar tickerId agg = headMay =<< (bsBars <$> M.lookup tickerId (bars agg))
baseTime = UTCTime (fromGregorian 1970 1 1) 0

19
test/Test/Driver/Junction/QuoteThread.hs

@ -1,5 +1,6 @@
{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeSynonymInstances #-} {-# LANGUAGE TypeSynonymInstances #-}
@ -16,6 +17,7 @@ import Test.Tasty.SmallCheck as SC
import ATrade.Driver.Junction.QuoteThread (addSubscription, import ATrade.Driver.Junction.QuoteThread (addSubscription,
startQuoteThread, startQuoteThread,
stopQuoteThread) stopQuoteThread)
import ATrade.Logging (Message)
import ATrade.Quotes.HistoryProvider (HistoryProvider (..)) import ATrade.Quotes.HistoryProvider (HistoryProvider (..))
import ATrade.Quotes.TickerInfoProvider (TickerInfoProvider (..)) import ATrade.Quotes.TickerInfoProvider (TickerInfoProvider (..))
import ATrade.QuoteSource.Client (QuoteData (QDBar)) import ATrade.QuoteSource.Client (QuoteData (QDBar))
@ -26,23 +28,21 @@ import ATrade.RoboCom.Types (BarSeries (bsBars),
BarSeriesId (BarSeriesId), BarSeriesId (BarSeriesId),
InstrumentParameters (InstrumentParameters)) InstrumentParameters (InstrumentParameters))
import ATrade.Types import ATrade.Types
import Colog.Core (LogAction (..))
import Colog.Core.Class (HasLog (..))
import Control.Concurrent (forkIO, threadDelay) import Control.Concurrent (forkIO, threadDelay)
import Control.Concurrent.BoundedChan (newBoundedChan, readChan, import Control.Concurrent.BoundedChan (newBoundedChan, readChan,
writeChan) writeChan)
import Control.Exception (bracket) import Control.Exception (bracket)
import Control.Monad (forever) import Control.Monad (forever)
import Control.Monad.Reader import Control.Monad.Reader
import Data.IORef (newIORef, readIORef) import Data.IORef (IORef, newIORef, readIORef)
import qualified Data.Map.Strict as M import qualified Data.Map.Strict as M
import qualified Data.Text as T import qualified Data.Text as T
import Data.Time (UTCTime (UTCTime), import Data.Time (UTCTime (UTCTime),
fromGregorian) fromGregorian)
import System.IO (BufferMode (LineBuffering), import System.IO (BufferMode (LineBuffering),
hSetBuffering, stderr) hSetBuffering, stderr)
import System.Log.Formatter
import System.Log.Handler (setFormatter)
import System.Log.Handler.Simple
import System.Log.Logger
import System.ZMQ4 (withContext) import System.ZMQ4 (withContext)
import Test.Mock.HistoryProvider (MockHistoryProvider, import Test.Mock.HistoryProvider (MockHistoryProvider,
mkMockHistoryProvider, mkMockHistoryProvider,
@ -70,13 +70,16 @@ instance TickerInfoProvider TestM where
tip <- asks tickerInfoProvider tip <- asks tickerInfoProvider
liftIO $ mockGetInstrumentParameters tip tickers liftIO $ mockGetInstrumentParameters tip tickers
instance HasLog TestEnv Message TestM where
getLogAction env = LogAction $ \msg -> return ()
qsEndpoint = "inproc://qs" qsEndpoint = "inproc://qs"
mockHistoryProvider = mkMockHistoryProvider $ M.fromList [(BarSeriesId "FOO" (BarTimeframe 3600), bars)] mockHistoryProvider = mkMockHistoryProvider $ M.fromList [(BarSeriesId "FOO" (BarTimeframe 3600), bars)]
where where
bars = [] bars = []
mockTickerInfoProvider = mkMockTickerInfoProvider $ M.fromList [("FOO", InstrumentParameters 10 0.1)] mockTickerInfoProvider = mkMockTickerInfoProvider $ M.fromList [("FOO", InstrumentParameters "FOO" 10 0.1)]
unitTests = testGroup "Driver.Junction.QuoteThread" [ unitTests = testGroup "Driver.Junction.QuoteThread" [
testSubscription testSubscription
@ -85,12 +88,14 @@ unitTests = testGroup "Driver.Junction.QuoteThread" [
testSubscription :: TestTree testSubscription :: TestTree
testSubscription = testCase "Subscription" $ withContext $ \ctx -> do testSubscription = testCase "Subscription" $ withContext $ \ctx -> do
barsRef <- newIORef M.empty barsRef <- newIORef M.empty
tiRef <- newIORef M.empty
serverChan <- newBoundedChan 2000 serverChan <- newBoundedChan 2000
let clientSecurityParams = defaultClientSecurityParams
bracket bracket
(startQuoteSourceServer serverChan ctx qsEndpoint defaultServerSecurityParams) (startQuoteSourceServer serverChan ctx qsEndpoint defaultServerSecurityParams)
stopQuoteSourceServer $ \_ -> stopQuoteSourceServer $ \_ ->
bracket bracket
(startQuoteThread barsRef ctx qsEndpoint Nothing Nothing (`runReaderT` (TestEnv mockHistoryProvider mockTickerInfoProvider))) (startQuoteThread barsRef tiRef ctx qsEndpoint clientSecurityParams (`runReaderT` (TestEnv mockHistoryProvider mockTickerInfoProvider)) (LogAction $ \_ -> return ()))
stopQuoteThread $ \qt -> do stopQuoteThread $ \qt -> do
chan <- newBoundedChan 2000 chan <- newBoundedChan 2000

2
test/Test/RoboCom/Indicators.hs

@ -7,8 +7,6 @@ module Test.RoboCom.Indicators
import Test.Tasty import Test.Tasty
import Test.Tasty.HUnit import Test.Tasty.HUnit
import Test.Tasty.QuickCheck as QC
import Test.Tasty.SmallCheck as SC
import ATrade.Types import ATrade.Types
import qualified Data.Text as T import qualified Data.Text as T

165
test/Test/RoboCom/Positions.hs

@ -0,0 +1,165 @@
{-# LANGUAGE OverloadedStrings #-}
module Test.RoboCom.Positions
(
unitTests
) where
import Test.Tasty
import Test.Tasty.HUnit
import ATrade.Types
import qualified Data.List as L
import qualified Data.Map.Strict as M
import qualified Data.Text as T
import Data.Time.Calendar
import Data.Time.Clock
import ATrade.RoboCom.Monad
import ATrade.RoboCom.Positions
import ATrade.RoboCom.Types
data TestState = TestState
{
positions :: [Position],
testInt :: Int
}
defaultState = TestState {
positions = [],
testInt = 0
}
data TestConfig = TestConfig
instance ParamsHasMainTicker TestConfig where
mainTicker _ = "TEST_TICKER"
instance StateHasPositions TestState where
getPositions = positions
setPositions a p = a { positions = p }
defaultStrategyEnvironment = StrategyEnvironment
{
seInstanceId = "test_instance",
seAccount = "test_account",
seVolume = 1,
seBars = M.empty,
seLastTimestamp = (UTCTime (fromGregorian 1970 1 1) 0)
}
unitTests = testGroup "RoboCom.Positions" [
testEnterAtMarket,
testEnterAtMarketSendsAction,
testDefaultHandlerSubmissionDeadline,
testDefaultHandlerAfterSubmissionPositionIsWaitingOpen,
testDefaultHandlerPositionWaitingOpenOrderOpenExecuted1
]
testEnterAtMarket = testCase "enterAtMarket creates position in PositionWaitingOpenSubmission state" $ do
let (newState, actions, _) = runStrategyElement TestConfig defaultState defaultStrategyEnvironment element
assertBool "Should be exactly 1 position" ((length . positions) newState == 1)
let pos = head . positions $ newState
assertBool "Should be in PositionWaitingOpenSubmission" (isPositionWaitingOpenSubmission . posState $ pos)
let (PositionWaitingOpenSubmission order) = posState pos
assertBool "Account should be 'test_account'" (orderAccountId order == "test_account")
assertBool "Security should be 'TEST_TICKER'" (orderSecurity order == "TEST_TICKER")
assertBool "Order price should be Market" (orderPrice order == Market)
assertBool "Order quantity should be 1" (orderQuantity order == 1)
assertBool "Executed order quantity should be 0" (orderExecutedQuantity order == 0)
assertBool "Order operation should be Buy" (orderOperation order == Buy)
assertBool "Order signal id should be correct" (orderSignalId order == (SignalId "test_instance" "long" ""))
where
element = enterAtMarket "long" Buy
isPositionWaitingOpenSubmission (PositionWaitingOpenSubmission _) = True
isPositionWaitingOpenSubmission _ = False
testEnterAtMarketSendsAction = testCase "enterAtMarket sends ActionSubmitOrder" $ do
let (newState, actions, _) = runStrategyElement TestConfig defaultState defaultStrategyEnvironment element
case (L.find isActionOrder actions) of
Just (ActionOrder order) -> do
assertBool "Account should be 'test_account'" (orderAccountId order == "test_account")
assertBool "Security should be 'TEST_TICKER'" (orderSecurity order == "TEST_TICKER")
assertBool "Order price should be Market" (orderPrice order == Market)
assertBool "Order quantity should be 1" (orderQuantity order == 1)
assertBool "Executed order quantity should be 0" (orderExecutedQuantity order == 0)
assertBool "Order operation should be Buy" (orderOperation order == Buy)
assertBool "Order signal id should be correct" (orderSignalId order == (SignalId "test_instance" "long" ""))
Nothing -> assertFailure "Should be exactly 1 ActionOrder"
where
element = enterAtMarket "long" Buy
isActionOrder (ActionOrder _) = True
isActionOrder _ = False
testDefaultHandlerSubmissionDeadline = testCase "defaultHandler after submission deadline marks position as cancelled" $ do
let (newState, actions, _) = runStrategyElement TestConfig defaultState defaultStrategyEnvironment element
let (newState', actions', _) = runStrategyElement TestConfig newState defaultStrategyEnvironment { seLastTimestamp = afterDeadline } $ defaultHandler (NewTick tick)
let pos = head . positions $ newState'
assertBool "Cancelled position" (posState pos == PositionCancelled)
where
element = enterAtMarket "long" Buy
afterDeadline = (UTCTime (fromGregorian 1970 1 1) 100)
tick = Tick {
security = "TEST_TICKER",
datatype = LastTradePrice,
timestamp = afterDeadline,
value = fromDouble 12.00,
volume = 1 }
testDefaultHandlerAfterSubmissionPositionIsWaitingOpen = testCase "defaultHandler after successful submission sets position state as PositionWaitingOpen" $ do
let (newState, actions, _) = runStrategyElement TestConfig defaultState defaultStrategyEnvironment element
let pos = head . positions $ newState
let (PositionWaitingOpenSubmission order) = posState pos
let (newState', actions', _) = runStrategyElement TestConfig newState defaultStrategyEnvironment { seLastTimestamp = beforeDeadline } $ defaultHandler (OrderSubmitted order {orderId = 1 })
let pos' = head . positions $ newState'
assertEqual "New position state should be PositionWaitingOpen" (posState pos') PositionWaitingOpen
where
element = enterAtMarket "long" Buy
beforeDeadline = (UTCTime (fromGregorian 1970 1 1) 1)
testDefaultHandlerPositionWaitingOpenOrderCancelledExecuted0 = testCase "defaultHandler in PositionWaitingOpen, if order is cancelled and nothing is executed, marks position as cancelled" $ do
let (newState, actions, _) = runStrategyElement TestConfig defaultState defaultStrategyEnvironment element
let pos = head . positions $ newState
let (PositionWaitingOpenSubmission order) = posState pos
let (newState', actions', _) = runStrategyElement TestConfig newState defaultStrategyEnvironment { seLastTimestamp = ts1 } $ defaultHandler (OrderSubmitted order {orderId = 1 })
let (newState'', actions'', _) = runStrategyElement TestConfig newState defaultStrategyEnvironment { seLastTimestamp = ts2 } $ defaultHandler (OrderUpdate 1 Cancelled)
let pos = head . positions $ newState''
assertEqual "New position state should be PositionCancelled" (posState pos) PositionCancelled
where
element = enterAtMarket "long" Buy
ts1 = (UTCTime (fromGregorian 1970 1 1) 1)
ts2 = (UTCTime (fromGregorian 1970 1 1) 2)
testDefaultHandlerPositionWaitingOpenOrderOpenExecuted1 = testCase "defaultHandler in PositionWaitingOpen, if order is cancelled and something is executed, marks position as open" $ do
let (newState, actions, _) = runStrategyElement TestConfig defaultState defaultStrategyEnvironment element
let pos = head . positions $ newState
let (PositionWaitingOpenSubmission order) = posState pos
let (newState', actions', _) = runStrategyElement TestConfig newState defaultStrategyEnvironment { seLastTimestamp = ts1, seVolume = 2 } $ defaultHandler (OrderSubmitted order {orderId = 1 })
let (newState'', actions'', _) = runStrategyElement TestConfig newState' defaultStrategyEnvironment { seLastTimestamp = ts2 } $ defaultHandler (NewTrade trade)
let (newState''', actions''', _) = runStrategyElement TestConfig newState'' defaultStrategyEnvironment { seLastTimestamp = ts3 } $ defaultHandler (OrderUpdate 1 Cancelled)
let pos = head . positions $ newState'''
assertEqual "New position state should be PositionOpen" (posState pos) PositionOpen
where
element = enterAtMarket "long" Buy
ts1 = (UTCTime (fromGregorian 1970 1 1) 1)
ts2 = (UTCTime (fromGregorian 1970 1 1) 2)
ts3 = (UTCTime (fromGregorian 1970 1 1) 3)
trade = Trade
{
tradeOrderId = 1,
tradePrice = fromDouble 10,
tradeQuantity = 1,
tradeVolume = fromDouble 10,
tradeVolumeCurrency = "FOO",
tradeOperation = Buy,
tradeAccount = "test_account",
tradeSecurity = "TEST_TICKER",
tradeTimestamp = ts3,
tradeCommission = fromDouble 0,
tradeSignalId = SignalId "test_instance" "long" ""
}

4
test/Test/RoboCom/Utils.hs

@ -7,11 +7,9 @@ module Test.RoboCom.Utils
import Test.Tasty import Test.Tasty
import Test.Tasty.HUnit import Test.Tasty.HUnit
import Test.Tasty.QuickCheck as QC
import Test.Tasty.SmallCheck as SC
import ATrade.Types import ATrade.Types
import qualified Data.Text as T import qualified Data.Text as T
import Data.Time.Calendar import Data.Time.Calendar
import Data.Time.Clock import Data.Time.Clock

Loading…
Cancel
Save