Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    JesseNinjaTrader Customer Service

    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.
          JesseNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Johnny Santiago, 10-11-2019, 09:21 AM
          95 responses
          6,193 views
          0 likes
          Last Post xiinteractive  
          Started by xiinteractive, 04-09-2024, 08:08 AM
          2 responses
          11 views
          0 likes
          Last Post xiinteractive  
          Started by Irukandji, Today, 09:34 AM
          1 response
          3 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Started by RubenCazorla, Today, 09:07 AM
          1 response
          5 views
          0 likes
          Last Post RubenCazorla  
          Started by TraderBCL, Today, 04:38 AM
          3 responses
          25 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Working...
          X