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

Plotting secondary data series indicator on primary data series chart

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

    Plotting secondary data series indicator on primary data series chart

    Hello Ninjatrader community,

    I have read a bunch of forum posts on the subject and tried a few ways of coding this, but I still can't make it work.

    Here is what I am trying to do:
    I am trying to create a strategy that will use AND display multiple time frame indicators. To be exact, the strategy should display the same indicator but with different data series as input.

    So, for example, I would like to run my strategy on a 15 minutes chart (primary series) and display the EMA indicator based on this data series. This works, the EMA is displayed correctly using the 15 minute series as input. Additionally, I would also like to plot the EMA based on a 60 minute time frame, on the same 15 minute chart, and also be able to use this indicator for my calculations.

    To do this, I followed this advice from the "AddChartIndicator" page of the help guide, where it says : "An indicator being added via AddChartIndicator() cannot use any additional data series hosted by the calling strategy, but can only use the strategy's primary data series. If you wish to use a different data series for the indicator's input, you can add the series in the indicator itself and explicitly reference it in the indicator code (please make sure though the hosting strategy has the same AddDataSeries() call included as well)". So, I created a copy of the EMA indicator (HourlyEMA) which uses an additional data series (60 minutes) and changed all calculations of that indicator to use this new 60 minutes data series. In my strategy, I also added the 60 minutes data series and tried plotting/referencing my indicator like this:

    Code:
    var hourlyEma = HourlyEMA(Closes[1], 9);
    hourlyEma.Plots[0].Brush = Brushes.Red;
    hourlyEma.Plots[0].Width = 1;
    AddChartIndicator(hourlyEma);
    Here is my problem: the indicator is plotted like a step-ladder (see attached image).

    I believe this problem is referenced in the "Multi-Time Frame & Instruments" page of the help guide, where it says : "A multi-series indicator will hold the same number of data points for plots as the primary series. Setting values to plots should be done in the primary series in OnBarUpdate(). If you are using calculations based off of a larger secondary series, it may plot like a step ladder because there are more data points available than there are actual meaningful data values."

    I guess that when plotting the 60 minutes indicator, the strategy still uses the data points of the 15 minutes chart, which makes the 60 minutes EMA plotted only at the beginning of the chart.

    How could I solve this problem, and have my 60 minutes indicator plotted correctly on my 15 minutes chart AND be able to use this 60 minutes indicator for my calculations?

    Thanks a lot,
    Alex
    Last edited by alexismailhot; 09-04-2019, 05:07 AM.

    #2
    Hello alexismailhot,

    Thank you for your post.

    I think the issue may be in how you're handling the indicator. I'm attaching an example that plots this properly. Basically, in the indicator part, you want to make sure you're setting the values of the series for the primary bars to be the same as whatever the value for the secondary series is. (This is what causes the "Stairstep" effect you'll see on the chart.) Then in your strategy, you're accessing the values via the primary series and plotting those on the chart. Take a look at the attached strategy and indicator and I think this will make more sense.

    Please let us know if we may be of further assistance to you.

    Attached Files
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      That was exactly my problem, thank you very much!

      Comment


        #4
        Hate to revive an old thread but this is the easiest place for me to start.

        I am attempting to create a Daily Bollinger overlay/indicator to be used in a strategy. I re-coded the indicator to have the added DataSeries, and process its data in the secondary time series.

        Debugging in Visual studio confirms I am reaching the code block within OnBarUpdate() that updates the SMA/Upper/Lower bands. But it appears it only hits the breakpoint twice. When running the strategy in the strategy analyzer and looking at the chart, nothing is plotting.

        Here is the re-coded indicator body:
        Code:
        protected override void OnStateChange()
        {[INDENT]if (State == State.SetDefaults)
        {[/INDENT][INDENT=2]Description = "Plots the Bollinger bands with a forced Daily time frame";[/INDENT][INDENT=2]Name = "DailyBollinger";
        IsOverlay = true;
        IsSuspendedWhileInactive = true;
        NumStdDev = 2;
        Period = 20;
        
        AddPlot(Brushes.Goldenrod, NinjaTrader.Custom.Resource.BollingerUpperBand);
        AddPlot(Brushes.Goldenrod, NinjaTrader.Custom.Resource.BollingerMiddleBand);
        AddPlot(Brushes.Goldenrod, NinjaTrader.Custom.Resource.BollingerLowerBand);[/INDENT][INDENT]}
        else if (State == State.Configure)
        {[/INDENT][INDENT=2]AddDataSeries(BarsPeriodType.Day, 1);[/INDENT][INDENT]}
        else if (State == State.DataLoaded)
        {[/INDENT][INDENT=2]sma = SMA(Period);
        stdDev = StdDev(Period);[/INDENT][INDENT]}[/INDENT]
         
         }
        
        protected override void OnBarUpdate()
        {[INDENT]if(BarsInProgress == 1)
        {[/INDENT][INDENT=2]**double sma0 = sma[0];
        double stdDev0 = stdDev[0];
        
        Upper[0] = sma0 + NumStdDev * stdDev0;
        Middle[0] = sma0;
        Lower[0] = sma0 - NumStdDev * stdDev0;[/INDENT][INDENT]}[/INDENT]
         
         }
        NOTE: the ** indicates breakpoint

        The calling strategy contains the added data series, so I am struggling to figure out why this isn't working. I am posting because digging into the GUI / drawing code base may be deeper then I want to go right now.
        Last edited by BraisedInBlue; 11-25-2020, 09:06 PM.

        Comment


          #5
          Hello BraisedInBlue,

          Thank you for your post.

          You've got a couple issues there. I believe your indicator may in fact have been plotting, but since you only assign values to your plots once a day when the Daily series processes, you wouldn't see a consistent plot. Also, your SMA and StdDev were being calculated off the primary series, so I've changed that in the attached revised version as well. Let me know if you don't see this version plotting as you'd expect.

          Please let us know if we may be of further assistance to you.
          Attached Files
          Kate W.NinjaTrader Customer Service

          Comment


            #6
            Unfortunately, when I use this it doesnt seem to be plotting in the chart view after a back test with the strategy analyzer. If I use the normal bollinger indicator it will plot in the chart view of the backtest.

            The changes you made make sense to me, I definitely was missing the use of the secondary series for the SMA/STD dev.

            From what I can see in the debugger, the secondary data series isn't propagating (CurrentBars[1] is staying -1).

            I can confirm that the strategy code is iterating the multiple bar objects / time-frames okay, so I am not sure why the indicator is unable to load the daily data.

            I tested the code out in a normal chart (not within the strategy analyzer) and it does work. So I am not sure why the secondary series wont process when within the strategy analzyer.
            Last edited by BraisedInBlue; 11-27-2020, 02:59 PM.

            Comment


              #7
              Hello BraisedInBlue,

              Thank you for your reply.

              I've created an example strategy that plots that Daily Bollinger in a backtest. Note, I've made a further change to DailyBollingerRevised so when you import this and it asks you if you want to replace that script, say yes.

              The big thing to note is that since you need at least the number of Daily bars as the Period you'll need to load more data in the Strategy Analyzer than just the days you actually want the strategy to be tested over. So for example if I wanted to test over the past 10 trading days but my period for the Daily Bollinger is 20, I will need to load 30 days worth of data in the Strategy Analyzer so it has that first 20 days to compute the Daily Bollinger.

              Also of note is that the Daily series used in the Daily Bollinger would also need to be added to the strategy.

              Let me know if you're not seeing this example work in the Strategy Analyzer with at least 30 days loaded.

              Please let us know if we may be of further assistance to you.
              Attached Files
              Kate W.NinjaTrader Customer Service

              Comment


                #8
                Thank you for the update, I ended up stumbling upon a similar update that was needed for it to graph properly. Once I got the correct SDK version setup in VS I was able to use the debugger and figured out what was causing the indicator to puke.

                Comment


                  #9
                  I thought other folks seeking help with this topic might have a similar question but on a slightly different use case--so if this should be posted in its own thread let me know.

                  I have a 900-Tick chart with a MACD indicator plotting. I want to plot a dot on the MACD.Default plot line every 300 ticks

                  I was thinking of creating an indicator that would be placed on the 900-Tick chart
                  * create a secondary data series of 300 Tick,
                  * then on OnBarUpdate: if( BarsInProgress == 1 && IsFirstTickOfBar)
                  * set the Value[1] plot value to the then live value from the MACD.Default indicator
                  * configure the plot to plot dots
                  * (I will also plot a 2nd MACD.Default value configured as a line - so these dots show up on top of a line)

                  I can't determine how to insure the 300-Tick (dot value) value being assigned on this indicator is the then current "last tick' value of the MACD.Default



                  Any help would be appreciated.

                  Comment


                    #10
                    Hello EminiTrader,

                    Thank you for your reply.

                    I'm a little unclear on what you are trying to achieve here. It's important to keep in mind that in a multi-series script, all plots will always be synced to the primary series. While you can assign values to a plot from a secondary series, if the secondary series is of a shorter time frame than the primary, and you are assigning to the plot from the secondary, this will simply update that value multiple times. Can you give a little more detail or a mockup of what you're trying to achieve so I can better understand if what you're looking to do would be possible using plots?

                    Thanks in advance; I look forward to assisting you further.
                    Kate W.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Kate View Post
                      Hello alexismailhot,

                      Thank you for your post.

                      I think the issue may be in how you're handling the indicator. I'm attaching an example that plots this properly. Basically, in the indicator part, you want to make sure you're setting the values of the series for the primary bars to be the same as whatever the value for the secondary series is. (This is what causes the "Stairstep" effect you'll see on the chart.) Then in your strategy, you're accessing the values via the primary series and plotting those on the chart. Take a look at the attached strategy and indicator and I think this will make more sense.

                      Please let us know if we may be of further assistance to you.
                      Hello,

                      I'm trying to plot a Swing(5) from a 5-minute data series on a 1-minute data series chart. I can see the information you provided in the quote is more or less what I need, but I don't know how to translate the solution to the Swing indicator. Do I need to make a secondary series copy of the swing indicator? Can you please guide me on how this can be done?

                      Thank you

                      Bob Perez

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by yertle, Today, 08:38 AM
                      6 responses
                      24 views
                      0 likes
                      Last Post ryjoga
                      by ryjoga
                       
                      Started by algospoke, Yesterday, 06:40 PM
                      2 responses
                      24 views
                      0 likes
                      Last Post algospoke  
                      Started by ghoul, Today, 06:02 PM
                      3 responses
                      15 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by jeronymite, 04-12-2024, 04:26 PM
                      3 responses
                      46 views
                      0 likes
                      Last Post jeronymite  
                      Started by Barry Milan, Yesterday, 10:35 PM
                      7 responses
                      23 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Working...
                      X