I have a formula with the apply stop N bar stop however some of the backtest trades are being stopped out at >N bars, in the example below the stop is 10 bars with a 1 bar trade delay so should all be 11 bars however some trades are much greater than 11 bars.
Here is my formula with some help previously from @fxshrat
_SECTION_BEGIN("Random_Entry_Random_Exit");
SetTradeDelays(1,1,1,1);
maxpos = 1; // maximum number of open positions
SetOption("InitialEquity", 100000 ); // set initial equity = 100K
SetOption( "MaxOpenPositions", maxpos );
SetPositionSize( 1000, spsValue ); // 1K in each trade
RandomNumber = Random();
Buy = RandomNumber > 0.9;
Sell = 0;
NBarStop = 10;
ApplyStop(stopTypeNBar, stopModeBars, NBarStop);
BuyPrice = Close;
SellPrice = Close;
if ( Status("action") == actionIndicator) {
eq = Equity(1,0);
Plot( C, "Price", colorDefault, styleBar );
PlotShapes(Buy*shapeUpArrow, colorGreen, 0, L);
PlotShapes((Sell>0)*shapeDownArrow, colorRed, 0, H);
}
_SECTION_END();
screen shot example showing majority of trades at N+1 bars however some also > N+1 bars
also with the example of the longest trade at 27 bars
When I view the chart, the plotted green buy and red sell arrows do not correspond with the entry and exit dates on the list of trades in the backtest results.
Also, every time I left click on the chart the location of the plotted buy and sell arrows change position.
Also, if I right click and select edit formula on the chart window, a new window is opened in the formula editor with a new copy of my formula duplicated under the original copy.
I'm not sure if the above issues I am having are related or unrelated and being a beginner I am struggling to problem solve the issues so any suggestions are appreciated.