I have strategy that I am backtesting that makes use of the ExitShortStop function. Occasionally in my backtest results, an entry order is executed but the stop is not triggered when it should be. In one case where I notice is when the entry is on the open of the next bar and the same bar closes way above where my stop should have been executed. Could this be due to the fact that my entry and exit should have been on the same bar and that the backtesting engine does not enter the stop since the bar closes above the stop price? Would I need to worry about this if I were trading the strategy in realtime? Should I replace ExitStopShort with SetStopLoss and translate the price to ticks?
Here is a code snippet to make sure I don't have any syntax errors along with a screenshot of where the stop does not get executed: Please note the stop variable is set to 1.00.
//This code limits my daily loss to 400
if(Performance.AllTrades.TradesPerformance.Currenc y.CumProfit - priorTradesCumProfit <= -400 && Position.MarketPosition==MarketPosition.Flat)
return;
//Simplified short entry
if(ToTime(Time[0]) >= timeOpen && ToTime(Time[0])< timeClose &&
Position.MarketPosition == MarketPosition.Flat)
{
if(Close[0]>Close[1] )
{
//PrintWithTimeStamp(Close[0].ToString()+" "+DayOpen.ToString());
EnterShort("Sell");
}
}
if(Position.MarketPosition == MarketPosition.Short)
{
//double DStop = Position.AvgPrice+stop;
double DStop = Position.AvgPrice + ((stop*4)*TickSize);
double DTarget=Position.AvgPrice-target;
PrintWithTimeStamp("Entered Short. DayOpen is:"+DayOpen.ToString()+"EntryPrice :"+Position.AvgPrice+" ProfitTarget :"+DTarget.ToString()+ " Stop :"+DStop.ToString());
ExitShortStop(Position.AvgPrice + ((stop*4)*TickSize),"BuyStp","Sell");
ExitShortLimit(DTarget,"Sell");
}
As you can see from the screenshot, a short is entered around 1430 and it should have been immediately stopped out 4 ticks later but does not execute until after the market closes below the stop price and then closes back above around 15:00.
Thanks for looking into this.
Comment