Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multi-instrument indicator with other indicators as inputs, multi-instrument strategy

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

    Multi-instrument indicator with other indicators as inputs, multi-instrument strategy

    I am a very early beginner coder, hence apologies in advance.

    My ultimate goal is to develop a multi-instrument strategy, using a multi-instrument indicator as part of its overall set up. In creating the code, I use:
    • StrategyBuilder for strategy development, and
    • NinjaScript Editor for indicator development.
    My ''single instrument variant'' of the strategy and my ''single instrument variant'' of the indicator (named TDENVIRONMENT) run through StrategyAnalyser with no issue, eg TDENVIRONMENT is visible on its Chart window. I haven't used TDENVIRONMENT in trading conditions as yet, but I expect that it will work. I must note that TDENVIRONMENT indicator is complex, as it consists of multiple sets of conditions, and uses a number of OTHER custom indicators as inputs (and shares parameters with them).

    My attempts to extend my strategy and my indicator to incorporate a secondary instrument have resulted in run-time errors saying that:

    A. All those OTHER indicators which I use as inputs into TDENVIRONMENT have "(indicator name) tried to load additional data. All data must first be loaded by the hosting NinjaScript in its configure state. Attempted to load (primary security) Default: 5 Range"; and

    B. "Value of property "prop1" of NinjaScript TDENVIRONMENT is 0 and not in valid range...". I have checked many times, I am not setting any of my parameters to 0...

    I would appreciate any help/advice/insight from the NT community here re what should I do/where do I need to look to address these. In particular:

    (1) Should my OTHER indicators, which feed into TDENVIRONMENT, also make calls on the secondary instrument? At this state, I have incorporated a call for the secondary instrument into multi-instrument indicator (please see below), but NOT into OTHER indicators that feed into it.

    (2) Is it ok that my primary series and my secondary series share the same set of parameters, or should each one have their own set?

    (3) Does BarsRequiredToPlot really need to be defined based on how many bars is needed for calculation of all the OTHER indicators? Eg if there is EMA(20) and EMA(600), does BarsRequiredToPlot need to be 600, or can it be 20?

    (4) Does BarsRequiredToTrade in the strategy need to correspond to BarsRequiredToPlot in the indicator?

    (5) Does anything in my script snippets below raise a concern?


    Here is what I have done in my two multi-instrument scripts:

    (1) Added two plots to the indicator and supplied default its default parameter values:

    if (State==State.SetDefaults)

    AddPlot(Brushes.Blue, "TDENVIRONMENT Primary Series");
    AddPlot(Brushes.Green, "TDENVIRONMENT Secondary Series");

    HMAcoreperiod =1800;
    SMAcoreperiod =1800;

    param1 = 300;
    param2 = 200; etc.​​

    (2) I have added secondary data series to both the strategy and to the indicator:

    if (State==State.Configure)

    AddDataSeries("XXXX", BarsPeriodType.Range, 5, Data.MarketDataType.Last);

    (3) Initialised the Plots in the indicator. TDENVIRONMENT has a large number of parameters which it "inherits" from the OTHER single instrument indicators. For simplicity, I have cut down the number of these/removed their names below.:

    // initialize the TDENVIRONMENT using the primary series and assign to TDENVIRONMENT1
    TDENVIRONMENT1 = TDENVIRONMENT(BarsArray[0], param1,param2, param3);

    // initialize the TDENVIRONMENT using the secondary series and assign to TDENVIRONMENT2
    TDENVIRONMENT2 = TDENVIRONMENT(BarsArray[1], param1,param2, param3);​

    (4) Added BarsRequiredToPlot logic to the indicator

    protected override void OnBarUpdate()
    {
    // ensure both series have at least one bar, and have sufficient number to plot
    if (CurrentBars[0] < 1 || CurrentBars[1] < 1 || CurrentBars[0] <=BarsRequiredToPlot || CurrentBars[1] <= BarsRequiredToPlot)
    return;

    // when secondary series are processing, set the secondary plot to the TDENVIRONMENT with the secondary series input
    if (BarsInProgress == 1)
    TDENVIRONMENTSecondary[0] = TDENVIRONMENT2[0];

    // when the primary series is processing set the primary plot to the TDENVIRONMENT with the primary series input
    if (BarsInProgress == 0)
    {
    TDENVIRONMENTPrimary[0] = TDENVIRONMENT1[0];

    // if the secondary series did not close, set the current bar's value to the previous bar's value to prevent gaps
    if (!TDENVIRONMENTSecondary.IsValidDataPoint(0))
    TDENVIRONMENTSecondary[0] = TDENVIRONMENTSecondary[1];

    (5) Have defined parameter fields in the indicator, including, amongst other parameters, the following:


    [Browsable(false)]
    [XmlIgnore]
    public Series<double> TDENVIRONMENTPrimary
    {
    get { return Values[0]; }
    }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> TDENVIRONMENTSecondary
    {
    get { return Values[1]; }
    }

    Many thanks
    TD​​

    #2
    Hello TanyaD,

    A. All those OTHER indicators which I use as inputs into TDENVIRONMENT have "(indicator name) tried to load additional data. All data must first be loaded by the hosting NinjaScript in its configure state. Attempted to load (primary security) Default: 5 Range"; and

    If an indicator loads secondary data you need the strategy to add that same data. You can do that in the additional data screen of the builder to avoid that error.

    B. "Value of property "prop1" of NinjaScript TDENVIRONMENT is 0 and not in valid range...". I have checked many times, I am not setting any of my parameters to 0...
    ​You need to make sure a default of 1 or more is defined for that property, it has a minimum set of 1 or more based on the message. If you are not setting a default that is the reason because 0 is the default for an int or double.

    (1) Should my OTHER indicators, which feed into TDENVIRONMENT, also make calls on the secondary instrument? At this state, I have incorporated a call for the secondary instrument into multi-instrument indicator (please see below), but NOT into OTHER indicators that feed into it.
    ​Only you would be able to answer this, adding secondary data is usually done for a specific programming reason. If you are unclear on the reason for adding the secondary data you may need to take a step back and see if that is really necessary to add that data.

    (2) Is it ok that my primary series and my secondary series share the same set of parameters, or should each one have their own set?
    ​I am not sure what you mean, if the primary is the same parameters as the secondary series being added there would not be a reason to add that series. Adding a secondary series would be for the purpose of using data that is not the primary series.

    (3) Does BarsRequiredToPlot really need to be defined based on how many bars is needed for calculation of all the OTHER indicators? Eg if there is EMA(20) and EMA(600), does BarsRequiredToPlot need to be 600, or can it be 20?
    ​BarsRequiredToPlot is an amount of bars your calculation requires before plotting. The default is 20, you can choose any amount for this value based on what you are doing. This just indicates when the indicator plot is producing a valid value, it won't be charted until after this amount. This does not apply to using indicators in code, this is for chart visuals and can be used conditionally to control when to access an indicators value.

    (4) Does BarsRequiredToTrade in the strategy need to correspond to BarsRequiredToPlot in the indicator?
    ​No, BarsRequiredToTrade is the amount of bars to wait before an entry can be placed. Any order submitted before this amount of bars is ignored.


    (5) Does anything in my script snippets below raise a concern?
    ​That would not be something I can help with, you would need to run and test the script to see if it works as you designed it to. If it does not and you have a more specific question about a part of what you made you could make a post to go over that specific part of the code or concept.



    Just as a suggestion I would recommend that you test the strategy that uses a single series in realtime before trying to modify it into a multi series script. You would want to know ahead of time if the multiseries changes you made affected the end result. You will additionally see differences in backtesting if you are using the added series for fill granularity so it would be important to know exactly how it performs in all testing modes before adding more series. I would also suggest making a copy of the original indicator and strategy and edit the copies when adding the multi series items in so you can always revert back if needed.

    Comment


      #3
      Many thanks Jesse for your comments and suggestions. I should have said that I work on my multi-instrument indicator using NT's SampleSecondarySeriesasInputSeries as its base. While the latter is very useful to have, it covers only a very simple scenario. My multi-instrument indicator is an indicator-on-indicator, for example HMA((ROC(SMA(Close,5),10),20). My single instrument TDENVIRONMENT indicator is a little more complex than that and I know that extending it to multi-instrument will require more work, but having a more complex multi-instrument example would be extremely helpful.

      Yes, I have added exactly the same series to the strategy as I have added to the indicator; and I am running my Strategy Analyser on that specific BarsPeriodType, period and Data.MarketDataType. I run StrategyAnalyser using exact same settings, yet the runtime error relates to the primary instrument, while it seems to be happy with the secondary instrument.

      Yes, I have set all the defaults. I will dig deeper to figure out what's happening underneath...

      Re adding secondary data to the OTHER indicators. What I meant is, going with my example of building a multi-instrument HMA((ROC(SMA(Close,5),10),20), do I need to add secondary instrument to (a) the HMA only, or (b) do I need to do the same in ROC and to SMA as well? I thought that (a) is applicable, but I am new to this:... This in part prompted my question re availability of a more complex example of a multi-instrument indicator.

      Re my question re parameters shared between the two series - let me re-phrase that. I've Initialised the Plots in the multi-instrument indicator. TDENVIRONMENT has a large number of parameters which it "inherits" from its underlying/constituent single instrument indicators - going with my example here, the single instrument version looks like HMA(ROC(SMA(Close),param1),param2),param3); and my multi-instrument plots are below - is it ok that I have the same parameter names for both of them/can it confuse the system?:

      // initialize the TDENVIRONMENT using the primary series and assign to TDENVIRONMENT1
      TDENVIRONMENT1 = TDENVIRONMENT(BarsArray[0], param1,param2, param3);

      // initialize the TDENVIRONMENT using the secondary series and assign to TDENVIRONMENT2
      TDENVIRONMENT2 = TDENVIRONMENT(BarsArray[1], param1,param2, param3);​

      Thank you for your response re BarsRequiredToPlot and BarsRequiredToTrade, this is very helpful. Could you please confirm that the moment I can see an indicator on the screen, it also avails itself to the trading rules?​

      ​Yes, absolutely, I do keep versions of indicators and of strategies as I progress with this development, this has saved me a few times

      Many thanks again Jesse, I look forward to your response. TD

      Comment


        #4
        Hello TanyaD,

        I should have said that I work on my multi-instrument indicator using NT's SampleSecondarySeriesasInputSeries as its bas. ..... but having a more complex multi-instrument example would be extremely helpful.
        That is a good starting point however that sample only covers a few basics, I highly suggest reading the multi series documentation page and going through all the samples in that page for a complete understanding of using multiple series in scripts. That is the link I added in the last post. There are not any other script examples I can suggest because using multi series or instruments is a very complex task and requires planning before going ahead with coding it.


        Yes, I have added exactly the same series to the strategy as I have added to the indicator; and I am running my Strategy Analyser on that specific BarsPeriodType, period and Data.MarketDataType. I run StrategyAnalyser using exact same settings, yet the runtime error relates to the primary instrument, while it seems to be happy with the secondary instrument.

        Are you trying to use AddDataSeries with variables? If you did use variables that will fail in a lot of use cases and produce warnings about loading data. AddDataSeries statements need to be hard coded to be valid. You otherwise would need to have all of the same secondary series that the indicator uses also added in the strategy and in the correct state which is State.Configure.


        Re adding secondary data to the OTHER indicators. What I meant is, going with my example of building a multi-instrument HMA((ROC(SMA(Close,5),10),20), do I need to add secondary instrument to (a) the HMA only, or (b) do I need to do the same in ROC and to SMA as well? I thought that (a) is applicable, but I am new to this:... This in part prompted my question re availability of a more complex example of a multi-instrument indicator.
        I wouldn't be able to answer this because how you use the indicators or what data you supply to them directly relates to the reasons you are adding the secondary series. You would have to decide on what indicators you want to use and what data those indicators used based on your goal.


        Re my question re parameters shared between the two series - let me re-phrase that. I've Initialised the Plots in the multi-instrument indicator. TDENVIRONMENT has a large number of parameters which it "inherits" from its underlying/constituent single instrument indicators - going with my example here, the single instrument version looks like HMA(ROC(SMA(Close),param1),param2),param3); and my multi-instrument plots are below - is it ok that I have the same parameter names for both of them/can it confuse the system?:
        ​Again this is going to depend on how you want to use the added series, I couldn't really answer this as I don't know your overall goal. You would have to first explore the reason you are adding the secondary series and based on why you added that series you could explore passing data or parameters to indicators as directed by your goal.

        Thank you for your response re BarsRequiredToPlot and BarsRequiredToTrade, this is very helpful. Could you please confirm that the moment I can see an indicator on the screen, it also avails itself to the trading rules?​
        ​When your strategy adds the indicator visually that is just the indicators visual. To know how your strategy is trading you would have to add prints into its code to see what it is logically doing.

        Comment


          #5
          Hi Jesse, many thanks for this.

          Yes, I have been through all the documentation, everything that relates to multi-instrument/multi-time frame and to AddDataSeries().

          I intend to use the readings from (a) the primary instrument (TDENVIRONMENT1) and (b) from the secondary instrument (TDENVIRONMENT2) as part of my logic in the Strategy to enter and exit positions on my primary instrument. As a side question, is my understanding that it is impossible to enter and exit positions on the secondary instrument from a strategy that enters and exits trades on the primary instrument, correct?

          Attached below are the single-series indicator (TDENVIRONMENT.cs) and the multi-series indicator (TDENVIROMENTcopiedandchangedSampleSecondarySeries asInputSeries.cs). I would truly appreciate your help if you could cast your eye over these.

          Thanks again
          TD
          Attached Files

          Comment


            #6
            ....further to the above, in respect of the multi-series indicator, I would like only the series for the secondary instrument to show up on the chart. This is because the single instrument indicator uses BackBrush...

            Comment


              #7
              Hello TanyaD, In the scripts that you attached only 1 of the indicators is using AddDataSeries. If you want your secondary indicator to use secondary data it either needs to add that data in its own code and reference it directly or you need to use the Input property similar to how the SMA is coded. https://ninjatrader.com/support/help.../nt8/input.htm

              Comment


                #8
                Thanks Jesse, but changing the single instrument script from Close to Input still didn't work, I must be doing something not quite right.

                Comment


                  #9
                  Hello TanyaD,

                  Close is not correct, you need to use Input if you want to use the input series. The SMA is a simple example of that concept, you would replace Close with Input in your calculations.

                  Comment

                  Latest Posts

                  Collapse

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