Hi J¨¹rgen, sorry for posting about this topic again, but I finally extensively tried what you suggested and I now understand what you meant by making the order self destruct.
I ran into the problem where an order did not fill at all and it did not self destruct after 3:50, so I am trying to take your advice into consideration now using the timecondition. Is this the correct way of doing it? I will try it on paper trade tomorrow once I have figured it out.
To ensure that if
an order has not been filled, the order self destructs at 3:50pm
an order has filled or partially filled, the trailing stop loss protects it and the closing trade is placed at 3:50 making sure that there are no open positions.
Is my code below correct?
def BracketOrder(self, parentOrderId:int, openinglimitPrice:float, quantity:Decimal, trailingPercent:float, timeswitch:str, adaptive_type:str):
# 1st part: Buy Adaptive Algo order
opening = Order()
opening.orderId = parentOrderId
opening.action = "BUY"
opening.orderType = "LMT"
opening.lmtPrice = openinglimitPrice
opening.totalQuantity = abs(quantity)
opening.transmit = False
opening.tif = 'GTC'
algo.FillAdaptiveParams(opening, adaptive_type)
timeCondition = TimeCondition()
timeCondition.time = timeswitch
timeCondition.isMore = True
opening.conditions.append(timeCondition)
# 2nd part: Trailing Stop Sell order
trailingStopLoss = Order()
trailingStopLoss.orderId = opening.orderId + 1
trailingStopLoss.action = "SELL"
trailingStopLoss.trailingPercent = trailingPercent
trailingStopLoss.orderType = "TRAIL"
trailingStopLoss.totalQuantity = abs(quantity)
trailingStopLoss.parentId = parentOrderId
trailingStopLoss.tif = "GTC"
trailingStopLoss.transmit = False
# 3rd part: Sell Market order
closing = Order()
closing.orderId = opening.orderId + 2
closing.action = "SELL"
closing.orderType = "MKT"
closing.totalQuantity = abs(quantity)
closing.parentId = parentOrderId
closing.tif = "GTC"
closing.goodAfterTime = timeswitch
closing.transmit = True
closing.conditionsCancelOrder = False
timeCondition = TimeCondition()
timeCondition.time = timeswitch
timeCondition.isMore = True
closing.conditions.append(timeCondition)
bracketOrder = [opening, trailingStopLoss, closing]
return bracketOrder