Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

multi-instrument strategies & backtesting

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

    multi-instrument strategies & backtesting

    Dear NinjaTraders,

    I am new to the NinjaTrader platform and have some questions on multi-instrument strategies.

    If I understand correctly, running a strategy on an instrument list will not allow me to really implement a multi-instrument due to the way the strategy analyzer works: it seems the instruments are simulated sequentially and not in parallel, meaning first Initialize and all OnBarUpdate calls are made for one instrument, before moving on and starting the next instrument with Initialize

    That seems to leave me only with the option to add all the instruments during Initialize. However, I am not happy with that approach for two reasons:
    1) the strategy analyzer will no longer allow me to see the performance of the individual instruments
    2) I now have the instruments hard-coded in Initialize while I really should have these set up in a file, most naturally an instrument list. Having this now all happen in the instrument list, makes the concept of the primary instrument questionable - my strategy would probably not require that

    I will probably have more questions... It would be really great to have some sample code that does the following:
    * work on an instrument list
    * execute trades on all instruments
    * allocate the available cash across (a subset of) the stocks

    Is there any example like that available?

    Thanks a lot for your help,
    best regards

    Felix

    #2
    Hi Felix welcome to our forums - your understanding is correct, true portfolio testing would not be supported. It's an item though on our enhancement consideration list and I will make sure to count a vote on your behalf in.

    Presently the best approach would be adding the symbols in Initialize() via the Add() method and then using a MultiInstrument script for combined performance and quantity allocations.

    For single instrument scripts you could run a combined basket test performance though as well in SA -

    Comment


      #3
      thank you so much for your response. Just to make sure I understood you correctly, is the code below how I should try to do this? Is there anything I can do to show the performance broken out per instrument in the strategy analyzer?

      Thank you so much,
      best regards, Felix



      protected override void Initialize()
      {
      CalculateOnBarClose = true;

      ClearOutputWindow();

      // unsupported code. taken from
      // http://www.ninjatrader.com/support/f...nt+instruments
      NinjaTrader.Cbi.InstrumentList list = NinjaTrader.Cbi.InstrumentList.GetObject("NASDAQ 100");
      if (list != null) {
      foreach (Instrument i in list.Instruments) {
      //Print(i.FullName);
      Add(i.FullName, PeriodType.Day, 1);
      }
      } else {
      //Log("AddInstrumentsByListExample: There is no instrument list named Custom", LogLevel.Information);
      }
      }

      protected override void OnBarUpdate() {
      if (BarsInProgress != 0) return;

      double max = -1;
      int maxIndex = -1;

      // loop through all instruments
      for (int i = 0; i < BarsArray.Length; i++) {
      if (CurrentBars[i] < 0) continue; // skip, if no bars exist

      // find the best instrument
      double indicator = LinRegSlope(BarsArray[i],30)[0];
      if (indicator > max) {
      max = indicator;
      maxIndex = i;
      }
      }
      Print("trading " + BarsArray[maxIndex].Instrument.FullName);

      // loop through all positions
      for (int j = 0; j < BarsArray.Length; j++) {
      if (j == maxIndex) {
      if (Positions[j].MarketPosition == MarketPosition.Flat) {
      // enter new position
      EnterLong(j, DefaultQuantity, "");
      } else {
      // keep
      }
      } else {
      // exit
      ExitLong(j, Positions[j].Quantity, "", "");
      }
      }
      }

      Comment


        #4
        Correct Felix, however the add from the list may / may not work - as unsupported we have not any documented outcomes here. But for our next major release update more support is added in in this area.

        The Strategy Analyzer would report the performance for all instruments, you could not split it up.

        Comment


          #5
          Hi Bertrand,
          thank you so much for your help!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          558 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          324 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          545 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          547 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X