I am using a bar counter varable in order to exit a position.
When I backtest the strategy the bars are counted correcty - When I use the strategy in realtime the position is exited a few bars early.
I suspect the reason this is happening is because I am using Eurex Equity Index Futures trading hours for the strategy - while the futures trade in Extened hours.
i.e. for some reason bars in realtime are counted in full extended hours and not in the trading hours designed for the strategy.
Here is part of my code.
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
BarsExitLong = 35;
BarsExitShort = 15;
}
protected override void OnBarUpdate()
{
{
int lots;
barsShort = 0;
double Ptarget = Close[0] - ProfitMultiplier;
if (TradingFutures)
{
lots = (int) (InitialCapital / 50000);
shortEntryOrder1 = EnterShort( lots , "S1");
shortEntryOrder2 = EnterShort( lots , "S2");
stopPrice = High[0] + StopMultiplier;
base.SetStopLoss("S1", CalculationMode.Price, stopPrice, false );
base.SetStopLoss("S2", CalculationMode.Price, stopPrice, false );
SetProfitTarget("S2", CalculationMode.Price, Ptarget, false);
}
{
barsShort ++;
if ( (barsShort >= BarsExitShort) && (Close[0] < (Position.AveragePrice - _atr[0]) ) )
{
base.ExitShort("STarget" + barsShort, "S1");
}
}
In the photo of the realtime strategy implementation that I attach the exit is after 11 bars. The programming setting is for exit after 15 bars.
Please advise on why this is happening and how to correct.
Kind regards
Ioannis