Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bar count to exit position is not working as expected

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Bar count to exit position is not working as expected

    Hello,

    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()
    {
    if (IsFlat && (_smaL[2]<=_smaL[1]) && (_smaL[1] > _smaL[0]) )
    {
    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);
    }
    if (IsShort)
    {
    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

Latest Posts

Collapse

Topics Statistics Last Post
Started by CarlTrading, 03-31-2026, 09:41 PM
1 response
81 views
1 like
Last Post NinjaTrader_ChelseaB  
Started by CarlTrading, 04-01-2026, 02:41 AM
0 responses
42 views
0 likes
Last Post CarlTrading  
Started by CaptainJack, 03-31-2026, 11:44 PM
0 responses
64 views
2 likes
Last Post CaptainJack  
Started by CarlTrading, 03-30-2026, 11:51 AM
0 responses
68 views
0 likes
Last Post CarlTrading  
Started by CarlTrading, 03-30-2026, 11:48 AM
0 responses
55 views
0 likes
Last Post CarlTrading  
Working...
X