|
|
|
@ -16,24 +16,21 @@ module TXMLConnector.Internal |
|
|
|
) where |
|
|
|
) where |
|
|
|
|
|
|
|
|
|
|
|
import ATrade.Logging (Message, Severity (..), log) |
|
|
|
import ATrade.Logging (Message, Severity (..), log) |
|
|
|
import Colog (HasLog, LogAction (LogAction)) |
|
|
|
import Colog (HasLog, LogAction) |
|
|
|
import Config (SubscriptionConfig (SubscriptionConfig), |
|
|
|
import Config (SubscriptionConfig (SubscriptionConfig), |
|
|
|
TransaqConnectorConfig (..), |
|
|
|
TransaqConnectorConfig (..), |
|
|
|
transaqHost, transaqLogLevel, |
|
|
|
transaqHost, transaqLogLevel, |
|
|
|
transaqLogPath, transaqLogin, |
|
|
|
transaqLogPath, transaqLogin, |
|
|
|
transaqPassword, transaqPort) |
|
|
|
transaqPassword, transaqPort) |
|
|
|
import Control.Concurrent (ThreadId, forkIO, threadDelay) |
|
|
|
import Control.Concurrent (forkIO, threadDelay) |
|
|
|
import Control.Concurrent.STM (TVar, atomically, modifyTVar', |
|
|
|
import Control.Concurrent.STM (TVar, atomically, modifyTVar', |
|
|
|
newEmptyTMVar, newEmptyTMVarIO, |
|
|
|
orElse, putTMVar, readTMVar, |
|
|
|
newTVarIO, orElse, putTMVar, |
|
|
|
readTVar, readTVarIO, |
|
|
|
readTMVar, readTVar, |
|
|
|
takeTMVar, tryPutTMVar, |
|
|
|
readTVarIO, takeTMVar, |
|
|
|
tryReadTMVar, writeTVar) |
|
|
|
tryPutTMVar, tryReadTMVar, |
|
|
|
|
|
|
|
writeTVar) |
|
|
|
|
|
|
|
import Control.Concurrent.STM.TBQueue (TBQueue, flushTBQueue, |
|
|
|
import Control.Concurrent.STM.TBQueue (TBQueue, flushTBQueue, |
|
|
|
newTBQueue, readTBQueue, |
|
|
|
readTBQueue, writeTBQueue) |
|
|
|
writeTBQueue) |
|
|
|
import Control.Monad (forM_, void, when) |
|
|
|
import Control.Monad (forM_, forever, void, when) |
|
|
|
|
|
|
|
import Control.Monad.Extra (whileM) |
|
|
|
import Control.Monad.Extra (whileM) |
|
|
|
import qualified Data.Bimap as BM |
|
|
|
import qualified Data.Bimap as BM |
|
|
|
import Data.Maybe (mapMaybe) |
|
|
|
import Data.Maybe (mapMaybe) |
|
|
|
@ -43,8 +40,7 @@ import Text.XML.Light.Input (parseXML) |
|
|
|
import Text.XML.Light.Types (Content (Elem), |
|
|
|
import Text.XML.Light.Types (Content (Elem), |
|
|
|
Element (elName), |
|
|
|
Element (elName), |
|
|
|
QName (qName)) |
|
|
|
QName (qName)) |
|
|
|
import TickTable (TickTable, insertTick, |
|
|
|
import TickTable (TickTable, insertTick) |
|
|
|
lookupTick, newTickTable) |
|
|
|
|
|
|
|
import Transaq (AllTradesTrade (..), |
|
|
|
import Transaq (AllTradesTrade (..), |
|
|
|
Candle (..), ClientData (..), |
|
|
|
Candle (..), ClientData (..), |
|
|
|
CommandCancelOrder (..), |
|
|
|
CommandCancelOrder (..), |
|
|
|
@ -68,7 +64,6 @@ import Transaq (AllTradesTrade (..), |
|
|
|
ResponseMarkets (ResponseMarkets), |
|
|
|
ResponseMarkets (ResponseMarkets), |
|
|
|
ResponseOrders (ResponseOrders), |
|
|
|
ResponseOrders (ResponseOrders), |
|
|
|
ResponseQuotations (ResponseQuotations), |
|
|
|
ResponseQuotations (ResponseQuotations), |
|
|
|
ResponseQuotes (ResponseQuotes), |
|
|
|
|
|
|
|
ResponseResult (..), |
|
|
|
ResponseResult (..), |
|
|
|
ResponseSecurities (ResponseSecurities), |
|
|
|
ResponseSecurities (ResponseSecurities), |
|
|
|
ResponseTrades (ResponseTrades), |
|
|
|
ResponseTrades (ResponseTrades), |
|
|
|
@ -80,12 +75,11 @@ import Transaq (AllTradesTrade (..), |
|
|
|
TransaqResponseC (fromXml), |
|
|
|
TransaqResponseC (fromXml), |
|
|
|
UnfilledAction (..), |
|
|
|
UnfilledAction (..), |
|
|
|
kCandleKindId, kPeriod, state) |
|
|
|
kCandleKindId, kPeriod, state) |
|
|
|
import TXML (LogLevel, MonadTXML, |
|
|
|
import TXML (MonadTXML, freeCallback, |
|
|
|
freeCallback, initialize, |
|
|
|
initialize, sendCommand, |
|
|
|
sendCommand, setCallback) |
|
|
|
setCallback) |
|
|
|
|
|
|
|
|
|
|
|
import ATrade.Broker.Backend (BrokerBackend (..), |
|
|
|
import ATrade.Broker.Backend (BrokerBackendNotification (..)) |
|
|
|
BrokerBackendNotification (..)) |
|
|
|
|
|
|
|
import ATrade.QuoteSource.Server (QuoteSourceServerData (..)) |
|
|
|
import ATrade.QuoteSource.Server (QuoteSourceServerData (..)) |
|
|
|
import ATrade.Types (Bar (..), |
|
|
|
import ATrade.Types (Bar (..), |
|
|
|
BarTimeframe (unBarTimeframe), |
|
|
|
BarTimeframe (unBarTimeframe), |
|
|
|
@ -96,13 +90,11 @@ import ATrade.Types (Bar (..), |
|
|
|
TickerId, Trade (..), |
|
|
|
TickerId, Trade (..), |
|
|
|
fromDouble, toDouble) |
|
|
|
fromDouble, toDouble) |
|
|
|
import qualified ATrade.Types as AT |
|
|
|
import qualified ATrade.Types as AT |
|
|
|
import Colog.Monad (WithLog) |
|
|
|
|
|
|
|
import Control.Applicative ((<|>)) |
|
|
|
import Control.Applicative ((<|>)) |
|
|
|
import Control.Concurrent.BoundedChan (BoundedChan, writeChan) |
|
|
|
import Control.Concurrent.BoundedChan (BoundedChan, writeChan) |
|
|
|
import Control.Concurrent.STM.TMVar (TMVar) |
|
|
|
import Control.Concurrent.STM.TMVar (TMVar) |
|
|
|
import Control.Error (headMay) |
|
|
|
import Control.Error (headMay) |
|
|
|
import Control.Monad.IO.Class (MonadIO (liftIO)) |
|
|
|
import Control.Monad.IO.Class (MonadIO (liftIO)) |
|
|
|
import Control.Monad.Reader (ReaderT (runReaderT)) |
|
|
|
|
|
|
|
import Control.Monad.Reader.Class (MonadReader, asks) |
|
|
|
import Control.Monad.Reader.Class (MonadReader, asks) |
|
|
|
import Data.Int (Int64) |
|
|
|
import Data.Int (Int64) |
|
|
|
import qualified Data.Map.Strict as M |
|
|
|
import qualified Data.Map.Strict as M |
|
|
|
@ -111,7 +103,6 @@ import Data.Time.Clock (UTCTime, diffUTCTime, |
|
|
|
import FSM (FSMCallback (..), |
|
|
|
import FSM (FSMCallback (..), |
|
|
|
FSMState (isTerminalState), |
|
|
|
FSMState (isTerminalState), |
|
|
|
makeFsm, runFsm) |
|
|
|
makeFsm, runFsm) |
|
|
|
import GHC.Exts (IsList (..)) |
|
|
|
|
|
|
|
import Prelude hiding (log) |
|
|
|
import Prelude hiding (log) |
|
|
|
import TickerInfoServer (TickerInfo (..), |
|
|
|
import TickerInfoServer (TickerInfo (..), |
|
|
|
TickerInfoServerHandle, |
|
|
|
TickerInfoServerHandle, |
|
|
|
@ -286,7 +277,7 @@ handleTransaqData transaqData = do |
|
|
|
liftIO $ atomically $ do |
|
|
|
liftIO $ atomically $ do |
|
|
|
candles <- readTVar cur |
|
|
|
candles <- readTVar cur |
|
|
|
putTMVar tmvar $ ResponseHistory $ HistoryResponse |
|
|
|
putTMVar tmvar $ ResponseHistory $ HistoryResponse |
|
|
|
{ hrBars = (candleToBar $ cSecurity respCandle) <$> (cCandles respCandle <> candles) |
|
|
|
{ hrBars = candleToBar (cSecurity respCandle) <$> (cCandles respCandle <> candles) |
|
|
|
, hrMoreData = False |
|
|
|
, hrMoreData = False |
|
|
|
} |
|
|
|
} |
|
|
|
_ -> log Warning "TXMLConnector.WorkThread" "Incoming candles without response var" |
|
|
|
_ -> log Warning "TXMLConnector.WorkThread" "Incoming candles without response var" |
|
|
|
@ -325,7 +316,7 @@ handleTransaqData transaqData = do |
|
|
|
|
|
|
|
|
|
|
|
fromTransaqTrade transaqTrade order maybeTickerInfo = |
|
|
|
fromTransaqTrade transaqTrade order maybeTickerInfo = |
|
|
|
let vol = case maybeTickerInfo of |
|
|
|
let vol = case maybeTickerInfo of |
|
|
|
Just tickerInfo -> (tPrice transaqTrade / tiTickSize tickerInfo * tiTickPrice tickerInfo) |
|
|
|
Just tickerInfo -> tPrice transaqTrade / tiTickSize tickerInfo * tiTickPrice tickerInfo |
|
|
|
Nothing -> tPrice transaqTrade in |
|
|
|
Nothing -> tPrice transaqTrade in |
|
|
|
Trade |
|
|
|
Trade |
|
|
|
{ |
|
|
|
{ |
|
|
|
@ -436,7 +427,7 @@ handleConnected = do |
|
|
|
Left result -> do |
|
|
|
Left result -> do |
|
|
|
case headMay (parseXML result) >>= parseContent of |
|
|
|
case headMay (parseXML result) >>= parseContent of |
|
|
|
Just (TransaqResponseResult (ResponseSuccess (Just transactionId))) -> do |
|
|
|
Just (TransaqResponseResult (ResponseSuccess (Just transactionId))) -> do |
|
|
|
brState <- asks brokerState |
|
|
|
State <- asks brokerState |
|
|
|
respVar <- asks responseVar |
|
|
|
respVar <- asks responseVar |
|
|
|
liftIO $ atomically $ do |
|
|
|
liftIO $ atomically $ do |
|
|
|
modifyTVar' (bsOrderMap brState) (M.insert (orderId order) order) |
|
|
|
modifyTVar' (bsOrderMap brState) (M.insert (orderId order) order) |
|
|
|
@ -473,29 +464,24 @@ handleConnected = do |
|
|
|
resp <- liftIO . atomically $ readTMVar respVar |
|
|
|
resp <- liftIO . atomically $ readTMVar respVar |
|
|
|
transactionMap <- liftIO $ readTVarIO (bsOrderTransactionIdMap brState) |
|
|
|
transactionMap <- liftIO $ readTVarIO (bsOrderTransactionIdMap brState) |
|
|
|
case BM.lookup oid transactionMap of |
|
|
|
case BM.lookup oid transactionMap of |
|
|
|
Just (TransactionId transactionId) -> do |
|
|
|
Just (TransactionId transactionId') -> sendCancelOrder transactionId' |
|
|
|
v <- sendCommand . toXml $ (CommandCancelOrder $ toInteger transactionId) |
|
|
|
Just (ExchangeOrderId eoid) -> sendCancelOrder eoid |
|
|
|
case v of |
|
|
|
|
|
|
|
Left result -> do |
|
|
|
|
|
|
|
log Debug "TXMLConnector.WorkThread" $ "Cancellation result: " <> (T.pack . show) result |
|
|
|
|
|
|
|
liftIO . atomically $ putTMVar resp ResponseOrderCancelled |
|
|
|
|
|
|
|
_ -> liftIO . atomically $ putTMVar resp ResponseOrderCancelled |
|
|
|
|
|
|
|
Just (ExchangeOrderId eoid) -> do |
|
|
|
|
|
|
|
v <- sendCommand . toXml $ (CommandCancelOrder $ toInteger eoid) |
|
|
|
|
|
|
|
case v of |
|
|
|
|
|
|
|
Left result -> do |
|
|
|
|
|
|
|
log Debug "TXMLConnector.WorkThread" $ "Cancellation result: " <> (T.pack . show) result |
|
|
|
|
|
|
|
liftIO . atomically $ putTMVar resp ResponseOrderCancelled |
|
|
|
|
|
|
|
_ -> liftIO . atomically $ putTMVar resp ResponseOrderCancelled |
|
|
|
|
|
|
|
_ -> do |
|
|
|
_ -> do |
|
|
|
log Debug "TXMLConnector.WorkThread" $ "Unable to locate transaction ID for order: " <> (T.pack . show) oid |
|
|
|
log Debug "TXMLConnector.WorkThread" $ "Unable to locate transaction ID for order: " <> (T.pack . show) oid |
|
|
|
liftIO . atomically $ putTMVar resp ResponseOrderCancelled |
|
|
|
liftIO . atomically $ putTMVar resp ResponseOrderCancelled |
|
|
|
pure Nothing |
|
|
|
pure Nothing |
|
|
|
_ -> pure Nothing |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
where |
|
|
|
where |
|
|
|
requestTimeoutValue = 10 |
|
|
|
requestTimeoutValue = 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sendCancelOrder transactionId' = do |
|
|
|
|
|
|
|
v <- sendCommand . toXml $ (CommandCancelOrder $ toInteger transactionId) |
|
|
|
|
|
|
|
case v of |
|
|
|
|
|
|
|
Left result -> do |
|
|
|
|
|
|
|
log Debug "TXMLConnector.WorkThread" $ "Cancellation result: " <> (T.pack . show) result |
|
|
|
|
|
|
|
liftIO . atomically $ putTMVar resp ResponseOrderCancelled |
|
|
|
|
|
|
|
_ -> liftIO . atomically $ putTMVar resp ResponseOrderCancelled |
|
|
|
|
|
|
|
|
|
|
|
checkRequestTimeout = do |
|
|
|
checkRequestTimeout = do |
|
|
|
now <- liftIO getCurrentTime |
|
|
|
now <- liftIO getCurrentTime |
|
|
|
tsVar <- asks requestTimestamp |
|
|
|
tsVar <- asks requestTimestamp |
|
|
|
|