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

Questions about AddDataSeries() and Secondary PriceBars

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

    Questions about AddDataSeries() and Secondary PriceBars

    Hello,

    It seems like the NT8 AddDataSeries() method and secondary pricebars handling is significantly better than it was in NT7. First off, thank you! Secondly, I have some questions about these things:

    Question #1: Is it true that in NT8, calling AddDataSeries() twice (in the appropriate OnStateChange() "else if (State == State.Configure)" branch) with the exact same values always adds 2 secondary pricebars to the strategy, instead of adding the secondary pricebars once?

    For instance, if I create a strategy with 2 input parameters of type "string" called "Symbol1" and "Symbol2", and the user decides that they want both symbols to be "SPY", will 2 secondary pricebars always be added?

    Code:
    AddDataSeries(Symbol1, Data.BarsPeriodType.Day, 1, Data.MarketDataType.Last);	
    AddDataSeries(Symbol2, Data.BarsPeriodType.Day, 1, Data.MarketDataType.Last);
    Is there any case where NT8 wouldn't add one secondary pricebars series for each call of "AddDataSeries()"?


    Question #2: I noticed that both input parameters and internal variables declared in the NT8 Strategy Builder get assigned a default value in OnStateChange() "if (State == State.SetDefaults)" branch. Is it safe to use any input param or internal variable as one of the parameters passed to AddDataSeries(), and it is a safe assumption that the default values assigned to the input param or internal variable will be the value that is used?


    Question #3: Why does the NT8 Strategy Builder "Add Additional Data" dialogbox disable the "Price based on (last, bid, ask)" combobox when "Use primary instrument" is checked? Does this mean that as a NinjaScript programmer, I shouldn't be calling "AddDataSeries()" using the primary instrument as the symbol but using Bid or Ask data instead of "Last" data?


    Thank you very much in advance,

    EquityTrader

    #2
    Hello EquityTrader,

    Thank you for writing in.

    Yes, if you were to add two calls for AddDataSeries with the same parameters passed into it, you would have 2 additional data series added.

    You could always test this by adding print statements such as Print(Closes[1][0]); and Print(Closes[2][0];, whereas the first print statement is referring to the 1st additional data series and the send to the 2nd additional data series, you should see both statements print.

    It’s not a good idea to pass Ticker variables into the AddDataSeries call, cause if you disable strategy then change parameter, the strategy won't reflect that change because the strategy has already been initialized. So to answer question 2, it is not recommended.

    Regarding question three, when checking Use primary instrument, the syntax the builders uses becomes AddDataSeries(BarsPeriodType periodType, int period), which does not all you to designate the Price based on. I will submit a feature request for you to allow this to be specified.

    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hi NinjaTrader_AlanP,

      Thank you for the detailed and informative responses. Your answer to Question #1 is great news for NinjaScript programmers, since we don't need to consider the possibility that calling "AddDataSeries()" with a duplicate set of parameters might not add another element to BarsArray.

      Your answer to Question #3 is good news for NinjaScript programmers also, since it means that the NinjaTrader platform developers didn't make the decision to disable the "Price Based On" combobox due to a complication or intricacy that NinjaScript programmers need to worry about.

      I am still trying to understand the very important ramifications of your answer to Question #2, which was:

      It’s not a good idea to pass Ticker variables into the AddDataSeries call, cause if you disable strategy then change parameter, the strategy won't reflect that change because the strategy has already been initialized. So to answer question 2, it is not recommended.
      And a statement similar to your answer that I found in the NT8 manual at
      http://ninjatrader.com/support/helpG...dataseries.htm, which was:

      Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result into an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner.
      Question #2A: Does your answer and the excerpt from the manual mean that the "instrumentName" passed to AddDataSeries() can only ever be a string literal, like "GLD", and can never really safely be anything else?

      Question #2B: Can the instrumentName and other parameters to AddDataSeries() at least safely be "this.InstrumentName", "this.BarsPeriod", or "this.BarsPeriodType" as in the code below, taken from NinjaTrader_JessicaP's post at http://ninjatrader.com/support/forum...3&postcount=19 ?

      AddDataSeries(Instrument.FullName, new BarsPeriod() { MarketDataType = BarsPeriod.MarketDataType, BarsPeriodType = BarsPeriod.BarsPeriodType, Value = BarsPeriod.Value * 8}, Instrument.MasterInstrument.TradingHours.Name, null);
      Question #2C: Must every parameter passed to AddDataSeries() should be 100% hard-coded, or is instrumentName the only parameter that must be hard-coded?

      For everyone's reference, the AddDataSeries() method signature with the most parameters is below:

      AddDataSeries(string instrumentName, BarsPeriod barsPeriod, int barsToLoad, string tradingHoursName, bool? isResetOnNewTradingDay)

      Thank you for all of your help,

      EquityTrader

      Comment


        #4
        Hello EquityTrader,

        2A: Yes, it should be a string and not a string variable.

        2B: Yes, that looks fine.

        2C: Every parameter should be hardcoded. From the helpguide “
        Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables..”



        Please let us know if you need further assistance.’
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Hi NinjaTrader_AlanP,

          Thank you for your answers and for your patience.

          Your answer to Question 2B seems to contradict a quote from the NinjaTrader 8 documentation, so I still need your help in determining which is right and which is wrong, or if maybe both answers are right and I am not understanding something.

          In response to the following code...

          Code:
          AddDataSeries(Instrument.FullName, new BarsPeriod() { MarketDataType = BarsPeriod.MarketDataType, BarsPeriodType = BarsPeriod.BarsPeriodType, Value = BarsPeriod.Value * 8}, Instrument.MasterInstrument.TradingHours.Name, null);
          Which calls the following AddDataSeries overloaded method...

          Code:
          AddDataSeries(string instrumentName, BarsPeriod barsPeriod, int barsToLoad, string tradingHoursName, bool? isResetOnNewTradingDay)
          You wrote:

          2B: Yes, that looks fine.
          However, on the "NinjaScript Best Practices page", located at http://ninjatrader.com/support/helpG..._practices.htm , I found the following quote under its "State management practices" section:

          Note: All additional data series must be added in State.Configure. Since objects such as Instrument, BarsPeriod, TradingHours, etc. are NOT guaranteed to be available until State.DataLoaded, you cannot reliably use the primary instrument properties as arguments in AddDataSeries(). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. In some cases, you may be able to use a BarsRequest() to obtain market data for other instruments and intervals.
          Question 2B-I: Is the code I pasted above really OK to use as you suggested, or in light of the quote NT8 documentation is it actually unsafe to use?

          If the code above is not OK, then could you please answer the following question also:

          Question 2B-II:How can a NinjaScript programmer create a Strategy that can be used on any arbitrary stock in the S&P 500 that also has a secondary data series that differs from the Primary Data Series only in its "tradingHoursName" or "isResetOnNewTradingDay"?

          Note how in the question above, the AddDataSeries() method call can't have a hardcoded instrumentName, and unfortunately there appears to be no AddDataSeries() method overload that lets me set "tradingHoursName" or "isResetOnNewTradingDay" without also having to set the instrumentName. I hope this is not impossible to do.

          Thank you again for your help,

          EquityTrader

          Comment


            #6
            Hello,

            It's been almost a day and a half since I asked the two questions below.

            Can anyone from NinjaTrader give an official answer to the two questions I asked?

            Thank you very much,

            EquityTrader

            Comment


              #7
              Hello EquityTrader,

              Thank you for your patience.

              I misunderstood your question on 2B; the correct answer is that this is not suggested.

              There is no syntax which would allow you to not specify the instrument name and also include a trading sessions template. I will put a feature request for the syntax,
              AddDataSeries(BarsPeriodType periodType, int period, string tradingHoursName)

              You should hard code the instrument name using the following syntax for now,
              AddDataSeries(string instrumentName, BarsPeriod barsPeriod, string tradingHoursName)

              Please let us know if you need further assistance.
              Alan P.NinjaTrader Customer Service

              Comment


                #8
                Hi NinjaTrader_AlanP,

                Thank you very much for your help on this, both for your answer and the feature request.

                I have a suggestion for a feature request regarding this issue that would solve many more problems for NinjaScript developers than only adding one more specific method overload like "AddDataSeries(BarsPeriodType periodType, int period, string tradingHoursName)", which is:

                Feature Request: It should be possible to create a Secondary Data Series with most of its properties exactly matching those of an arbitrary Primary Data Series (i.e. one whose instrumentName, bar period, and other properties are unknown at compile time so can't be hard-coded by the NinjaScript developer), while having some properties that differ from those of the Primary Data Series.

                Probably the simplest way of supporting this feature would be to allow NinjaScript programmers to call the most complete AddDataSeries() method overload with parameter values that refer to properties of the current Strategy, such as Instrument.FullName or BarsPeriod.MarketDataType, like the code below:

                AddDataSeries(Instrument.FullName, new BarsPeriod() { MarketDataType = BarsPeriod.MarketDataType, BarsPeriodType = BarsPeriod.BarsPeriodType, Value = BarsPeriod.Value * 8}, Instrument.MasterInstrument.TradingHours.Name, null);

                To solve the problem described above by adding more AddDataSeries() method overloads, of which there are already eight, is that you will need far more method overloads than the eight that exist already.

                For instance, I asked about solving the same problem for the "isResetOnNewTradingDay" parameter. You would actually need three more overloads just to support the three combinations of these two parameters ("isResetOnNewTradingDay" and "tradingHoursName"), and this would only solve the problem for keeping the instrumentName the same. I would also want to be able to keep the Bar Period Type and Value and the Market Data Type the same, which would cause the number of overloads needed to explode.

                Thank you very much for your help,

                EquityTrader

                Comment


                  #9
                  Hello EquityTrader,

                  Thank you for your suggestion, I have included it with your feature request.

                  Please let us know if you need further assistance.
                  Alan P.NinjaTrader Customer Service

                  Comment


                    #10
                    Hello EquityTrader,

                    Your suggestion is being tracked under feature request ID, SFT-2032.

                    Please let us know if you need further assistance.
                    Last edited by NinjaTrader_AlanP; 03-30-2017, 12:35 PM.
                    Alan P.NinjaTrader Customer Service

                    Comment


                      #11
                      Passing "null" for instrumentName to AddDataSeries() == Primary instrumentName?

                      Hello,

                      In the previous posts in this thread, it has been agreed that the following line of code is not safe:

                      Code:
                      AddDataSeries(Instrument.FullName, new BarsPeriod() { MarketDataType = BarsPeriod.MarketDataType, BarsPeriodType = BarsPeriod.BarsPeriodType, Value = BarsPeriod.Value * 8}, Instrument.MasterInstrument.TradingHours.Name, null);
                      For the following reason, copied from the NT8 user manual:

                      Note: All additional data series must be added in State.Configure. Since objects such as Instrument, BarsPeriod, TradingHours, etc. are NOT guaranteed to be available until State.DataLoaded, you cannot reliably use the primary instrument properties as arguments in AddDataSeries(). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. In some cases, you may be able to use a BarsRequest() to obtain market data for other instruments and intervals.
                      It was also determined that there is no possible way to pass the "Primary Data Series' Instrument Name" into any "AddDataSeries()" method overload that accepts "instrumentName" as a parameter.

                      I believe that this conclusion might be incorrect. While it isn't safe to pass "Instrument.FullName" as the first parameter to this method, I've discovered that passing "null" (without the quotes) as the first parameter seems to reliably create the secondary data series with the instrumentName of the primary data series.

                      Question: Is it true that passing "null" (without the quotes) as the first parameter to the AddDataSeries() method above safely and reliably sets the instrumentName to the Primary DataSeries' InstrumentName?


                      Thank you in advance,

                      EquityTrader

                      Comment


                        #12
                        Hello EquityTrader,

                        No, "null" is not safe for the same reason using the code below is not suggested. While it may work, like the code below, its not suggested.

                        Code:
                        AddDataSeries(Instrument.FullName, new BarsPeriod() { MarketDataType = BarsPeriod.MarketDataType, BarsPeriodType = BarsPeriod.BarsPeriodType, Value = BarsPeriod.Value * 8}, Instrument.MasterInstrument.TradingHours.Name, null);
                        Please let us know if you need further assistance.
                        Alan P.NinjaTrader Customer Service

                        Comment


                          #13
                          Hi NinjaTrader_AlanP,

                          Thank you for your response. Too bad... I was really hoping I had found a safe and reliable workaround to this rather limiting aspect of the NT8 API's AddDataSeries() method.


                          Moving on to another topic... I noticed that passing "null" for the "tradingHoursName" parameter ends up creating a secondary data series with a Trading Hours Template that is the default one for the secondary symbol. For instance, if the primary series is "SPY" and the secondary symbol is "AUDUSD", the Trading Hours Template for the secondary "AUDUSD" series is "Forex".

                          Question #2: Is it safe to pass "null" for the "tradingHoursName" parameter, if the objective is to create a secondary data series that uses the default "Trading Hours Template" for the secondary data series, so long as all other parameters to AddDataSeries() are 100% hard-coded literal values, like "SPY"?

                          Thank you in advance,

                          EquityTrader

                          Comment


                            #14
                            Hello EquityTrader,

                            That would not be recommended.

                            As stated in the helpguide,

                            Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided.


                            Please let us know if you need further assistance.
                            Alan P.NinjaTrader Customer Service

                            Comment


                              #15
                              Hi NinjaTrader_AlanP,

                              Thank you for your answer.

                              For the sake of completeness, I do want to point out an exception to the quote you posted, which is literally on the same page ( https://ninjatrader.com/support/help...dataseries.htm ) as the quote you posted:

                              In the grid titled "Parameters", the description for the "isResetOnNewTradingDay" parameter of the AddDataSeries() method includes the following statement:

                              *Will accept true, false or null as the input. If null is used, the data series will use the settings of the primary data series.
                              So on the same page that we are warned to not rely on such run-time variables as the value for "isResetOnNewTradingDay" of the primary data series, we are told that we are allowed pass "null" to "isResetOnNewTradingDay" to use the settings of the primary data series.

                              It was this ability of "isResetOnNewTradingDay" to use the value from the primary data series at run-time that gave me hope that maybe some of the other parameters to "AddDataSeries()" could also break the rule stated in the quote you posted, but evidently, this very desirable flexibility of the "isResetOnNewTradingDay" parameter unfortunately is unique to it and it alone.

                              I suggest that one of these two contradictory statements on this help page be modified so that they can both be true.

                              Thank you,

                              EquityTrader

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by hurleydood, 09-12-2019, 10:45 AM
                              14 responses
                              1,096 views
                              0 likes
                              Last Post Board game geek  
                              Started by cre8able, Yesterday, 04:16 PM
                              1 response
                              16 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by cre8able, Yesterday, 04:22 PM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by stafe, 04-15-2024, 08:34 PM
                              5 responses
                              28 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by StrongLikeBull, Yesterday, 04:05 PM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X