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


Receiving OnBarUpdate for multiple instruments

  • Filter
  • Time
  • Show
Clear All
new posts

    Receiving OnBarUpdate for multiple instruments


    I am trying to modify the AddOn Framework sample to allow me to submit framework managed orders for multiple instruments.

    I can't figure out how to manage them simultaneously so that I could use the OnBarUpdate event for any and all of them, and e.g. adjust profit targets and stop losses for them independently.

    I would be grateful for any pointers.

    Thank you very much,


    Hello liborc,

    What type of script are you creating?

    The Addon class does not have an OnBarUpdate but it is possible to do a BarsRequest in an Addon to get data and add a method handler.

    Are you instead making a NinjaScript Strategy?

    Have you added the additional series to the script using AddDataSeries?
    (Are you trying to submit an order to a different instrument without loading data for it?)
    Chelsea B.NinjaTrader Customer Service



      I am creating an add on. In the simplest form, I would like to start off by modifying the Add-On Framework example. In that code, you guys implemented the OnBarUpdate event handler (around line 1329). I would like to modify this sample to handle more than just one instrument.

      I have figured out how to submit orders for multiple instruments by creating a new instrument by using NinjaTrader.Cbi.Instrument.GetInstrument()

      However, the OnBarUpdate event on line 1329 seems to handle only bar updates for the currently selected instrument and I would like to know how to make it fire for all of the instruments I create.

      I hope that makes sense.

      Thank you,



        Hi Libor,

        I wish that dave had chosen a different name for his example to be clear that the OnBarUpdate method in the Addon frame work is not a NinjaScript overloaded method but simply a custom method he happened to call OnBarUpdate (because there isn't already an OnBarUpdate in the Addon namespace) and was added as an event handler to the barsRequest.Update event.

        In other words, my answer is the same. Do a BarsRequest for each instrument you would like data for and create a method to handle the BarsRequest.Update event.
        Chelsea B.NinjaTrader Customer Service


          Hello Chelsea,

          thank you for the quick answer, It makes it much clearer to know it is a name coincidence rather than function overload.

          I have managed to move forward, but am not much of a programmer and have been struggling. Maybe if I explain what I am trying to achieve, you will be kind enough to provide me with more pointers.

          So: My strategy consists in handpicking stock (potentially also other types of instruments in the future), and placing limit orders at levels I calculate up front. I end up with a few dozens of tickers every day, and I would like to automize the order entry: entry and all sorts of exits (stop loss, profit target, trailing stops). I am not really planning on using ATMs.

          I save the tickers in a MySQL database, along with the entry price, stop loss, and profit target prices.

          What I am doing using much of the code from the Add On Framework sample is:

          1) load the ticke list of the day from MySQL into the add-on
          2) create an array of instruments, fetch the marketData objects for them. Then do the barsRequest:

          int daysToLookBack = 10;
          NinjaTrader.Cbi.Instrument[] thisInstrument = new NinjaTrader.Cbi.Instrument[8];
          NinjaTrader.Data.MarketData[] thisMarketData; // = new NinjaTrader.Data.MarketData(thisInstrument[0]);
          thisMarketData = new MarketData[8];

          int i = 0;

          foreach (DataRowView item in dgData.Items) //dgData is just a listbox I am iterating through

          thisInstrument[i] = NinjaTrader.Cbi.Instrument.GetInstrument(item.Row. ItemArray[1].ToString());
          thisMarketData[i] = new MarketData(thisInstrument[i]);
          thisMarketData[i].Update += OnMarketData;

          if (thisBarsRequest[i] != null)
          if (thisBarsRequestSubscribed[i])
          thisBarsRequest[i].Update -= OnBarUpdate;
          thisBarsRequestSubscribed[i] = false;
          thisBarsRequest[i] = null;
          thisBarsRequest[i] = DoBarsRequest(thisInstrument[i], daysToLookBack, true);


          What I am struggling with is how to attach a real-time event handler for each one of the instruments in my instrument array.

          I am also attaching the export of my (very messy) add-on code.

          Thank you very much again, best,

          Attached Files


            Hello Libor,

            I've put together an example in an addon that adds bars requests for multiple instruments and prints to the output window.

            I'm also including a link to an example in an indicator with a bars request of a secondary instrument.

            (update May 5th, 2020 - request to update the future instruments to 06-20. On line 130 feel free to change the symbolsList to any instruments you are currently receiving real-time data for.)
            Attached Files
            Last edited by NinjaTrader_ChelseaB; 05-05-2020, 11:52 AM.
            Chelsea B.NinjaTrader Customer Service



              first, big thanks for great supporting us with samples for AddOn development. I really work a lot with them.
              When working through the above sample, I found an error at the DoBarsRequest() method:
              There is int i = 0;
              and the usage of i for barsRequests[i]
              but some i++; is missing.
              This causes the effect:
              From my testlist: symbolsList = new string[] {"EURUSD", "EURCHF", "AUDCAD", "AUDCHF", "AUDCNH"};
              the request of EURCHF and AUDCHF is done, the others not ?!?
              With i++; at line 165 your samplecode runs without error.


                Hello GoSPvC,

                Thank you for noting that and letting me know about it.

                I've changed the script and updated my post.
                Chelsea B.NinjaTrader Customer Service


                  Hi, I am trying to combine the multiinstruments-sample to a AddOn with tabs.
                  Therefor this code:
                  Closed += (o, e) =>
                                  // Clean up handlers (from NT-DoBarsRequest-Sample):
                                  if (barsRequests != null)
                                      foreach (BarsRequest barRequest in barsRequests)
                                          if (barRequest != null)
                                              barRequest.Update -= OnBarUpdate;
                  has to move to the constructor of the Tab, but here is no "Closed" available.
                  Have I to put it to a destructor instead, or what is the correct way ??

                  Thanks, GoS


                    Hello GoS,

                    Please take a look at an example that detects tab changes. This may be a partial if not complete solution.
                    Chelsea B.NinjaTrader Customer Service


                    Latest Posts


                    Topics Statistics Last Post
                    Started by Bullero, Today, 11:37 AM
                    1 response
                    Last Post NinjaTrader_Spencer  
                    Started by Ousher, Today, 09:01 AM
                    1 response
                    Last Post NinjaTrader_Jesse  
                    Started by nepenthean, Today, 08:23 AM
                    1 response
                    Last Post NinjaTrader_JustinD  
                    Started by Adam3123, 08-12-2019, 08:33 PM
                    37 responses
                    1 like
                    Last Post NinjaTrader_PatrickG  
                    Started by WHICKED, Yesterday, 11:12 PM
                    1 response
                    Last Post NinjaTrader_Jim