Browse Source

Adjust to libatrade-0.10

stable
Denis Tereshkin 4 years ago
parent
commit
7e72d1ae0a
  1. 36
      src/ATrade/Driver/Real/BrokerClientThread.hs

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

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

Loading…
Cancel
Save