Announcement

Collapse
No announcement yet.

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()
        {
        if (State == State.SetDefaults) {
        Description = "Plots the Bollinger bands with a forced Daily time frame";
        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);
        } else if (State == State.Configure) {
        AddDataSeries(BarsPeriodType.Day, 1);
        } else if (State == State.DataLoaded) {
        sma = SMA(Period); stdDev = StdDev(Period);
        }
        } protected override void OnBarUpdate() {
        if(BarsInProgress == 1) {
        **double sma0 = sma[0]; double stdDev0 = stdDev[0]; Upper[0] = sma0 + NumStdDev * stdDev0; Middle[0] = sma0; Lower[0] = sma0 - NumStdDev * stdDev0;
        }
        }
        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

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by toughiee, Today, 12:04 AM
                0 responses
                2 views
                0 likes
                Last Post toughiee  
                Started by vdoan20, Yesterday, 08:30 PM
                0 responses
                7 views
                0 likes
                Last Post vdoan20
                by vdoan20
                 
                Started by gupagoer, Yesterday, 07:42 PM
                0 responses
                9 views
                0 likes
                Last Post gupagoer  
                Started by Holligoly, Yesterday, 05:13 PM
                2 responses
                11 views
                0 likes
                Last Post Holligoly  
                Started by Raginpower, Yesterday, 01:53 PM
                0 responses
                14 views
                0 likes
                Last Post Raginpower  
                Working...
                X