Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

detect last bar?

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

    detect last bar?

    Is there a way to run code ONLY on the very last bar on a chart?

    I've printed Bars.Count & CurrentBar and they differ by 2-3 -- the relationship does not seem fixed.

    #2
    Hello,

    Did you visit this site:


    It tells you about the relationship.

    Do you consider the current bar to be the "last bar"? If so, use !Historical or if(Historical) return; :
    DenNinjaTrader Customer Service

    Comment


      #3
      Well "if (Historical) return;" will stop the code from running on ALL the bars --
      in cases where I am connected to a data feed but the session is closed or slow (no incoming ticks), there is no output on the very last bar either.

      If that is the best I can get, let me know. But it makes it hard to run tests after trading hours or on week-ends -- I would like the final result to calculate at those times as well.

      Any other suggestions?

      Comment


        #4
        Hello,

        Well "if (Historical) return;" will stop the code from running on ALL the bars --
        in cases where I am connected to a data feed but the session is closed or slow (no incoming ticks), there is no output on the very last bar either.

        >>Yes, that is why I mentioned "do you consider the last bar to be the current bar", meaning the building bar. I see what you want now.

        If that is the best I can get, let me know. But it makes it hard to run tests after trading hours or on week-ends -- I would like the final result to calculate at those times as well.

        Any other suggestions?

        >>I suggest comparing the current time with the time stamp of the last bar on the chart. If the current time is >= than the last bar time stamp you have the last bar on the chart. Try it and post your code and I'll help if needed.
        DenNinjaTrader Customer Service

        Comment


          #5
          Thanks, Ben.

          I also tried using " if (CurrentBar >= Bars.Count-3) "

          thinking if (CalculateOnBarClose = true then the last bar would be Bars.Count-2;
          or if (CalculateOnBarClose = false then the last bar would be Bars.Count-1

          but sometimes there was no output, until I changed it to 3. As I said the last bar # did not seem consistent even when the Bar count was unchanged.

          I'll keep playing.

          Comment


            #6
            I had it done a few days ago but dumped the code and forgot it.

            Comment


              #7
              Hello,

              The number of bars (Count) should not change regardless of the CalculateOnBarClose setting. Count should be consistant also. If it is not, please show me exactly how you are determining this and I will test it.
              DenNinjaTrader Customer Service

              Comment


                #8
                Here you go.

                Please note that this trick only works with CalculateOnBarClose=true

                if you find one with CalculateOnBarClose=false, I'm interested...


                Code:
                if(Bars.LastBarOfSession && Time[0].Date.CompareTo(DateTime.Now.Date)==0)

                Comment


                  #9
                  SARdynamite, thanks for the code, I'll play with it. It is odd but I cannot find Bars.LastBarOfSession discussed in the help files.

                  Ben, when I want to know what a value is, I print it to the output window. In this case, it is very simple:

                  msg = "Statistics: Bar Count = " + Bars.Count;
                  msg += " & cb = " + CurrentBar + " Price = " + Close[0] ;
                  Print(Time[0] + " : " + msg);

                  The Bars.Count was always constant (after the session closed for the week-end) but on the last line printed, the CurrentBar tended to stop on slightly different numbers, when re-applying indicators for example.

                  Comment


                    #10
                    This is it.

                    Initialize

                    Code:
                    private DateTime myDateTime;
                    OnBarUpdate

                    Code:
                    [COLOR=#000000][COLOR=#007700]if (Bars.LastBarOfSession && [/COLOR][COLOR=#0000bb]myDateTime[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Date[/COLOR][COLOR=#007700]==[/COLOR][COLOR=#0000bb]DateTime[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Now[/COLOR][COLOR=#007700].[/COLOR][COLOR=#0000bb]Date[/COLOR][COLOR=#007700])  [/COLOR][/COLOR]

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    595 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