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

Problem with bar numbers

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

    Problem with bar numbers

    Hi friends

    I've tried everything - I just can't get this right!

    Let's say there's a value A that I can only define with:

    Code:
    if (CurrentBar < Count - 2)
    return;
    (It depends on properties back from the most recent completed Renko bar.)

    However, I need to use this value in a Plot which later in the code is defined over a range, say (Count - 20) to (Count - 10).

    The problem seems to be the obvious one: as I've defined it with CurrentBar < Count - 2, the value doesn't seem to be recognized over the 'earlier' range.

    Also, is it best to define this value as double A in Variables or in OnBarUpdate?

    I hope I've made myself clear and thanks in advance for any help with this.

    #2
    HI arbuthnot,

    I'm not clear on what you are trying to do.

    Are you trying to set something in historical data?

    If so, set the int historicalCount in #region Variables.

    Then in OnStartUp() set historicalCount to Count.

    Then in OnBarUpdate this won't grow as you proceed forward.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks, Chelsea, for your very rapid response. Much appreciated!

      I've simplified the problem down to its bare essentials and I've now got a 'toy' code as follows:

      Code:
       #region Variables
      		
      		double A;
      		
      		#endregion
      
      
              protected override void Initialize()
              {
                  Add(new Plot(new Pen(Color.Blue, 6), PlotStyle.Line, "Plot0"));
      			Overlay	= true;
              }
      
      
              protected override void OnBarUpdate()
              {
      
      			if(CurrentBar == Count - 2)
      				
      				[B][COLOR="Blue"]A = (High[10] + High[20])/2[/COLOR][/B];
      				
      			if(
      				CurrentBar < Count - 12
      				&& CurrentBar >= Count - 22
      				)
      				
      			Plot0.Set([B][COLOR="blue"]A[/COLOR][/B]); [B][COLOR="DarkGreen"]// This comes out as zero.[/COLOR][/B]
      You'll see from the attached image that the range of the plot is right but the value of A is set to zero.

      Can you suggest a way of setting A in the plot to the correct value?

      Much obliged.
      Attached Files

      Comment


        #4
        Hello arbuthnot,

        Add prints to your code one line above where the value is used.

        Print(CurrentBar+" == "+(Count-2));

        Print(High[10]: "+High[10]+" - High[20]: "+High[20]+" - (High[10] + High[20])/2: "+((High[10] + High[20])/2));

        Print(CurrentBar+" < "+(Count - 12)+" && "+CurrentBar+" >= "+(Count - 22));

        Also, have you tried my suggestion?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks again, Chelsea.

          Do forgive me: I'm 7 hours ahead of Denver and it's getting lllllllllllllllllate !!!

          However, I've amended the code (without the prints) as follows and it still doesn't seem to work:

          Code:
          #region Variables
          		
          		double A;
          		
          		int historicalCount;
          		
          		#endregion
          
          
                  protected override void Initialize()
                  {
                      Add(new Plot(new Pen(Color.Blue, 6), PlotStyle.Line, "Plot0"));
          			Overlay	= true;
                  }
          
          
                  protected override void OnBarUpdate()
                  {
          			
          historicalCount = Count;
          			
          			if(CurrentBar == historicalCount - 2)
          				
          				A = (High[10] + High[20])/2;
          				
          			if(
          				CurrentBar < historicalCount - 12
          				&& CurrentBar >= historicalCount - 22
          				)
          				
          			Plot0.Set(A);
          It might be because I'm too tired but have I amended the code as per your suggestions? Do you have any further ideas?

          I'm probably hitting the hay pretty soon and thanks again.

          Comment


            #6
            Hello arbuthnot,

            My suggestion was to place historicalCount = Count; in OnStartUp where it will only be run once.

            Currently, what you have is the exact same code using different variable names.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Again, Chelsea, much obliged.

              I simply didn't 'register' the OnStartUp reference in your original post. Sorry!

              This is how I've amended the code in light of this - but it still doesn't do the trick:

              Code:
              #region Variables
              		
              		double A;
              		
              	[COLOR="Blue"][B]	int historicalCount;[/B][/COLOR]
              		
              		#endregion
              
              
                      protected override void Initialize()
                      {
                          Add(new Plot(new Pen(Color.Blue, 6), PlotStyle.Line, "Plot0"));
              			Overlay	= true;
                      }
              
              [B][COLOR="blue"]		protected override void OnStartUp()
              		{
              			historicalCount = Count;
              		}[/COLOR][/B]
              
                      protected override void OnBarUpdate()
                      {
              			
              			if(CurrentBar == historicalCount - 2)
              				
              				A = (High[10] + High[20])/2;
              				
              			if(
              				CurrentBar < historicalCount - 12
              				&& CurrentBar >= historicalCount - 22
              				)
              				
              			Plot0.Set(A);
              I remember that. about a year or so ago, you introduced me to the OnStartUp method and it certainly solved that problem!

              I'll be turning in now but if you have any further suggestions, they will be, as always, much appreciated.

              Comment


                #8
                Hello arbuthnot,

                Thank you for your response.

                Try printing the value when it is set:
                Code:
                			if(CurrentBar == historicalCount - 2)
                                        {				
                				A = (High[10] + High[20])/2;
                				Print(A);
                				}

                Comment


                  #9
                  Thanks very much, Patrick.

                  I included the print commands you and Chelsea suggested.

                  I think the fundamental problem here is this:

                  I need to access data on the extreme right of the chart, calculated, in a generalized form, as follows:

                  if(CurrentBar == Count - 2)

                  A = whatever formula


                  The essential problem seems to be that it doesn't appear possible to access this calculation for further calculation on previous bars (which is what I've been trying to do).

                  (I don't need access to this information in printed form.)

                  When processing an indicator, NT scrolls through every bar, going from left to right on the chart, and there doesn't seem a way for an indicator to feed information obtained on the right edge of the chart back to previous bars.

                  It seems that I may be asking too much of NT but is there a way round this? Maybe using a second indicator to reference this one after all the bars are processed.

                  Any further suggestions will be much appreciated.

                  Comment


                    #10
                    Originally posted by arbuthnot View Post
                    ... When processing an indicator, NT scrolls through every bar, going from left to right on the chart, and there doesn't seem a way for an indicator to feed information obtained on the right edge of the chart back to previous bars.
                    You can refer to historical bars, and you can change anything that is held in an indicator relative to said bars. Maybe if you explained in words what you are trying to do on those historical bars? I cannot make head or tail of the math that you have put down so far.

                    Comment


                      #11
                      Hello arbuthnot,

                      You do need this information in printed form so that you can visually ensure that the value is correct.

                      From how I understand this, you want something calculated on the last historical bar is this correct?

                      (Count-2) this is the last historical bar correct?
                      A = (High[10] + High[20])/2; is calculated on the last historical bar, correct?

                      And then for all bars previous to the 9th bar before the last historical bar and for all bars that are at least 12 bars after the last historical bar, you would like to set a value, is this correct?

                      For all bars previous to the 9th bar, the A will not be calculated yet, as this happens on the last historical bar. So this will be set with a 0.

                      For all bars 12 bars after the lasta historical bar, this should be set to whatever value was calculated on the last historical bar.
                      Last edited by NinjaTrader_ChelseaB; 03-02-2015, 01:36 PM.
                      Chelsea B.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by trilliantrader, 04-18-2024, 08:16 AM
                      6 responses
                      26 views
                      0 likes
                      Last Post trilliantrader  
                      Started by arvidvanstaey, Yesterday, 02:19 PM
                      5 responses
                      14 views
                      0 likes
                      Last Post NinjaTrader_Zachary  
                      Started by Rapine Heihei, Yesterday, 08:25 PM
                      1 response
                      12 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Started by Mongo, Yesterday, 11:05 AM
                      6 responses
                      27 views
                      0 likes
                      Last Post Mongo
                      by Mongo
                       
                      Started by ct, 05-07-2023, 12:31 PM
                      7 responses
                      207 views
                      0 likes
                      Last Post NinjaTrader_BrandonH  
                      Working...
                      X