Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

basic counter for a condition

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

    basic counter for a condition

    i could use some help putting together a counter for a condition i'm trying to code.


    i want to have a condition called uptrend when these conditions are met (i don't know if a boolean variable would work):



    if (Rising(smav) && Rising(emav) && Rising(hmav))


    now, i would like the strategy to keep a count of how many consecutive bars this condition has been true for (or confirmed) and only act after a minimum count that i could define as an input has been met or exceeded.


    let's say i define a variable as trendcount and the input is 3.


    then, the counter for this uptrend condition would only follow through with the actions i code after 3 consecutive bars have met the conditions defined as uptrend in this case.


    thanks.

    #2
    Hello rtwave,

    This can definitely be done through NinjaScript.

    First, you will need a static variable to store your run length in, so that it can persist between OnBarUpdate calls. In the Variables section, below the line

    Code:
    [FONT=Courier New]// User defined variables (add any user defined variables below)[/FONT]
    You will want to add this line,
    Code:
    [FONT=Courier New]private int runLength = 0;[/FONT]
    Next, we can template out your increment code,

    Code:
    [FONT=Courier New]if (Rising(smav) && Rising(emav) && Rising(hmav))
    {
      ++runLength;
      // your conditional code here
    }[/FONT]
    Next and immediately after the previous code, we can add your reset code,

    Code:
    [FONT=Courier New]else
    {
      runLength = 0;
    }[/FONT]
    Finally, we can template out the code that acts after a minimum length consecutive run. We'll say that you defined the User Input "trendcount" already. Please let us know if you need help setting up User Input variables.

    Code:
    [FONT=Courier New]if (runLength >= trendcount)
    {
      // your conditional code here
    }[/FONT]
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_JessicaP View Post
      Hello rtwave,

      This can definitely be done through NinjaScript.

      First, you will need a static variable to store your run length in, so that it can persist between OnBarUpdate calls. In the Variables section, below the line

      Code:
      [FONT=Courier New]// User defined variables (add any user defined variables below)[/FONT]
      You will want to add this line,
      Code:
      [FONT=Courier New]private int runLength = 0;[/FONT]
      Next, we can template out your increment code,

      Code:
      [FONT=Courier New]if (Rising(smav) && Rising(emav) && Rising(hmav))
      {
        ++runLength;
        // your conditional code here
      }[/FONT]
      Next and immediately after the previous code, we can add your reset code,

      Code:
      [FONT=Courier New]else
      {
        runLength = 0;
      }[/FONT]
      Finally, we can template out the code that acts after a minimum length consecutive run. We'll say that you defined the User Input "trendcount" already. Please let us know if you need help setting up User Input variables.

      Code:
      [FONT=Courier New]if (runLength >= trendcount)
      {
        // your conditional code here
      }[/FONT]
      Not meaning to wax pedantic, but you mean a "class" variable, not a "static" variable. "static" has a very specific meaning in c#.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by DannyP96, 05-18-2026, 02:38 PM
      1 response
      23 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 05-11-2026, 05:56 AM
      0 responses
      115 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 05-10-2026, 08:12 PM
      0 responses
      68 views
      0 likes
      Last Post CarlTrading  
      Started by Hwop38, 05-04-2026, 07:02 PM
      0 responses
      225 views
      0 likes
      Last Post Hwop38
      by Hwop38
       
      Started by CaptainJack, 04-24-2026, 11:07 PM
      0 responses
      409 views
      0 likes
      Last Post CaptainJack  
      Working...
      X