Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Adding Daily SMA to Strategy

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

    Adding Daily SMA to Strategy

    I am trying to initialize a multi-time series indicator:

    AddDataSeries(BarsPeriodType.Day, 1);
    SMA2 = SMA(BarsArray[1],X);

    But it does not work within OnStateChange()

    Is there a way to add this daily indicator or must I use the SMA(BarsArray[1],X); in each instance within OnBarUpdate?

    Additionally, am I able plot this indicator from OnStateChange(), if so how?

    #2
    Hello jlkramer16,

    You should be able to use BarsArray[1] in OnStateChange when State == State.DataLoaded as stated in the help guide.

    From the help guide:
    "Warning: This property should NOT be accessed within the OnStateChange() method before the State has reached State.DataLoaded"
    http://ninjatrader.com/support/helpG.../barsarray.htm

    Are you putting the call to the indicator in State.DataLoaded or State.Historical?

    You could then set the values called from this indicator to a plot internal to the calling script that has values set in OnBarUpdate().
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      OK, I was calling it from State.Configure where I set the other indicators and use AddCharIndicator to add them to the chart. Can I call AddChartIndicator to plot this daily indicator within State.DataLoaded or must I plot internally in OnBarUpdate()?

      Also follow up/related question. Is there any reason if I want a daily bar not to use 288 5 min bars (60min/5min *24hrs) instead of AddDataSeries(BarsPeriodType.Day, 1)?
      Last edited by jlkramer16; 01-04-2017, 09:45 AM.

      Comment


        #4
        Hello jlkramer16

        You cannot use AddChartIndicator() on an indicator that is loading a secondary series.

        Instead you would need to create a new plot, call the value of the indicator using a secondary series and set the called value to the value of the plot.

        I am not able to understand your second inquiry. You can add a daily series as a secondary series. You can add a 5 minute series as a secondary series. You can add a 1440 minute secondary series.
        There is no reason you can't add a secondary series of any type you want, as long as the data provider supports that data type.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Right but say my default bar is a 5min bar and I want a 100 Day SMA. Why shouldn't I just use 288*100 of those bars to construct it? What is the benefit of creating a multi-time series and not just aggregating my default bars into long term indicators?

          Comment


            #6
            Hello jlkramer16,

            This is a matter of preference and what you want in your script.

            If you want OnBarUpdate to update every 5 minutes and trigger the logic in your script, then use a 5 minute series. If you only want updates 1 once per day, or you want to supply a daily series to an indicator, then use a daily series.

            Use the series you need use.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Understood. I am having trouble implementing the conversion from a large number of 5 min bars to adding a daily series.

              Strangely when I simply add the line below to State.Configure the script no longer makes any trades when using the strategy analyzer.

              AddDataSeries(BarsPeriodType.Day, 1);

              Also is there any reference or sample code of how to plot an indicator using the secondary bars data?

              Comment


                #8
                Hello jlkramer16,

                I would suggest that you use prints to debug your script to find why the strategy is not placing trades.

                Below I am providing a link to a video that demonstrates using prints to understand behavior as part of the NinjaScript Editor 401 course for NinjaTrader 7.
                https://www.youtube.com/watch?v=K8v_...tu.be&t=48m45s

                I've also quickly demonstrated the same behavior for NinjaTrader 8.
                http://www.screencast.com/t/TvUGSvfxdXt

                Attached, I've created an example that demonstrates plotting a value from an indicator using secondary series as the input series for the indicator.
                Attached Files
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Dear all,

                  I face a similar problem, I worked alongside the MTF strategy example in NT8 trying to Print the daily value of the 200period SMA from a minute strategy script without success, nothing appears in the output window through my code below. Would be great if someone could help!

                  PHP Code:
                          #region Variables
                          
                          
                  private SMA DailySMA1;
                          
                          
                          else if (
                  State == State.Configure)
                              {
                                  
                  AddDataSeries(Data.BarsPeriodType.Day1);
                              }
                              else if (
                  State == State.DataLoaded)
                              {                
                                  
                  DailySMA1                    SMA(BarsArray[1], 200);
                              }

                          protected 
                  override void OnBarUpdate()
                          {
                              if (
                  CurrentBars[0] <= BarsRequiredToTrade)
                              return;

                              if (
                  DailySMA1 == null)
                              {
                                  
                  DailySMA1 SMA(BarsArray[1], 200);
                                  Print (
                  "SMA is" SMA(BarsArray[1], 200)); //Print values to output window function
                                  
                  Print ("DailySMA1 is" DailySMA1); //Print values to output window function
                              

                  Comment


                    #10
                    Hello sagetrade,

                    Your code requires DailySMA1 to be null for the condition to be true.

                    A null value is the same as no value or 'not set'. If a variable has been assigned an object, then the variable is no longer null.

                    Below is a public link to a 3rd party educational site on what null is.
                    https://www.dotnetperls.com/null

                    If SMA(BarsArray[1], 200) has been assigned to DailySMA1, then DailySMA1 is not null.

                    Trying adding an else to confirm this.

                    if (DailySMA1 == null)
                    {
                    DailySMA1 = SMA(BarsArray[1], 200);
                    Print ("SMA is" + SMA(BarsArray[1], 200)); //Print values to output window function
                    Print ("DailySMA1 is" + DailySMA1); //Print values to output window function
                    }
                    else
                    {
                    Print("SMA1 is not null");
                    }


                    Basically, the code is attempting to assign SMA(BarsArray[1], 200); a second time only if DailySMA1 is null, and then print the values of the entire series (not a bar of that series).
                    Last edited by NinjaTrader_ChelseaB; 05-01-2018, 09:02 AM.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Understood. It was simply the way it was done in the example code. Now with having assigned DailySMA1 to SMA(BarsArray[1], 200) within State.DataLoaded, how can i access its value ie. DailySMA1[0] from within OnBarUpdate?

                      Comment


                        #12
                        Hello sagetrade,

                        There should not be an example that triggers an action when an SMA variable is null.

                        Also, I'm not familiar with any examples that assign an indicator variable twice.

                        Do you have a link to the example in question?



                        If DailySMA1 has been assigned an indicator object you can call this with an index in OnBarUpdate.

                        For example:
                        Print(DailySMA1[0]);


                        A great example that demonstrates assigning an indicator to a variable and then using values for this in OnBarUpdate is the SampleMACrossOver strategy that is included with NinjaTrader 8.

                        Click New -> NinjaScript Editor
                        On the right in the explorer pane, double click to example Strategies
                        Double click to open SampleMACrossOver

                        Another example shows how to use a secondary series as an input series when calling an indicator and assigning this to a variable.
                        https://ninjatrader.com/support/foru...d.php?t=109177


                        Also, below is a link to a forum post with helpful information about getting started with NinjaScript.
                        http://ninjatrader.com/support/forum...609#post503609
                        Last edited by NinjaTrader_ChelseaB; 05-01-2018, 11:16 AM.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks Chelsea, I see more clearly now and adjusted the code to the below. It is now bringing me the correct values to the print window and showcases it on the chart as a red dot. This basically serves my needs.

                          The only thing that does not work is showing the indicator on the chart via "AddChartIndicator(DailySMA1);". How can I realise that?


                          PHP Code:
                                  #region Variables
                                  
                                  
                          private SMA DailySMA1;
                                  
                                  
                                  else if (
                          State == State.Configure)
                                      {
                                          
                          AddDataSeries(Data.BarsPeriodType.Day1);
                                      }
                                      else if (
                          State == State.DataLoaded)
                                      {                
                                          
                          DailySMA1 SMA(BarsArray[1], 200);
                          DailySMA1.Plots[0].Brush Brushes.YellowGreen;
                                          
                          AddChartIndicator(DailySMA1);
                                      }

                                  protected 
                          override void OnBarUpdate()
                                  {
                                      if (
                          CurrentBars[0] <= BarsRequiredToTrade)
                                      return;

                                      if (
                          DailySMA1[0] != 0)
                                      {
                                          Print (
                          "DailySMA1 is" DailySMA1[0]); //Print values to output window function
                                          
                          Draw.Dot(this"tag1"true0DailySMA1[0], Brushes.Red); // Use for debugging
                                      

                          Last edited by sagetrade; 05-02-2018, 01:38 PM.

                          Comment


                            #14
                            Hello sagetrade,

                            Assuming this is a strategy, I would theorize that there aren't 200 days of historical days of data on the chart possibly..

                            Is the SampleSecondarySeriesAsInputSeries_NT8 script I have linked plotting on the same chart?

                            If you change the AddDataSeries() call in SampleSecondarySeriesAsInputSeries to:
                            Code:
                            AddDataSeries(BarsPeriodType.Day, 1);
                            does the example script still plot?
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_ChelseaB View Post
                              Hello sagetrade,

                              Assuming this is a strategy, I would theorize that there aren't 200 days of historical days of data on the chart possibly..

                              Strategy is correct. I have at least 220d on the chart.

                              Is the SampleSecondarySeriesAsInputSeries_NT8 script I have linked plotting on the same chart?

                              No. Not for 60m timeframe and not for 1d timeframe charts.

                              If you change the AddDataSeries() call in SampleSecondarySeriesAsInputSeries to:
                              Code:
                              AddDataSeries(BarsPeriodType.Day, 1);
                              does the example script still plot?
                              It does not show anything in the 60m timeframe, but now shows a yellow line in the 1d chart (similar behaviour to my code, SMA(200) is shown only when I change to the daily chart not showing in minute charts, which is what I want to achieve here.
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by southpacific001, Yesterday, 09:25 PM
                              1 response
                              6 views
                              0 likes
                              Last Post Vendor_NT_Mobile  
                              Started by EnveousColnago, Today, 07:39 AM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_ShawnB  
                              Started by fitmar2525, Today, 06:44 AM
                              0 responses
                              8 views
                              0 likes
                              Last Post fitmar2525  
                              Started by David36, Today, 04:30 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post David36
                              by David36
                               
                              Started by cincai, Today, 04:09 AM
                              1 response
                              4 views
                              0 likes
                              Last Post cincai
                              by cincai
                               
                              Working...
                              X