In an intraday strategy, I would like to include a condition that will stop the placing of further trades if I’ve already had 2 consecutive losses that day. Also, I would like to code this in a manner that will allow me to backtest the effect of this condition.
My code(below) is based on the example in NT 6 documentation covering “Trade Class”.
However, in order to be able to backtest the condition, I have changed ” .RealtimeTrades. ” everywhere to “ .AllTrades. ”.
The difficulty this gives me is that, instead of stopping the placing of new trades in a given day after a second consecutive loss in that day, the condition once triggered also prevents ANY future trades being taken on succeeding days, too.
How can I change the coding to get a condition that re-evaluates each day, and - if triggered - will only prevent trading on that day (rather than on all future days, too)?
[FONT=Courier New][COLOR=green][COLOR=green][FONT=Courier New]// Section to keep track of whether there have been 2 successive losses in a row, and to stop trading if there have[/FONT][/COLOR] [COLOR=blue][FONT=Courier New]if[/FONT][/COLOR][COLOR=black][FONT=Courier New] (Performance.AllTrades.Count > [/FONT][/COLOR][COLOR=purple][FONT=Courier New]1[/FONT][/COLOR][COLOR=black][FONT=Courier New]) [/FONT][/COLOR] [COLOR=black][FONT=Courier New]{ [/FONT][/COLOR] [COLOR=green][FONT=Courier New]// Get the last completed trades [/FONT][/COLOR] [COLOR=black][FONT=Courier New]Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - [/FONT][/COLOR][COLOR=purple][FONT=Courier New]1[/FONT][/COLOR][COLOR=black][FONT=Courier New]]; [/FONT][/COLOR] [COLOR=black][FONT=Courier New]Trade secondlastTrade = Performance.AllTrades[Performance.AllTrades.Count - [/FONT][/COLOR][COLOR=purple][FONT=Courier New]2[/FONT][/COLOR][COLOR=black][FONT=Courier New]]; [/FONT][/COLOR] [COLOR=green][FONT=Courier New]// Calculate the PnL for the last two completed trades [/FONT][/COLOR] [COLOR=blue][FONT=Courier New]double[/FONT][/COLOR][COLOR=black][FONT=Courier New] lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;[/FONT][/COLOR] [COLOR=blue][FONT=Courier New]double[/FONT][/COLOR][COLOR=black][FONT=Courier New] secondlastProfit = secondlastTrade.ProfitCurrency * secondlastTrade.Quantity;[/FONT][/COLOR] [COLOR=blue][FONT=Courier New]if[/FONT][/COLOR][COLOR=black][FONT=Courier New](lastProfit < [/FONT][/COLOR][COLOR=purple][FONT=Courier New]0[/FONT][/COLOR][COLOR=black][FONT=Courier New] && secondlastProfit < [/FONT][/COLOR][COLOR=purple][FONT=Courier New]0[/FONT][/COLOR][COLOR=black][FONT=Courier New])[/FONT][/COLOR] [COLOR=black][FONT=Courier New]{[/FONT][/COLOR] [COLOR=black][FONT=Courier New]keepTrading = [/FONT][/COLOR][COLOR=purple][FONT=Courier New]0[/FONT][/COLOR][COLOR=black][FONT=Courier New];[/FONT][/COLOR] [COLOR=black][FONT=Courier New]}[/FONT][/COLOR] [COLOR=black][FONT=Courier New]}[/FONT][/COLOR] [/COLOR][/FONT]
Comment