AddPlot()

View as Markdown

Definition

Adds plot objects that define how an indicator or strategy data series render on a chart. When this method is called to add a plot, an associated Series<double> object is created held in the Values collection.

Plots are ONLY visible from the UI property grid when AddPlot() is called from State.SetDefaults. If your indicator or strategy dynamically adds plots during State.Configure, you will NOT have an opportunity to select the plot or to set the plot configuration via the UI. Alternatively, you may use custom public Brush, Stroke, or PlotStyle properties which are accessible in State.SetDefaults and pass those values to AddPlot() during State.Configure. Calling AddPlot() in this manner should be reserved for special cases. Please see the examples below.

Methods and Properties

PropertyDescription
ArePlotsConfigurableDetermines if the plot(s) used in an indicator are configurable within the indicator dialog window.
DisplacementAn offset value that shifts the visually displayed value of an indicator.
PlotBrushesHolds an array of color series objects holding historical bar colors.
PlotsA collection holding all of the Plot objects that define their visualization characteristics.

Syntax

AddPlot(Brush brush, string name)

AddPlot(Stroke stroke, PlotStyle plotStyle, string name)

This method should ONLY be called within the OnStateChange() method during State.SetDefaults or State.Configure

Parameters

ParameterDescription
brushA Brush object used to construct the plot
nameA string representing the name of the plot
plotStyleA PlotStyle object used to construct the style of the plot. Possible values:
• PlotStyle.Bar
• PlotStyle.Block
• PlotStyle.Cross
• PlotStyle.Dot
• PlotStyle.Hash
• PlotStyle.HLine
• PlotStyle.Line
• PlotStyle.PriceBox
• PlotStyle.Square
• PlotStyle.TriangleDown
• PlotStyle.TriangleLeft
• PlotStyle.TriangleRight
• PlotStyle.TriangleUp
strokeA Stroke object used to construct the plot
  1. We suggest using the NinjaScript wizard to generate your plots.
  2. Plot objects DO NOT hold the actual script values. They simply define how the script’s values are plotted on a chart.
  3. A script may calculate multiple values and therefore hold multiple plots to determine the display of each calculated value. Script values are held in the script’s Values collection.
  4. If you script calls AddPlot() multiple times, then multiple Values series are added per the “three value series” example below
  5. For MultiSeries scripts, plots are synched to the primary series of the NinjaScript object.
  6. Plots will become visible once the script’s BarsRequiredToPlot value has been satisfied. By default, the value is 20.

Examples

Indicator using various AddPlot() signatures

1protected override void OnStateChange()
2{
3 if (State == State.SetDefaults)
4 {
5 Name = "Examples Indicator";
6
7 // Adds a blue line style plot
8 AddPlot(Brushes.Blue, "MyPlot");
9
10 // Adds a blue historgram style plot
11 AddPlot(new Stroke(Brushes.Blue), PlotStyle.Bar, "MyPlot2");
12
13 // Ensures that the width of the PlotStyle.Bar plot matches the width of the data series
14 Plots[1].AutoWidth = true;
15
16 // Adds a blue Dash-Line style plot with 5pixel width and 50% opacity
17 AddPlot(new Stroke(Brushes.Blue, DashStyleHelper.Dash, 5, 50), PlotStyle.Line, "MyPlot3");
18 }
19}

Indicator using a public Series\<double\> to expose a plot with a friendly name

This is required for making plots accessible in the strategy builder. For an example on exposing other variables publicly, see exposing indicator values that are not plots

1protected override void OnStateChange()
2
3{
4 if (State == State.SetDefaults)
5 {
6 Name = "Examples Indicator";
7 // Adds a blue line style plot
8 AddPlot(Brushes.Blue, "MyPlot");
9 // Adds a blue historgram style plot
10 AddPlot(new Stroke(Brushes.Blue), PlotStyle.Bar, "MyPlot2");
11 }
12}
13protected override void OnBarUpdate()
14{
15 MyPlot[0] = Close[0] + High[0] / 2;
16 MyPlot[1] = Close[0] + High[0] / 2;
17}
18[Browsable(false)]
19[XmlIgnore]
20public Series<double> MyPlot
21{
22 get { return Values[0]; }
23}
24[Browsable(false)]
25[XmlIgnore]
26public Series<double> MyPlot2
27{
28 get { return Values[1]; }
29}

Indicator which adds three value series

1protected override void OnStateChange()
2{
3 if (State == State.SetDefaults)
4 {
5 Name = "Examples Indicator";
6 // Add three plots and associated Series<double> objects
7 AddPlot(Brushes.Blue, "PlotA"); // Defines the plot for Values[0]
8 AddPlot(Brushes.Red, "PlotB"); // Defines the plot for Values[1]
9 AddPlot(Brushes.Green, "PlotC"); // Defines the plot for Values[2]
10 }
11}
12protected override void OnBarUpdate()
13{
14 Values[0][0] = Median[0]; // Blue "Plot A"
15 Values[1][0] = Low[0]; // Red "Plot B"
16 Values[2][0] = High[0]; // Green "Plot C"
17}

Indicator which dynamically adds a plot in state.configure

1protected override void OnStateChange()
2{
3 if (State == State.SetDefaults)
4 {
5 Name = "Examples Indicator";
6 // logical property which user can set
7 UseSpecialMode = false;
8 // Default brush selection pushed to the UI
9 MyBrush = Brushes.Red;
10 }
11 else if (State == State.Configure)
12 {
13 // if user enables logical property
14 if (UseSpecialMode)
15 {
16 // add plot using default selected brush and special plot name
17 AddPlot(MyBrush, "My Special Plot");
18 }
19 else
20 {
21 // otherwise use default selected brush and regular plot name
22 AddPlot(MyBrush, "My Regular Plot");
23 }
24 }
25}
26protected override void OnBarUpdate()
27{
28 if (UseSpecialMode)
29 Value[0] = Close[0] + High[0] / 2;
30 else Value[0] = Close[0] * TickSize / 2;
31}
32[XmlIgnore]
33public Brush MyBrush { get; set; }
34public bool UseSpecialMode { get; set; }