sdoosa-algo-trade-python icon indicating copy to clipboard operation
sdoosa-algo-trade-python copied to clipboard

Enhancement request: Please add move SL to cost when SL in other leg is hit.

Open vasanthkumarv opened this issue 3 years ago • 2 comments

vasanthkumarv avatar May 30 '21 12:05 vasanthkumarv

Hi Sreeni, I'm novice coder, Tried updating trackSLOrder in trademanger to include moveSL to cost. excuse if its not logical.

@staticmethod def trackSLOrder(trade): if trade.tradeState != TradeState.ACTIVE: return if trade.stopLoss == 0: # Do not place SL order if no stopLoss provided return if trade.slOrder == None: # Place SL order TradeManager.placeSLOrder(trade) else: if trade.slOrder.orderStatus == OrderStatus.COMPLETE: # SL Hit exit = trade.slOrder.averagePrice exitReason = TradeExitReason.SL_HIT if trade.initialStopLoss == trade.stopLoss else TradeExitReason.TRAIL_SL_HIT TradeManager.setTradeToCompleted(trade, exit, exitReason) # Make sure to cancel target order if exists TradeManager.cancelTargetOrder(trade) # Move the SL to cost if SL is hit in other leg if isMoveSLToCost == True: symbol = trade.slOrder.tradingSymbol if "PE" in symbol: changeSymbol = symbol.replace("PE", "CE") else: changeSymbol = symbol.replace("CE", "PE")

	for v in TradeManager.trades:
		if v.tradingSymbol == changeSymbol:
			trade = v
		
	if trade.tradeState == TradeState.ACTIVE:
		newSL = getLastTradedPrice(changeSymbol)
		omp = OrderModifyParams()
		omp.newTriggerPrice = newSL
		try:
			oldSL = trade.stopLoss
			TradeManager.getOrderManager().modifyOrder(trade.slOrder, omp)
			logging.info('TradeManager: Move SL to cost: Successfully modified stopLoss from %f to %f for tradeID %s', oldSL, newSL, trade.tradeID)
				
		except Exception as e:
			logging.error('TradeManager: Failed to modify SL order for tradeID %s orderId %s: Error => %s', trade.tradeID, trade.slOrder.orderId, str(e))		

  elif trade.slOrder.orderStatus == OrderStatus.CANCELLED:
    # SL order cancelled outside of algo (manually or by broker or by exchange)
    logging.error('SL order %s for tradeID %s cancelled outside of Algo. Setting the trade as completed with exit price as current market price.', trade.slOrder.orderId, trade.tradeID)
    exit = TradeManager.symbolToCMPMap[trade.tradingSymbol]
    TradeManager.setTradeToCompleted(trade, exit, TradeExitReason.SL_CANCELLED)
    # Cancel target order if exists
    TradeManager.cancelTargetOrder(trade)

  else:
    TradeManager.checkAndUpdateTrailSL(trade)

vasanthkumarv avatar May 30 '21 15:05 vasanthkumarv

Hi Vasanth,

This is done and tested . You can refer https://github.com/rrajesh145/algo_tradiing/commit/9392d97324c6a043256d9c223951a1ea97da6be2

rrajesh145 avatar Jun 01 '21 10:06 rrajesh145