Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

while loop breaks indicator?

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

    while loop breaks indicator?

    I am developing an indicator and I want to look backwards to see what happened before the current candle. I am using a while loop and it stops the indicator from working.

    I print some text above each candle. If I comment out the while and let the code fall thru once it prints the text. If I uncomment the while, the text doesn't print.

    A simplified code sample is attached.

    Are while loops illegal in indicators?
    Attached Files

    #2
    Hello and thank you for your post,

    You are able to use while() loops within indicators.

    The indicator did not cause an error for me and did print text both uncommented and commented.

    Please make sure you have enough bars loaded on your chart.
    Let me know if I can be of further assistance.
    Last edited by NinjaTrader_Lance; 11-06-2012, 05:01 PM.
    LanceNinjaTrader Customer Service

    Comment


      #3
      OK I thought of that too, just forgot to do it.

      However, that does not fix my problem. It still stops printing text when I take off the comments.

      Does it matter that I'm using 5 range bar chart on CL?

      Comment


        #4
        I've tried opening a new CL 6 range OHLC bar chart with 5 days data.

        The text prints when I comment out the while loop. The text DOES NOT print when the while loop is active.

        Please try this experiment and tell me what you learn.

        Comment


          #5
          Hello,

          To verify that I'm not missing a separate while loop this is the section you are referring to correct?

          Can you check both your Output Window and log tab for errors?
          Open[nBarAgo] > Close[nBarAgo] might be causing index out of range error

          Code:
                          while( (nSMAMoves <= 1) && (Open[nBarAgo] > Close[nBarAgo]) && (nBarAgo < 7) )
                          {
                              // down close n
                              nDownCloseBars++;
          
                              if( Falling( SMA(5) ) )
                              {
                                  // we fell from prev bar
                                  nSMAMoves++;
                              }
                              
                              nBarAgo++;
          
                          }
          I've attached an image of what I see with it commented (as sent in your original file) /uncommented (as above).

          Neither way is printing to the Output Window for me.

          Please ensure we are both using the exact same file you attached.

          I look forward to assisting you further.
          Attached Files
          LanceNinjaTrader Customer Service

          Comment


            #6
            Yes, you have the code correct.

            There are no errors in log or output windows.

            The only difference I see is that you are using 5range candle sticks.

            Here's my screen shot running with the loop/
            Attached Files

            Comment


              #7
              WAIT:

              Strike that, reverse it...

              here is the error in Output:

              Error on calling 'OnBarUpdate' method for indicator 'WhileLoopErr' on bar 1: Object reference not set to an instance of an object.

              I have "BarsRequired = 20;" in Initialize(); What is causing this?

              Comment


                #8
                I've added:
                if( CurrentBar < 20 )
                return;
                to OnBarUpdate();

                How come "BarsRequired = 20; " didn't work?

                Comment


                  #9
                  Hello,

                  Originally posted by TiggerTrader View Post
                  I've added:
                  if( CurrentBar < 20 )
                  return;
                  to OnBarUpdate();

                  How come "BarsRequired = 20; " didn't work?
                  You just beat me to it. Glad you were able to figure it out. Bars Required acts differently between strategies and indicators.

                  With indicators: The number of bars on a chart required before the indicator plots. This is NOT the same as a minimum number of bars required to calculate the indicator values. http://www.ninjatrader.com/support/h...rsrequired.htm

                  For using indicators you are required to check the current bar
                  http://www.ninjatrader.com/support/h...currentbar.htm

                  With Strategies:The number of historical bars required before the strategy starts processing calls to the OnBarUpdate() method. This property is generally set via the UI when starting a strategy. http://www.ninjatrader.com/support/h...srequired2.htm

                  Please let me know if I can be of further assistance.
                  LanceNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by dsfhbrejej, Today, 04:44 PM
                  0 responses
                  1 view
                  0 likes
                  Last Post dsfhbrejej  
                  Started by pserrera, Today, 04:29 PM
                  0 responses
                  2 views
                  0 likes
                  Last Post pserrera  
                  Started by dsfhbrejej, Today, 03:54 PM
                  0 responses
                  5 views
                  0 likes
                  Last Post dsfhbrejej  
                  Started by briteusa, Today, 03:53 PM
                  0 responses
                  4 views
                  0 likes
                  Last Post briteusa  
                  Started by kathreynuliex, Today, 03:00 PM
                  0 responses
                  3 views
                  0 likes
                  Last Post kathreynuliex  
                  Working...
                  X