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

Multiple Time Frame Moving Averages - Remove Plots and DataSeries

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

    Multiple Time Frame Moving Averages - Remove Plots and DataSeries

    I'm writing an indicator that dynamically adds moving averages from other time frames based on the primary data series time frame. For example, say I'm on a 5 min chart, the indicator will add the DataSeries and Plots for the 15 min, 60 min, and 240 min time frames. This all works as expected when you first add the indicator, but when you change the primary time frame to something else, I need it to remove all added DataSeries and Plots so that it can add the correct ones for the new time frame.

    I can't seem to find any documentation on this. Is it possible? Right now, it does some weird things and I need to debug to figure out what's going on.

    Thanks

    #2
    Question: "when you change the primary timeframe to something else ... so that it can add the correct ones for the new timeframe." What are those new "correct ones"?

    Comment


      #3
      Originally posted by sandman View Post
      Question: "when you change the primary timeframe to something else ... so that it can add the correct ones for the new timeframe." What are those new "correct ones"?
      "I need it to remove all added DataSeries and Plots so that it can add the correct ones for the new time frame."

      I want to display a certain set of moving averages from different time frames based on the primary period. So in the example I gave, the 5 min chart, I want to plot a moving average for 15 min, 60 min, and 240 min time frames. If I change the period on the same chart to X minutes, I want to remove the old moving averages and replace them with ones from the A, B, and C time frames. This means removing the old added DataSeries and Plots so the indicator starts off clean.

      Comment


        #4
        Hello claxxical,

        Thanks for your inquiry.

        It is not supported to add data series dynamically (for example adding a data series with information dependent on user defined input or on data loaded by the script. However it is worth mentioning that adding data series dynamically does work in many cases and you could very often get away with passing Instrument.FullName in Add() to add a data series based on the primary.

        In NinjaTrader 8, if you are simply adding new time frames, you could pass null as the instrument name in AddDataSeries() and you will add a new time frame relative to the primary data series. Please see the AddDataSeries() documentation for more details.

        AddDataSeries() - https://ninjatrader.com/support/help...dataseries.htm

        When you change the data series the script is applied to, the NinjaScript will go through its life cycle and reload all necessary data before processing it in OnBarUpdate(). Your plots would then be recalculated based on the newly loaded data.

        Please let us know if we can be of further assistance.
        Last edited by NinjaTrader_Jim; 10-05-2018, 08:03 AM.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hi Jim,

          I don't really understand what you mean by:

          "It is not supported to add data series dynamically (for example adding a data series with information dependent on user defined input or on data loaded by the script. However it is worth mentioning that adding data series dynamically does work in many cases and you could very often get away with passing Instrument.FullName in Add() to add a data series based on the primary."

          I'm just going off the example code found here:

          https://ninjatrader.com/support/help...series_obj.htm

          This code clearly demonstrates how to do this. My code also dynamically adds data series without any problems. Only when I change primary time frames do I get strange behavior from not removing the added data series and plots.

          I'm also only coding this in NT7.

          Comment


            #6
            Hello claxxical,

            Those overloads work fine for adding other time frames. "Dynamically adding data series" would mean using Bars, BarsArray, Instrument or user defined input for the Add() method. This will not always be guaranteed, but works in many cases.

            I'm not fully understanding the issue here. If you switch instruments/time frames and change the primary data series, the script will reprocess the new data it is given. For example, the test script below will add another data series and plot that data series in the indicator panel. When we change the primary data series, we see the data for the new instrument gets loaded.

            Code:
                public class AddTest : Indicator
                {
                    private double tempVal;
            
                    protected override void Initialize()
                    {
            			Add( PeriodType.Tick, 10);
                        Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                        Overlay				= false;
                    }
            
                    protected override void OnBarUpdate()
                    {
            			if(BarsInProgress != 0)
            				tempVal = Close[0];
            			else
            				Value[0] = tempVal;
                    }
                }
            Do you see different behavior when testing this script? If not, I may suggest reducing any calculations for your indicator plots and to take debugging steps there to observe that the data from the new instruments is coming in as expected and to better determine how the plots are not showing as you would like.

            If you would like to show a simplified example similar to the above where I can get a better idea on what you're doing, I could give further input.

            I'll be happy to assist you further.
            JimNinjaTrader Customer Service

            Comment


              #7
              Here is what my code looks like in pseudo code. This is how I'm dynamically adding data series:

              Code:
              private var _predefinedTimeFrames = new Dictionary<int, List<int>> ()
              {
              	{ 5, new List<int>() { 5, 15, 60, 240 } },
              	{ 15, new List<int>() { 15, ..., ... } },
              	...
              };
              
              protected override void Initialize()
              {			
              	foreach time frame as defined in _predefinedTimeFrames above, based on the chart's primary time frame
              	{
              		Add(PeriodType.Minute, time frame);
              		Add(new Plot(...));
              	}
              }
              When you first add the indicator, for example to a 5 min chart, you can see the correct Plots in the indicator configuration panel itself. This is all well and good. If I then change that same chart to a 15 minute chart, I expect to see only the correct Plots that I defined in _predefinedTimeFrames for the 15 minute dictionary value. This means that the Plots and DataSeries that were created in the very beginning, for the 5 minute chart, need to be removed so that we can start off in a reset state before adding the 15 minute items. This part, where I change time frames, is where I'm getting weird behavior, so I wanted to know if it's possible to dynamically remove DataSeries and Plots. Since I'm not removing anything, what I would expect to see is all the Plots from the 5 minute setting, PLUS all the Plots for the 15 minute setting, but instead I get a random mixture of both, where only some of the 5 and some of the 15 are there. Sometimes, the entire platform crashes.

              I tried putting the code above that adds items in OnStartUp() but that doesn't even add anything. Does Initialize() run every time the chart's time frame changes? I'm assuming it does right?
              Last edited by claxxical; 10-05-2018, 11:27 AM.

              Comment


                #8
                Hello claxxical,

                There is not a way to remove plots or remove a data series. However, you could hide plots by setting the color to transparent. Plots and data will be offloaded when changing instruments and then the script will return to the Initialize() method.

                My recommendations would be to add debugging prints to your loop to observe what is actually being added for each data series and plot. I may also suggest checking your OnBarUpdate() logic focusing on BarsInProgress indexes, because if you change the number of data series getting loaded, you will have different BarsInProgress indexes.

                Another suggestion would be to add all the plots you may need, and then to hide the plots depending on the data getting loaded, and to ignore setting values to those plots. For example: Plots[0].Pen.Color = Color.Transparent;

                Please let us know if we can be of further assistance.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Will plots and data be offloaded when changing time frames? Seems reasonable as this is a complete reload of data? I don't want to set plots to transparent because it will waste memory and cpu. The clean method would be to completely remove them.

                  Comment


                    #10
                    Through general use, we can see that Initialize and OnStartUp are called again and we should expect data to be reloaded and plots to be reset when changing the primary data series. (This is a "mostly yes" answer.)

                    Dynamically adding data series would not be general use and we can observe other issues when attempting.

                    Please consider the demonstration here - https://drive.google.com/file/d/1p3u...w?usp=drivesdk

                    We always recommend hard coding additional data series and there is not a way to remove a data series or a plot from NinjaScript. There may be cases where this can work, but this is not supported.

                    Please let us know if you have any questions.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Wow thanks a lot for that video. I'm experiencing the same kind of weird issues. So do you suggest hard-coding the max number of DataSeries I expect to support in all my time frames and leaving x number of them unused if they're not needed?

                      Comment


                        #12
                        Just in case you were not aware of this I thought I'd mention this possibility. It does not remove but dynamically changes the DataSeries being used.

                        Add(PeriodType.Minute, BarsPeriod.Value * 3); // with a 5min primary gives 15min //with a 10min primary gives 30min
                        Add(PeriodType.Minute, BarsPeriod.Value * 12); // with a 5min primary gives 60min //with a 10min primary gives 120min
                        Add(PeriodType.Minute, BarsPeriod.Value * 48); // with a 5min primary gives 240min //with a 10min primary gives 480min

                        sandman

                        Comment


                          #13
                          Thanks for the tip sandman.

                          claxxical, My recommendation would be to design the code so it works with hard coded data series. (This is what is supported.) Any approach to load data dynamically based on the primary data series could incur issues, especially if the number of data series changes. So if you would like to take that sort of approach, I would advise to have a working implementation that you can fallback on in case some part does not work.

                          This may not be as ideal as you would like it, but would have to be done within the current scope of support.

                          I'll leave the thread open for any other community members who would like to share their input for loading data in this fashion and any issues they have had to troubleshoot.
                          JimNinjaTrader Customer Service

                          Comment


                            #14
                            sandman thanks for the suggestion! It's a good thought but not what I'm looking for as I want specific time frames shown based on the primary, not fixed multiples. I do a similar thing to this with my moving averages on the charts if I want to fake a larger time frame and it only works for that specific time frame. If I change from a 15 min chart to 60 for example, those fixed intervals are no longer what I want.

                            NinjaTrader_Jim this is exactly what I did and it works now.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by gemify, 11-11-2022, 11:52 AM
                            6 responses
                            803 views
                            2 likes
                            Last Post ultls
                            by ultls
                             
                            Started by ScottWalsh, Today, 04:52 PM
                            0 responses
                            4 views
                            0 likes
                            Last Post ScottWalsh  
                            Started by ScottWalsh, Today, 04:29 PM
                            0 responses
                            7 views
                            0 likes
                            Last Post ScottWalsh  
                            Started by rtwave, 04-12-2024, 09:30 AM
                            2 responses
                            22 views
                            0 likes
                            Last Post rtwave
                            by rtwave
                             
                            Started by tsantospinto, 04-12-2024, 07:04 PM
                            5 responses
                            70 views
                            0 likes
                            Last Post tsantospinto  
                            Working...
                            X