Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Looping hint

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

    Looping hint

    Hello,

    I'm trying to create an indicator to count some occurrencies and print them in the output window.

    Let's say I want to count how many times Close is above a Moving Average that could loop from 10 to 20 periods.

    For any of them, the code must return the number of occurrencies.

    My output should look like:

    EMA 10 = xxxx
    EMA 11 = yyyy
    EMA 12 = zzzz
    .........
    EMA 20 = wwww

    I'm doing something like:

    Code:
    int OccFound = 0;
    for (int x = 10; x < 19; x++) 
    { 
    
           if (Close[0] > EMA(x))
           {
    	OccFound = OccFound + 1;			    
           }			
    				
    Print("EMA "+x+" # "+OccFound);				
    				
    }
    But id doesn't seems to work. Any hint?

    Thanks.
    Last edited by MAX; 10-29-2016, 10:39 AM.

    #2
    Hello,

    What happens when you currently run the code, do you get some output or none at all?

    If you are getting no output at all, are you getting any errors either in the control center log window or the output window?

    I look forward to being of further assistance.

    Comment


      #3
      I receive an output for all bar in the chart that looks like:

      Code:
      24/10/2016 00:00:00EMA 10 # 0 
      24/10/2016 00:00:00EMA 11 # 0
      24/10/2016 00:00:00EMA 12 # 0 
      24/10/2016 00:00:00EMA 13 # 0 
      24/10/2016 00:00:00EMA 14 # 0
      ............................................
      ............................................
      24/10/2016 00:00:00EMA 20 # 0
      while I was expecting only 20 rows (only on the last bar), each returning the total occurency for any EMA.

      Comment


        #4
        Originally posted by MAX View Post
        I receive an output for all bar in the chart that looks like:

        Code:
        24/10/2016 00:00:00EMA 10 # 0 
        24/10/2016 00:00:00EMA 11 # 0
        24/10/2016 00:00:00EMA 12 # 0 
        24/10/2016 00:00:00EMA 13 # 0 
        24/10/2016 00:00:00EMA 14 # 0
        ............................................
        ............................................
        24/10/2016 00:00:00EMA 20 # 0
        while I was expecting only 20 rows (only on the last bar), each returning the total occurency for any EMA.
        Was the Close[0] over the EMA?

        Your output suggests not.

        Code:
        Print("Close[0]=" + Close[0] + " EMA(x)=" + EMA(x) + " x="+x+" # "+OccFound);

        Comment


          #5
          Hello,

          Thank you for that detail, it sounds like you have this running in OnBarUpdate without first checking that you are not in Historical data.

          If so this loop would be run once for each bar.

          If this is only to be used in Realtime, you could just prevent this from happening in Historical:

          Code:
          if(Historical) return;


          Otherwise if you wanted this to only run when it reaches the last bars on the chart, you would need to compare the CurrentBar against the Count

          Code:
          if(CurrentBar < Count - 2) return;


          I look forward to being of further assistance.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          596 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          343 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          103 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          556 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          554 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X