Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Same condition follows in x bars

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

    Same condition follows in x bars

    Hi,

    I'm trying to code a condition that follows the same condition, but I don't know how to let NT7 calculate it over X(let's say 10) bars.

    For instance:

    if a Close[1] < SMA(20)[1] and Close[0] > SMA(20)[0] after this happens I want the startegy to execute on the same condition if it occurs again 'withinin 10 bars. How would I code that?
    Last edited by Robert87; 09-09-2019, 05:21 AM.

    #2
    Hello Robert87,

    Thanks for your post.

    " I want the strategy to execute on the same condition if it occurs again 'withinin 10 bars. How would I code that?"

    If I understand correctly you are looking for a crossover condition and then want to trigger if the same crossover condition occurs within the next 10 bars.

    There are a couple of ways to do this, One would be to use the MRO() method (Most Recent Occurrence) which will return the number of bars ago that a condition occurred. Please see: https://ninjatrader.com/support/help...urence_mro.htm

    Another way would be to use a bool variable that you create and set to false initially and int variable that you use for a counter that you set to zero initially. When the condition occurs you set the bool to the other state and start increment the int counter on each OnBarUpdate (or each new bar). You can then check to see if the condition occurs and if the bar counter is less than 10. Once the counter gets to 10, you would then reset the counter and turn the bool back to false to wait for the next condition.

    Pseudo code example:

    if (your conditions)
    {
    if (yourbool is true and yourcounter <= 10)
    {
    // do something on the 2nd occurrence within 10 bars
    }
    else
    {
    myCounter = 0; // set the counter
    yourbool = true; // set the bool true to look for the 2nd occurrence
    }
    }

    if (yourbool is true)
    {
    myCounter++; // increment my counter
    }

    if (yourbool and mycounter >=10)
    {
    yourbool = false; // reset bool as condition period is over
    }

    Comment


      #3
      Thank you for your help!

      But I don't fully understand it. I've got this now:



      if(ChaikinMoneyFlow(14)[1] < 0 &&
      ChaikinMoneyFlow(14)[0] > 0)
      {
      if(yourbool is true and yourcounter <= 10)
      {
      EnterLong("on 2nd time");
      }
      else
      {
      myCounter = 0;
      yourbool = true;
      }
      }
      if(yourbool is true)
      {
      myCounter++;
      }
      if(yourbool and myCounter >=10)
      {
      yourbool=false
      }
      }

      Comment


        #4
        Hello Robert87,

        Thanks for your reply.

        What I was showing was not complete code but pseudo code to convey the concept.

        If you have already created and declared the bool and counter then you need only change the word "and" to && for the C# operator meaning "and".

        If you have not declared the variables, this is done at the class level, in #Region Variables, for example:

        private bool yourbool = false;
        private int yourcounter = 0;

        NOTE: in my pseudo code example I used both myCounter and yourcounter which was in error, they should both be the same, IE: yourcounter.
        Last edited by NinjaTrader_PaulH; 09-09-2019, 11:09 AM. Reason: Added reference to region variables

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        558 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        324 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        545 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        547 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X