• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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 to exit a trade when long or short.

    The strategy works fine during back- testing but when running real time exits the trade a few bars too early.

    Please find below a part of my code

    ---------------------
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    BarsExitLong = 35;
    BarsExitShort = 15;
    }
    }

    protected override void OnBarUpdate()
    {
    if (IsShort)
    {
    barsShort ++;

    if ( (barsShort >= BarsExitShort) && (Close[0] < (Position.AveragePrice - _atr[0]) ) )
    {
    base.ExitShort("STarget" + barsShort, "S1");
    }
    }

    if (IsFlat && (_smaS[2]<=_smaS[1]) && (_smaS[1] > _smaS[0]) )
    {
    int lots;

    barsShort = 0;

    double Ptarget = Close[0] - ProfitMultiplier;


    if (TradingFutures)
    {
    lots = (int) (InitialCapital / 50000);

    shortEntryOrder1 = EnterShort( lots , "S1");

    stopPrice = High[0] + ATRMultiplier;

    base.SetStopLoss("S1", CalculationMode.Price, stopPrice, false );
    }
    }
    }
    -----------------------------


    I suspect that the reason for this is the fact that I am running the strategy in real time on eurex regular trading hours while the program is computing the bars in extended trading hours.


    In this particular instance in the attached photo the trade was exited after 11 bars while the programming was for 15 bars.


    Please advise and why this is happening and how to rectify.

    Kind regards

    Ioannis

    #2
    Hello, I posted this question some time ago.

    Can someone from support take a look please?

    Comment


      #3
      Hello im197,

      I would likely suggest to try using BarsSinceEntryExecution here rather than counting bars. Using this method would allow you to use the signal names you use to target an entry and find out how many bars it has been since that entry. This cuts out the counting logic you have which may help. Can you try this in the test you are using to see if that helps resolve the issue?

      Code:
      BarsSinceEntryExecution() > 10
      https://ninjatrader.com/support/help...tsub=barssince

      I look forward to being of further assistance.
      JesseNinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Newtrader101, Today, 06:01 PM
      0 responses
      2 views
      0 likes
      Last Post Newtrader101  
      Started by Newtrader101, Today, 05:32 PM
      0 responses
      4 views
      0 likes
      Last Post Newtrader101  
      Started by ntbone, Yesterday, 11:53 AM
      1 response
      11 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by Johnny Santiago, Yesterday, 11:36 AM
      1 response
      14 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by bouncetherubble, Yesterday, 07:07 PM
      2 responses
      9 views
      0 likes
      Last Post bouncetherubble  
      Working...
      X