Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multi instrument strategy question

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

    #16
    Hello,

    You would just need to use exactly what you have now, only changing the overloads you are using in the Enter statements.

    For example, you currently have this:

    EnterShort(voladjust1, "SP STOCKS U-H Short");

    Whichever stock this is for, you would need to specify its index using the altranet overload set I had mentioned before or:

    EnterShort(barsInProgressIndex, voladjust1, "SP STOCKS U-H Short");

    You would replace barsInProgressIndex with the index number for the instrument you are trying to submit the order to.

    Here is the overload set again for reference:

    EnterShort(int barsInProgressIndex, int quantity, string signalName)

    Looking at this, when you call EnterShort() the items inside the ( ) would need to match what is listed or the index of the instrument, the quantity and then the signal name.


    I look forward to being of further assistance.

    Comment


      #17
      is this what you mean?

      // so I have done the following...; remember this same condition will be applied to all 25 stocks.

      if (Position.MarketPosition == MarketPosition.Flat
      && PriorDayOHLC().PriorOpen[0] < PriorDayOHLC().PriorClose[0]
      && (CurrentDayOHL().CurrentOpen[0] - PriorDayOHLC().PriorClose[0]) > (DailyATR(5)[0]*0.1)
      && ((CurrentDayOHL().CurrentOpen[0] - PriorDayOHLC().PriorClose[0]) <= (DailyATR(5)[0]*0.3))
      && CurrentDayOHL().CurrentOpen[0] > PriorDayOHLC().PriorHigh[0]
      && WilliamsR(10)[1]>-50
      && CurrentDayOHL().CurrentOpen[0] > EMA(10)[0]
      && Bars.FirstBarOfSession

      && Bars.TickCount <1000)

      {
      EnterShort(0,voladjust1, "SP STOCK 1 U-H Short");
      EnterShort(1,voladjust1, "SP STOCK 2 U-H Short");
      EnterShort(2,voladjust1, "SP STOCK 3 U-H Short");
      EnterShort(3,voladjust1, "SP STOCK 4 U-H Short");
      EnterShort(4,voladjust1, "SP STOCK 5 U-H Short");
      EnterShort(5,voladjust1, "SP STOCK 6 U-H Short");
      EnterShort(6,voladjust1, "SP STOCK 7 U-H Short");
      EnterShort(7,voladjust1, "SP STOCK 8 U-H Short");
      EnterShort(9,voladjust1, "SP STOCK 9 U-H Short");
      EnterShort(10,voladjust1, "SP STOCK 11 U-H Short");
      EnterShort(11,voladjust1, "SP STOCK 12 U-H Short");
      EnterShort(12,voladjust1, "SP STOCK 13 U-H Short");
      EnterShort(13,voladjust1, "SP STOCK 14 U-H Short");
      EnterShort(14,voladjust1, "SP STOCK 15 U-H Short");
      EnterShort(15,voladjust1, "SP STOCK 16 U-H Short");
      EnterShort(16,voladjust1, "SP STOCK 17 U-H Short");
      EnterShort(17,voladjust1, "SP STOCK 18 U-H Short");
      EnterShort(19,voladjust1, "SP STOCK 19 U-H Short");
      EnterShort(20,voladjust1, "SP STOCK 20 U-H Short");
      EnterShort(21,voladjust1, "SP STOCK 21 U-H Short");
      EnterShort(22,voladjust1, "SP STOCK 22 U-H Short");
      EnterShort(23,voladjust1, "SP STOCK 23 U-H Short");
      EnterShort(24,voladjust1, "SP STOCK 24 U-H Short");
      }
      Last edited by elliot5; 05-27-2015, 10:23 AM. Reason: update

      Comment


        #18
        Hello,

        Thank you for the reply.

        If this is still not working it is likely due to what you have set as the EntriesPerDirection.

        These are unique orders as they have different signal names, but you have 1 entry per direction specified. If this is to send an order for All instruments at once, you would need to increase the entries per direction to accommodate the amount of instruments or unique entries that will be used.


        I look forward to being of further assistance.

        Comment


          #19
          e

          I will know if it works tomorrow. Am I using the correct code in general here though? Those simple conditions should be tested on 25 different stocks and then executed on those that fit the criteria....




          Also the voldivide function to adjust the size... would that be calculated individually for each member of the array? Or will it take the calculated size for the primary bar series and apply it to all members of the array? Do i have to calculate the voldivide variable individually for each of the members of the array?
          Last edited by elliot5; 05-27-2015, 11:04 AM. Reason: update

          Comment


            #20
            Hello,

            Yes the usage seems correct so long as the condition becomes true and you want to submit this to multiple instruments at once.

            For the voldivide, I do not see a paste of that so I am unsure what it is doing currently.

            By default, if you have not specified a BarsInProgressIndex on any data series you are using it would use the primary, for example if you are getting the Close price, Close[0] would be the primary where Closes[1][0] would be the secondary.

            For other things like TickSize(if you are using this), you would need to instead use Instruments or:

            Instruments[1].MasterInstrument.TickSize

            I look forward to being of further assistance.

            Comment


              #21
              Just remember that I am trying to run that condition for each stock individually...and execute only those that fit. I havent mentioned any array member or bars in progress for those basic conditions... will it use the indicators for the other members of the array when calculating their conditions?

              Basically does the ADD function mean that NT reads conditions and can apply them in a kind of loop across an array ? or will I need to stipulate the barsindex in separate replicate condition sets?





              U guys really really need to put up a very simple example multi instrument strategy for conditions to be applied across multiple stocks. Regards
              Last edited by elliot5; 05-27-2015, 11:26 AM.

              Comment


                #22
                Hello,

                Because DailyATR is not a standard indicator I can not tell you exact instruction, but this would be using the charts series how you have it now.

                This most likely performs like the other indicators where you can provide a series, you can test this using the NinjaScript editor.

                For example for a primary series you would use
                SMA(12)
                for a secondary you would use
                SMA(Closes[1], 12)

                If that indicator has a IDataSeries as one of its overloads, you can do this by using the above way. it would most likely look like:

                DailyATR(Closes[1], 5)[0]

                I look forward to being of further assistance.

                Comment


                  #23
                  How to reference the barsinprogressindex on indicator

                  How would you reference for example the 3rd data series on these two indicators?


                  CurrentDayOHL().CurrentOpen[0]
                  EMA(10)[0]

                  Comment


                    #24
                    Hello,

                    For any standard indicator or item that comes with NinjaTrader, you can refer to the help guide page for that indicator to see the multiple ways it can be used.

                    CurrentDayOHL


                    EMA


                    For these if you look in the documentation, we can see the CurrentDayOHL for example:

                    Returns current session open value
                    CurrentDayOHL().CurrentOpen[int barsAgo]
                    CurrentDayOHL(IDataSeries input).CurrentOpen[int barsAgo]
                    These are two of the Overload sets that can be used, either ( ) no overloads, or (IDataSeries input).

                    This means we can pass any DataSeries or indicator to this, so instead of just calling CurrentDayOHL(), you would put the data series you want to use inbetween the ( ).

                    Lets assume you wanted the Second series from the Add statements to be used, the DataSeries would be index 1 so the statement would look like the following:

                    CurrentDayOHL(Closes[1]).CurrentOpen[0]

                    The EMA is the same process, in the overloads you see in the help guide, you just need to mirror this in the code and place the Closes[Index] where it asks for the IDataSeries.

                    I look forward to being of further assistance.

                    Comment


                      #25
                      Originally posted by everington_f View Post
                      // so I have done the following...; remember this same condition will be applied to all 25 stocks.
                      I find this statement incomplete. Can you explain this statement more? For example, please describe if you mean this single condition to be applied *SEPARATELY* to each stock, and that if this condition is TRUE for that stock, then only the stocks meeting the condition should have trades executed?

                      Originally posted by everington_f View Post
                      if (Position.MarketPosition == MarketPosition.Flat
                      && PriorDayOHLC().PriorOpen[0] < PriorDayOHLC().PriorClose[0]
                      && (CurrentDayOHL().CurrentOpen[0] - PriorDayOHLC().PriorClose[0]) > (DailyATR(5)[0]*0.1)
                      && ((CurrentDayOHL().CurrentOpen[0] - PriorDayOHLC().PriorClose[0]) <= (DailyATR(5)[0]*0.3))
                      && CurrentDayOHL().CurrentOpen[0] > PriorDayOHLC().PriorHigh[0]
                      && WilliamsR(10)[1]>-50
                      && CurrentDayOHL().CurrentOpen[0] > EMA(10)[0]
                      && Bars.FirstBarOfSession

                      && Bars.TickCount <1000)

                      {
                      EnterShort(0,voladjust1, "SP STOCK 1 U-H Short");
                      EnterShort(1,voladjust1, "SP STOCK 2 U-H Short");
                      EnterShort(2,voladjust1, "SP STOCK 3 U-H Short");
                      EnterShort(3,voladjust1, "SP STOCK 4 U-H Short");
                      EnterShort(4,voladjust1, "SP STOCK 5 U-H Short");
                      EnterShort(5,voladjust1, "SP STOCK 6 U-H Short");
                      EnterShort(6,voladjust1, "SP STOCK 7 U-H Short");
                      EnterShort(7,voladjust1, "SP STOCK 8 U-H Short");
                      EnterShort(9,voladjust1, "SP STOCK 9 U-H Short");
                      EnterShort(10,voladjust1, "SP STOCK 11 U-H Short");
                      EnterShort(11,voladjust1, "SP STOCK 12 U-H Short");
                      EnterShort(12,voladjust1, "SP STOCK 13 U-H Short");
                      EnterShort(13,voladjust1, "SP STOCK 14 U-H Short");
                      EnterShort(14,voladjust1, "SP STOCK 15 U-H Short");
                      EnterShort(15,voladjust1, "SP STOCK 16 U-H Short");
                      EnterShort(16,voladjust1, "SP STOCK 17 U-H Short");
                      EnterShort(17,voladjust1, "SP STOCK 18 U-H Short");
                      EnterShort(19,voladjust1, "SP STOCK 19 U-H Short");
                      EnterShort(20,voladjust1, "SP STOCK 20 U-H Short");
                      EnterShort(21,voladjust1, "SP STOCK 21 U-H Short");
                      EnterShort(22,voladjust1, "SP STOCK 22 U-H Short");
                      EnterShort(23,voladjust1, "SP STOCK 23 U-H Short");
                      EnterShort(24,voladjust1, "SP STOCK 24 U-H Short");
                      }
                      I find this code incomplete and probably still wrong. Where is this code being executed? OnBarUpdate? Are you sure you want to enter trades for all 25 stocks based upon this single condition being true for the primary instrument only? If that is not the case ...

                      If you have 24 calls to Add() in Initialize(), because you are adding 24 more stocks to the primary data series already on the chart, then OnBarUpdate() will be called 25 times for each of the 25 instruments (primary instrument + 24 additional instruments).

                      On each of the 25 calls to OnBarUpdate, the BarsInProgress value will vary from 0 to 24.

                      Thus, have you considered simply re-using BarsInProgress as the first argument to EnterShort()?

                      Code:
                      protected override override OnBarUpdate()
                      {
                          if (Position.MarketPosition == MarketPosition.Flat &&
                              PriorDayOHLC().PriorOpen[0] < PriorDayOHLC().PriorClose[0] &&
                              (CurrentDayOHL().CurrentOpen[0] - PriorDayOHLC().PriorClose[0]) > (DailyATR(5)[0]*0.1) &&
                              (CurrentDayOHL().CurrentOpen[0] - PriorDayOHLC().PriorClose[0]) <= (DailyATR(5)[0]*0.3) &&
                              CurrentDayOHL().CurrentOpen[0] > PriorDayOHLC().PriorHigh[0] &&
                              WilliamsR(10)[1] > -50 &&
                              CurrentDayOHL().CurrentOpen[0] > EMA(10)[0] &&
                              Bars.FirstBarOfSession && Bars.TickCount < 1000)
                          {
                              EnterShort(BarsInProgress, voladjust1, "SP STOCK U-H Short-" + BarsInProgress);
                          }
                      }
                      But this code is still wrong because you'll need to remove the first boolean condition regarding checking for MarketPosition == Flat, otherwise the first stock matching this condition that enters a trade stops all remaining stocks from being traded.

                      This code also means you'd be making this check in the context of each stock, is that what you want to do? That is, for each of the 25 instruments, do you want to have each instrument go through this check *INDEPENDENTLY* in order to enter the trade for that instrument?

                      It's critical to realize that every instrument added with Add() gets its "own" call to OnBarUpdate() --- and the critical thing to know here is that for each call to the OnBarUpdate(), NinjaTrader automatically sets BarsInProgress beforehand to the correct "index" value representing that instrument.

                      How do you know the index values of BarsInProgress?
                      The primary instrument on the chart is always 0, the first instrument added with Add() is index 1, the 2nd instrument added is index 2, and so on ... in your case if you're calling Add() 24 total times, well, OnBarUpdate is called 25 times with BarsInProgress set to 0 to 24.

                      Hope that helps.
                      Last edited by bltdavid; 06-03-2015, 12:05 AM.

                      Comment


                        #26
                        that is correct. I mean this single condition to be applied *SEPARATELY* to each stock, and that if this condition is TRUE for that stock, then only the stocks meeting the condition should have trades executed.

                        Comment


                          #27
                          strange strategy behaviour

                          && PriorDayOHLC(Closes[i]).PriorOpen[0] < PriorDayOHLC(Closes[i]).PriorClose[0]
                          && CurrentDayOHL(Closes[i]).CurrentOpen[0] > PriorDayOHLC(Closes[i]).PriorHigh[0]
                          && Bars.FirstBarOfSession
                          && Bars.TickCount < 50)

                          {
                          tag = "SP STOCK" + i + "V5 Short";
                          EnterShort(i, (int)(adjust1 / voldivide0), tag);
                          SetStopLoss(tag, CalculationMode.Price, ((DailyATR(Closes[i], 5)[0] * 0.3) + CurrentDayOHL(Closes[i]).CurrentOpen[0]), false);
                          SetProfitTarget(tag, CalculationMode.Price, PriorDayOHLC(Closes[i]).PriorClose[0]);
                          fire[i] = 2;
                          }


                          This snippet of code refers to a strategy that should of course calculate on todays opening level versus yesterday. But instead it behaves as if yesterday is today and fires based on yesterdays values - then sets stops and targets based on the values 2 days ago... how is that possible? This is a multi instrument version of a strategy that has worked perfectly for years as a single instrument ( as in no array and Closes[i] involved.) Very strange.

                          Comment


                            #28
                            Hello,

                            If the logic was sound before converting the script, more than likely this has to do with how the script was re-formed using the additional series.

                            Rather than guessing what may be the issue, the best solution would be to Print the values out and the times they are being set and used. This could indicate where the problem is.

                            If the problem is not apparent, in certain cases it can be more efficient to fallback to the original and create a new revision.

                            This in a sense would be starting over, but being that this is a whole new script, you would have the working original, the non working copy with the updated code, and a new copy of the working original. Using these you don't need to re invent the wheel but just copy segments into the new script to test that they work as you expect.

                            I look forward to being of further assistance.

                            Comment


                              #29
                              Ok I was just thinking perhaps that the strategybase.closes should be strategybase.opens.... there just arent enough moving parts in this strategy for me to think of anything else. Many thanks for your guidance.

                              The orignal script simple had Addd() instruments and a loop... referencing each array member via the Closes(i).. Will keep looking.

                              Comment


                                #30
                                Multi Instrument Strategy

                                My strategy uses onbarupdate and two loops that goes through the instrument array of 50 stocks.

                                Loop 1 creates a counter variable that I want to use in Loop 2 which executes orders.

                                The problem I have is that:

                                OnBarUpdate()
                                }
                                Fires Loop 1
                                and
                                Loop2
                                {

                                so that my executions in loop 2 happen before the first loop has fully finished calculating the counter variable -

                                How can I make sure that when OnBarUpdate fires... Loop 1 completes before starting on the second for (i = 0; i < 50; i++) loop? Regards and thx

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                633 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                364 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                105 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                567 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                568 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X