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

Counting Bars after Boolean true

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

    Counting Bars after Boolean true

    Hello i want to count X bars after a bool is set true:

    if
    (Indicator[0] == 3);
    {
    IndicatorLong = true;
    }

    if
    (IndicatorLong == true && ;
    {
    NewBarCounter++;
    }

    if
    (IndicatorLong= true && NewBarCounter>= 5);
    {
    IndicatorLong= false;
    NewBarCounter= 0;
    }

    I dont know how to implment that the counter is increment by 1 after every new bar.

    Can someone help here?

    Thanks

    #2
    Hello Lopat,

    What you posted should essentially do that but I do see some errors in the way you made your conditions. In C# you never use the semi colon on an if statement, your if condition should always be formatted like this:

    Code:
    if(someCondition)
    {
    
    }
    You currently have
    Code:
     if(someCondition);
    Which prevents the logic below the if condition from happening when the condition is true.

    Your condition should look like this:
    Code:
    if(Indicator[0] == 3)
    {
         IndicatorLong = true;
    }​

    You may also need to change the Indicator[0] == 3 to Indicator[0] >= 3, checking if two double type numbers are exactly equal in C# is not possible.

    Another item here would be to add prints so that you can output how your logic is working when you run the script. That will help to make sure its doing what you had expected.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi thanks! I change the code.

      does this part increment by 1 after each bar?

      if
      (IndicatorLong == true)
      {
      NewBarCounter++;
      }​

      or do I need to add that the counter increment after each new bar? And when yes. How do I do it?

      Comment


        #4
        Hello Lopat,

        Assuming that you are using OnBarClose processing yes. Once IndicatorLong is true that condition will be true. In C# you can increment a variable by using the ++ operator. Using ++ is the same as NewBarCounter = NewBarCounter + 1;
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thanks a lot. It is working now like I want it. Thanks for the quick response

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Vietanhnguyen2hotmailcom, Yesterday, 10:29 AM
          4 responses
          23 views
          0 likes
          Last Post Vietanhnguyen2hotmailcom  
          Started by PhillT, 04-19-2024, 02:16 PM
          4 responses
          35 views
          0 likes
          Last Post PhillT
          by PhillT
           
          Started by ageeholdings, 05-01-2024, 05:22 AM
          5 responses
          37 views
          0 likes
          Last Post ageeholdings  
          Started by reynoldsn, Today, 02:34 PM
          0 responses
          13 views
          0 likes
          Last Post reynoldsn  
          Started by nightstalker, Today, 02:05 PM
          0 responses
          23 views
          0 likes
          Last Post nightstalker  
          Working...
          X