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

Initializing a public DataSeries

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

    Initializing a public DataSeries

    After Ray suggested I needed to make a few shared data variables public, I discovered that whenever I tried to save the chart template, I would get the error, "Indicator 'NinjaTrader.Indicator.blah' could not be serialized"

    Sifting through the forum and docs, it seems I need to move the public DataSeries to the Properties section, which I have done. But when trying to load the indicator, I get an array out of bounds error which suggests that I need to initialize the DataSeries.

    How do I do that? Everything I have tried leads to compile error.

    #2
    Hello,

    There are instructions on how to initialize a data series in this link:



    It may look something like this:

    protected override void Initialize()
    {
    myDataSeries = new DataSeries(this);

    }

    If you post some code we can take a look at it.
    DenNinjaTrader Customer Service

    Comment


      #3
      That method complains loudly if I am trying to initialize an already public DataSeries declared in properties section...

      Here is what I have in the properties section:

      #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 HistTickUp
      {
      get { return Values[0]; }
      }


      The Indicator blows up right at first call to HistTickUp.Set(0);

      "Array out of bounds"

      There is no other declaration of HistTickUp in the file since that too will cause compiler errors.

      Comment


        #4
        Hello,

        Your code looks OK from what you posted. The issue is that you are accessing array elements that do not exits yet. This link should resolve it:


        If it does not, please post all of your code.
        DenNinjaTrader Customer Service

        Comment


          #5
          Already performing that suggested function. It does not matter how many bars I see before doing the first .Set, it blows up.

          Including all of the code. I've commented out the approach which will allow the Indicator to run, but will not allow me to save it in a template.

          Code:
          #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>
              /// Rahul Mohindra Indicator
              /// </summary>
              [Description("Rahul Mohindar Indicator v2.0.0")]
              public class RMO : Indicator
              {
                  #region Variables
                  // Wizard generated variables
                      private int len1 = 2; // Default setting for Len1
                      private int len2 = 10; // Default setting for Len2
                      private int len3 = 30; // Default setting for Len3
                      private int len4 = 81; // Default setting for Len4
                      private bool    soundAlert        = true;
                      private string    alertFile        = "Alert4.wav";
                      private    int        alertInterval     = 60;
                      // User defined variables (add any user defined variables below)
          //            public DataSeries    vST1;
          //            public DataSeries    vST2;
          //            public DataSeries    vST3;
          //            public DataSeries    plotRMO;
                  #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(new Pen(Color.Blue, 4), PlotStyle.Bar, "Bull Trend"));
                      Add(new Plot(new Pen(Color.Red, 4), PlotStyle.Bar, "Bear Trend"));
                      Add(new Plot(new Pen(Color.Gray, 4), PlotStyle.Bar, "Gray Zone"));
              
                      Add(new Line(Color.Gold, 0, "ZeroLine"));
                      Lines[0].Pen.DashStyle = DashStyle.Dot;
                      Lines[0].Pen.Width = 2;
                      
                      CalculateOnBarClose    = false;
                      Overlay                = false;
                      PriceTypeSupported    = false;
                      DrawOnPricePanel    = true;
                      PaintPriceMarkers    = false;
                      AutoScale             = true;
                      
          //            vST1 = new DataSeries(this);
          //            vST2 = new DataSeries(this);
          //            vST3 = new DataSeries(this);
          //            plotRMO = new DataSeries(this);
                  }
                  
                  
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                      if (CurrentBar < 15) {
                          return;
                      }
                      
                      vST1.Set(100 *
                          (Close[0] - ((
                          SMA(Close,Len1)[0] +
                          SMA(SMA(Close,Len1),Len1)[0] +
                          SMA(SMA(SMA(Close,Len1),Len1),Len1)[0] +
                          SMA(SMA(SMA(SMA(Close,Len1),Len1),Len1),Len1)[0] +
                          SMA(SMA(SMA(SMA(SMA(Close,Len1),Len1),Len1),Len1),Len1)[0] +
                          SMA(SMA(SMA(SMA(SMA(SMA(Close,Len1),Len1),Len1),Len1),Len1),Len1)[0] +
                          SMA(SMA(SMA(SMA(SMA(SMA(SMA(Close,Len1),Len1),Len1),Len1),Len1),Len1),Len1)[0] +
                          SMA(SMA(SMA(SMA(SMA(SMA(SMA(SMA(Close,Len1),Len1),Len1),Len1),Len1),Len1),Len1),Len1)[0] +
                          SMA(SMA(SMA(SMA(SMA(SMA(SMA(SMA(SMA(Close,Len1),Len1),Len1),Len1),Len1),Len1),Len1),Len1),Len1)[0] +
                          SMA(SMA(SMA(SMA(SMA(SMA(SMA(SMA(SMA(SMA(Close,Len1),Len1),Len1),Len1),Len1),Len1),Len1),Len1),Len1),Len1)[0]) / 10))
                          / (High[HighestBar(Close, Len2 - 1)] - Low[LowestBar(Close, Len2 - 1)]));
              
                      vST2.Set(EMA(vST1, Len3)[0]);
                      vST3.Set(EMA(vST2, Len3)[0]);
                      plotRMO.Set(EMA(vST1, Len4)[0]);
          
                      // Questionable if this works
                      if (CrossAbove(vST3, vST2, 1) && plotRMO[0] < 0) {
                          DrawArrowDown(CurrentBar.ToString(), false, 0, High[0] + (1 + TickSize), Color.IndianRed);
                          if (soundAlert) {
                              Alert(CurrentBar.ToString(), Priority.High, "Bear Trend Established", @AlertFile, alertInterval, Color.Red, Color.White);
                          }
                      }
                      else if (CrossAbove(vST2, vST3, 1) && plotRMO[0] > 0) {
                          DrawArrowUp(CurrentBar.ToString(), false, 0, Low[0] - (1 + TickSize), Color.RoyalBlue);
                          if (soundAlert) {
                              Alert(CurrentBar.ToString(), Priority.High, "Bull Trend Established", @AlertFile, alertInterval, Color.Blue, Color.White);
                          }
                      }
                      if ((plotRMO[0] > 0) && (vST2[0] > 0) && (vST3[0] > 0)) {
                          HistTickUp.Set(plotRMO[0]);
                          BarColor = Color.Blue;
                      }
                      else if ((plotRMO[0] < 0) && (vST2[0] < 0) && (vST3[0] < 0)) {
                          HistTickDown.Set(plotRMO[0]);
                          BarColor = Color.Red;
                      }
                      else {
                          GrayZone.Set(plotRMO[0]);
                          BarColor = Color.Gray;
                      }
                  }
          
                  #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 HistTickUp
                  {
                      get { return Values[0]; }
                  }
          
                  [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 HistTickDown
                  {
                      get { return Values[1]; }
                  }
          
                  [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 GrayZone
                  {
                      get { return Values[2]; }
                  }
                  
                  [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    vST1
                  {
                      get { return Values[3]; }
                }
                  
                  [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    vST2
                  {
                      get { return Values[4]; }
                }
                  
                  [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    vST3
                  {
                      get { return Values[5]; }
                }
                  
                  [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    plotRMO
                  {
                      get { return Values[6]; }
                }
                  
                  [Description("")]
                  [Category("Parameters")]
                  public int Len1
                  {
                      get { return len1; }
                      set { len1 = Math.Max(1, value); }
                  }
          
                  [Description("")]
                  [Category("Parameters")]
                  public int Len2
                  {
                      get { return len2; }
                      set { len2 = Math.Max(1, value); }
                  }
          
                  [Description("")]
                  [Category("Parameters")]
                  public int Len3
                  {
                      get { return len3; }
                      set { len3 = Math.Max(1, value); }
                  }
          
                  [Description("")]
                  [Category("Parameters")]
                  public int Len4
                  {
                      get { return len4; }
                      set { len4 = Math.Max(1, value); }
                  }
                  
                  [Description("How often do you want the alert to sound?")]
                  [Category("Sound and Display")]
                  [Gui.Design.DisplayName("Alert Interval (sec.)")]
                  public int AlertInterval
                  {
                      get { return alertInterval; }
                      set { alertInterval = value; }
                  }
                  
                  [Description("Where is the alert sound file located?  File name of .wav file to play. Provide either the absolute file path or just the name if the file is located in NinjaTrader Installation Folder sounds folder")]
                  [Category("Sound and Display")]
                  [Gui.Design.DisplayName("Alert .wav File")]
                  public string AlertFile
                  {
                      get { return alertFile; }
                      set { alertFile = value; }
                  }
          
                  [Description("Sound an alert when an RMO Pattern is signaled")]
                  [Category("Sound and Display")]
                  [Gui.Design.DisplayName("Alert Active?")]
                  public bool SoundAlert
                  {
                      get { return soundAlert; }
                      set { soundAlert = value; }
                  }
                  
                  public override void GetMinMaxValues(ChartControl chartControl, ref double min, ref double max)
                  {
                      min = -15; // As far south as it can go.
                      max = +15; // Set a top limit too.
                  } 
                  #endregion
              }
          }

          Comment


            #6
            Hi RandyT,

            We will get back to you in a little bit.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Hello,


              To make a dataseries available outside of your program:

              //in variables
              private DataSeires vST1;

              //in Initialize()
              vST1 = new DataSeries(this);

              //in OnBarUpdate()
              vST1.Set(...some value here...)

              //in properties
              [Browsable(false)]
              [XmlIgnore()]
              public DataSeries VST1
              {
              get { return vST1}
              }

              Note: the first letter capitalization is important. Also note we are returning vST1 and not "Values[0]".

              So when you try to get this value from another strategy it will look something like this:

              indicator_name().VST1[0]

              This link will hlep:


              There is an example of it here:
              DenNinjaTrader Customer Service

              Comment


                #8
                Thanks Ben, all sorted out now and lesson learned.

                Comment


                  #9
                  Any idea why I would not get data plots for Dow Mini when interval is set below 10 minutes?

                  The value to be plotted is showing a value of Infinity. But only for the Dow Mini.

                  Comment


                    #10
                    RandyT,

                    Unfortunately you will need to debug your NInjaScript to determine why you are getting infinity as your value. Please proceed as per this tip: http://www.ninjatrader-support.com/v...ead.php?t=3418
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Very interesting... I tried to apply the same code into a strategy to make a dataseries readable into another strategy, but when I'm into the second strategy I cannot "see" the public dataseries of the first strategy. Can you explain why it works with indicators but not with strategies?

                      Originally posted by NinjaTrader_Ben View Post
                      Hello,


                      To make a dataseries available outside of your program:

                      //in variables
                      private DataSeires vST1;

                      //in Initialize()
                      vST1 = new DataSeries(this);

                      //in OnBarUpdate()
                      vST1.Set(...some value here...)

                      //in properties
                      [Browsable(false)]
                      [XmlIgnore()]
                      public DataSeries VST1
                      {
                      get { return vST1}
                      }

                      Note: the first letter capitalization is important. Also note we are returning vST1 and not "Values[0]".

                      So when you try to get this value from another strategy it will look something like this:

                      indicator_name().VST1[0]

                      This link will hlep:


                      There is an example of it here:
                      http://www.ninjatrader-support.com/v...ead.php?t=4991

                      Comment


                        #12
                        stefy, correct cross strategy communication is not supported, as there's no method available for calling a strategy to return a value.
                        BertrandNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by bortz, 11-06-2023, 08:04 AM
                        47 responses
                        1,606 views
                        0 likes
                        Last Post aligator  
                        Started by jaybedreamin, Today, 05:56 PM
                        0 responses
                        9 views
                        0 likes
                        Last Post jaybedreamin  
                        Started by DJ888, 04-16-2024, 06:09 PM
                        6 responses
                        19 views
                        0 likes
                        Last Post DJ888
                        by DJ888
                         
                        Started by Jon17, Today, 04:33 PM
                        0 responses
                        6 views
                        0 likes
                        Last Post Jon17
                        by Jon17
                         
                        Started by Javierw.ok, Today, 04:12 PM
                        0 responses
                        15 views
                        0 likes
                        Last Post Javierw.ok  
                        Working...
                        X