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

24
src/ATrade/BarAggregator.hs

@ -72,25 +72,28 @@ handleTicks ticks aggregator = foldl f ([], aggregator) ticks @@ -72,25 +72,28 @@ handleTicks ticks aggregator = foldl f ([], aggregator) ticks
handleTick :: Tick -> BarAggregator -> (Maybe Bar, BarAggregator)
handleTick tick = runState $ do
lLastTicks %= M.insert (security tick, datatype tick) tick
tws <- gets tickTimeWindows
timeWindows <- gets tickTimeWindows
mybars <- gets bars
if (any (isInTimeInterval tick) tws)
if any (isInTimeInterval tick) timeWindows
then
case M.lookup (security tick) mybars of
Just series -> case bsBars series of
(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
LastTradePrice ->
if volume tick > 0
then
if currentBn == barNumber (timestamp tick) (fromIntegral . unBarTimeframe $ bsTimeframe series)
if currentBn == barNumber (timestamp tick) timeframeInSeconds
then do
lBars %= M.insert (security tick) series { bsBars = updateBar b tick : bs }
return Nothing
else do
lBars %= M.insert (security tick) series { bsBars = barFromTick tick : b : bs }
return . Just $ b
let barEndTimestamp = barEndTime b timeframeInSeconds
let resultingBar = b { barTimestamp = barEndTimestamp }
lBars %= M.insert (security tick) series { bsBars = barFromTick tick : resultingBar : bs }
return . Just $ resultingBar
else
return Nothing
_ ->
@ -140,15 +143,16 @@ handleTick tick = runState $ do @@ -140,15 +143,16 @@ handleTick tick = runState $ do
updateTime :: Tick -> BarAggregator -> (Maybe Bar, BarAggregator)
updateTime tick = runState $ do
lLastTicks %= M.insert (security tick, datatype tick) tick
tws <- gets tickTimeWindows
timeWindows <- gets tickTimeWindows
mybars <- gets bars
if (any (isInTimeInterval tick) tws)
if any (isInTimeInterval tick) timeWindows
then
case M.lookup (security tick) mybars of
Just series -> case bsBars series of
(b:bs) -> do
let currentBn = barNumber (barTimestamp b) (fromIntegral . unBarTimeframe $ bsTimeframe series)
let thisBn = barNumber (timestamp tick) (fromIntegral . unBarTimeframe $ bsTimeframe series)
let timeframeInSeconds = fromIntegral . unBarTimeframe $ bsTimeframe series
let currentBn = barNumber (barTimestamp b) timeframeInSeconds
let thisBn = barNumber (timestamp tick) timeframeInSeconds
if
| currentBn == thisBn -> do
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 ( @@ -13,59 +13,101 @@ module ATrade.Driver.Backtest (
backtestMain
) where
import ATrade.Driver.Types (InitializationCallback,
StrategyInstanceParams (..))
import ATrade.Exceptions
import ATrade.Quotes
import ATrade.Quotes.Finam as QF
import ATrade.Quotes.QTIS
import ATrade.RoboCom.Monad (Event (..), EventCallback,
MonadRobot (..),
StrategyEnvironment (..),
appendToLog, seBars, seLastTimestamp)
import ATrade.RoboCom.Positions
import ATrade.RoboCom.Types (BarSeries (..), Bars, InstrumentParameters (InstrumentParameters),
Ticker (..), Timeframe (..))
import ATrade.Types
import Conduit (awaitForever, runConduit, yield,
(.|))
import Control.Exception.Safe
import Control.Lens hiding (ix, (<|), (|>))
import Control.Monad.ST (runST)
import Control.Monad.State
import Data.Aeson (FromJSON (..), Value (..), decode)
import Data.Aeson.Types (parseMaybe)
import Data.ByteString.Lazy (readFile, toStrict)
import Data.Default
import Data.HashMap.Strict (lookup)
import Data.List (partition)
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.IO (putStrLn)
import qualified Data.Text.Lazy as TL
import Data.Time.Calendar (fromGregorian)
import Data.Time.Clock (DiffTime, UTCTime (..))
import Data.Vector ((!), (!?), (//))
import qualified Data.Vector as V
import Options.Applicative hiding (Success)
import Prelude hiding (lookup, putStrLn, readFile)
import Safe (headMay)
import System.ZMQ4 hiding (Event)
import ATrade.Driver.Junction.Types (StrategyDescriptor (StrategyDescriptor),
StrategyDescriptorE (StrategyDescriptorE),
TickerConfig, confStrategy,
confTickers, eventCallback,
strategyBaseName, tickerId,
timeframe)
import ATrade.Exceptions (RoboComException (UnableToLoadConfig, UnableToLoadFeed))
import ATrade.Logging (Message, Severity (Error, Trace),
fmtMessage, logWith)
import ATrade.Quotes.QTIS (TickerInfo (tiLotSize, tiTickSize),
qtisGetTickersInfo)
import ATrade.RoboCom.ConfigStorage (ConfigStorage (loadConfig))
import ATrade.RoboCom.Monad (Event (..), MonadRobot (..),
StrategyEnvironment (..),
appendToLog, seLastTimestamp)
import ATrade.RoboCom.Types (BarSeries (..),
BarSeriesId (BarSeriesId), Bars,
InstrumentParameters (InstrumentParameters),
Ticker (..))
import ATrade.Types (Bar (Bar, barHigh, barLow, barOpen, barSecurity, barTimestamp),
BarTimeframe (BarTimeframe),
Operation (Buy),
Order (orderAccountId, orderId, orderOperation, orderPrice, orderQuantity, orderSecurity, orderSignalId),
OrderId,
OrderPrice (Limit, Market),
OrderState (Cancelled, Executed, Submitted),
Price, TickerId, Trade (..),
fromDouble)
import Colog (LogAction, (>$<))
import Colog.Actions (logTextStdout)
import Conduit (ConduitT, Void, awaitForever,
runConduit, yield, (.|))
import Control.Exception.Safe (catchAny, throw)
import Control.Lens (makeLenses, use, (%=), (+=),
(.=), (^.))
import Control.Monad.ST (runST)
import Control.Monad.State (MonadIO, MonadPlus (mzero),
MonadState, MonadTrans (lift),
State, StateT (StateT),
execState, forM_, gets, when)
import Data.Aeson (FromJSON (..), Value (..),
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
deriving (Show, Eq)
data Params = Params {
strategyBasename :: String,
strategyConfigFile :: FilePath,
qtisEndpoint :: String,
paramsFeeds :: [Feed]
} deriving (Show, Eq)
data BacktestState c s = BacktestState {
_descriptor :: StrategyDescriptor c s,
_cash :: Double,
_robotState :: s,
_robotParams :: c,
@ -75,101 +117,135 @@ data BacktestState c s = BacktestState { @@ -75,101 +117,135 @@ data BacktestState c s = BacktestState {
_tradesLog :: [Trade],
_orderIdCounter :: Integer,
_pendingTimers :: [UTCTime],
_logs :: [T.Text]
_logs :: [T.Text],
_barsMap :: M.Map BarSeriesId BarSeries,
_availableTickers :: NonEmpty BarSeriesId
}
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 = Params
<$> strOption (
long "config" <> short 'c'
)
long "strategy-name" <> short 'n')
<*> strOption (
long "config" <> short 'c')
<*> strOption
( long "qtis" <> short 'q' <> metavar "ENDPOINT/ID" )
( long "qtis" <> short 'q' <> metavar "ENDPOINT/ID")
<*> some (option feedArgParser (
long "feed" <> short 'f'
))
long "feed" <> short 'f'))
feedArgParser :: ReadM Feed
feedArgParser = eitherReader (\s -> case splitOn ":" s of
[tid, fpath] -> Right $ Feed (T.pack tid) fpath
_ -> Left $ "Unable to parse feed id: " ++ s)
backtestMain :: (FromJSON c, StateHasPositions s) => DiffTime -> s -> EventCallback c s -> IO ()
backtestMain _dataDownloadDelta defaultState callback = do
params <- execParser opts
(tickerList, config) <- loadStrategyConfig params
logger :: (MonadIO m) => LogAction m Message
logger = fmtMessage >$< logTextStdout
let instanceParams = StrategyInstanceParams {
strategyInstanceId = "foo",
strategyAccount = "foo",
strategyVolume = 1,
tickers = tickerList,
strategyQTISEp = Nothing }
backtestMain :: M.Map T.Text StrategyDescriptorE -> IO ()
backtestMain descriptors = do
params <- execParser opts
let log = logWith logger
let strategyName = T.pack $ strategyBasename params
feeds <- loadFeeds (paramsFeeds params)
bars <- makeBars (T.pack $ qtisEndpoint params) tickerList
runBacktestDriver feeds config bars
case M.lookup strategyName descriptors of
Just (StrategyDescriptorE desc) -> flip catchAny (\e -> log Error "Backtest" $ "Exception: " <> (T.pack . show $ e)) $
runBacktestDriver desc feeds params
Nothing -> log Error "Backtest" $ "Can't find strategy: " <> strategyName
where
opts = info (helper <*> paramsParser)
( fullDesc <> header "ATrade strategy backtesting framework" )
makeBars :: T.Text -> [Ticker] -> IO (M.Map TickerId BarSeries)
makeBars qtisEp tickersList =
makeBars :: T.Text -> [TickerConfig] -> IO (M.Map BarSeriesId BarSeries)
makeBars qtisEp confs =
withContext $ \ctx ->
M.fromList <$> mapM (mkBarEntry ctx qtisEp) tickersList
mkBarEntry ctx qtisEp tickerEntry = do
info <- qtisGetTickersInfo ctx qtisEp (code tickerEntry)
return (code tickerEntry, BarSeries (code tickerEntry) (Timeframe (timeframeSeconds tickerEntry)) [] (InstrumentParameters (fromInteger $ tiLotSize info) (tiTickSize info)))
runBacktestDriver feeds params tickerList = do
let s = runConduit $ barStreamFromFeeds feeds .| backtestLoop
let finalState = execState (unBacktestingMonad s) $ defaultBacktestState defaultState params tickerList
print $ finalState ^. cash
print $ finalState ^. tradesLog
forM_ (reverse $ finalState ^. logs) putStrLn
loadStrategyConfig :: (FromJSON c) => Params -> IO ([Ticker], c)
loadStrategyConfig params = do
content <- readFile (strategyConfigFile params)
case loadStrategyConfig' content of
Just (tickersList, config) -> return (tickersList, config)
_ -> throw $ UnableToLoadConfig (T.pack . strategyConfigFile $ params)
loadStrategyConfig' content = do
v <- decode content
case v of
Object o -> do
mbTickers <- "tickers" `lookup` o
mbParams <- "params" `lookup` o
tickers' <- parseMaybe parseJSON mbTickers
params <- parseMaybe parseJSON mbParams
return (tickers', params)
_ -> Nothing
M.fromList <$> mapM (mkBarEntry ctx qtisEp) confs
mkBarEntry ctx qtisEp conf = do
info <- qtisGetTickersInfo ctx qtisEp (tickerId conf)
return (BarSeriesId (tickerId conf) (timeframe conf),
BarSeries
(tickerId conf)
(timeframe conf)
[]
(InstrumentParameters (tickerId conf) (fromInteger $ tiLotSize info) (tiTickSize info)))
runBacktestDriver desc feeds params = do
bigConf <- loadConfig (T.pack $ strategyConfigFile params)
case confTickers bigConf of
tickerList@(firstTicker:restTickers) -> do
bars <- makeBars (T.pack $ qtisEndpoint params) tickerList
let s = runConduit $ barStreamFromFeeds feeds .| backtestLoop desc
let finalState =
execState (unBacktestingMonad s) $ defaultBacktestState def (confStrategy bigConf) desc bars (fmap toBarSeriesId (firstTicker :| restTickers))
print $ finalState ^. cash
print $ finalState ^. tradesLog
forM_ (reverse $ finalState ^. logs) putStrLn
_ -> return ()
toBarSeriesId conf = BarSeriesId (tickerId conf) (timeframe conf)
barStreamFromFeeds :: (Monad m) => V.Vector (BarTimeframe, [Bar]) -> ConduitT () (BarSeriesId, Bar) m ()
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 ()
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
Just ix -> do
f <- feeds !? ix
(tf, f) <- feeds !? ix
h <- headMay f
return (h, feeds // [(ix, tail f)])
return (tf, h, feeds // [(ix, (tf, tail f))])
_ -> Nothing
indexOfNextFeed feeds = runST $ do
minTs <- newSTRef Nothing
minIx <- newSTRef Nothing
forM_ [0..(V.length feeds-1)] (\ix -> do
let feed = feeds ! ix
let (_, feed) = feeds ! ix
curTs <- readSTRef minTs
case feed of
x:_ -> case curTs of
@ -182,126 +258,124 @@ backtestMain _dataDownloadDelta defaultState callback = do @@ -182,126 +258,124 @@ backtestMain _dataDownloadDelta defaultState callback = do
_ -> return ())
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
_env <- gets _strategyEnvironment
let newTimestamp = barTimestamp bar
strategyEnvironment . seBars %= (flip updateBars bar)
barsMap %= updateBars bsId bar
strategyEnvironment . seLastTimestamp .= newTimestamp
enqueueEvent (NewBar bar)
lift handleEvents)
handleEvents = do
events <- use pendingEvents
case events of
x :<| xs -> do
pendingEvents .= xs
handleEvent x
handleEvents
_ -> return ()
executePendingOrders bar = do
executeMarketOrders bar
executeLimitOrders bar
executeLimitOrders bar = do
(limitOrders, otherOrders'') <- partition
(\o -> case orderPrice o of
Limit _ -> True
_ -> False) <$> use pendingOrders
let (executableOrders, otherOrders') = partition (isExecutable bar) limitOrders
pendingOrders .= otherOrders' ++ otherOrders''
forM_ executableOrders $ \order -> order `executeAtPrice` priceForLimitOrder order bar
isExecutable bar order = case orderPrice order of
Limit price -> if orderOperation order == Buy
then price >= barLow bar
else price <= barHigh bar
_ -> True
priceForLimitOrder order bar = case orderPrice order of
Limit price -> if orderOperation order == Buy
then if price >= barOpen bar
then barOpen bar
else price
else if price <= barOpen bar
then barOpen bar
else price
_ -> error "Should've been limit order"
executeMarketOrders bar = do
(marketOrders, otherOrders) <- partition (\o -> orderPrice o == Market) <$> use pendingOrders
pendingOrders .= otherOrders
forM_ marketOrders $ \order ->
order `executeAtPrice` barOpen bar
executeAtPrice order price = do
ts <- use $ strategyEnvironment . seLastTimestamp
let thisTrade = mkTrade order price ts
tradesLog %= (\log' -> thisTrade : log')
pendingEvents %= (\s -> (OrderUpdate (orderId order) Executed) <| s)
pendingEvents %= (\s -> (NewTrade thisTrade) <| s)
mkTrade :: Order -> Price -> UTCTime -> Trade
mkTrade order price ts = Trade {
tradeOrderId = orderId order,
tradePrice = price,
tradeQuantity = orderQuantity order,
tradeVolume = (fromIntegral . orderQuantity $ order) * price,
tradeVolumeCurrency = "pt",
tradeOperation = orderOperation order,
tradeAccount = orderAccountId order,
tradeSecurity = orderSecurity order,
tradeTimestamp = ts,
tradeCommission = 0,
tradeSignalId = orderSignalId order
}
handleEvent event@(NewBar bar) = do
executePendingOrders bar
handleEvents -- This should pass OrderUpdate events to the callback before NewBar events
firedTimers <- fireTimers (barTimestamp bar)
mapM_ (\x -> enqueueEvent (TimerFired x)) firedTimers
handleEvent' event
return ()
handleEvent event = handleEvent' event
handleEvent' event = callback event
updateBars barMap newbar = M.alter (\case
Nothing -> Just BarSeries { bsTickerId = barSecurity newbar,
bsTimeframe = Timeframe 60,
bsBars = [newbar, newbar] }
Just bs -> Just bs { bsBars = updateBarList newbar (bsBars bs) }) (barSecurity newbar) barMap
updateBarList newbar (_:bs) = newbar:newbar:bs
updateBarList newbar _ = newbar:[newbar]
fireTimers ts = do
(firedTimers, otherTimers) <- partition (< ts) <$> use pendingTimers
pendingTimers .= otherTimers
return firedTimers
loadFeeds :: [Feed] -> IO (V.Vector [Bar])
loadFeeds feeds = V.fromList <$> mapM loadFeed feeds
loadFeed (Feed tid path) = do
content <- readFile path
case QF.parseQuotes $ toStrict content of
Just quotes -> return $ fmap (rowToBar tid) quotes
_ -> throw $ UnableToLoadFeed (T.pack path)
rowToBar tid r = Bar tid (rowTime r) (rowOpen r) (rowHigh r) (rowLow r) (rowClose r) (rowVolume r)
enqueueEvent event = pendingEvents %= (\s -> s |> event)
instance (Default c, Default s) => Default (BacktestState c s)
where
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 [] []
enqueueEvent (NewBar (bsIdTf bsId, bar))
lift (handleEvents desc))
bsIdTf (BarSeriesId _ tf) = tf
handleEvents :: StrategyDescriptor c s -> BacktestingMonad c s ()
handleEvents desc = do
events <- use pendingEvents
case events of
x :<| xs -> do
pendingEvents .= xs
handleEvent desc x
handleEvents desc
_ -> return ()
executePendingOrders bar = do
executeMarketOrders bar
executeLimitOrders bar
executeLimitOrders bar = do
(limitOrders, otherOrders'') <- partition
(\o -> case orderPrice o of
Limit _ -> True
_ -> False) <$> use pendingOrders
let (executableOrders, otherOrders') = partition (isExecutable bar) limitOrders
pendingOrders .= otherOrders' ++ otherOrders''
forM_ executableOrders $ \order -> order `executeAtPrice` priceForLimitOrder order bar
isExecutable bar order = case orderPrice order of
Limit price -> if orderOperation order == Buy
then price >= barLow bar
else price <= barHigh bar
_ -> True
priceForLimitOrder order bar = case orderPrice order of
Limit price -> if orderOperation order == Buy
then if price >= barOpen bar
then barOpen bar
else price
else if price <= barOpen bar
then barOpen bar
else price
_ -> error "Should've been limit order"
executeMarketOrders bar = do
(marketOrders, otherOrders) <- partition (\o -> orderPrice o == Market) <$> use pendingOrders
pendingOrders .= otherOrders
forM_ marketOrders $ \order ->
order `executeAtPrice` barOpen bar
executeAtPrice order price = do
ts <- use $ strategyEnvironment . seLastTimestamp
let thisTrade = mkTrade order price ts
tradesLog %= (thisTrade :)
pendingEvents %= (\s -> OrderUpdate (orderId order) Executed <| s)
pendingEvents %= (\s -> NewTrade thisTrade <| s)
mkTrade :: Order -> Price -> UTCTime -> Trade
mkTrade order price ts = Trade {
tradeOrderId = orderId order,
tradePrice = price,
tradeQuantity = orderQuantity order,
tradeVolume = (fromIntegral . orderQuantity $ order) * price,
tradeVolumeCurrency = "pt",
tradeOperation = orderOperation order,
tradeAccount = orderAccountId order,
tradeSecurity = orderSecurity order,
tradeTimestamp = ts,
tradeCommission = 0,
tradeSignalId = orderSignalId order
}
handleEvent :: StrategyDescriptor c s -> Event -> BacktestingMonad c s ()
handleEvent desc event@(NewBar (_, bar)) = do
executePendingOrders bar
handleEvents desc -- This should pass OrderUpdate events to the callback before NewBar events
firedTimers <- fireTimers (barTimestamp bar)
mapM_ (enqueueEvent . TimerFired) firedTimers
handleEvent' desc event
return ()
handleEvent desc event = handleEvent' desc event
handleEvent' desc event = eventCallback desc event
updateBars bsId newbar barMap = M.adjust (\bs -> bs { bsBars = newbar : bsBars bs }) bsId barMap
fireTimers ts = do
(firedTimers, otherTimers) <- partition (< ts) <$> use pendingTimers
pendingTimers .= otherTimers
return firedTimers
loadFeeds :: [Feed] -> IO (V.Vector (BarTimeframe, [Bar]))
loadFeeds feeds = V.fromList <$> mapM loadFeed feeds
loadFeed (Feed tid path) = do
content <- readFile path
case parseQuotes $ toStrict content of
Just quotes -> case headMay quotes of
Just first -> return (BarTimeframe (rowTimeframe first), fmap (rowToBar tid) quotes)
Nothing -> 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)
enqueueEvent :: MonadState (BacktestState c s) m => Event -> m ()
enqueueEvent event = pendingEvents %= (|> event)
defaultBacktestState :: s -> c -> StrategyDescriptor c s -> M.Map BarSeriesId BarSeries -> NonEmpty BarSeriesId -> BacktestState c s
defaultBacktestState s c desc = BacktestState desc 0 s c (StrategyEnvironment "" "" 1 (UTCTime (fromGregorian 1970 1 1) 0)) [] Seq.empty [] 1 [] []
newtype BacktestingMonad s c a = BacktestingMonad { unBacktestingMonad :: State (BacktestState s c) a }
deriving (Functor, Applicative, Monad, MonadState (BacktestState s c))
@ -315,21 +389,38 @@ instance MonadRobot (BacktestingMonad c s) c s where @@ -315,21 +389,38 @@ instance MonadRobot (BacktestingMonad c s) c s where
submitOrder order = do
oid <- nextOrderId
let orderWithId = order { orderId = oid }
pendingOrders %= ((:) orderWithId)
pendingEvents %= (\s -> s |> (OrderSubmitted orderWithId))
pendingOrders %= (orderWithId :)
pendingEvents %= (\s -> s |> OrderUpdate oid Submitted)
return oid
cancelOrder oid = do
orders <- use pendingOrders
let (matchingOrders, otherOrders) = partition (\o -> orderId o == oid) orders
case matchingOrders of
[] -> return ()
xs -> do
mapM_ (\o -> pendingEvents %= (\s -> s |> (OrderUpdate (orderId o) Cancelled))) xs
mapM_ (\o -> pendingEvents %= (\s -> s |> OrderUpdate (orderId o) Cancelled)) xs
pendingOrders .= otherOrders
appendToLog txt = logs %= ((:) (TL.toStrict txt))
setupTimer time = pendingTimers %= ((:) time)
appendToLog _ txt = logs %= ((TL.toStrict txt) :)
setupTimer time = pendingTimers %= (time :)
enqueueIOAction _actionId _action = error "Backtesting io actions is not supported"
getConfig = use robotParams
getState = use robotState
setState s = robotState .= s
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 @@ @@ -1,99 +1,61 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
module ATrade.Driver.Junction
(
junctionMain
) where
import ATrade.Broker.Client (BrokerClientHandle,
startBrokerClient,
import ATrade.Broker.Client (startBrokerClient,
stopBrokerClient)
import ATrade.Broker.Protocol (Notification (OrderNotification, TradeNotification),
NotificationSqnum (unNotificationSqnum),
getNotificationSqnum)
import ATrade.Driver.Junction.BrokerService (BrokerService,
getNotifications,
import ATrade.Driver.Junction.BrokerService (getNotifications,
mkBrokerService)
import ATrade.Driver.Junction.JunctionMonad (JunctionEnv (..),
JunctionM (..),
saveRobots,
startRobot)
import ATrade.Driver.Junction.ProgramConfiguration (ProgramConfiguration (..),
ProgramOptions (ProgramOptions, configPath))
import ATrade.Driver.Junction.QuoteStream (QuoteStream (addSubscription, removeSubscription),
QuoteSubscription (QuoteSubscription),
SubscriptionId (SubscriptionId))
import ATrade.Driver.Junction.QuoteThread (DownloaderEnv (DownloaderEnv),
QuoteThreadHandle,
withQThread)
import qualified ATrade.Driver.Junction.QuoteThread as QT
import ATrade.Driver.Junction.RobotDriverThread (RobotDriverHandle, RobotEnv (RobotEnv),
RobotM (..),
createRobotDriverThread,
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),
import ATrade.Driver.Junction.RemoteControl (handleRemoteControl)
import ATrade.Driver.Junction.RobotDriverThread (RobotDriverHandle, postNotificationEvent)
import ATrade.Driver.Junction.Types (StrategyDescriptorE)
import ATrade.Logging (Message (..), Severity (Debug, Info, Trace, Warning),
fmtMessage,
logWarning,
logWith)
import ATrade.Quotes.QHP (mkQHPHandle)
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)
import ATrade.Types (ClientSecurityParams (ClientSecurityParams),
OrderId,
Trade (tradeOrderId))
import Colog (HasLog (getLogAction, setLogAction),
LogAction,
logTextStdout,
(>$<))
import ATrade.Types (OrderId, Trade (tradeOrderId))
import Colog (LogAction (LogAction),
cfilter,
hoistLogAction,
logTextStderr,
(<&), (>$<))
import Colog.Actions (logTextHandle)
import Control.Concurrent (threadDelay)
import Control.Exception.Safe (MonadThrow,
bracket)
import Control.Concurrent.QSem (newQSem)
import Control.Monad (forM_, forever)
import Control.Monad.Extra (whenM)
import Control.Monad.IO.Class (MonadIO (liftIO))
import Control.Monad.Reader (MonadReader, 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 Control.Monad.Reader (ReaderT (runReaderT))
import Data.IORef (IORef,
atomicModifyIORef',
newIORef,
readIORef)
import Data.List.NonEmpty (NonEmpty ((:|)))
import qualified Data.Map.Strict as M
import Data.Set (notMember)
import qualified Data.Set as S
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 (ConnectInfo (..),
Connection,
PortID (UnixSocket),
import Database.Redis (ConnectInfo (..), PortID (UnixSocket),
checkedConnect,
defaultConnectInfo,
get, mset,
runRedis)
defaultConnectInfo)
import Dhall (auto, input)
import Options.Applicative (Parser,
execParser,
@ -109,79 +71,31 @@ import System.IO (BufferMode (LineBu @@ -109,79 +71,31 @@ import System.IO (BufferMode (LineBu
Handle,
IOMode (AppendMode),
hSetBuffering,
openFile,
withFile)
import System.ZMQ4 (withContext)
import System.ZMQ4.ZAP (loadCertificateFromFile)
import System.ZMQ4 (Router (Router),
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 }
deriving (Functor, Applicative, Monad, MonadReader JunctionEnv, MonadIO, MonadThrow)
locked :: (MonadIO m, MonadUnliftIO m) => QSem -> LogAction m a -> LogAction m a
locked sem action = LogAction (\m -> withQSem sem (action <& m))
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 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)
logger :: (MonadIO m) => M.Map T.Text Severity -> Handle -> LogAction m Message
logger loglevels h = cfilter checkLoglevel (fmtMessage >$< (logTextStderr <> logTextHandle h))
where
checkLoglevel msg =
case M.lookup (msgComponent msg) loglevels of
Just level -> msgSeverity msg >= level
Nothing -> True
junctionMain :: M.Map T.Text StrategyDescriptorE -> IO ()
junctionMain descriptors = do
opts <- parseOptions
let initialLogger = fmtMessage >$< logTextStdout
let initialLogger = fmtMessage >$< logTextStderr
logWith initialLogger Info "Junction" $ "Reading config from: " <> (T.pack . show) (configPath opts)
@ -189,83 +103,57 @@ junctionMain descriptors = do @@ -189,83 +103,57 @@ junctionMain descriptors = 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
tickerInfoMap <- newIORef M.empty
log Info "Junction" $ "Connecting to redis: " <> redisSocket cfg
redis <- checkedConnect (defaultConnectInfo { connectPort = UnixSocket (T.unpack $ redisSocket cfg) })
log Info "Junction" "redis: connected"
withContext $ \ctx -> do
log Debug "Junction" "0mq context created"
let downloaderLogAction = logger h
let downloaderEnv = DownloaderEnv (mkQHPHandle ctx (qhpEndpoint cfg)) ctx (qtisEndpoint cfg) downloaderLogAction
let downloaderEnv = DownloaderEnv (mkQHPHandle ctx (qhpEndpoint cfg)) ctx (qtisEndpoint cfg) (hoistLogAction liftIO globalLogger)
robotsMap <- newIORef M.empty
ordersMap <- newIORef M.empty
handledNotifications <- newIORef S.empty
withBroker cfg ctx robotsMap ordersMap handledNotifications (logger h) $ \bro ->
withQThread downloaderEnv barsMap cfg ctx (logger h) $ \qt -> do
broService <- mkBrokerService bro ordersMap
let junctionLogAction = logger h
let env =
JunctionEnv
{
peRedisSocket = redis,
peConfigPath = robotsConfigsPath cfg,
peQuoteThread = qt,
peBroker = bro,
peRobots = robotsMap,
peLogAction = junctionLogAction
}
withJunction env $ do
startRobots h cfg barsMap broService
forever $ do
notifications <- liftIO $ getNotifications broService
forM_ notifications (liftIO . handleBrokerNotification robotsMap ordersMap handledNotifications (logger h))
saveRobots
liftIO $ threadDelay 1000000
withBroker cfg robotsMap ordersMap handledNotifications globalLogger $ \bro ->
withQThread downloaderEnv barsMap tickerInfoMap cfg ctx globalLogger $ \qt ->
withSocket ctx Router $ \rcSocket -> do
liftIO $ bind rcSocket (T.unpack . remoteControlEndpoint $ cfg)
broService <- mkBrokerService bro ordersMap
let junctionLogAction = hoistLogAction liftIO globalLogger
let env =
JunctionEnv
{
peRedisSocket = redis,
peConfigPath = robotsConfigsPath cfg,
peQuoteThread = qt,
peBroker = bro,
peRobots = robotsMap,
peRemoteControlSocket = rcSocket,
peLogAction = junctionLogAction,
peIoLogAction = globalLogger,
peProgramConfiguration = cfg,
peBarsMap = barsMap,
peTickerInfoMap = tickerInfoMap,
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
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")
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)
startRobots :: ProgramConfiguration -> JunctionM ()
startRobots cfg = forM_ (instances cfg) startRobot
withJunction :: JunctionEnv -> JunctionM () -> IO ()
withJunction env = (`runReaderT` env) . unJunctionM
@ -276,8 +164,8 @@ junctionMain descriptors = do @@ -276,8 +164,8 @@ junctionMain descriptors = do
LogAction IO Message ->
Notification ->
IO ()
handleBrokerNotification robotsRef ordersMapRef handled logger notification= do
logWith logger Trace "Junction" $ "Incoming notification: " <> (T.pack . show . unNotificationSqnum . getNotificationSqnum) notification
handleBrokerNotification robotsRef ordersMapRef handled logger' notification= do
logWith logger' Trace "Junction" $ "Incoming notification: " <> (T.pack . show . unNotificationSqnum . getNotificationSqnum) notification
whenM (notMember (getNotificationSqnum notification) <$> readIORef handled) $ do
robotsMap <- readIORef robotsRef
ordersMap <- readIORef ordersMapRef
@ -285,8 +173,8 @@ junctionMain descriptors = do @@ -285,8 +173,8 @@ junctionMain descriptors = do
case getNotificationTarget robotsMap ordersMap notification of
Just robot -> postNotificationEvent robot notification
Nothing -> do
logWith logger Warning "Junction" $ "Unknown order: " <> (T.pack . show) (notificationOrderId notification)
logWith logger Debug "Junction" $ "Ordermap: " <> (T.pack . show) (M.toList ordersMap)
logWith logger' Warning "Junction" $ "Unknown order: " <> (T.pack . show) (notificationOrderId notification)
logWith logger' Debug "Junction" $ "Ordermap: " <> (T.pack . show) (M.toList ordersMap)
atomicModifyIORef' handled (\s -> (S.insert (getNotificationSqnum notification) s, ()))
@ -298,30 +186,15 @@ junctionMain descriptors = do @@ -298,30 +186,15 @@ junctionMain descriptors = do
notificationOrderId (OrderNotification _ oid _) = oid
notificationOrderId (TradeNotification _ trade) = tradeOrderId trade
withBroker cfg ctx robotsMap ordersMap handled logger f = do
securityParameters <- loadBrokerSecurityParameters cfg
withBroker cfg robotsMap ordersMap handled logger' f = do
bracket
(startBrokerClient
"broker"
ctx
(brokerIdentity cfg)
(brokerEndpoint cfg)
(brokerNotificationEndpoint cfg)
[handleBrokerNotification robotsMap ordersMap handled logger]
securityParameters
logger)
[handleBrokerNotification robotsMap ordersMap handled logger']
logger')
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
options = info (optionsParser <**> helper)
(fullDesc <>

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

@ -12,7 +12,7 @@ module ATrade.Driver.Junction.BrokerService @@ -12,7 +12,7 @@ module ATrade.Driver.Junction.BrokerService
import qualified ATrade.Broker.Client as Bro
import ATrade.Broker.Protocol (Notification (..))
import ATrade.Logging (Message, logDebug)
import ATrade.Logging (Message, logDebug, logWarning)
import ATrade.Types (Order (..), OrderId)
import Colog (WithLog)
import Control.Monad.IO.Class (MonadIO (liftIO))
@ -38,19 +38,27 @@ submitOrder service identity order = do @@ -38,19 +38,27 @@ submitOrder service identity order = do
oid <- nextOrderId service
logDebug "BrokerService" $ "New order, id: " <> (T.pack . show) oid
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
where
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
_ <- 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 ()
getNotifications :: BrokerService -> IO [Notification]
getNotifications :: (MonadIO m, WithLog env Message m) => BrokerService -> m [Notification]
getNotifications service = do
v <- Bro.getNotifications (broker service)
v <- liftIO $ Bro.getNotifications (broker service)
case v of
Left _ -> return []
Left err -> do
logWarning "BrokerServer" $ "Get notifications order error: " <> err
return []
Right n -> return n

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

@ -0,0 +1,258 @@ @@ -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 @@ @@ -1,4 +1,7 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module ATrade.Driver.Junction.ProgramConfiguration
(
@ -6,8 +9,12 @@ module ATrade.Driver.Junction.ProgramConfiguration @@ -6,8 +9,12 @@ module ATrade.Driver.Junction.ProgramConfiguration
ProgramConfiguration(..)
) where
import ATrade.Driver.Junction.Types (StrategyInstanceDescriptor)
import ATrade.Logging (Severity (..))
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)
newtype ProgramOptions =
@ -23,15 +30,43 @@ data ProgramConfiguration = @@ -23,15 +30,43 @@ data ProgramConfiguration =
brokerNotificationEndpoint :: T.Text,
brokerServerCert :: Maybe FilePath,
brokerClientCert :: Maybe FilePath,
brokerIdentity :: T.Text,
quotesourceEndpoint :: T.Text,
quotesourceServerCert :: Maybe FilePath,
quotesourceClientCert :: Maybe FilePath,
qhpEndpoint :: T.Text,
qtisEndpoint :: T.Text,
remoteControlEndpoint :: T.Text,
redisSocket :: T.Text,
robotsConfigsPath :: FilePath,
logBasePath :: FilePath,
logLevels :: [(T.Text, Severity)],
instances :: [StrategyInstanceDescriptor]
} 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

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

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

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

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

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

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

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

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
module ATrade.Driver.Junction.Types
@ -16,7 +17,8 @@ module ATrade.Driver.Junction.Types @@ -16,7 +17,8 @@ module ATrade.Driver.Junction.Types
import ATrade.RoboCom.Monad (EventCallback)
import ATrade.Types (BarTimeframe (..), TickerId)
import Data.Aeson (FromJSON (..), ToJSON (..))
import Data.Aeson (FromJSON (..), ToJSON (..), withObject,
(.:))
import Data.Default (Default)
import Data.IORef (IORef)
import qualified Data.Text as T
@ -66,6 +68,17 @@ data StrategyInstanceDescriptor = @@ -66,6 +68,17 @@ data 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 =
StrategyInstance
{

114
src/ATrade/Driver/Real.hs

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

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

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

3
src/ATrade/Quotes/QTIS.hs

@ -37,9 +37,8 @@ instance ToJSON TickerInfo where @@ -37,9 +37,8 @@ instance ToJSON TickerInfo where
"lot_size" .= tiLotSize 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
logInfo "QTIS" $ "Requesting ticker: " <> tickerId <> " from " <> endpoint
liftIO $ withSocket ctx Req $ \sock -> do
connect sock $ T.unpack endpoint
send sock [] $ BL.toStrict tickerRequest

14
src/ATrade/RoboCom/Monad.hs

@ -20,7 +20,9 @@ module ATrade.RoboCom.Monad ( @@ -20,7 +20,9 @@ module ATrade.RoboCom.Monad (
also,
t,
st,
getFirstTickerId) where
getFirstTickerId,
getTickerAnyTimeframe
) where
import ATrade.RoboCom.Types
import ATrade.Types
@ -30,6 +32,7 @@ import Data.Aeson.Types @@ -30,6 +32,7 @@ import Data.Aeson.Types
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import Data.Time.Clock
import qualified Data.List as L
import Language.Haskell.Printf
import Language.Haskell.TH.Quote (QuasiQuoter)
import ATrade.Logging (Severity)
@ -51,11 +54,20 @@ class (Monad m) => MonadRobot m c s | m -> c, m -> s where @@ -51,11 +54,20 @@ class (Monad m) => MonadRobot m c s | m -> c, m -> s where
setState (f oldState)
getEnvironment :: m StrategyEnvironment
getTicker :: TickerId -> BarTimeframe -> m (Maybe BarSeries)
getTickerInfo :: TickerId -> m (Maybe InstrumentParameters)
getAvailableTickers :: m (NonEmpty BarSeriesId)
getFirstTickerId :: forall c s m. (Monad m, MonadRobot m c s) => m BarSeriesId
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 = t

258
src/ATrade/RoboCom/Positions.hs

@ -1,3 +1,4 @@ @@ -1,3 +1,4 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiWayIf #-}
@ -8,18 +9,20 @@ @@ -8,18 +9,20 @@
{-|
- Module : ATrade.RoboCom.Combinators
- Description : Reusable behavioural components of strategies
- Copyright : (c) Denis Tereshkin 2016
- License : Proprietary
- Copyright : (c) Denis Tereshkin 2021
- License : BSD 3-clause
- Maintainer : denis@kasan.ws
- Stability : experimental
- 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
(
StateHasPositions(..),
ParamsSize(..),
PositionState(..),
Position(..),
posIsOpen,
@ -45,12 +48,10 @@ module ATrade.RoboCom.Positions @@ -45,12 +48,10 @@ module ATrade.RoboCom.Positions
onTradeEvent,
onActionCompletedEvent,
enterAtMarket,
enterAtMarketForTicker,
enterAtMarketWithParams,
enterAtLimit,
enterAtLimitWithVolume,
enterAtLimitWithParams,
enterAtLimitForTicker,
enterAtLimitForTickerWithVolume,
enterAtLimitForTickerWithParams,
enterLongAtMarket,
enterShortAtMarket,
@ -65,8 +66,14 @@ module ATrade.RoboCom.Positions @@ -65,8 +66,14 @@ module ATrade.RoboCom.Positions
setLimitStopLoss,
setTakeProfit,
setStopLossAndTakeProfit,
handlePositions
) where
handlePositions,
calculateSizeIVS,
calculateSizeIVSWith,
calculateSizeFixed,
calculateSizeFixedCash,
calculateSizeFixedCashWith,
calculateSizeIVSWithMinimum) where
import GHC.Generics
@ -74,17 +81,18 @@ import ATrade.RoboCom.Monad @@ -74,17 +81,18 @@ import ATrade.RoboCom.Monad
import ATrade.RoboCom.Types
import ATrade.Types
import Control.Lens
import Control.Lens hiding (op)
import Control.Monad
import ATrade.Logging (Severity (Trace, Warning))
import ATrade.RoboCom.Monad (MonadRobot (getAvailableTickers))
import ATrade.Logging (Severity (Trace, Warning))
import qualified ATrade.RoboCom.Indicators as I
import Data.Aeson
import qualified Data.List as L
import qualified Data.List.NonEmpty as NE
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.List as L
import qualified Data.List.NonEmpty as NE
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import Data.Time.Clock
import GHC.Records (HasField (..))
data PositionState = PositionWaitingOpenSubmission Order
| PositionWaitingOpen
@ -146,8 +154,44 @@ modifyPositions f = do @@ -146,8 +154,44 @@ modifyPositions f = do
pos <- getPositions <$> getState
modifyState (\s -> setPositions s (f pos))
class ParamsHasMainTicker a where
mainTicker :: a -> (BarTimeframe, TickerId)
class ParamsSize a where
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.
findAndModify :: (a -> Bool) -> (a -> a) -> [a] -> [a]
@ -179,20 +223,23 @@ orderDeadline maybeDeadline lastTs = @@ -179,20 +223,23 @@ orderDeadline maybeDeadline lastTs =
dispatchPosition :: (StateHasPositions s, MonadRobot m c s) => Event -> Position -> m Position
dispatchPosition event pos = case posState pos of
PositionWaitingOpenSubmission pendingOrder -> handlePositionWaitingOpenSubmission pendingOrder
PositionWaitingOpen -> handlePositionWaitingOpen
PositionOpen -> handlePositionOpen
PositionWaitingPendingCancellation -> handlePositionWaitingPendingCancellation
PositionWaitingCloseSubmission pendingOrder -> handlePositionWaitingCloseSubmission pendingOrder
PositionWaitingClose -> handlePositionWaitingClose
PositionClosed -> handlePositionClosed pos
PositionCancelled -> handlePositionCancelled pos
dispatchPosition event pos =
case posState pos of
PositionWaitingOpenSubmission pendingOrder -> handlePositionWaitingOpenSubmission pendingOrder
PositionWaitingOpen -> handlePositionWaitingOpen
PositionOpen -> handlePositionOpen
PositionWaitingPendingCancellation -> handlePositionWaitingPendingCancellation
PositionWaitingCloseSubmission pendingOrder -> handlePositionWaitingCloseSubmission pendingOrder
PositionWaitingClose -> handlePositionWaitingClose
PositionClosed -> handlePositionClosed pos
PositionCancelled -> handlePositionCancelled pos
where
handlePositionWaitingOpenSubmission pendingOrder = do
lastTs <- view seLastTimestamp <$> getEnvironment
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
OrderUpdate oid Submitted -> do
return $ if orderId pendingOrder == oid
@ -374,6 +421,25 @@ newPosition order account tickerId operation quantity submissionDeadline = do @@ -374,6 +421,25 @@ newPosition order account tickerId operation quantity submissionDeadline = do
modifyPositions (\p -> position : p)
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 _ = modifyPositions (L.filter (not . posIsDead))
@ -461,16 +527,31 @@ onActionCompletedEvent event f = case event of @@ -461,16 +527,31 @@ onActionCompletedEvent event f = case event of
ActionCompleted tag v -> f tag v
_ -> 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
env <- getEnvironment
enterAtMarketWithParams (env ^. seAccount) (env ^. seVolume) (SignalId (env ^. seInstanceId) operationSignalName "") operation
bsId <- getFirstTickerId
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 account quantity signalId operation = do
BarSeriesId tickerId _ <- getFirstTickerId
oid <- submitOrder $ order tickerId
newPosition ((order tickerId) { orderId = oid }) account tickerId operation quantity 20
enterAtMarketWithParams :: (StateHasPositions s, MonadRobot m c s) => T.Text -> TickerId -> Int -> SignalId -> Operation -> m Position
enterAtMarketWithParams account tid quantity signalId operation = do
oid <- submitOrder $ order tid
newPosition ((order tid) { orderId = oid }) account tid operation quantity 20
where
order tickerId = mkOrder {
orderAccountId = account,
@ -481,36 +562,36 @@ enterAtMarketWithParams account quantity signalId operation = do @@ -481,36 +562,36 @@ enterAtMarketWithParams account quantity signalId operation = do
orderSignalId = signalId
}
enterAtLimit :: (StateHasPositions s, MonadRobot m c s) => NominalDiffTime -> T.Text -> Price -> Operation -> m Position
enterAtLimit timeToCancel operationSignalName price operation = do
enterAtLimit :: (StateHasPositions s, ParamsSize c, MonadRobot m c s) => T.Text -> Price -> Operation -> m Position
enterAtLimit operationSignalName price operation = do
bsId <- getFirstTickerId
env <- getEnvironment
enterAtLimitWithParams timeToCancel (env ^. seAccount) (env ^. seVolume) (SignalId (env ^. seInstanceId) 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
enterAtLimitForTicker bsId operationSignalName price operation
enterAtLimitForTickerWithVolume :: (StateHasPositions s, MonadRobot m c s) => TickerId -> NominalDiffTime -> T.Text -> Price -> Int -> Operation -> m Position
enterAtLimitForTickerWithVolume tickerId timeToCancel operationSignalName price vol operation = do
enterAtLimitForTicker :: (StateHasPositions s, ParamsSize c, MonadRobot m c s) => BarSeriesId -> T.Text -> Price -> Operation -> m Position
enterAtLimitForTicker (BarSeriesId tid tf) operationSignalName price operation = do
acc <- view seAccount <$> getEnvironment
inst <- view seInstanceId <$> getEnvironment
enterAtLimitForTickerWithParams tickerId timeToCancel acc vol (SignalId inst operationSignalName "") price operation
enterAtLimitForTicker :: (StateHasPositions s, MonadRobot m c s) => TickerId -> NominalDiffTime -> T.Text -> Price -> Operation -> m Position
enterAtLimitForTicker tickerId timeToCancel operationSignalName price operation = do
acc <- view seAccount <$> getEnvironment
inst <- view seInstanceId <$> getEnvironment
vol <- view seVolume <$> getEnvironment
enterAtLimitForTickerWithParams tickerId timeToCancel acc vol (SignalId inst operationSignalName "") price operation
enterAtLimitForTickerWithParams :: (StateHasPositions s, MonadRobot m c s) => TickerId -> NominalDiffTime -> T.Text -> Int -> SignalId -> Price -> Operation -> m Position
maybeSeries <- getTicker tid tf
case maybeSeries of
Just series -> do
cfg <- getConfig
let quantity = getPositionSize cfg series operation
let roundedPrice = roundTo (ipTickSize . bsParams $ series) price
enterAtLimitForTickerWithParams tid (fromIntegral $ unBarTimeframe tf) acc quantity (SignalId inst operationSignalName "") roundedPrice operation
Nothing -> rejectedPosition
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
lastTs <- view seLastTimestamp <$> getEnvironment
oid <- submitOrder order
@ -527,23 +608,23 @@ enterAtLimitForTickerWithParams tickerId timeToCancel account quantity signalId @@ -527,23 +608,23 @@ enterAtLimitForTickerWithParams tickerId timeToCancel account quantity 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
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
enterLongAtLimit :: (StateHasPositions s, MonadRobot m c s) => NominalDiffTime -> Price -> T.Text -> m Position
enterLongAtLimit timeToCancel price operationSignalName = enterAtLimit timeToCancel operationSignalName price Buy
enterLongAtLimit :: (StateHasPositions s, ParamsSize c, MonadRobot m c s) => Price -> T.Text -> m Position
enterLongAtLimit price operationSignalName = enterAtLimit operationSignalName price Buy
enterLongAtLimitForTicker :: (StateHasPositions s, MonadRobot m c s) => TickerId -> NominalDiffTime -> Price -> T.Text -> m Position
enterLongAtLimitForTicker tickerId timeToCancel price operationSignalName = enterAtLimitForTicker tickerId timeToCancel operationSignalName price Buy
enterLongAtLimitForTicker :: (StateHasPositions s, ParamsSize c, MonadRobot m c s) => BarSeriesId -> Price -> T.Text -> m Position
enterLongAtLimitForTicker tickerId price operationSignalName = enterAtLimitForTicker tickerId operationSignalName price Buy
enterShortAtLimit :: (StateHasPositions s, MonadRobot m c s) => NominalDiffTime -> Price -> T.Text -> m Position
enterShortAtLimit timeToCancel price operationSignalName = enterAtLimit timeToCancel operationSignalName price Sell
enterShortAtLimit :: (StateHasPositions s, ParamsSize c, MonadRobot m c s) => Price -> T.Text -> m Position
enterShortAtLimit price operationSignalName = enterAtLimit operationSignalName price Sell
enterShortAtLimitForTicker :: (StateHasPositions s, MonadRobot m c s) => TickerId -> NominalDiffTime -> Price -> T.Text -> m Position
enterShortAtLimitForTicker tickerId timeToCancel price operationSignalName = enterAtLimitForTicker tickerId timeToCancel operationSignalName price Sell
enterShortAtLimitForTicker :: (StateHasPositions s, ParamsSize c, MonadRobot m c s) => BarSeriesId -> Price -> T.Text -> m Position
enterShortAtLimitForTicker tickerId price operationSignalName = enterAtLimitForTicker tickerId operationSignalName price Sell
exitAtMarket :: (StateHasPositions s, MonadRobot m c s) => Position -> T.Text -> m Position
exitAtMarket position operationSignalName = do
@ -580,23 +661,32 @@ exitAtLimit :: (StateHasPositions s, MonadRobot m c s) => NominalDiffTime -> Pri @@ -580,23 +661,32 @@ exitAtLimit :: (StateHasPositions s, MonadRobot m c s) => NominalDiffTime -> Pri
exitAtLimit timeToCancel price position operationSignalName = do
lastTs <- view seLastTimestamp <$> getEnvironment
inst <- view seInstanceId <$> getEnvironment
case posCurrentOrder position of
Just order -> cancelOrder (orderId order)
Nothing -> doNothing
oid <- submitOrder (closeOrder inst)
appendToLog Trace $ [t|exitAtLimit: %?, deadline: %?|] (posTicker position) (timeToCancel `addUTCTime` lastTs)
modifyPosition (\pos ->
pos { posCurrentOrder = Nothing,
posState = PositionWaitingCloseSubmission (closeOrder inst) { orderId = oid },
posNextState = Just PositionClosed,
posSubmissionDeadline = Just $ 10 `addUTCTime` lastTs,
posExecutionDeadline = Just $ timeToCancel `addUTCTime` lastTs }) position
cfg <- getConfig
(BarSeriesId tid tf) <- getFirstTickerId
maybeSeries <- getTicker tid tf
case maybeSeries of
Just series -> do
let roundedPrice = roundTo (ipTickSize . bsParams $ series) price
case posCurrentOrder position of
Just order -> cancelOrder (orderId order)
Nothing -> doNothing
oid <- submitOrder (closeOrder inst roundedPrice)
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
closeOrder inst = mkOrder {
closeOrder inst roundedPrice = mkOrder {
orderAccountId = posAccount position,
orderSecurity = posTicker position,
orderQuantity = (abs . posBalance) position,
orderPrice = Limit price,
orderPrice = Limit roundedPrice,
orderOperation = if posBalance position > 0 then Sell else Buy,
orderSignalId = SignalId inst operationSignalName ""
}

18
src/ATrade/RoboCom/Types.hs

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

7
stack.yaml

@ -18,7 +18,7 @@ @@ -18,7 +18,7 @@
#
# resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.yaml
resolver: lts-18.18
resolver: lts-20.26
# User packages to be built.
# Various formats can be used as shown in the example below.
@ -48,8 +48,9 @@ extra-deps: @@ -48,8 +48,9 @@ extra-deps:
- binary-ieee754-0.1.0.0
- th-printf-0.7
- normaldistribution-1.1.0.3
- co-log-0.4.0.1@sha256:3d4c17f37693c80d1aa2c41669bc3438fac3e89dc5f479e57d79bc3ddc4dfcc5,5087
- ansi-terminal-0.10.3@sha256:e2fbcef5f980dc234c7ad8e2fa433b0e8109132c9e643bc40ea5608cd5697797,3226
- co-log-0.5.0.0
- 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
# flags: {}

107
test/Test/BarAggregator.hs

@ -10,18 +10,21 @@ import ATrade.BarAggregator @@ -10,18 +10,21 @@ import ATrade.BarAggregator
import ATrade.RoboCom.Types
import ATrade.Types
import Data.List
import qualified Data.Map.Strict as M
import qualified Data.Text as T
import Data.List.NonEmpty (NonEmpty (..))
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.Clock
import Safe
import Hedgehog as HH
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import Test.Tasty
import Test.Tasty.Hedgehog
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck as QC
import Test.Tasty.SmallCheck as SC
import ArbitraryInstances
unitTests = testGroup "BarAggregator" [
@ -33,9 +36,36 @@ unitTests = testGroup "BarAggregator" [ @@ -33,9 +36,36 @@ unitTests = testGroup "BarAggregator" [
properties = testGroup "BarAggregator" [
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 = testCase "Tick with unknown ticker id" $ do
@ -94,11 +124,13 @@ testTwoTicksInDifferentBars = testCase "Two ticks - different bar" $ do @@ -94,11 +124,13 @@ testTwoTicksInDifferentBars = testCase "Two ticks - different bar" $ do
let (mbar, newagg) = handleTick (tick testTimestamp1 12.00) agg
mbar @?= Nothing
let (mbar', newagg') = handleTick (tick testTimestamp2 14.00) newagg
mbar' @?= Just (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" 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" barEndTime 12.00 12.00 12.00 12.00 1]
where
testTimestamp1 = (UTCTime (fromGregorian 1970 1 1) 58)
testTimestamp2 = (UTCTime (fromGregorian 1970 1 1) 61)
testTimestamp1 = UTCTime (fromGregorian 1970 1 1) 58
barEndTime = UTCTime (fromGregorian 1970 1 1) 60
testTimestamp2 = UTCTime (fromGregorian 1970 1 1) 61
tick ts val = Tick {
security = "TEST_TICKER",
datatype = LastTradePrice,
@ -106,27 +138,42 @@ testTwoTicksInDifferentBars = testCase "Two ticks - different bar" $ do @@ -106,27 +138,42 @@ testTwoTicksInDifferentBars = testCase "Two ticks - different bar" $ do
value = fromDouble val,
volume = 1 }
prop_allTicksInOneBar :: TestTree
prop_allTicksInOneBar = QC.testProperty "All ticks in one bar" $ QC.forAll (QC.choose (1, 86400)) $ \timeframe ->
QC.forAll (QC.listOf1 (genTick "TEST_TICKER" baseTime timeframe)) $ \ticks ->
let ticks' = sortOn timestamp ticks in
let (newbars, agg) = handleTicks ticks' (mkAggregator "TEST_TICKER" timeframe) in
null newbars &&
((barHigh <$> currentBar "TEST_TICKER" agg) == Just (maximum $ value <$> ticks)) &&
((barLow <$> currentBar "TEST_TICKER" agg) == Just (minimum $ value <$> ticks)) &&
((barOpen <$> currentBar "TEST_TICKER" agg) == (value <$> headMay ticks')) &&
((barClose <$> currentBar "TEST_TICKER" agg) == (value <$> lastMay ticks')) &&
((barVolume <$> currentBar "TEST_TICKER" agg) == Just (sum $ volume <$> ticks))
prop_allTicksInOneBar = testProperty "All ticks in one bar" $ property $ do
tf <- forAll $ Gen.integral (Range.constant 1 86400)
ticks <- forAll $ Gen.list (Range.linear 1 100) (genTick "TEST_TICKER" baseTime tf)
let ticks' = sortOn timestamp ticks
let (newbars, agg) = handleTicks ticks' (mkAggregator "TEST_TICKER" tf)
let (Just lastBar) = currentBar "TEST_TICKER" agg
HH.assert $ null newbars
assertBarCorrespondence lastBar $ NE.fromList ticks
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))
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 @@ @@ -1,5 +1,6 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeSynonymInstances #-}
@ -16,6 +17,7 @@ import Test.Tasty.SmallCheck as SC @@ -16,6 +17,7 @@ import Test.Tasty.SmallCheck as SC
import ATrade.Driver.Junction.QuoteThread (addSubscription,
startQuoteThread,
stopQuoteThread)
import ATrade.Logging (Message)
import ATrade.Quotes.HistoryProvider (HistoryProvider (..))
import ATrade.Quotes.TickerInfoProvider (TickerInfoProvider (..))
import ATrade.QuoteSource.Client (QuoteData (QDBar))
@ -26,23 +28,21 @@ import ATrade.RoboCom.Types (BarSeries (bsBars), @@ -26,23 +28,21 @@ import ATrade.RoboCom.Types (BarSeries (bsBars),
BarSeriesId (BarSeriesId),
InstrumentParameters (InstrumentParameters))
import ATrade.Types
import Colog.Core (LogAction (..))
import Colog.Core.Class (HasLog (..))
import Control.Concurrent (forkIO, threadDelay)
import Control.Concurrent.BoundedChan (newBoundedChan, readChan,
writeChan)
import Control.Exception (bracket)
import Control.Monad (forever)
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.Text as T
import Data.Time (UTCTime (UTCTime),
fromGregorian)
import System.IO (BufferMode (LineBuffering),
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 Test.Mock.HistoryProvider (MockHistoryProvider,
mkMockHistoryProvider,
@ -70,13 +70,16 @@ instance TickerInfoProvider TestM where @@ -70,13 +70,16 @@ instance TickerInfoProvider TestM where
tip <- asks tickerInfoProvider
liftIO $ mockGetInstrumentParameters tip tickers
instance HasLog TestEnv Message TestM where
getLogAction env = LogAction $ \msg -> return ()
qsEndpoint = "inproc://qs"
mockHistoryProvider = mkMockHistoryProvider $ M.fromList [(BarSeriesId "FOO" (BarTimeframe 3600), bars)]
where
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" [
testSubscription
@ -85,12 +88,14 @@ unitTests = testGroup "Driver.Junction.QuoteThread" [ @@ -85,12 +88,14 @@ unitTests = testGroup "Driver.Junction.QuoteThread" [
testSubscription :: TestTree
testSubscription = testCase "Subscription" $ withContext $ \ctx -> do
barsRef <- newIORef M.empty
tiRef <- newIORef M.empty
serverChan <- newBoundedChan 2000
let clientSecurityParams = defaultClientSecurityParams
bracket
(startQuoteSourceServer serverChan ctx qsEndpoint defaultServerSecurityParams)
stopQuoteSourceServer $ \_ ->
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
chan <- newBoundedChan 2000

2
test/Test/RoboCom/Indicators.hs

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

165
test/Test/RoboCom/Positions.hs

@ -0,0 +1,165 @@ @@ -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 @@ -7,11 +7,9 @@ module Test.RoboCom.Utils
import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck as QC
import Test.Tasty.SmallCheck as SC
import ATrade.Types
import qualified Data.Text as T
import qualified Data.Text as T
import Data.Time.Calendar
import Data.Time.Clock

Loading…
Cancel
Save