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

Program Not Processing 2nd Data Series

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

    Program Not Processing 2nd Data Series

    Hello:
    -Still a newbie at Ninja, I've been coding a simple script on and off over the past few weeks which involves a chart with 2 Data Series. It runs OK up until the date of 3/15/13, but from then on produces a chart with only partial output. After 3/15/13 it fills in background colors correctly for the primary Data Series , but doesn't seem to be processing the secondary DataSeries at all.

    At chart creation I enter 2 new Data Series via the "Data Series..." chart drop down menu. I then add 1 moving average type indicator to the 2nd Data Series with the "Indicator..." drop down menu. I was using template <none> (to avoid confusion on my part), but now seem to be missing that option in my template drop down menu.

    The second data series is also "Added" to code with the same period type as first, i.e.,
    Code:
    Add("NQ 03-13", PeriodType.Minute, 1);
    Without this line the program produces the following runtime error and doesn't run.
    "Index was outside the bounds of the array"
    I don't "Add" the indicator to the code, but rather access it for 2ndary Data Series processing ala
    Code:
    if (BarsInProgress == 1) 			
                Value0=MA(n).[0];
                Value1=MA(n).[1];
    I've been stumped over this for some days now, so any help would be greatly appreciated!

    #2
    Hello tradenj,

    Thanks for your note.

    I am not sure on this as you code leaves me with more questions. However, I notice that you use:
    Code:
    if (BarsInProgress == 1) 			
                Value0=MA(n).[0];
                Value1=MA(n).[1];
    What would happen with this code without brackets around the if's execution block is:
    The public variable Value1 will always be set to MA(n).[1] (regardless of what the index of BarsInProgress is).
    If the bars in progress is 1 (i.e. the second dataseries is processing) then the public Variable Value0 will be set to MA(n).[0].

    Wrapping all code you would like to execute if this if statement is true may correct the issue. For example:
    Code:
    if (BarsInProgress == 1)
    {
                Value0=MA(n).[0];
                Value1=MA(n).[1];
    }

    Also, if you are trying to set the value of a plot I think you may be trying to use Values[0] and Values[1]. (Again I'm not sure on this because I do not have your complete code)

    For example:
    Code:
    Values[0].Set(MA(n).[0]);

    The error "Index was outside the bounds of the array" indicates that you are trying to access a index of a dataseries that does not exist. This would certainly be the case if you are trying to access a secondary data series that has not been added with the Add method.

    It could also mean that there are not enough bars in the historical data that you are trying to access.

    For example:
    if (CurrentBar == 1)
    Print(Close[2].ToString());
    Would always cause this error as the current bar is 1, and Close[2] or the close of 2 bars ago doesn't exist.


    Please let me know if this does not resolve your inquiry.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello, and thank you for your response.

      1) Sorry to have been so ambiguous and misleading with sketchy code details:
      (a) RE: Brackets - Was attempting to "imply" curly brackets with the code indentation but should have been more explicit.
      (b) RE: Plot - Was just assigning the 2ndary Data Series moving average plot values from the 0th and 1st chart bars respectively to variables named Value0 and Value1, e.g.
      Code:
      double Value0=0;  
      double Value1=0; 
      if (BarsInProgress == 1)
      {
                  Value0=SMA(14).[0];
                  Value1=SMA(14).[1];
      }

      2) RE: Runtime Error - Thanks for verifying that the 2ndary Data Series must be "Added". The program however continues to produce output only up until 3/15/13, so am still stumped.

      3) I've been breaking code down to test in small pieces and have come to recognize another thing I don't understand. I'll address this now as it could be causing unforeseen side effects. The code below backcolors aqua when the Primary Data Series exhibits a "downbar" followed by an "upbar" and when the Secondary Data Series exhibits an "upbar" whose low <= SMA of the Secondary. I run it on a chart with primary series ^NDX 1 minute and secondary series NQ 06-13 1 minute (which is "Added). After execution I bring up the data box to compare SMA values printed to the output window during execution with those on corresponding bars of the chart but find no match. For example 3/26/13 3:59 PM: SMA Output = 2805.23, SMA Data Box = 2798.46.

      4) One further issue. Sometimes when I'm editing a script and do a save, some other script will also be changed to match the one being edited. This is very unnerving and I don't understand what I've done to cause it to happen.

      Thanks again for your thoughts.

      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.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 Another : Strategy
          {
              #region Variables
              // Wizard generated variables
              private int myInput0 = 1; // Default setting for MyInput0
              // User defined variables (add any user defined variables below)
      		public double SMA0 = 0;	public double SMA1 = 0;
      		public double SMA2 = 0;	public double SMA3 = 0;
      		public bool DrawOnPricePanel = true;
              #endregion
      
              /// <summary>
              /// This method is used to configure the strategy and is called once before any strategy method is called.
              /// </summary>
              
      		/** ************************************************************************************* */ 
      		public void NDXBarDnUp_NQLowBelowSMA_ColorAqua()
      		{
      			SMA0=SMA(Low,14)[0];    /* Get current bar ^NQ SMA value */
      					
      			if ((Closes[0][0] > Opens[0][0])&&	/* Primary NDX: Dn ==> Up */  	
      				(Closes[0][1] < Opens[0][1]) )    				
      			if  (Closes[1][0] > Opens[1][0] )	 	/* Secondary NQ: Up Bar */			
      			if  ( (Lows[1][0]<=SMA0) )			/* Secondary NQ: Low[0] <= SMA */
      					{
      						BackColorAllSeries[0] = Color.Aquamarine;
      						Print(Time[0].ToString());
      						Print("SMA0 = "+SMA0.ToString());
      					}
      		}
      		/*********************************************************************/
      		protected override void Initialize()
              {
      			Add("^NDX", PeriodType.Minute, 1);
      			CalculateOnBarClose = true;
      			DrawOnPricePanel = false;
              }
              /// <summary>
              /// Called on each bar update event (incoming NDX)
              /// </summary>
      		protected override void OnBarUpdate(     )
      		{
      			if ( ( CurrentBars[0] < BarsRequired) || ( CurrentBars[1] < BarsRequired) )
      			return;	
      			if (BarsInProgress == 0)
      			{
      				if (ToDay(Time[0]) != ToDay(Time[1]))
      					Print ("Its 9:31 and Day Change");
      			}
      			if (BarsInProgress == 1) 
      			{	
      				NDXBarDnUp_NQLowBelowSMA_ColorAqua();
      				if (ToTime(Time[0]) == 160000)					
      				{	
      					Print ("Its 4 PM");
      					
      				}
      			}
      		}
      
              #region Properties
              [Description("")]
              [GridCategory("Parameters")]
              public int MyInput0
              {
                  get { return myInput0; }
                  set { myInput0 = Math.Max(1, value); }
              }
              #endregion
          }
      }

      Comment


        #4
        Hello tradenj,

        Thank you for the complete code.

        I now see that your confusion may be on data series.

        The primary data series will be the primary data series on the chart. This primary data series does not have to be added. A secondary data series is anything you add using the Add() method.

        In your post you mention:
        I run it on a chart with primary series ^NDX 1 minute and secondary series NQ 06-13 1 minute (which is "Added). After execution I bring up the data box to compare SMA values printed to the output window during execution with those on corresponding bars of the chart but find no match. For example 3/26/13 3:59 PM: SMA Output = 2805.23, SMA Data Box = 2798.46.
        Yet in your code you have:
        Code:
        protected override void Initialize()
        {
        	Add("^NDX", PeriodType.Minute, 1);
        	CalculateOnBarClose = true;
        	DrawOnPricePanel = false;
        }
        By opening a chart with the ^NDX 1 Minute Instrument and adding this strategy to the chart, then changing the following code:

        Code:
        Add("^NDX", PeriodType.Minute, 1);
        
        and
        
        public void NDXBarDnUp_NQLowBelowSMA_ColorAqua()
        {
        	SMA0=SMA(Low,14)[0];    /* Get current bar ^NQ SMA value */
        					
        	if ((Closes[0][0] > Opens[0][0])&&	/* Primary NDX: Dn ==> Up */  	
        		(Closes[0][1] < Opens[0][1]) )    				
        		if  (Closes[1][0] > Opens[1][0] )	 	/* Secondary NQ: Up Bar */			
        			if  ( (Lows[1][0]<=SMA0) )			/* Secondary NQ: Low[0] <= SMA */
        			{
        				BackColorAllSeries[0] = Color.Aquamarine;
        				Print(Time[0].ToString());
        				Print("SMA0 = "+SMA0.ToString());
        			}
        }
        To:
        Code:
        Add("NQ 06-13", PeriodType.Minute, 1);
        
        and
        
        public void NDXBarDnUp_NQLowBelowSMA_ColorAqua()
        {
        	SMA0=SMA(Low,14)[0];    /* Get current bar ^NQ SMA value */
        Print(Time[0].ToString() + " - SMA0: " + SMA0.ToString());
        	if ((Closes[0][0] > Opens[0][0])&&	/* Primary NDX: Dn ==> Up */  	
        			(Closes[0][1] < Opens[0][1]) )    				
        		if  (Closes[1][0] > Opens[1][0] )	 	/* Secondary NQ: Up Bar */			
        			if  ( (Lows[1][0]<=SMA0) )			/* Secondary NQ: Low[0] <= SMA */
        			{
        				BackColorAllSeries[0] = Color.Aquamarine;
        				//Print(Time[0].ToString());
        				//Print("SMA0 = "+SMA0.ToString());
        			}
        }
        I could both run the script without any error messages and by opening a separate chart with the NQ 06-13 1M loaded with the SMA(14) applied to it I could see that the prints of SMA0 matched the SMA on the chart.

        Below is a link to the help guide on Multi-Time Frame & Instruments.
        http://www.ninjatrader.com/support/h...nstruments.htm

        Also, in NinjaTrader there is a SampleMultiTimeFrame strategy. Try adding that to a 1 minute chart and using this as a reference.


        Please do not hesitate to contact us for any other NinjaTrader inquiries you may have.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Belated Thanks.

          Hello,
          -Returning after a long break from Ninja and wanted to tell you that your last post helped out in a great way by drawing my attention to the “Added” Data Series. The error in this instance was more or less a blatant oversight on my part that was causing a big problem as you realized. The location however turned out to be the site of a different, somewhat more subtle error (regarding time interval) which existed in many of my other strategies. Discovering it solved lots of problems.

          So thanks and more thanks!
          Last edited by tradenj; 04-28-2013, 06:12 PM.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by ZenCortexCLICK, Today, 04:58 AM
          0 responses
          1 view
          0 likes
          Last Post ZenCortexCLICK  
          Started by sidlercom80, 10-28-2023, 08:49 AM
          172 responses
          2,279 views
          0 likes
          Last Post sidlercom80  
          Started by Irukandji, Yesterday, 02:53 AM
          2 responses
          17 views
          0 likes
          Last Post Irukandji  
          Started by adeelshahzad, Today, 03:54 AM
          0 responses
          4 views
          0 likes
          Last Post adeelshahzad  
          Started by Barry Milan, Yesterday, 10:35 PM
          3 responses
          13 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Working...
          X