Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 TheWhiteDragon, 01-21-2019, 12:44 PM
4 responses
541 views
0 likes
Last Post PaulMohn  
Started by GLFX005, Today, 03:23 AM
0 responses
3 views
0 likes
Last Post GLFX005
by GLFX005
 
Started by XXtrader, Yesterday, 11:30 PM
2 responses
11 views
0 likes
Last Post XXtrader  
Started by Waxavi, Today, 02:10 AM
0 responses
7 views
0 likes
Last Post Waxavi
by Waxavi
 
Started by TradeForge, Today, 02:09 AM
0 responses
14 views
0 likes
Last Post TradeForge  
Working...
X