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

Trying to do 2nd tutorial, but code changes are confusing me...

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

    Trying to do 2nd tutorial, but code changes are confusing me...

    Code:
    public class VolSMA : Indicator
    	{
    		protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				Description									= @"Moving Average of volume";
    				Name										= "VolSMA";
    				Calculate									= Calculate.OnEachTick;
    				IsOverlay									= false;
    				DisplayInDataBox							= true;
    				DrawOnPricePanel							= true;
    				DrawHorizontalGridLines						= true;
    				DrawVerticalGridLines						= true;
    				PaintPriceMarkers							= true;
    				ScaleJustification							= NinjaTrader.Gui.Chart.ScaleJustification.Right;
    				//Disable this property if your indicator requires custom values that cumulate with each new market data event. 
    				//See Help Guide for additional information.
    				IsSuspendedWhileInactive					= true;
    				Period					= 10;
    				AddLine(Brushes.Orange, 1, "Plot0");
    			}
    			else if (State == State.Configure)
    			{
    			}
    		}
    
    		protected override void OnBarUpdate()
    		{
    			//Add your custom indicator logic here.
    			double average = SMA(VOL(), Period)[0];
    			Plot0[0] = average;
    
    		}		
    		[NinjaScriptProperty]
    		[Range(1, int.MaxValue)]
    		[Display(Name="Period", Description="Number of Periods", Order=1, GroupName="Parameters")]
    		public int Period
    		{ get; set; }
    
    
    	}
    Compiling returns "The name 'Plot0' does not exist in the current context.

    #2
    Originally posted by CyJackX View Post
    Code:
    public class VolSMA : Indicator
    	{
    		protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				Description									= @"Moving Average of volume";
    				Name										= "VolSMA";
    				Calculate									= Calculate.OnEachTick;
    				IsOverlay									= false;
    				DisplayInDataBox							= true;
    				DrawOnPricePanel							= true;
    				DrawHorizontalGridLines						= true;
    				DrawVerticalGridLines						= true;
    				PaintPriceMarkers							= true;
    				ScaleJustification							= NinjaTrader.Gui.Chart.ScaleJustification.Right;
    				//Disable this property if your indicator requires custom values that cumulate with each new market data event. 
    				//See Help Guide for additional information.
    				IsSuspendedWhileInactive					= true;
    				Period					= 10;
    				AddLine(Brushes.Orange, 1, "Plot0");
    			}
    			else if (State == State.Configure)
    			{
    			}
    		}
    
    		protected override void OnBarUpdate()
    		{
    			//Add your custom indicator logic here.
    			double average = SMA(VOL(), Period)[0];
    			Plot0[0] = average;
    
    		}		
    		[NinjaScriptProperty]
    		[Range(1, int.MaxValue)]
    		[Display(Name="Period", Description="Number of Periods", Order=1, GroupName="Parameters")]
    		public int Period
    		{ get; set; }
    
    
    	}
    Compiling returns "The name 'Plot0' does not exist in the current context.
    You have not declared any Plot in your code, so you cannot assign anything to a Plot that has not been declared. Are you sure that where you declare the Line in State.SetDefaults with "AddLine(Brushes.Orange, 1, "Plot0");". you did not actually intend to declare a Plot?

    Comment


      #3
      Originally posted by koganam View Post
      You have not declared any Plot in your code, so you cannot assign anything to a Plot that has not been declared. Are you sure that where you declare the Line in State.SetDefaults with "AddLine(Brushes.Orange, 1, "Plot0");". you did not actually intend to declare a Plot?
      I just followed what the Wizard did for me; I assumed it would work the same as it worked for the 1st tutorial. So I have to add a plot, as well as add a line? Or do I establish the line in the making of the Plot?

      Comment


        #4
        Originally posted by CyJackX View Post
        I just followed what the Wizard did for me; I assumed it would work the same as it worked for the 1st tutorial. So I have to add a plot, as well as add a line? Or do I establish the line in the making of the Plot?
        I have not looked at those tutorials in NT8, so it is hard to say. Maybe you could just move on the next one while we wait to see if NT made a mistake in their documentation?

        I may take a look later, in which case I may be able to tell you something better than this post.

        Comment


          #5
          Maybe I couldn't find it, but I don't think they have those tutorials in NT8, so that's why it's an exercise finding out what's obsolete from the NT7 tutorials! I found that adding this helped:
          public Series<double> Plot0
          {
          get { return Values[0]; }
          }

          But then it's a bar graph, I'm not sure if it's supposed to be a line instead?

          Comment


            #6
            Originally posted by CyJackX View Post
            Maybe I couldn't find it, but I don't think they have those tutorials in NT8, so that's why it's an exercise finding out what's obsolete from the NT7 tutorials! I found that adding this helped:
            public Series<double> Plot0
            {
            get { return Values[0]; }
            }

            But then it's a bar graph, I'm not sure if it's supposed to be a line instead?
            Ah, that is why I am confused. There do not seem to be any tutorials for indicators in NT8 Help.

            You need to properly specify the properties of the Plot.

            ref: http://ninjatrader.com/support/helpG...s/?addplot.htm

            Comment


              #7
              Hello CyJackX,

              When using AddPlot() in NinjaTrader 8, this will create a Values[] index. This will not automatically create a public series to access the plot from another script.

              This means you can use Value[0] = average; or Values[0][0] = average; and you would not need to add a public series named Plot0.

              Value - http://ninjatrader.com/support/helpG...n-us/value.htm
              Values - http://ninjatrader.com/support/helpG...-us/values.htm

              However, I'd like to clarify something you've mentioned in post #3. You've mentioned you created this using the Indicator Wizard, is this correct? (In the NinjaScript Editor of NinaTrader 8 you have right-clicked Indicators -> selected New Indicator -> and on the Plots and Lines page of the Wizard added a new plot and typed Plot0 as the name, is this correct?
              The plot was created with the point and click Indicator interface and no public series was created?
              I am not able to reproduce this behavior. Are you able to reproduce this?
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_ChelseaB View Post
                Hello CyJackX,

                When using AddPlot() in NinjaTrader 8, this will create a Values[] index. This will not automatically create a public series to access the plot from another script.

                This means you can use Value[0] = average; or Values[0][0] = average; and you would not need to add a public series named Plot0.

                Value - http://ninjatrader.com/support/helpG...n-us/value.htm
                Values - http://ninjatrader.com/support/helpG...-us/values.htm

                However, I'd like to clarify something you've mentioned in post #3. You've mentioned you created this using the Indicator Wizard, is this correct? (In the NinjaScript Editor of NinaTrader 8 you have right-clicked Indicators -> selected New Indicator -> and on the Plots and Lines page of the Wizard added a new plot and typed Plot0 as the name, is this correct?
                The plot was created with the point and click Indicator interface and no public series was created?
                I am not able to reproduce this behavior. Are you able to reproduce this?
                I'm having a similar issue. I figured out that I should be able to assign the average to value (Value[0] = average and it will compile but nothing will show up on my chart. The first tutorial worked and I was able to assign 1 to plot0[0] if the open and closed were equal and assign 0 if not, and it worked. On the second tutorial, I've tried assigning value to plot0[0] and Value[0] but still nothing shows on the chart. The area for the data is there but no bars. Does that make sense?

                Comment


                  #9
                  Hello BrandonWalter,

                  You are attempting to set a custom series instead of the Value series that already exists, is this correct?

                  Did you create the custom series as a public series?

                  May I see the code for this custom series?

                  Does the script compile?
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Code:
                    namespace NinjaTrader.NinjaScript.Indicators
                    {
                    	public class VolSMA : Indicator
                    	{
                    		protected override void OnStateChange()
                    		{
                    			if (State == State.SetDefaults)
                    			{
                    				Description									= @"Moving average of volume";
                    				Name										= "VolSMA";
                    				Calculate									= Calculate.OnBarClose;
                    				IsOverlay									= false;
                    				DisplayInDataBox							= true;
                    				DrawOnPricePanel							= true;
                    				DrawHorizontalGridLines						= false;
                    				DrawVerticalGridLines						= true;
                    				PaintPriceMarkers							= true;
                    				ScaleJustification							= NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    				//Disable this property if your indicator requires custom values that cumulate with each new market data event. 
                    				//See Help Guide for additional information.
                    				IsSuspendedWhileInactive					= true;
                    				Period					= 10;
                    				AddPlot(Brushes.Red, "Plot0");
                    			}
                    			else if (State == State.Configure)
                    			{
                    				AddDataSeries("ES 03-17", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
                    			}
                    		}
                    
                    		protected override void OnBarUpdate()
                    		{
                    			double average = SMA(VOL(), Period)[0];
                    			//I used "Plot0[0] = average" here as well
                    			Value[0] = average;
                    		}
                    
                    		#region Properties
                    		[NinjaScriptProperty]
                    		[Range(1, int.MaxValue)]
                    		[Display(Name="Period", Description="Number of periods", Order=1, GroupName="Parameters")]
                    		public int Period
                    		{ get; set; }
                    
                    		[Browsable(false)]
                    		[XmlIgnore]
                    		public Series<double> Plot0
                    		{
                    			get { return Values[0]; }
                    		}
                    		#endregion
                    
                    	}
                    }
                    It compiles both ways

                    Comment


                      #11
                      Hello BrandonWalter,

                      Do you have data on your chart?
                      What instrument, bar type, interval are you testing this on?

                      I'm showing this is working on my end. Attached is an exported script that I've tested.
                      Below is a link to a short video of the test.
                      Free online storage and sharing with Screencast.com. 2 GB of storage and 2 GB of bandwidth per month for free. We won't compress, alter or take ownership of your content.


                      As a heads up, I am seeing you are adding a secondary series but there is no use of BarsInProgress.
                      This means the plot will be set when both the primary and secondary series triggers.
                      Something to keep in mind, but shouldn't prevent the script from plotting.
                      Attached Files
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        I do have data on my chart and I'm using the ES. I was testing it on a 5 minute bar chart.

                        When I removed the secondary data series, it worked. The secondary data series was something that was automatically generated in the wizard and because I'm just starting to go through the tutorials, I didn't think to remove it. That video was helpful too. Thanks.

                        Comment


                          #13
                          BrandonWalter,

                          I forgot one super important thing in my video that made the test deceptive.

                          Anytime something is changed OnStateChange when State is .SetDefaults or .Configure like the way AddDataSeries() is called, the script has to be removed and re-added for those changes to take effect. (They are only called as the script is first added to prevent any settings that are set by the user from being unset)

                          This would mean that the 1 minute series would start updating before the primary bar (which is where the plot values come from).
                          A CurrentBar check can prevent this.

                          if (CurrentBars[0] < 1 || CurrentBars[1] < 1)
                          return;

                          This would prevent the code from executing until both BarsInProgress have at least 1 bar to work with.

                          As it is, your script would work on a 1 minute chart or less (as this would ensure the primary series also has a bar) but would not work on a 5 minute chart unless that CurrentBar check is added.

                          That said, if you have created a secondary series with the Strategy Builder, you have to actually use this secondary series before this current bar check will automatically be added (based on how many bars are needed to prevent error).

                          The Indicator Wizard doesn't have this ability to add conditions and requires that you would add this logic after the script framework is generated. The number of bars needed for each series will need to match any bars ago calls. For example if you call 5 bars ago, you would need to ensure the series have 5 bars instead of 1 bar in that CurrentBar check.
                          Chelsea B.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

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