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 NullPointStrategies, Today, 05:17 AM
      0 responses
      48 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      126 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      66 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      42 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      46 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X