|
|
|
@ -661,23 +661,32 @@ exitAtLimit :: (StateHasPositions s, MonadRobot m c s) => NominalDiffTime -> Pri |
|
|
|
exitAtLimit timeToCancel price position operationSignalName = do |
|
|
|
exitAtLimit timeToCancel price position operationSignalName = do |
|
|
|
lastTs <- view seLastTimestamp <$> getEnvironment |
|
|
|
lastTs <- view seLastTimestamp <$> getEnvironment |
|
|
|
inst <- view seInstanceId <$> getEnvironment |
|
|
|
inst <- view seInstanceId <$> getEnvironment |
|
|
|
|
|
|
|
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 |
|
|
|
case posCurrentOrder position of |
|
|
|
Just order -> cancelOrder (orderId order) |
|
|
|
Just order -> cancelOrder (orderId order) |
|
|
|
Nothing -> doNothing |
|
|
|
Nothing -> doNothing |
|
|
|
oid <- submitOrder (closeOrder inst) |
|
|
|
oid <- submitOrder (closeOrder inst roundedPrice) |
|
|
|
appendToLog Trace $ [t|exitAtLimit: %?, deadline: %?|] (posTicker position) (timeToCancel `addUTCTime` lastTs) |
|
|
|
appendToLog Trace $ [t|exitAtLimit: %?, deadline: %?|] (posTicker position) (timeToCancel `addUTCTime` lastTs) |
|
|
|
modifyPosition (\pos -> |
|
|
|
modifyPosition (\pos -> |
|
|
|
pos { posCurrentOrder = Nothing, |
|
|
|
pos { posCurrentOrder = Nothing, |
|
|
|
posState = PositionWaitingCloseSubmission (closeOrder inst) { orderId = oid }, |
|
|
|
posState = PositionWaitingCloseSubmission (closeOrder inst roundedPrice) { orderId = oid }, |
|
|
|
posNextState = Just PositionClosed, |
|
|
|
posNextState = Just PositionClosed, |
|
|
|
posSubmissionDeadline = Just $ 10 `addUTCTime` lastTs, |
|
|
|
posSubmissionDeadline = Just $ 10 `addUTCTime` lastTs, |
|
|
|
posExecutionDeadline = Just $ timeToCancel `addUTCTime` lastTs }) position |
|
|
|
posExecutionDeadline = Just $ timeToCancel `addUTCTime` lastTs }) position |
|
|
|
|
|
|
|
Nothing -> do |
|
|
|
|
|
|
|
appendToLog Warning $ "Unable to locate first bar series" |
|
|
|
|
|
|
|
return position |
|
|
|
where |
|
|
|
where |
|
|
|
closeOrder inst = mkOrder { |
|
|
|
closeOrder inst roundedPrice = mkOrder { |
|
|
|
orderAccountId = posAccount position, |
|
|
|
orderAccountId = posAccount position, |
|
|
|
orderSecurity = posTicker position, |
|
|
|
orderSecurity = posTicker position, |
|
|
|
orderQuantity = (abs . posBalance) position, |
|
|
|
orderQuantity = (abs . posBalance) position, |
|
|
|
orderPrice = Limit price, |
|
|
|
orderPrice = Limit roundedPrice, |
|
|
|
orderOperation = if posBalance position > 0 then Sell else Buy, |
|
|
|
orderOperation = if posBalance position > 0 then Sell else Buy, |
|
|
|
orderSignalId = SignalId inst operationSignalName "" |
|
|
|
orderSignalId = SignalId inst operationSignalName "" |
|
|
|
} |
|
|
|
} |
|
|
|
|