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

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
    }

    Paul H.NinjaTrader Customer Service

    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
        Paul H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by fx.practic, 10-15-2013, 12:53 AM
        5 responses
        5,404 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Started by Shai Samuel, 07-02-2022, 02:46 PM
        4 responses
        95 views
        0 likes
        Last Post Bidder
        by Bidder
         
        Started by DJ888, Yesterday, 10:57 PM
        0 responses
        7 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by MacDad, 02-25-2024, 11:48 PM
        7 responses
        159 views
        0 likes
        Last Post loganjarosz123  
        Started by Belfortbucks, Yesterday, 09:29 PM
        0 responses
        8 views
        0 likes
        Last Post Belfortbucks  
        Working...
        X