Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using a DataSeries as source input for an Indicator (bis)

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

    Using a DataSeries as source input for an Indicator (bis)

    I am still trying to use a DataSeries object as input for an Indicator, and I am boiling it down to the very basic ...

    ... PlotInput is a simple Indicator (overlay on chart), which only difference with the default generated script is to use Input[0] instead of Close[0].
    (well, not really, it also set the Pen witdth: Plots[0].Pen.Width = 5; )

    The following strategy just use a DataSeries object to hold the Close values, and uses the PlotInput indicator on that DataSeries object.

    By default, Ninja uses MaximumBarsLookBack = 256 and the results are beyond my understanding (see attached).


    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Enter the description of your strategy here
    /// </summary>
    [Description("Enter the description of your strategy here")]
    public class MyCustomStrategy : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int myInput0 = 1; // Default setting for MyInput0
    // User defined variables (add any user defined variables below)
    private DataSeries aDataSeries;
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    aDataSeries = new DataSeries(this);
    Add( PlotInput( aDataSeries ) );
    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    aDataSeries.Set( Close[0] );
    PlotInput( aDataSeries );
    }

    #region Properties
    [Description("")]
    [GridCategory("Parameters")]
    public int MyInput0
    {
    get { return myInput0; }
    set { myInput0 = Math.Max(1, value); }
    }
    #endregion
    }
    }
    Attached Files

    #2
    dom,

    Could you post the PlotInput() code?
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      There you go :

      #region Using declarations
      using System;
      using System.ComponentModel;
      using System.Diagnostics;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Data;
      using NinjaTrader.Gui.Chart;
      #endregion

      // This namespace holds all indicators and is required. Do not change it.
      namespace NinjaTrader.Indicator
      {
      /// <summary>
      /// This Indicator just plots Input[0]
      /// </summary>
      [Description("This Indicator just plots Input[0]")]
      public class PlotInput : Indicator
      {
      #region Variables
      // Wizard generated variables
      // User defined variables (add any user defined variables below)
      #endregion

      /// <summary>
      /// This method is used to configure the indicator and is called once before any bar data is loaded.
      /// </summary>
      protected override void Initialize()
      {
      Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Dot, "Plot0"));
      Plots[0].Pen.Width = 5;
      Overlay = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Use this method for calculating your indicator values. Assign a value to each
      // plot below by replacing 'Close[0]' with your own formula.
      Plot0.Set(Input[0]);
      }

      #region Properties
      [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
      [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
      public DataSeries Plot0
      {
      get { return Values[0]; }
      }

      #endregion
      }
      }

      #region NinjaScript generated code. Neither change nor remove.
      // This namespace holds all indicators and is required. Do not change it.
      namespace NinjaTrader.Indicator
      {
      public partial class Indicator : IndicatorBase
      {
      private PlotInput[] cachePlotInput = null;

      private static PlotInput checkPlotInput = new PlotInput();

      /// <summary>
      /// This Indicator just plots Input[0]
      /// </summary>
      /// <returns></returns>
      public PlotInput PlotInput()
      {
      return PlotInput(Input);
      }

      /// <summary>
      /// This Indicator just plots Input[0]
      /// </summary>
      /// <returns></returns>
      public PlotInput PlotInput(Data.IDataSeries input)
      {
      if (cachePlotInput != null)
      for (int idx = 0; idx < cachePlotInput.Length; idx++)
      if (cachePlotInput[idx].EqualsInput(input))
      return cachePlotInput[idx];

      lock (checkPlotInput)
      {
      if (cachePlotInput != null)
      for (int idx = 0; idx < cachePlotInput.Length; idx++)
      if (cachePlotInput[idx].EqualsInput(input))
      return cachePlotInput[idx];

      PlotInput indicator = new PlotInput();
      indicator.BarsRequired = BarsRequired;
      indicator.CalculateOnBarClose = CalculateOnBarClose;
      #if NT7
      indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
      indicator.MaximumBarsLookBack = MaximumBarsLookBack;
      #endif
      indicator.Input = input;
      Indicators.Add(indicator);
      indicator.SetUp();

      PlotInput[] tmp = new PlotInput[cachePlotInput == null ? 1 : cachePlotInput.Length + 1];
      if (cachePlotInput != null)
      cachePlotInput.CopyTo(tmp, 0);
      tmp[tmp.Length - 1] = indicator;
      cachePlotInput = tmp;
      return indicator;
      }
      }
      }
      }

      // This namespace holds all market analyzer column definitions and is required. Do not change it.
      namespace NinjaTrader.MarketAnalyzer
      {
      public partial class Column : ColumnBase
      {
      /// <summary>
      /// This Indicator just plots Input[0]
      /// </summary>
      /// <returns></returns>
      [Gui.Design.WizardCondition("Indicator")]
      public Indicator.PlotInput PlotInput()
      {
      return _indicator.PlotInput(Input);
      }

      /// <summary>
      /// This Indicator just plots Input[0]
      /// </summary>
      /// <returns></returns>
      public Indicator.PlotInput PlotInput(Data.IDataSeries input)
      {
      return _indicator.PlotInput(input);
      }
      }
      }

      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      public partial class Strategy : StrategyBase
      {
      /// <summary>
      /// This Indicator just plots Input[0]
      /// </summary>
      /// <returns></returns>
      [Gui.Design.WizardCondition("Indicator")]
      public Indicator.PlotInput PlotInput()
      {
      return _indicator.PlotInput(Input);
      }

      /// <summary>
      /// This Indicator just plots Input[0]
      /// </summary>
      /// <returns></returns>
      public Indicator.PlotInput PlotInput(Data.IDataSeries input)
      {
      if (InInitialize && input == null)
      throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

      return _indicator.PlotInput(input);
      }
      }
      }
      #endregion

      Comment


        #4
        dom,

        Just quickly glancing I can't see anything wrong with this setup, however if you notice it looks like the plot is periodic. In other words it repeats itself halfway through that one chart you have there. I will try to replicate on my end and play with it a little to see if I can identify some issue.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Hint: MaximumBarLookBack = 256 doesn't work at all for DataSeries (and probably IntSeries, etc).

          I have already proved (in other thread) that ContainsValue(0) returns BS when MaximumBarLookBack = 256, which led me to question the very basic functionality of DataSeries when MaximumBarLookBack = 256 ... and as you can see by yourself, it just doesn't work.

          Comment


            #6
            dom,

            For plots MaximumBarsLookBack is overridden to be infinite. Also, I am aware that the DataSeries.Count will keep increasing even though the data series may have reached its 256 limit. This needs to be checked manually unfortunately. Any data series you add internally that is not a plot will have MaximumBarsLookback to 256 as long as its set however, plots are the only things that will override to infinite.
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              Unless I misunderstand you, MaximumBarsLookBack = 256 *doesn't work* for DataSeries (& others), you guys know it but rather pretend it works than fix it.

              Correct?

              Comment


                #8
                dom,

                DataSeries objects that hold indicator plot values are always set to MaximumBarsLookBack.Infinite which ensures that charts will always display the entire indicator's calculated values.
                Adam P.NinjaTrader Customer Service

                Comment


                  #9
                  Adam,

                  Get back to my original code & screen capture. As you admitted a few posts ago, there is nothing wrong with the code and it doesn't work.

                  I told you it doesn't work when MaximumBarsLookBack = 256. This is a fact, and it is a bug. I can wait for that bug to be fixed, but if you keep arguing & don't take it to the development team it will never get fixed.

                  Comment


                    #10
                    dom993,

                    There is no argument here. I am attempting to help you resolve your issue, explain it, and/or find a resolution even if that means reporting to development. I am trying to help you, not trying to obfuscate anything.

                    I was describing to you the behaviors of MaximumBarsLookback according to what I have seen, read, and/or tested on my end. I only briefly glanced at your code and it looked ok with this quick glance. I didn't have time to finish testing but I am finished testing now.

                    In MyCustomStrategy you are creating a dataseries which is not a plot that has MaximumBarsLookBack set to 256. Then, you are passing this series to your PlotInput indicator. The PlotInput indicator then assigns this data series to the plot inside that indicator, which has MaximumBarsLookBack set to Infinite. Essentially this is passing a 256 series to something that expects an Infinite series. I will mention this to development to see if they could eliminate the looping behavior you see in future releases.

                    Most indicators were designed to expect an Infinite look back data series since this is generally what a plot is set to, and in fact even if you set your plots to be 256 it will override and set them to Infinite automatically. However, there is an option in NinjaTrader to design your indicators to expect 256 look back based-input, and you can find a link below that explains how to do this.



                    This fixes your MyCustomStrategy code :

                    Code:
                    protected override void Initialize()
                    {	
                    	CalculateOnBarClose = true;
                    	MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
                    	aDataSeries = new DataSeries(this);
                    	Add( PlotInput( aDataSeries ) );
                    }
                    Though alternatively you could try to change the PlotInput to have ForceMaximumBarsLookBack256 = true;

                    Please let me know if I may assist further.
                    Last edited by NinjaTrader_AdamP; 05-15-2012, 02:51 PM.
                    Adam P.NinjaTrader Customer Service

                    Comment


                      #11
                      Adam,

                      You are not helping me by stating that using MaximumBarsLookBack.Infinite "fixes" my code ... my code is not broken the least, and what you are suggesting (and that I knew from the beginning) is a workaround, not a fix.

                      A fix would allow me to work with MaximumBarsLookBack.TwoHundredFiftySix for my DataSeries. My issue is memory preservation, and the workaround does nothing at all for it.

                      Also, from the documentation (Ninjascript/Indicator/Add()) :

                      "Adds plot and line objects that define how an indicator data series is visualized on a chart. When this method is called to add a plot, an associated DataSeries object is created that is held in the Values collection."

                      Would you say that the associated DataSeries that is created by Ninja in the indicator Add() function somehow forgets to override MaximumBarsLookBack to Infinite ? I certainly think so.

                      Comment


                        #12
                        Hello,

                        This is Brett responding for Adam.

                        Your case here is very similar to what I am already working with you on another thread. Before we do anything further on this I want to clarify the other issue first as that needs attention first and most likely is related. We will come back to this ones that one has been clarified.
                        BrettNinjaTrader Product Management

                        Comment


                          #13
                          Good - thanks - keep me posted

                          Comment


                            #14
                            Alright so with the TwoHundredFiftySix thread answered in the end was unrelated to this thread.

                            I have reviewed this thread again.

                            The bottom line is this line here not supported:

                            Add( PlotInput( aDataSeries ) );

                            It may or may not work. NinjaTrader does not support dynamic Add calls, you can try to do them however you may run into nuances or issues.. Instead you would need to use the StrategyPlot sample if you needed to plot something from a strategy.

                            When running a strategy on a chart you may find the need to plot values onto a chart. If these values are internal strategy calculations that are difficult to migrate to an indicator, you can use the following technique to achieve a plot. NinjaTrader 8 With NinjaTrader 8 we introduced strategy plots which provide the ability


                            As far as allowing this functionality in NinjaTrader this is something that I have added a request into development to look into the possibility of supporting this in a next major release.

                            Let me know if any questions.
                            BrettNinjaTrader Product Management

                            Comment


                              #15
                              Brett,

                              I tried that example on a chart with 5,000 bars, using the default for MaximumBarsLookBack for everything (hence, 256) ... the Plots are only displayed for the last 256 bars or the chart, which would indicate that the DataSeries used in Values[] are limited to 256 elements?

                              Does this contradicts the following (from MaximumBarsLookBack help) : "DataSeries objects that hold indicator plot values are always set to MaximumBarsLookBack.Infinite which ensures that charts will always display the entire indicator's calculated values.", or am I undertsanding something wrong?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              637 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              366 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              107 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              569 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              571 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X