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

Printing only the last value

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

    Printing only the last value

    Hi.

    On the chart I want to count the times when the close is greater than the open and then print only the final result on the output window.

    Below is my code:
    if (Close[0] > Open[0])
    {
    CountEvent = CountEvent + 1;
    }
    Print(CountEvent);

    This way on the output window I have each string for any event that is true, but I only need the print of the last value. How can I do?

    Thanks for any answer.

    #2
    Code:
    if (Close[0] > Open[0])
    {            
         CountEvent = CountEvent + 1;                
    }    
    if (!Historical)
         Print(CountEvent);
    if (!Historical) checks to see if the bar is a historical bar or a real time bar. If it is a historical bar it will ignore the print command. If it is a real time bar then it will do the print.

    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Is there any command to do the same in both realtime and historical?

      Comment


        #4
        When you say "Final" that implies that there is no more real-time data coming in and thus the chart has a finite number of bars. So in real-time, your "final" number is growing by 1 on each new bar where your condition is met.
        RayNinjaTrader Customer Service

        Comment


          #5
          There are a couple of problems with checking for "!Historical". First, it won't work if you limit the time range to only backtest data (e.g., June 1 to June 30), and also it won't work if you're not currently connected to a datafeed.

          Try this instead:
          if (CurrentBar == Count) then Print CountEvent;
          Well I thought that would print the last bar's total... but it didn't.

          I added the following statement to try to debug this:
          Print(String.Format( "CurrentBar={0}/Count={1}: CountEvent={2} Historical={3}", CurrentBar, Count, CountEvent, Historical ));
          however, the display I get stops one bar shy of the total (Count). For example...
          CurrentBar=0/Count=2281: CountEvent=1 Historical=True
          CurrentBar=1/Count=2281: CountEvent=1 Historical=True
          ...
          CurrentBar=2278/Count=2281: CountEvent=837 Historical=True
          CurrentBar=2279/Count=2281: CountEvent=838 Historical=True
          <then it stops>
          Its only displaying 2280 bars, yet Count is 2281. I've tried this with several different instruments and time frames, but it always stops one bar short of Count.

          So, is the documentation wrong on this?

          Best regards,

          KBJ



          Last edited by KBJ; 10-01-2007, 07:17 PM.

          Comment


            #6
            CurrentBar is zero based where Count is 1 based. This means that:

            Count == CurrentBar + 1
            RayNinjaTrader Customer Service

            Comment


              #7
              Ray,

              Thanks for looking at this.

              The problem that I see is that for the last time that OnBarUpdate is called, Count == CurrentBar + 2, so it never gets to the last bar, and never gets as far as Count == CurrentBar + 1.

              Please look at my example, again. This is very easy to reproduce.

              Best regards,

              KBJ

              Comment


                #8
                This is the case when you have CalculateOnBarClose set to true. Because NinjaTrader will not act till it knows the bar is closed you are essentially one bar behind with CurrentBar. This behaves this way because everything you do at the end of, say bar 5, is done actually at the very beginning of bar 6. Pragmatically, this is how you would want it to behave if CalculateOnBarClose is true because you want to reference the bar that just finished. You could set CalculateOnBarClose to false if this is not how you desire your calculations to be done.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  CalculateOnBarClose=False: Due to NT internal, multi threaded architecture there is no guarantee that
                  Code:
                  CurrentBar == Bars.Count - 1
                  However, it's guaranteed that
                  Code:
                  CurrentBar <= Bars.Count - 1
                  Reason: OnBarUpdate is called once per tick. However, as OnBarUpdate is triggered there might be more inflight ticks which already have been added to the bars series (in a different thread).

                  Comment


                    #10
                    Josh & Dierk,

                    Thanks. Mystery solved.

                    I'd coded it checking for Count-1 in prior strategies, but could never figure out why it had to be that way. Now I know.

                    Perhaps you've done this already, but if not, may I suggest updating the descriptions of CurrentBar and Count in the help to include this valuable data?

                    Best regards,

                    KBJ

                    Comment


                      #11
                      Thanks for the suggestion. I will ammend the documentation for NT 6.5.
                      RayNinjaTrader Customer Service

                      Comment


                        #12
                        Hello.
                        I coded a simple indicator to print a specified value only on the last bar of the chart. Nevertheless the trouble is that the indicator is always returning "true" on my parameter in spite of the code where "false" is indicated.
                        At the bottom of the code I programmed an if statement which make the choice to add 1 or 2 to the currentbar to make it of the same value of "Count".

                        How can it be?

                        Code:
                        protected override void Initialize()
                                {
                                    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Bar, "value1"));
                        			
                                    CalculateOnBarClose	= false;
                                    Overlay = false;
                                    PriceTypeSupported	= false;
                        			
                                }
                        
                        
                        protected override void OnBarUpdate()
                                {
                        
                        
                        if (CalculateOnBarClose = true)
                        			{
                        			BarstoAdd = 2;
                        			}
                        			else if (CalculateOnBarClose = false)
                        			{	
                        			BarstoAdd = 1;	
                        			}	
                        				
                        			
                        			if (CurrentBar + BarstoAdd == Count) 
                        			Print("value1" + " " + CountPatterns + " " + CurrentBar+ " " + Count);
                        }

                        Comment


                          #13
                          Max,

                          Because you keep setting the value to true.

                          if (CalculateOnBarClose = true)

                          Should be

                          if (CalculateOnBarClose == true)
                          RayNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by GussJ, 03-04-2020, 03:11 PM
                          11 responses
                          3,221 views
                          0 likes
                          Last Post xiinteractive  
                          Started by andrewtrades, Today, 04:57 PM
                          1 response
                          10 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by chbruno, Today, 04:10 PM
                          0 responses
                          6 views
                          0 likes
                          Last Post chbruno
                          by chbruno
                           
                          Started by josh18955, 03-25-2023, 11:16 AM
                          6 responses
                          437 views
                          0 likes
                          Last Post Delerium  
                          Started by FAQtrader, Today, 03:35 PM
                          0 responses
                          9 views
                          0 likes
                          Last Post FAQtrader  
                          Working...
                          X