• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Receiving OnBarUpdate for multiple instruments

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

    Receiving OnBarUpdate for multiple instruments

    Hello,

    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,

    Libor

    #2
    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.
    http://ninjatrader.com/support/helpG...arsrequest.htm

    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

    Comment


      #3
      Hi,

      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,

      Libor

      Comment


        #4
        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.

        http://ninjatrader.com/support/helpG...arsrequest.htm
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          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);
          i++;
          }

          //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
          //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

          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,

          Libor
          Attached Files

          Comment


            #6
            Hello Libor,

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

            (edit)
            I'm also including a link to an example in an indicator with a bars request of a secondary instrument.
            https://ninjatrader.com/support/foru...802#post455802
            Attached Files
            Last edited by NinjaTrader_ChelseaB; 04-23-2018, 07:44 AM.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi,

              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.

              Comment


                #8
                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

                Comment


                  #9
                  Hi, I am trying to combine the multiinstruments-sample to a AddOn with tabs.
                  Therefor this code:
                  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

                  Comment


                    #10
                    Hello GoS,

                    Please take a look at an example that detects tab changes. This may be a partial if not complete solution.
                    http://ninjatrader.com/support/forum...182#post502182
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by pmmkm, Yesterday, 11:13 PM
                    0 responses
                    3 views
                    0 likes
                    Last Post pmmkm
                    by pmmkm
                     
                    Started by pulpiwapi, 03-04-2019, 11:18 PM
                    11 responses
                    149 views
                    0 likes
                    Last Post music_p13  
                    Started by indy73, 06-25-2019, 10:52 PM
                    3 responses
                    20 views
                    0 likes
                    Last Post sledge
                    by sledge
                     
                    Started by priceisking, Yesterday, 12:51 PM
                    1 response
                    23 views
                    0 likes
                    Last Post NinjaTrader_PaulH  
                    Started by chartish, Yesterday, 10:33 AM
                    3 responses
                    30 views
                    0 likes
                    Last Post chartish  
                    Working...
                    X