Browse Source

junction: startRobot refactoring

master
Denis Tereshkin 4 years ago
parent
commit
1cf0c6d4ea
  1. 15
      src/ATrade/Driver/Junction.hs
  2. 34
      src/ATrade/Driver/Junction/JunctionMonad.hs

15
src/ATrade/Driver/Junction.hs

@ -146,19 +146,24 @@ junctionMain descriptors = do
peBroker = bro, peBroker = bro,
peRobots = robotsMap, peRobots = robotsMap,
peRemoteControlSocket = rcSocket, peRemoteControlSocket = rcSocket,
peLogAction = junctionLogAction peLogAction = junctionLogAction,
peProgramConfiguration = cfg,
peBarsMap = barsMap,
peTickerInfoMap = tickerInfoMap,
peBrokerService = broService,
peDescriptors = descriptors
} }
withJunction env $ do withJunction env $ do
startRobots (hoistLogAction liftIO globalLogger) cfg barsMap tickerInfoMap broService startRobots (hoistLogAction liftIO globalLogger) cfg
forever $ do forever $ do
notifications <- liftIO $ getNotifications broService notifications <- liftIO $ getNotifications broService
forM_ notifications (liftIO . handleBrokerNotification robotsMap ordersMap handledNotifications globalLogger) forM_ notifications (liftIO . handleBrokerNotification robotsMap ordersMap handledNotifications globalLogger)
saveRobots saveRobots
handleRemoteControl 1000000 handleRemoteControl 1000000
where where
startRobots :: LogAction IO Message -> ProgramConfiguration -> IORef Bars -> IORef TickerInfoMap -> BrokerService -> JunctionM () startRobots :: LogAction IO Message -> ProgramConfiguration -> JunctionM ()
startRobots gLogger cfg barsMap tickerInfoMap broService = forM_ (instances cfg) $ \inst -> do startRobots gLogger cfg = forM_ (instances cfg) $ \inst -> do
startRobot gLogger cfg barsMap tickerInfoMap broService descriptors inst startRobot gLogger inst
withJunction :: JunctionEnv -> JunctionM () -> IO () withJunction :: JunctionEnv -> JunctionM () -> IO ()
withJunction env = (`runReaderT` env) . unJunctionM withJunction env = (`runReaderT` env) . unJunctionM

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

@ -76,7 +76,8 @@ import Data.Time.Clock.POSIX (getPOSIXTime)
import Database.Redis (Connection, get, import Database.Redis (Connection, get,
mset, runRedis) mset, runRedis)
import Dhall (auto, input) import Dhall (auto, input)
import Prelude hiding (readFile) import Prelude hiding (log,
readFile)
import System.IO (BufferMode (LineBuffering), import System.IO (BufferMode (LineBuffering),
IOMode (AppendMode), IOMode (AppendMode),
hSetBuffering, hSetBuffering,
@ -86,13 +87,18 @@ import System.ZMQ4 (Rep, Socket)
data JunctionEnv = data JunctionEnv =
JunctionEnv JunctionEnv
{ {
peRedisSocket :: Connection, peRedisSocket :: Connection,
peConfigPath :: FilePath, peConfigPath :: FilePath,
peQuoteThread :: QuoteThreadHandle, peQuoteThread :: QuoteThreadHandle,
peBroker :: BrokerClientHandle, peBroker :: BrokerClientHandle,
peRobots :: IORef (M.Map T.Text RobotDriverHandle), peRobots :: IORef (M.Map T.Text RobotDriverHandle),
peRemoteControlSocket :: Socket Rep, peRemoteControlSocket :: Socket Rep,
peLogAction :: LogAction JunctionM Message peLogAction :: LogAction JunctionM 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 } newtype JunctionM a = JunctionM { unJunctionM :: ReaderT JunctionEnv IO a }
@ -145,11 +151,13 @@ instance QuoteStream JunctionM where
qt <- asks peQuoteThread qt <- asks peQuoteThread
QT.removeSubscription qt subId QT.removeSubscription qt subId
startRobot :: LogAction IO Message -> ProgramConfiguration -> IORef Bars -> IORef TickerInfoMap -> startRobot :: LogAction IO Message -> StrategyInstanceDescriptor -> JunctionM ()
BrokerService -> M.Map T.Text StrategyDescriptorE -> StrategyInstanceDescriptor -> JunctionM () startRobot ioLogger inst = do
startRobot ioLogger cfg barsMap tickerInfoMap broService descriptors inst = do descriptors <- asks peDescriptors
logger <- asks peLogAction cfg <- asks peProgramConfiguration
let log = logWith logger barsMap <- asks peBarsMap
tickerInfoMap <- asks peTickerInfoMap
broService <- asks peBrokerService
now <- liftIO getCurrentTime now <- liftIO getCurrentTime
let lLogger = hoistLogAction liftIO ioLogger let lLogger = hoistLogAction liftIO ioLogger
logWith lLogger Info "Junction" $ "Starting strategy: " <> strategyBaseName inst logWith lLogger Info "Junction" $ "Starting strategy: " <> strategyBaseName inst

Loading…
Cancel
Save