Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trouble to Code a Count if Function

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

    Trouble to Code a Count if Function

    Hello,

    I am a beginner to coding and I am trying to learn how to write a number of conditions that could help me in my strategy development, so apologies in advance if what I am asking might sound strange. I am currently having problem writing a code with the following logic:

    (Count the Number of Bars which have the close below an indicator) / Current numbers of bars in progress.

    If the result is > than X ,

    then set Bool to be true.

    For example.

    If current number of bars with close < 20EMA = 20

    Current number of bars in progress since beginning of session = 30

    do

    20/30 = 0.66

    if 0.66 > Threeshold

    then set Bool to be true.


    I hope it makes sense what I am asking and I thanks everyone in advance for the help



    #2
    Hello NorwegianCat92,

    Try the following:

    Print( string.Format("{0} | CountIf(() => Close[0] < SMA(2)[0], CurrentBar): {1} / CurrentBar: {2} = {3}", Time[0], CountIf(() => Close[0] < SMA(2)[0], CurrentBar), CurrentBar, CountIf(() => Close[0] < SMA(2)[0], CurrentBar) / CurrentBar) );

    if ( CountIf(() => Close[0] < SMA(2)[0], CurrentBar) / CurrentBar > Threshold)
    {
    myBool = true;
    }

    Below is a link to the help guide on CountIf().
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you Chelsea! I will try the code above.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Mindset, 04-21-2026, 06:46 AM
      0 responses
      88 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      134 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      68 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      118 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      67 views
      0 likes
      Last Post PaulMohn  
      Working...
      X