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

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 DJ888, 04-16-2024, 06:09 PM
      4 responses
      12 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by terofs, Today, 04:18 PM
      0 responses
      7 views
      0 likes
      Last Post terofs
      by terofs
       
      Started by nandhumca, Today, 03:41 PM
      0 responses
      6 views
      0 likes
      Last Post nandhumca  
      Started by The_Sec, Today, 03:37 PM
      0 responses
      3 views
      0 likes
      Last Post The_Sec
      by The_Sec
       
      Started by GwFutures1988, Today, 02:48 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Working...
      X