Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Displaying Multiple timeframe indicators on a strategy

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

    Displaying Multiple timeframe indicators on a strategy

    //
    // Copyright (C) 2024, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //
    region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class SampleMultiTimeFrameTwoStochastics : Strategy
    {
    private Series<double> primarySeries;
    private Series<double> secondarySeries;

    private Stochastics fifteenSecNineStochastic;

    private Stochastics OneMinNineStochastic;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    //Description = NinjaTrader.Custom.Resource.NinjaScriptStrategyDes criptionSampleMultiTimeFrameFourStoch;
    Name = "SampleMultiTimeFrameTwoStochastics"; //NinjaTrader.Custom.Resource.NinjaScriptStrategyNam eSampleMultiTimeFrameFourStoch;
    // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = false;
    }
    else if (State == State.Configure)
    {

    // Add a 15 minute Bars object to the strategy
    AddDataSeries(Data.BarsPeriodType.Second, 30);

    // Add a 5 minute Bars object to the strategy
    AddDataSeries(Data.BarsPeriodType.Minute, 1);

    }
    else if (State == State.DataLoaded)
    {
    fifteenSecNineStochastic = Stochastics(BarsArray[1], 3,9,1);
    fifteenSecNineStochastic.IsOverlay = false;
    fifteenSecNineStochastic.Panel = 2;
    AddChartIndicator(fifteenSecNineStochastic);


    OneMinNineStochastic = Stochastics(BarsArray[2],3, 14,1);
    OneMinNineStochastic.IsOverlay = false;
    OneMinNineStochastic.Panel = 3;
    AddChartIndicator(OneMinNineStochastic);
    }
    }

    protected override void OnBarUpdate()
    {
    if (OneMinNineStochastic.D[0] < 20 && fifteenSecNineStochastic.D[0] < 20)
    {
    Draw.ArrowUp(this, "SwingHigh_" + CurrentBar, false, 0, Low[0] - TickSize * 2, Brushes.Green);
    }

    }
    }
    }

    ​[/CODE]

    #2
    I should add that at this point it seems that only one of the indicators is showing up. The first stochastic should come from the 30 second time frame and that is showing. The second stochastic should be calculated from the 1 min time frame, there is a frame fro it but it is empty,

    Comment


      #3
      Do you only have those two data series? Or are you adding those two data series in addition to the one you already have? If it is only the two, then shouldn't it be BarsArray[0] and [1]?

      Comment


        #4
        I'm not totally sure how to answering that. I built this from what comes with Ninja trader as an example for a multi time frame strategy. I'm not sure I totally understand. But I put this strategy on a 30 second chart. And only one stochastic shows up.

        Comment


          #5
          Hmm..I've copy/pasted your code, and aside from needing to add a BarsRequired portion to the code, I found that I actually did get both to plot on the screen....sort of. I'm currently in playback mode and I only have data for one day, but both stochastics start at the same time, but the second one ends in the middle of the day. If you set your strategy and scroll to the beginning, do you see it plotting temporarily?

          Comment


            #6
            Did you have to add any other configurations? Does it only need one data aeries? Sorry for the simple questions I'm kind of new to this.

            Comment


              #7
              I'm not entirely sure. I haven't really worked with the stochastic too much, I added Prints when you calculate the .D[0] for both, but they don't seem to be adding up to the values being plotted on the charts, and there is that issue of it cutting off for the 1 min one.

              Comment


                #8
                Where did you get this code? Do you have a copy of the original?

                Comment


                  #9
                  Ok. BarsArray is correct. I added the Print(BarsArray[1].BarsPeriod) to determine the type, and they are 30 sec and 1 min for Bars[1] and 2 respectively.

                  Comment


                    #10
                    I'm wondering if it's the fact that I was on the 30 sec and the added time frame was the 1 min. It almost seemed like it was adding it's plots after every bar, so it ended halfway through. I tried to reverse it, and use the 1 min chart and added the 30 sec on there and now both show up from start to finish.

                    Comment


                      #11
                      It seems that for display purposes using the larger 1 min time frame as the primary works better. Weird result if you do the lower first. Thanks for looking at this!

                      Comment


                        #12
                        Looking at this more I realize that it does no display effectively because of how it processes bars. It doesnt really align them up to each other. the reason you get one not showing up when you do the lower time frame as the primary is because it just dumps out all the 1 min but does not space them out to align with the 30 second bars.

                        Comment


                          #13
                          Yeah, it seems so. I searched the Ninjatrader forums and found this for a related topic.

                          Note from the AddChartIndicator() help guide:

                          "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)"

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by stafe, 03-10-2025, 12:09 PM
                          7 responses
                          33 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by IanS00, Today, 01:55 PM
                          1 response
                          6 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by Marble, Today, 05:00 AM
                          2 responses
                          19 views
                          0 likes
                          Last Post Marble
                          by Marble
                           
                          Started by wallace15269, 01-16-2025, 12:05 PM
                          5 responses
                          98 views
                          0 likes
                          Last Post wallace15269  
                          Started by several, 03-18-2025, 03:53 AM
                          9 responses
                          123 views
                          1 like
                          Last Post ntwong
                          by ntwong
                           
                          Working...
                          X