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; } }
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!
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
NinjaTrader
Trying to do 2nd tutorial, but code changes are confusing me...
Collapse
X
-
Trying to do 2nd tutorial, but code changes are confusing me...
Code:Tags: None
-
Originally posted by CyJackX View PostCode: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; } }
-
Originally posted by koganam View PostYou 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
-
Originally posted by CyJackX View PostI 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 may take a look later, in which case I may be able to tell you something better than this post.
Comment
-
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
-
Originally posted by CyJackX View PostMaybe 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?
You need to properly specify the properties of the Plot.
ref: http://ninjatrader.com/support/helpG...s/?addplot.htm
Comment
-
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
-
Originally posted by NinjaTrader_ChelseaB View PostHello 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?
Comment
-
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 } }
Comment
-
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 FilesChelsea B.NinjaTrader Customer Service
Comment
-
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
-
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
by aligator
Today, 07:22 PM
|
||
Started by jaybedreamin, Today, 05:56 PM
|
0 responses
8 views
0 likes
|
Last Post
by jaybedreamin
Today, 05:56 PM
|
||
Started by DJ888, 04-16-2024, 06:09 PM
|
6 responses
18 views
0 likes
|
Last Post
by DJ888
Today, 05:12 PM
|
||
Started by Jon17, Today, 04:33 PM
|
0 responses
4 views
0 likes
|
Last Post
by Jon17
Today, 04:33 PM
|
||
Started by Javierw.ok, Today, 04:12 PM
|
0 responses
12 views
0 likes
|
Last Post
by Javierw.ok
Today, 04:12 PM
|
Comment