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

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 bortz, 11-06-2023, 08:04 AM
                    47 responses
                    1,602 views
                    0 likes
                    Last Post aligator  
                    Started by jaybedreamin, Today, 05:56 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post jaybedreamin  
                    Started by DJ888, 04-16-2024, 06:09 PM
                    6 responses
                    18 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by Jon17, Today, 04:33 PM
                    0 responses
                    4 views
                    0 likes
                    Last Post Jon17
                    by Jon17
                     
                    Started by Javierw.ok, Today, 04:12 PM
                    0 responses
                    12 views
                    0 likes
                    Last Post Javierw.ok  
                    Working...
                    X