Announcement

Collapse

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

Collapse

Creating a multi-instrument strategy

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

    Creating a multi-instrument strategy

    Hello,
    I am trying to create a strategy that trades two instruments at once based on each other's price movement. I have the following:
    if (BarsInProgress == 0)
    {
    if (CrossBelow(Close, Close[2], 1)) EnterLong(1, 1, "Entry for instrument B");
    }​
    The code was able to compile, but is not executing anything.
    Thank you

    #2
    Hello samish18,

    Thanks for your post.

    To clarify, how many secondary series (AddDataSeries() calls) do you have defined in your script?

    If you are trying to create a condition that checks if the Close price of the primary series crosses below the Close price of the second added secondary series in the script, Close[2] should be changed to Closes[2].

    Closes is used to reference the price series of added secondary series in the script. Closes[1] would reference the first added series in a script. Closes[2] would reference the second added series in the script, and so on.

    Closes: https://ninjatrader.com/support/help...nt8/closes.htm

    Working with Multi-Timeframe/Multi-Instrument NinjaScripts: https://ninjatrader.com/support/help...nstruments.htm
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      I am having issues with working with multi-instrument strategies in general. This script here is just trying to execute an order on instrument B when instrument A's price decreases (just troubleshooting). The code will compile, but upon backtesting, nothing will execute

      Comment


        #4
        Hello samish18,

        Thanks for your notes.

        What exactly are the issues you are referring to so that we may accurately assist you?

        Is the issue with the code you shared in post # 1?

        If so, how many secondary series (AddDataSeries() calls) do you have defined in your script?

        If you are only calling AddDataSeries() once in the script, this means Close[2] would need to be changed to Closes[1] so that it references the Close series of the first added series in the script.

        If you are calling AddDataSeries() twice in your script, Close[2] would need to be change to Closes[2] so that it references the Close series of the second added series in the script.

        Closes: https://ninjatrader.com/support/help...nt8/closes.htm

        For a detailed overview of working with Multi-Timeframe/Multi-Instrument NinjaScripts, please review this help guide page: https://ninjatrader.com/support/help...nstruments.htm

        We also have two reference samples you could view called SampleMultiTimeFrame and SampleMultiInstrument that you could view to get an idea of how to work with multi-timeframe/multi-instrument NinjaScripts.

        To view the scripts, open a New > NinjaScript editor window, open the Strategies folder, and double-click on the SampleMultiTimeFrame file or SampleMultiInstrument file.

        Further, if a script is not behaving as expected then debugging prints need to be added to a script that prints out all the values being used for the logic used to submit orders to see how it is evaluating.

        Below is a link to a forum post that demonstrates how to use prints to understand behavior.
        https://ninjatrader.com/support/foru...121#post791121
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          I only have one secondary data series in the script. Close[2] is correct, I am referencing the close price of instrument A 2 bars ago. This code is meant to execute an order for instrument B when the close price of instrument A is greater than it was 2 bars ago. The code compiles but nothing occurs in the backtest. I have looked at the sample multitimeframe strategy and similarly, when applied in backtesting, no trades occur.

          Comment


            #6
            Hello samish18,

            Thanks for your notes.

            Do you have Historical data for both of the instruments you are testing the script on?

            For example, if the script is being tested on ES 03-24 and you have an added data series for MES 03-24, do you have historical data for both instruments?

            What instrument are you backtesting the strategy on?

            What is the added secondary series in the script?

            What dates are you backtesting the strategy on?

            What is the bar type and interval you are running the backtest on (1-Minute, 500 Volume, 1000 Tick, etc.)?


            I suggest downloading the Historical data for both the instruments you want to use to test the script on for the dates you are testing the strategy over. To download Historical data go to Tools > Historical Data and expand the 'Download' section.

            Downloading Historical Data: https://ninjatrader.com/support/help...8/download.htm

            Then, after downloading all the historical data in the Historical Data window, test running a backtest on the strategy in the Strategy Analyzer.


            I have attached a modified version of the SampleMultiInstrument reference sample called SampleMultiInstrumentMod. The only change to this script is that it adds a secondary data series for MES 03-24 instead of MSFT.

            If we run a backtest using this script with the primary instrument set to ES 03-24, we can see backtest results appear in Summary Display of the Strategy Analyzer.

            See this demonstration video: https://brandonh-ninjatrader.tinytak...Ml8yMjc3NTYwNA
            Attached Files
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Thank you for sending this example, I downloaded it and tried running a backtest with ES as the primary instrument and ran into the same issue - no results. I'm not sure what is going on here: I downloaded your code, left it unchanged and backtested over the same period. I am able to pull up a chart of both instruments and have backtested other strategies successfully.

              Comment


                #8
                I tried some troubleshooting: I added a print statement in the first line of the onbarupdate and it never executed.

                Comment


                  #9
                  Hello samish18,

                  Thanks for your notes.

                  Are you connected to a data feed provider? If so, who are you connected to? (This is displayed in green on the lower-left corner of the Control Center window.)

                  To clarify, did you download Historical Minute data in the Historical Data window for both the ES 03-24 instrument and the MES 03-24 instrument for the dates you are testing over?

                  For example, did you download Historical Minute data in the Historical Data window for the ES 03-24 and MES 03-24 instruments from 01/01/2024 to 01/29/2024 and then ran a backtest on the SampleMultiInstrumentMod strategy attached in post # 6 using the same exact Strategy Analyzer settings seen in the demonstration video?

                  Also, please answer the questions seen in post # 6 so I may accurately assist.
                  • What instrument are you backtesting the strategy on?
                  • What is the added secondary series in the script?
                  • What dates are you backtesting the strategy on?
                  • What is the bar type and interval you are running the backtest on (1-Minute, 500 Volume, 1000 Tick, etc.)?​

                  I look forward to assisting further.
                  Brandon H.NinjaTrader Customer Service

                  Comment


                    #10
                    That's exactly what I did: downloaded a month of data for MES and ES per your example. My data feed light was green.
                    I am backtesting on ES and MES per your example, MES is the secondary, backtesting the first month of 2024, running on 1 minute bars.

                    Comment


                      #11
                      Hello samish18,

                      Thanks for your notes.

                      What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.?.?.?)​

                      Who are you connected to in the Control Center > Connections menu? (Live, NinjaTrader, Interactive Brokers, Kinetick, etc.)

                      To clarify, are you backtesting the SampleMultiInstrumentMod strategy linked in post # 6 or are you backtesting your own custom strategy?

                      If you run a backtest on the SampleMACrossover strategy that comes with NinjaTrader, do you see results appearing in the Strategy Analyzer window?

                      Please send me a screenshot of the Strategy Analyzer settings and send a screenshot showing you have Historical Minute data downloaded for the ES and MES in the Historical Data window.
                      • To send a screenshot with Windows 10 or newer I would recommend using the Windows Snipping Tool.
                      • Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save it as a jpeg file and send the file as an attachment.
                      ​We look forward to assisting further.
                      Brandon H.NinjaTrader Customer Service

                      Comment


                        #12
                        Version: 8.1.2.1
                        Connections: Simulation
                        I am backtesting the sample multiinstrument strategy you sent in post 6. I see results when I run any other non-multi insturment strategy (including MAcrossover).
                        Photo of strategy analyzer was taken after running strategy to show no results

                        Click image for larger version

Name:	
Views:	0
Size:	29.7 KB
ID:	1290120 Click image for larger version

Name:	
Views:	0
Size:	29.1 KB
ID:	1290121 Click image for larger version

Name:	
Views:	0
Size:	99.5 KB
ID:	1290122
                        Last edited by NinjaTrader_BrandonH; 02-06-2024, 12:20 PM.

                        Comment


                          #13
                          Hello samish18,

                          Thanks for your notes.

                          In the Strategy Analyzer screenshot you shared I see you are testing the SampleMultiInstrument strategy that comes with NinjaTrader. This strategy adds a MSFT data series to the script so you would have to have Historical Data downloaded for the MSFT instrument to run a backtest on that strategy.

                          This is not the SampleMultiInstrumentMod strategy that I attached in post # 6. The strategy I attached adds an MES data series to the script instead of a MSFT data series. This requires MES Historical Data be downloaded.

                          Change the Strategy drop-down menu in the Strategy Analyzer window to use the SampleMultiInstrumentMod strategy attached in post # 6. The name of the script I shared is "SampleMultiInstrumentMod" in the Strategy Analyzer, not "Sample multi-instrument".

                          See the attached screenshot.

                          If you run a backtest on the SampleMultiInstrumentMod script, do you see results appearing?
                          Attached Files
                          Brandon H.NinjaTrader Customer Service

                          Comment


                            #14
                            Incredible! What a silly mistake, thank you. I am now seeing the results you saw. Building off this, how can I enter orders on MES as well as ES?

                            Comment


                              #15
                              Hello samish18,

                              Thanks for your notes.

                              You would need to call two entry order methods in your script, one to submit the order to the primary series and one to submit the order to the added secondary series. This is done by specifying the BarsInProgressIndex argument when calling the Entry order method.

                              EnterLong(int barsInProgressIndex, int quantity, string signalName)

                              The primary series will have a BarsInProgressIndex of 0. The first added secondary series in the script will have a BarsInProgressIndex of 1, and so on.

                              For example:

                              EnterLong(0, 1, "ES Long Entry");
                              EnterLong(1, 1, "MES Long Entry");

                              Note we pass in a BarsInProgressIndex of 0 for the first Entry order method to submit the order to the primary series. We pass in a BarsInProgressIndex of 1 into the second Entry order method to submit the order to the first added secondary series in the script.

                              Working with Multi-Timeframe/Multi-Instrument NinjaScripts: https://ninjatrader.com/support/help...nstruments.htm
                              EnterLong(): https://ninjatrader.com/support/help.../enterlong.htm
                              Brandon H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by lightsun47, Today, 03:51 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post lightsun47  
                              Started by 00nevest, Today, 02:27 PM
                              1 response
                              8 views
                              0 likes
                              Last Post 00nevest  
                              Started by futtrader, 04-21-2024, 01:50 AM
                              4 responses
                              44 views
                              0 likes
                              Last Post futtrader  
                              Started by Option Whisperer, Today, 09:55 AM
                              1 response
                              13 views
                              0 likes
                              Last Post bltdavid  
                              Started by port119, Today, 02:43 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post port119
                              by port119
                               
                              Working...
                              X