Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to apply the same strategy to multiple instruments?

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

    How to apply the same strategy to multiple instruments?

    Currently I use the following two methods to apply my Strategy to multiple instruments:
    1) From NT menu, open a chart for stock ABC, then apply Strategy against this chart; repeat this steps to different instruments.
    2) From NT menu, select Strategies -> New Strategy.. -> Enable. repeat this steps to different instruments.

    Both above methods are from GUI interface, is there a way to apply my strategy to multiple instruments through reading a list of instruments? Thanks.

    #2
    Hello localappleseed, thanks for your post.

    Each strategy must be enabled on a single instrument individually. The only way of automatically adding a series is to use AddDataSeries within the script and have one script trade multiple instruments. We have a guide on multi instrument scripts here:



    Please let me know if I can assist any further.

    Comment


      #3
      Thanks, Chris.
      So it appears to me that the added new instruments must be hardcoded like:
      AddDataSeries("MSFT", BarsPeriodType.Minute, 5);

      Is there a better way to input the instruments instead of hard-coded?

      Comment


        #4
        Hello localappleseed, thanks for your reply.

        See the "Correlation" indicator as an example of dynamically loading a series. The case where this can and will go wrong is during a backtest in the strategy analyzer.

        Please let me know if I can assist any further.

        Comment


          #5
          So if I need to apply the Strategy to 10 instruments, I would have to use AddDataSeries() 10 times, correct?
          AddDataSeries("MSFT", BarsPeriodType.Minute, 5);
          AddDataSeries("DELL", BarsPeriodType.Minute, 5);
          ...
          ...
          AddDataSeries("NT", BarsPeriodType.Minute, 5);

          I am still thinking if it is possible to read a list of instruments from a file or something like that.

          Comment


            #6
            Hello localappleseed, thanks for your reply.

            You can use a list to load the instruments if you would like, it needs to be tested of course. I do recall doing something similar in the past, so loading in a list of strings during State.Configure should work fine. Doing this will be outside of the scope of support we can provide, but we do have an example here:


            Best regards.

            Comment


              #7
              Your help is very much appreciated!

              Comment


                #8
                Guys, I am new.
                Primary series - SPY. Strategy is running on SPY. But need to enter into AAPL or MSFT depending on condition. Added AddDataSeries for AAPL and MSFT obviously. Added AAPLShares and MSFTShares as additional inputs.

                What do you specify in Enter Long string to enter into long for AAPL instrument for AAPL#Shares?

                EnterShort(Convert.ToInt32(GetCurrentAsk(1),(AAPLS hares)), @"buyAAPL#Shares"); ????? Where do you place AAPLShares input variable? Where do you have ( ) brackets, commas, etc so it works with AAPLDataSeries as (1) and AAPLShares as an input variable for AAPL number of shares???

                Or this is not going to work with both AAPL and MSFT and I need to split this in two strategies and desgnate AAPL as a primary data series and run the strategy on SPY, and same for MSFT?

                Thank you.

                Comment


                  #9
                  Hello Alakman,

                  You can use a secondary series for that purpose in manual coding. When submitting the orders you would need to specify the BarsInprogress for the series you wanted to submit the order to. You can find examples of creating multi series scripts in the following link, if you take a look at the section titled Entering, Exiting and Retrieving Position Information you can see examples of how the script can be structured to submit to multiple instruments.

                  Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.

                  Comment


                    #10
                    Thank you, Jesse. Still can't compile for some reason. And it does not show me any errors. But Compile button is greyed out.
                    Could you please confirm if BarsInProgress is specified right before the Action on Entry/Exit, like this:

                    if (BarsInProgress == 1)
                    {
                    EnterLong(1,AAPLShares, @"BuyAAPL");
                    }

                    Also, could you please confirm if AAPLShares should be in quotation marks like "AAPLShares", if it is related to the input variable of AAPL shares = 250?
                    Thank you very much.​

                    Comment


                      #11
                      Hello Alakman,

                      Are you running the platform in normal mode and not safe mode? Compile could also be grayed out if you do not have file open, try opening a NinjaScript file from the editors explorer and press F5. If you hear a sound that means it compiled. If no errors were presented then there are no compile errors.

                      Regarding AAPLShares, if that is a variable then it does not need quotes.

                      Comment


                        #12
                        Thank you, Jesse. I resolved the issue with not compiling (it was an unfinished old strategy which couldn't get compiled, I had to delete it).
                        The issue I have now is that the strategy only enters long and enters correctly on Bars 1 and Bars 2 securities, but only if I choose Immediate entry. If I choose Wait Until Flat, then the strategy does not Enter / Exit on any of the Sets.

                        No problem to correctly enter in the FIRST positions for Bar 1 and Bar 2 (only in Immediately execute, but not in Wati Until Flat). Then, there's no Exit action or Entry action at all. I had it like this:

                        {
                        if (BarsInProgress == 0)
                        return;
                        if (CurrentBars[0] < 3)
                        return;​

                        // Set 1
                        if ((BlahBlah.Default[0] > BlahBlah[0])
                        && (MarketPosition.Flat == Position.MarketPosition))​

                        if (BarsInProgress == 1)
                        {
                        EnterLong(1,AAPLShares, @"BuyAAPL");
                        }​

                        What am I missing? If I add if (BarsInProgress == 0) to the beginning of each Set, it gives me a red flag in Account Syncing section...
                        Thank you for pointing me in the right direction.

                        Comment


                          #13
                          Hello Alakman,

                          Are you only looking at the realtime performance? When using wait until flat if the strategy is yellow that means it placed historical trades and is waiting for an exit before starting. If you only want to test this in realtime you can use if(State == State.Historical) return; to prevent that from placing historical trades.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by argusthome, 03-08-2026, 10:06 AM
                          0 responses
                          55 views
                          0 likes
                          Last Post argusthome  
                          Started by NabilKhattabi, 03-06-2026, 11:18 AM
                          0 responses
                          37 views
                          0 likes
                          Last Post NabilKhattabi  
                          Started by Deep42, 03-06-2026, 12:28 AM
                          0 responses
                          17 views
                          0 likes
                          Last Post Deep42
                          by Deep42
                           
                          Started by TheRealMorford, 03-05-2026, 06:15 PM
                          0 responses
                          19 views
                          0 likes
                          Last Post TheRealMorford  
                          Started by Mindset, 02-28-2026, 06:16 AM
                          0 responses
                          49 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Working...
                          X