Hey,
Can I please get some help with the below Algo. For some reason when take profit or stop loss is triggered it opens a new position in Oanda vs actually taking profit or closing the position if the price hits stop loss. It appears to work fine on the backtest but when I deploy it to a demo account it seems to have the above issue. As you can see from the code I use the limitOrder and StopMarketOrder for the triggers to set the stop loss / take profit price. In fact, even when I tried to liquidate the strategy to pause the algorithm it didn't work - I had to go into Oanda platform and manually close out the positions.
marketOrderTicket = self.MarketOrder(symbol, quantity)
stopMarketOrderTicket = self.StopMarketOrder(symbol, -quantity, stop_loss)
limitOrderTicket = self.LimitOrder(symbol, -quantity, profit_target)
Any help would be much appreciated. Please excuse some messiness in the code I'm relatively new to this.
Thank you in advance!
Damian
Damian Grabarczyk
ChatGPT suggested using the below. I'm wondering if that is the right approach? It suggested using the Stoploss and TakeProfit that is associated with the market order instead of LimitOrder or StopMarketOrder.
def consolidation_handler(self, sender: object, consolidated_bar: QuoteBar) -> None:
# ... (previous code)
for symbol in self.Data.keys():
invested = self.Portfolio[symbol].Invested
if not invested:
# ... (previous code)
stopLossInPips = self.stopLossTarget * pip * 10
profitTargetInPips = self.profitTarget * pip * 10
if current_price <= lowerBand:
# ... (previous code)
marketOrderTicket = self.MarketOrder(symbol, quantity)
marketOrderTicket.StopLoss = stopLossInPips
marketOrderTicket.TakeProfit = profitTargetInPips
elif current_price >= upperBand:
# ... (previous code)
marketOrderTicket = self.MarketOrder(symbol, -quantity)
marketOrderTicket.StopLoss = stopLossInPips
marketOrderTicket.TakeProfit = profitTargetInPips
Nico Xenox
Hey Damian Grabarczyk,
You could simply just buy a position in the consolidation handler and in the orderevent function do the rest. You could do something like this:
Best,
Nico
Damian Grabarczyk
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!