Browse Source

junction: startRobot refactoring

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

15
src/ATrade/Driver/Junction.hs

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

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

@ -76,7 +76,8 @@ import Data.Time.Clock.POSIX (getPOSIXTime) @@ -76,7 +76,8 @@ import Data.Time.Clock.POSIX (getPOSIXTime)
import Database.Redis (Connection, get,
mset, runRedis)
import Dhall (auto, input)
import Prelude hiding (readFile)
import Prelude hiding (log,
readFile)
import System.IO (BufferMode (LineBuffering),
IOMode (AppendMode),
hSetBuffering,
@ -92,7 +93,12 @@ data JunctionEnv = @@ -92,7 +93,12 @@ data JunctionEnv =
peBroker :: BrokerClientHandle,
peRobots :: IORef (M.Map T.Text RobotDriverHandle),
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 }
@ -145,11 +151,13 @@ instance QuoteStream JunctionM where @@ -145,11 +151,13 @@ instance QuoteStream JunctionM where
qt <- asks peQuoteThread
QT.removeSubscription qt subId
startRobot :: LogAction IO Message -> ProgramConfiguration -> IORef Bars -> IORef TickerInfoMap ->
BrokerService -> M.Map T.Text StrategyDescriptorE -> StrategyInstanceDescriptor -> JunctionM ()
startRobot ioLogger cfg barsMap tickerInfoMap broService descriptors inst = do
logger <- asks peLogAction
let log = logWith logger
startRobot :: LogAction IO Message -> StrategyInstanceDescriptor -> JunctionM ()
startRobot ioLogger inst = do
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

Loading…
Cancel
Save