Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vasanthkumarv opened this issue May 30, 2021 · 2 comments

Comments

@vasanthkumarv
Copy link

No description provided.

@vasanthkumarv
Copy link
Author

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)

@rrajesh145
Copy link

Hi Vasanth,

This is done and tested . You can refer
rrajesh145/algo_tradiing@9392d97

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants