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

Setting a number of days in AddDataSeries

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

    Setting a number of days in AddDataSeries

    I am trying to get the value of a 22 Period Daily Moving average. I am using the following to add the series:

    Code:
    AddDataSeries("SPY",BarsPeriodType.Day,1,22);
    I understand that to be adding 22 Daily bars. Is this not the correct way? I am getting an error that these are not the correct parameters. I just need to calc the 22 period average of those one time to set a fixed value that the indicator will use. Any help with the MA would be a bonus. Thanks

    #2
    Hello swcookie,

    The error would be correct, there is no overload pattern like this. You can find the accepted patterns in the help guide here: https://ninjatrader.com/support/help...=adddataseries

    The closest to what you are asking for would be the overload that includes a number of bars to load. You could calculate the number of bars you want that would equate to 22 days and then add that. For a daily series you would just need to use 22 for 22 Bars of daily data.


    Code:
    AddDataSeries(string instrumentName, BarsPeriod barsPeriod, int barsToLoad, string tradingHoursName, bool? isResetOnNewTradingDay)
    Alternatively, you would add just the daily series using the normal overload set,
    Code:
    AddDataSeries(string instrumentName, BarsPeriodType periodType, int period)
    and then use 22 days to load in the chart or where the strategy is applied.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      I am having a little trouble with the overload pattern. This is what I am using:
      Code:
      AddDataSeries("SPY",BarsPeriodType.Day,22,"Use instrument settings",true);
      Still not able to compile. I am not seeing any real life examples on the forum or in the docs, of what this looks like when its overloaded this way. Can you assist?

      Comment


        #4
        Hello swcooke,

        Thanks for the reply.

        The method is expecting you to make a new BarPeriod object and enter the name of the session template you want to use:

        AddDataSeries("SPY", new BarsPeriod { BarsPeriodType = BarsPeriodType.Day, Value = 1 }, 22, "US Equities RTH", true);

        To find the default trading hours template an instrument is using, go to Tools>Instruments>Search the instrument>Open the instrument configuration menu>look at the Trading Hours field.

        Please let me know if I can assist further.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Thanks for that. I am trying to calculate the 22 period MA 1 time at the beginning of the script so that the script can use its' value. Here is what I have but it does not match the 22 period MA that I add to a chart as just a basic indicator:

          Code:
          else if (State == State.Configure)
          {
              AddDataSeries("SPY", new BarsPeriod { BarsPeriodType = BarsPeriodType.Day, Value = 1 }, 22, "US Equities RTH", true);
          }
          else if (State == State.DataLoaded)
          {
              Print("Value is " + SMA(BarsArray[1], 22)[0]);  //this does not match what an SMA Indicator is returning for the last bar
          }

          Comment


            #6
            Hello swcooke,

            Thank you for the reply.

            In this case, you need to move your logic out of OnStateChange to OnBarUpdate. You cannot access price or indicator values in DataLoaded.

            Additionally, if you access the indicator on the first bar or once, that would either be before the BarsRequiredToPlot or would just be the first value for your indicator. The SMA changes over time so to get the same values you see on the chart, you would have to check its value for each OnBarUpdate call.

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by bortz, 11-06-2023, 08:04 AM
            47 responses
            1,611 views
            0 likes
            Last Post aligator  
            Started by jaybedreamin, Today, 05:56 PM
            0 responses
            9 views
            0 likes
            Last Post jaybedreamin  
            Started by DJ888, 04-16-2024, 06:09 PM
            6 responses
            19 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by Jon17, Today, 04:33 PM
            0 responses
            6 views
            0 likes
            Last Post Jon17
            by Jon17
             
            Started by Javierw.ok, Today, 04:12 PM
            0 responses
            22 views
            0 likes
            Last Post Javierw.ok  
            Working...
            X