Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Undersatanding BarsSinceExit

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

    Undersatanding BarsSinceExit

    I am having some difficulty understanding BarsSinceExit
    I have a strategy which uses one condition to enter a trade and one to exit.
    However when the trade exits, occasionally the entry condition is triggered in the same bar and immediately enters the trade again. I think a solution would be for the strategy to wait 1 bar after the exit, so I have been trying to use the‘BarsSinceExit’ command.
    It says in your guide that a value of -1 is returned if a previous exits does not exist, so this is what I have done……
    When the strategy starts and there are no previous trades I have Conditions 1 & 2 (below). After an exit has been executed, Conditions 3 & 4 would take the place of 1 & 2.
    Can you point me in the right direction, as this does not work….

    CalculateOnBarClose = false
    Protected override void OnBarUpdate()
    // Condition set 1 When a previous exit does not exist
    If BarsSinceExit () > -1
    {
    EnterLong(“”,””);
    }
    // Condition set 2 When a previous exit does not exist
    If BarsSinceExit () > -1
    {
    EnterShort(“”,””);
    }
    //Condition set 3 When an exit does exist (superceeds 1)
    If BarsSinceExit () > 1
    {
    EnterLong(“”,””);
    }

    //Condition set 4 When an exit does exist (superceeds 2)
    If BarsSinceExit () > 1
    {
    EnterShort(“”,””);
    }

    Thanks in advance...

    #2
    Hi John833,

    You want to use an equality check against -1 not a greater than. When you have never traded yet, the BarsSinceExit() will be -1 and not greater than that.

    Code:
    if (BarsSinceExit() == -1)
         EnterLong();
    Then on your other conditions:
    Code:
    if (BarsSinceExit() > 1)
         EnterLong();
    This will work fine.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks Josh
      It was such an obvious error.... I think I have been staring at it for so long I couldn't see the wood from the trees!

      Cheers

      Regards
      John

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      580 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      335 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      102 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      554 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      552 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X