Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Code on the current bar only?

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

    Code on the current bar only?

    Hi,

    This is a very simple question. When I code a strategy, for example, the OnBarUpdate() starts from the left most bar and does all the commands until the right most bar. I want to run commands only on the current/active bar, not on any of the historical bars. How can I do that?

    In pseudo code, I imagine it should look something like this:

    OnBarUpdate()

    if (Current_Bar) {
    do stuff
    }

    Thus, I want to "do stuff" only on the right most bar of the graph, which is the current active bar, not on any historical bars.
    Thanks,
    Matt

    #2
    Hi Matt, you can use the Historical property (http://www.ninjatrader.com/support/h...historical.htm) to skip over all the non-realtime bars.
    Code:
    OnBarUpdate()
    {
       if (Historical) return; // this skips all historical bars
       // rest of your code goes here
    }
    Please let me know if you have any other questions.
    AustinNinjaTrader Customer Service

    Comment


      #3
      So this seems to be exactly what I want, but it doesn't seem to be working. I am currently using the Simulated Data Feed. Will this cause a problem? If so, how do I get around this?

      Comment


        #4
        Hi again, what do you mean by "it doesn't seem to be working"? How are you testing? I just ran a test on the Sim Feed and didn't have any issues. Did you recompile your script (right-click the code -> compile) after making the changes?
        Attached Files
        AustinNinjaTrader Customer Service

        Comment


          #5
          Hi. OK, so I played around with my code. What you told me works only when I comment out the Initialize() block, which I have pasted below. It doesn't make sense. Why?

          protected override void Initialize()
          {
          Print("Initialization");
          SetProfitTarget("", CalculationMode.Ticks, TickProfitTarget);
          SetStopLoss("", CalculationMode.Ticks, TickStopLoss, false);

          CalculateOnBarClose = false;
          Add(PeriodType.Minute, 60);
          Print("End initialization");
          }

          Comment


            #6
            Hi matt3m,

            Commenting out the Initialize() block is not the solution here.

            What issue are you having exactly?

            Are you having trouble compiling? What messages?

            Does Historical property not work like you expect? What are you expecting?
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Hi.

              Well it turns out that it was this line

              Add(PeriodType.Minute, 60);

              that was causing the problem. It also turns out that I don't actually need that line, so I just deleted it, and now everything is working as it should.

              Comment


                #8
                OK, thanks for letting us know. Glad to hear it's working the way you expect.

                That line is for multiseries script, and may require additional coding to work it successfully into your script. If you apply an indicator to your chart but don't see anything, check log tab of control center for any error messages.

                Multiseries documentation is available here:
                Ryan M.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                583 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                339 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
                554 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                552 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X