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 AveryFlynn, Today, 04:57 AM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_Erick  
      Started by Max238, Today, 01:28 AM
      4 responses
      37 views
      0 likes
      Last Post Max238
      by Max238
       
      Started by r68cervera, Today, 05:29 AM
      1 response
      8 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by geddyisodin, Today, 05:20 AM
      1 response
      11 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by timko, Today, 06:45 AM
      2 responses
      14 views
      0 likes
      Last Post NinjaTrader_ChristopherJ  
      Working...
      X