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
How to draw a line using the PlotStyle from a Plot?
Collapse
X
-
It plots a solid line now, but doesn't fit to the OnBarUpdate plot in OnRender.
Why not?
double plotValue = myDoubleSeries1[0];
myDoubleSeries1[0] prints correct values in OnBarUpdate, but wrong values in OnRender.
The documentation and samples and online searches weren't helpful enough.
Code:#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.DrawingTools; using SharpDX; using SharpDX.Direct2D1; using SharpDX.DirectWrite; #endregion //This namespace holds Indicators in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Indicators { public class _OnRenderPlot : Indicator { int cBar; double cHigh; // Defines the Series object private Series<double> myDoubleSeries, myDoubleSeries1; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Indicator here."; Name = "_OnRenderPlot"; Calculate = Calculate.OnEachTick; IsOverlay = true; 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; // Adds a blue Dash-Line style plot with 5pixel width and 70% opacity AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Dash, 5, 70), PlotStyle.Hash, "MyPlot1"); } else if (State == State.Configure) { } else if (State == State.DataLoaded) { // Create a new Series object and assign it to the variable myDoubleSeries declared in the ‘Variables’ region above myDoubleSeries = new Series<double>(this); myDoubleSeries1 = new Series<double>(this); } } protected override void OnBarUpdate() { if (CurrentBar <21) return; if (Open[0] < Close[0]) { cBar = CurrentBar; cHigh = High[0]; MyPlot1[0] = cHigh; myDoubleSeries[0] = cHigh; } else { //MyPlot1[0] = double.NaN; } if ((CurrentBar - cBar) < 4) { MyPlot1[0] = cHigh; myDoubleSeries1[0] = cHigh; } Print(" "); //Print("myDoubleSeries0 " +myDoubleSeries[0]); Print("myDoubleSeries1 " +myDoubleSeries1[0]); } protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { // Print(" "); // Print("myDoubleSeries1 " +myDoubleSeries[0]); base.OnRender(chartControl, chartScale); SharpDX.Direct2D1.StrokeStyleProperties ssPropsWick = new SharpDX.Direct2D1.StrokeStyleProperties(); ssPropsWick.DashStyle = DashStyleSolidLine; SharpDX.Direct2D1.StrokeStyle solidLine = new SharpDX.Direct2D1.StrokeStyle(RenderTarget.Factory, ssPropsWick); SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode; RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.Aliased; // get the starting and ending bars from what is rendered on the chart float startX = chartControl.GetXByBarIndex(ChartBars, ChartBars.FromIndex); float endX = chartControl.GetXByBarIndex(ChartBars, ChartBars.ToIndex); // Loop through each Plot Values on the chart for (int seriesCount = 0; seriesCount < Values.Length; seriesCount++) { // get the value at the last bar on the chart (if it has been set) if (Values[seriesCount].IsValidDataPointAt(ChartBars.ToIndex)) { // double plotValue = cHigh; // double plotValue = MyPlot1[0]; double plotValue = myDoubleSeries1[0]; // convert the plot value to the charts "Y" axis point float chartScaleYValue = chartScale.GetYByValue(plotValue); // calculate the x and y values for the line to start and end SharpDX.Vector2 startPoint = new SharpDX.Vector2(startX, chartScaleYValue); SharpDX.Vector2 endPoint = new SharpDX.Vector2(endX, chartScaleYValue); // draw a line between the start and end point at each plot using the plots SharpDX Brush color and style RenderTarget.DrawLine(startPoint, endPoint, Plots[seriesCount].BrushDX, Plots[seriesCount].Width, solidLine); } } solidLine.Dispose(); RenderTarget.AntialiasMode = oldAntialiasMode; } #region Properties [Browsable(false)] [XmlIgnore] public Series<double> MyPlot1 { get { return Values[0]; } } [NinjaScriptProperty] [Display(Name="DashStyle Solid Line", Description="DashStyle Solid Line.", Order=0, GroupName="Plots Lines")] public SharpDX.Direct2D1.DashStyle DashStyleSolidLine { get; set; } #endregion } } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private _OnRenderPlot[] cache_OnRenderPlot; public _OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return _OnRenderPlot(Input, dashStyleSolidLine); } public _OnRenderPlot _OnRenderPlot(ISeries<double> input, SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { if (cache_OnRenderPlot != null) for (int idx = 0; idx < cache_OnRenderPlot.Length; idx++) if (cache_OnRenderPlot[idx] != null && cache_OnRenderPlot[idx].DashStyleSolidLine == dashStyleSolidLine && cache_OnRenderPlot[idx].EqualsInput(input)) return cache_OnRenderPlot[idx]; return CacheIndicator<_OnRenderPlot>(new _OnRenderPlot(){ DashStyleSolidLine = dashStyleSolidLine }, input, ref cache_OnRenderPlot); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } #endregion
Comment
-
Which one is better/faster?
I tested this, but it's not returning all the plots values:
Code:#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.DrawingTools; using SharpDX; using SharpDX.Direct2D1; using SharpDX.DirectWrite; #endregion //This namespace holds Indicators in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Indicators { public class _OnRenderPlot : Indicator { int cBar; double cHigh; // Defines the Series object private Series<double> myDoubleSeries, myDoubleSeries1; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Indicator here."; Name = "_OnRenderPlot"; Calculate = Calculate.OnEachTick; IsOverlay = true; 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; // Adds a blue Dash-Line style plot with 5pixel width and 70% opacity AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Dash, 5, 70), PlotStyle.Hash, "MyPlot1"); } else if (State == State.Configure) { } else if (State == State.DataLoaded) { // Create a new Series object and assign it to the variable myDoubleSeries declared in the ‘Variables’ region above myDoubleSeries = new Series<double>(this); myDoubleSeries1 = new Series<double>(this); } } protected override void OnBarUpdate() { if (CurrentBar <21) return; if (Open[0] < Close[0]) { cBar = CurrentBar; cHigh = High[0]; MyPlot1[0] = cHigh; myDoubleSeries[0] = cHigh; } else { //MyPlot1[0] = double.NaN; } if ((CurrentBar - cBar) < 4) { MyPlot1[0] = cHigh; myDoubleSeries1[0] = cHigh; } Print(" "); //Print("myDoubleSeries0 " +myDoubleSeries[0]); //Print("myDoubleSeries1 " +myDoubleSeries1[0]); } protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { // Print(" "); // Print("myDoubleSeries1 " +myDoubleSeries[0]); base.OnRender(chartControl, chartScale); SharpDX.Direct2D1.StrokeStyleProperties ssPropsWick = new SharpDX.Direct2D1.StrokeStyleProperties(); ssPropsWick.DashStyle = DashStyleSolidLine; SharpDX.Direct2D1.StrokeStyle solidLine = new SharpDX.Direct2D1.StrokeStyle(RenderTarget.Factory, ssPropsWick); SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode; RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.Aliased; // get the starting and ending bars from what is rendered on the chart float startX = chartControl.GetXByBarIndex(ChartBars, ChartBars.FromIndex); float endX = chartControl.GetXByBarIndex(ChartBars, ChartBars.ToIndex); // Loop through each Plot Values on the chart for (int seriesCount = 0; seriesCount < Values.Length; seriesCount++) { // get the value at the last bar on the chart (if it has been set) if (Values[seriesCount].IsValidDataPointAt(ChartBars.ToIndex)) { //double plotValue = cHigh; // double plotValue = MyPlot1[0]; double plotValue = myDoubleSeries1.GetValueAt(seriesCount); Print(" "); Print("plotValue " + plotValue); // convert the plot value to the charts "Y" axis point float chartScaleYValue = chartScale.GetYByValue(plotValue); // calculate the x and y values for the line to start and end SharpDX.Vector2 startPoint = new SharpDX.Vector2(startX, chartScaleYValue); SharpDX.Vector2 endPoint = new SharpDX.Vector2(endX, chartScaleYValue); // draw a line between the start and end point at each plot using the plots SharpDX Brush color and style RenderTarget.DrawLine(startPoint, endPoint, Plots[seriesCount].BrushDX, Plots[seriesCount].Width, solidLine); } } solidLine.Dispose(); RenderTarget.AntialiasMode = oldAntialiasMode; } #region Properties [Browsable(false)] [XmlIgnore] public Series<double> MyPlot1 { get { return Values[0]; } } [NinjaScriptProperty] [Display(Name="DashStyle Solid Line", Description="DashStyle Solid Line.", Order=0, GroupName="Plots Lines")] public SharpDX.Direct2D1.DashStyle DashStyleSolidLine { get; set; } #endregion } } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private _OnRenderPlot[] cache_OnRenderPlot; public _OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return _OnRenderPlot(Input, dashStyleSolidLine); } public _OnRenderPlot _OnRenderPlot(ISeries<double> input, SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { if (cache_OnRenderPlot != null) for (int idx = 0; idx < cache_OnRenderPlot.Length; idx++) if (cache_OnRenderPlot[idx] != null && cache_OnRenderPlot[idx].DashStyleSolidLine == dashStyleSolidLine && cache_OnRenderPlot[idx].EqualsInput(input)) return cache_OnRenderPlot[idx]; return CacheIndicator<_OnRenderPlot>(new _OnRenderPlot(){ DashStyleSolidLine = dashStyleSolidLine }, input, ref cache_OnRenderPlot); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } #endregion
double plotValue = myDoubleSeries1.GetValueAt(seriesCount);
prints a unique value:
plotValue 19195
Comment
-
This
// Loop through each Plot Values on the chart
for (int seriesCount = 0; seriesCount < Values.Length; seriesCount++)
{
Print("seriesCount " + seriesCount);
Print("Values.Length " + Values.Length);
returns
seriesCount 0
Values.Length 1
How to iterate over the plots bars in OnRender()?
Comment
-
I get these Prints
Code:myDoubleSeries1.GetValueAt(index) 18081 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18081 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18082 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18082 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18075.75 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18077 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18077.25 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18077.25 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18077.25 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18077.25 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18082 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18074.75 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18075.75 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19155 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19156 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19157 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19158 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19159 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19160 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19161 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19162 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19163 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19164 02/08/2024 00:00:07
Code:#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.DrawingTools; using SharpDX; using SharpDX.Direct2D1; using SharpDX.DirectWrite; #endregion //This namespace holds Indicators in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Indicators { public class _OnRenderPlot : Indicator { int cBar; double cHigh; // Defines the Series object private Series<double> myDoubleSeries, myDoubleSeries1; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Indicator here."; Name = "_OnRenderPlot"; Calculate = Calculate.OnEachTick; IsOverlay = true; 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; // Adds a blue Dash-Line style plot with 5pixel width and 70% opacity AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Dash, 5, 70), PlotStyle.Hash, "MyPlot1"); } else if (State == State.Configure) { } else if (State == State.DataLoaded) { // Create a new Series object and assign it to the variable myDoubleSeries declared in the ‘Variables’ region above myDoubleSeries = new Series<double>(this); myDoubleSeries1 = new Series<double>(this); } } protected override void OnBarUpdate() { if (CurrentBar <21) return; if (Open[0] < Close[0]) { cBar = CurrentBar; cHigh = High[0]; MyPlot1[0] = cHigh; myDoubleSeries[0] = cHigh; } else { //MyPlot1[0] = double.NaN; } if ((CurrentBar - cBar) < 4) { MyPlot1[0] = cHigh; myDoubleSeries1[0] = cHigh; } Print(" "); //Print("myDoubleSeries0 " +myDoubleSeries[0]); //Print("myDoubleSeries1 " +myDoubleSeries1[0]); } protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { // Print(" "); // Print("myDoubleSeries1 " +myDoubleSeries[0]); base.OnRender(chartControl, chartScale); SharpDX.Direct2D1.StrokeStyleProperties ssPropsWick = new SharpDX.Direct2D1.StrokeStyleProperties(); ssPropsWick.DashStyle = DashStyleSolidLine; SharpDX.Direct2D1.StrokeStyle solidLine = new SharpDX.Direct2D1.StrokeStyle(RenderTarget.Factory, ssPropsWick); SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode; RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.Aliased; // Loop through each Plot Values on the chart for (int seriesCount = 0; seriesCount < Values.Length; seriesCount++) { Print("seriesCount " + seriesCount); Print("Values.Length " + Values.Length); float startX = 0; float endX = 0; double plotValue = 0; for (int index = 0; index < CurrentBar; index++) { Print("myDoubleSeries1.GetValueAt(index) " + myDoubleSeries1.GetValueAt(index)); plotValue = myDoubleSeries1.GetValueAt(index); } // get the starting and ending bars from what is rendered on the chart startX = chartControl.GetXByBarIndex(ChartBars, seriesCount); endX = chartControl.GetXByBarIndex(ChartBars, Values.Length); // get the value at the last bar on the chart (if it has been set) if (Values[seriesCount].IsValidDataPointAt(ChartBars.ToIndex)) { //double plotValue = cHigh; // double plotValue = MyPlot1[0]; // double plotValue = myDoubleSeries1.GetValueAt(seriesCount); // Print(" "); // Print("plotValue " + plotValue); // convert the plot value to the charts "Y" axis point float chartScaleYValue = chartScale.GetYByValue(plotValue); // calculate the x and y values for the line to start and end SharpDX.Vector2 startPoint = new SharpDX.Vector2(startX, chartScaleYValue); SharpDX.Vector2 endPoint = new SharpDX.Vector2(endX, chartScaleYValue); // draw a line between the start and end point at each plot using the plots SharpDX Brush color and style RenderTarget.DrawLine(startPoint, endPoint, Plots[seriesCount].BrushDX, Plots[seriesCount].Width, solidLine); } } solidLine.Dispose(); RenderTarget.AntialiasMode = oldAntialiasMode; } #region Properties [Browsable(false)] [XmlIgnore] public Series<double> MyPlot1 { get { return Values[0]; } } [NinjaScriptProperty] [Display(Name="DashStyle Solid Line", Description="DashStyle Solid Line.", Order=0, GroupName="Plots Lines")] public SharpDX.Direct2D1.DashStyle DashStyleSolidLine { get; set; } #endregion } } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private _OnRenderPlot[] cache_OnRenderPlot; public _OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return _OnRenderPlot(Input, dashStyleSolidLine); } public _OnRenderPlot _OnRenderPlot(ISeries<double> input, SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { if (cache_OnRenderPlot != null) for (int idx = 0; idx < cache_OnRenderPlot.Length; idx++) if (cache_OnRenderPlot[idx] != null && cache_OnRenderPlot[idx].DashStyleSolidLine == dashStyleSolidLine && cache_OnRenderPlot[idx].EqualsInput(input)) return cache_OnRenderPlot[idx]; return CacheIndicator<_OnRenderPlot>(new _OnRenderPlot(){ DashStyleSolidLine = dashStyleSolidLine }, input, ref cache_OnRenderPlot); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } #endregion
Comment
-
This
Code:#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.DrawingTools; using SharpDX; using SharpDX.Direct2D1; using SharpDX.DirectWrite; #endregion //This namespace holds Indicators in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Indicators { public class _OnRenderPlot : Indicator { int cBar; double cHigh; // Defines the Series object private Series<double> myDoubleSeries, myDoubleSeries1; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Indicator here."; Name = "_OnRenderPlot"; Calculate = Calculate.OnEachTick; IsOverlay = true; 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; // Adds a blue Dash-Line style plot with 5pixel width and 70% opacity AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Dash, 5, 70), PlotStyle.Hash, "MyPlot1"); } else if (State == State.Configure) { } else if (State == State.DataLoaded) { // Create a new Series object and assign it to the variable myDoubleSeries declared in the ‘Variables’ region above myDoubleSeries = new Series<double>(this); myDoubleSeries1 = new Series<double>(this); } } protected override void OnBarUpdate() { if (CurrentBar <21) return; if (Open[0] < Close[0]) { cBar = CurrentBar; cHigh = High[0]; MyPlot1[0] = cHigh; myDoubleSeries[0] = cHigh; } else { //MyPlot1[0] = double.NaN; } if ((CurrentBar - cBar) < 4) { MyPlot1[0] = cHigh; myDoubleSeries1[0] = cHigh; } Print(" "); //Print("myDoubleSeries0 " +myDoubleSeries[0]); //Print("myDoubleSeries1 " +myDoubleSeries1[0]); } protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { // Print(" "); // Print("myDoubleSeries1 " +myDoubleSeries[0]); base.OnRender(chartControl, chartScale); SharpDX.Direct2D1.StrokeStyleProperties ssPropsWick = new SharpDX.Direct2D1.StrokeStyleProperties(); ssPropsWick.DashStyle = DashStyleSolidLine; SharpDX.Direct2D1.StrokeStyle solidLine = new SharpDX.Direct2D1.StrokeStyle(RenderTarget.Factory, ssPropsWick); SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode; RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.Aliased; // Loop through each Plot Values on the chart // for (int seriesCount = 0; seriesCount < Values.Length; seriesCount++) // { // Print("seriesCount " + seriesCount); // Print("Values.Length " + Values.Length); float startX = 0; float endX = 0; double plotValue = 0; for (int index = 0; index < CurrentBar; index++) { Print("myDoubleSeries1.GetValueAt(index) " + myDoubleSeries1.GetValueAt(index) + " " + Time[0]); plotValue = myDoubleSeries1.GetValueAt(index); // get the starting and ending bars from what is rendered on the chart startX = chartControl.GetXByBarIndex(ChartBars, index); endX = chartControl.GetXByBarIndex(ChartBars, CurrentBar); Print("startX " + startX + " " + Time[0]); Print("endX " + endX + " " + Time[0]); // get the value at the last bar on the chart (if it has been set) // if (Values[seriesCount].IsValidDataPointAt(ChartBars.ToIndex)) // { //double plotValue = cHigh; // double plotValue = MyPlot1[0]; // double plotValue = myDoubleSeries1.GetValueAt(seriesCount); // Print(" "); // Print("plotValue " + plotValue); // convert the plot value to the charts "Y" axis point float chartScaleYValue = chartScale.GetYByValue(plotValue); Print("chartScaleYValue " + chartScaleYValue + " " + Time[0]); // calculate the x and y values for the line to start and end SharpDX.Vector2 startPoint = new SharpDX.Vector2(startX, chartScaleYValue); SharpDX.Vector2 endPoint = new SharpDX.Vector2(endX, chartScaleYValue); Print("startPoint " + startPoint + " " + Time[0]); Print("endPoint " + endPoint + " " + Time[0]); // draw a line between the start and end point at each plot using the plots SharpDX Brush color and style RenderTarget.DrawLine(startPoint, endPoint, Plots[index].BrushDX, Plots[index].Width, solidLine); } // } solidLine.Dispose(); RenderTarget.AntialiasMode = oldAntialiasMode; } #region Properties [Browsable(false)] [XmlIgnore] public Series<double> MyPlot1 { get { return Values[0]; } } [NinjaScriptProperty] [Display(Name="DashStyle Solid Line", Description="DashStyle Solid Line.", Order=0, GroupName="Plots Lines")] public SharpDX.Direct2D1.DashStyle DashStyleSolidLine { get; set; } #endregion } } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private _OnRenderPlot[] cache_OnRenderPlot; public _OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return _OnRenderPlot(Input, dashStyleSolidLine); } public _OnRenderPlot _OnRenderPlot(ISeries<double> input, SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { if (cache_OnRenderPlot != null) for (int idx = 0; idx < cache_OnRenderPlot.Length; idx++) if (cache_OnRenderPlot[idx] != null && cache_OnRenderPlot[idx].DashStyleSolidLine == dashStyleSolidLine && cache_OnRenderPlot[idx].EqualsInput(input)) return cache_OnRenderPlot[idx]; return CacheIndicator<_OnRenderPlot>(new _OnRenderPlot(){ DashStyleSolidLine = dashStyleSolidLine }, input, ref cache_OnRenderPlot); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } #endregion
myDoubleSeries1.GetValueAt(index) 19190 02/08/2024 00:00:07
startX -1255668 02/08/2024 00:00:07
endX 789 02/08/2024 00:00:07
chartScaleYValue 460 02/08/2024 00:00:07
startPoint X:-1255668 Y:460 02/08/2024 00:00:07
endPoint X:789 Y:460 02/08/2024 00:00:07
myDoubleSeries1.GetValueAt(index) 19191 02/08/2024 00:00:07
startX -1255657 02/08/2024 00:00:07
endX 789 02/08/2024 00:00:07
chartScaleYValue 451 02/08/2024 00:00:07
startPoint X:-1255657 Y:451 02/08/2024 00:00:07
endPoint X:789 Y:451 02/08/2024 00:00:07
Indicator '_OnRenderPlot': Error on calling 'OnRender' method on bar 117297: Index was outside the bounds of the array.
Comment
-
Hello PaulMohn,
If you are trying to loop over the visible bars, this would be:
for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
{
Print( myDoubleSeries1.GetValueAt(index) );
}
What is the line of code causing the error?
The line of code under the last print appears to be:
float chartScaleYValue = chartScale.GetYByValue(plotValue);
Is this the code causing the error?Chelsea B.NinjaTrader Customer Service
Comment
-
Originally posted by PaulMohn View PostI get these Prints
Code:myDoubleSeries1.GetValueAt(index) 18081 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18081 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18082 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18082 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18075.75 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18077 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18077.25 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18077.25 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18077.25 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18077.25 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18082 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18074.75 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 18075.75 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19155 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19156 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19157 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19158 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19159 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19160 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19161 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19162 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19163 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19164 02/08/2024 00:00:07
Code:#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.DrawingTools; using SharpDX; using SharpDX.Direct2D1; using SharpDX.DirectWrite; #endregion //This namespace holds Indicators in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Indicators { public class _OnRenderPlot : Indicator { int cBar; double cHigh; // Defines the Series object private Series<double> myDoubleSeries, myDoubleSeries1; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Indicator here."; Name = "_OnRenderPlot"; Calculate = Calculate.OnEachTick; IsOverlay = true; 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; // Adds a blue Dash-Line style plot with 5pixel width and 70% opacity AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Dash, 5, 70), PlotStyle.Hash, "MyPlot1"); } else if (State == State.Configure) { } else if (State == State.DataLoaded) { // Create a new Series object and assign it to the variable myDoubleSeries declared in the ‘Variables’ region above myDoubleSeries = new Series<double>(this); myDoubleSeries1 = new Series<double>(this); } } protected override void OnBarUpdate() { if (CurrentBar <21) return; if (Open[0] < Close[0]) { cBar = CurrentBar; cHigh = High[0]; MyPlot1[0] = cHigh; myDoubleSeries[0] = cHigh; } else { //MyPlot1[0] = double.NaN; } if ((CurrentBar - cBar) < 4) { MyPlot1[0] = cHigh; myDoubleSeries1[0] = cHigh; } Print(" "); //Print("myDoubleSeries0 " +myDoubleSeries[0]); //Print("myDoubleSeries1 " +myDoubleSeries1[0]); } protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { // Print(" "); // Print("myDoubleSeries1 " +myDoubleSeries[0]); base.OnRender(chartControl, chartScale); SharpDX.Direct2D1.StrokeStyleProperties ssPropsWick = new SharpDX.Direct2D1.StrokeStyleProperties(); ssPropsWick.DashStyle = DashStyleSolidLine; SharpDX.Direct2D1.StrokeStyle solidLine = new SharpDX.Direct2D1.StrokeStyle(RenderTarget.Factory, ssPropsWick); SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode; RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.Aliased; // Loop through each Plot Values on the chart for (int seriesCount = 0; seriesCount < Values.Length; seriesCount++) { Print("seriesCount " + seriesCount); Print("Values.Length " + Values.Length); float startX = 0; float endX = 0; double plotValue = 0; for (int index = 0; index < CurrentBar; index++) { Print("myDoubleSeries1.GetValueAt(index) " + myDoubleSeries1.GetValueAt(index)); plotValue = myDoubleSeries1.GetValueAt(index); } // get the starting and ending bars from what is rendered on the chart startX = chartControl.GetXByBarIndex(ChartBars, seriesCount); endX = chartControl.GetXByBarIndex(ChartBars, Values.Length); // get the value at the last bar on the chart (if it has been set) if (Values[seriesCount].IsValidDataPointAt(ChartBars.ToIndex)) { //double plotValue = cHigh; // double plotValue = MyPlot1[0]; // double plotValue = myDoubleSeries1.GetValueAt(seriesCount); // Print(" "); // Print("plotValue " + plotValue); // convert the plot value to the charts "Y" axis point float chartScaleYValue = chartScale.GetYByValue(plotValue); // calculate the x and y values for the line to start and end SharpDX.Vector2 startPoint = new SharpDX.Vector2(startX, chartScaleYValue); SharpDX.Vector2 endPoint = new SharpDX.Vector2(endX, chartScaleYValue); // draw a line between the start and end point at each plot using the plots SharpDX Brush color and style RenderTarget.DrawLine(startPoint, endPoint, Plots[seriesCount].BrushDX, Plots[seriesCount].Width, solidLine); } } solidLine.Dispose(); RenderTarget.AntialiasMode = oldAntialiasMode; } #region Properties [Browsable(false)] [XmlIgnore] public Series<double> MyPlot1 { get { return Values[0]; } } [NinjaScriptProperty] [Display(Name="DashStyle Solid Line", Description="DashStyle Solid Line.", Order=0, GroupName="Plots Lines")] public SharpDX.Direct2D1.DashStyle DashStyleSolidLine { get; set; } #endregion } } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private _OnRenderPlot[] cache_OnRenderPlot; public _OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return _OnRenderPlot(Input, dashStyleSolidLine); } public _OnRenderPlot _OnRenderPlot(ISeries<double> input, SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { if (cache_OnRenderPlot != null) for (int idx = 0; idx < cache_OnRenderPlot.Length; idx++) if (cache_OnRenderPlot[idx] != null && cache_OnRenderPlot[idx].DashStyleSolidLine == dashStyleSolidLine && cache_OnRenderPlot[idx].EqualsInput(input)) return cache_OnRenderPlot[idx]; return CacheIndicator<_OnRenderPlot>(new _OnRenderPlot(){ DashStyleSolidLine = dashStyleSolidLine }, input, ref cache_OnRenderPlot); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } #endregion
This is returned
Code:seriesCount 0 Values.Length 1 myDoubleSeries1.GetValueAt(index) 19316 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19317 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19318 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19319 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19320 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19321 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19322 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19323 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19324 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19325 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19326 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19327 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19328 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19329 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19330 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19331 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19332 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19333 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19334 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19335 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19336 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19337 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19338 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19339 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19340 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19341 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19342 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19343 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19344 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19345 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19346 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19347 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19348 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19349 02/08/2024 00:00:07 myDoubleSeries1.GetValueAt(index) 19350 02/08/2024 00:00:07
Code:#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.DrawingTools; using SharpDX; using SharpDX.Direct2D1; using SharpDX.DirectWrite; #endregion //This namespace holds Indicators in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Indicators { public class _OnRenderPlot : Indicator { int cBar; double cHigh; // Defines the Series object private Series<double> myDoubleSeries, myDoubleSeries1; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Indicator here."; Name = "_OnRenderPlot"; Calculate = Calculate.OnEachTick; IsOverlay = true; 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; // Adds a blue Dash-Line style plot with 5pixel width and 70% opacity AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Dash, 5, 70), PlotStyle.Hash, "MyPlot1"); } else if (State == State.Configure) { } else if (State == State.DataLoaded) { // Create a new Series object and assign it to the variable myDoubleSeries declared in the ‘Variables’ region above myDoubleSeries = new Series<double>(this); myDoubleSeries1 = new Series<double>(this); } } protected override void OnBarUpdate() { if (CurrentBar <21) return; if (Open[0] < Close[0]) { cBar = CurrentBar; cHigh = High[0]; MyPlot1[0] = cHigh; myDoubleSeries[0] = cHigh; } else { //MyPlot1[0] = double.NaN; } if ((CurrentBar - cBar) < 4) { MyPlot1[0] = cHigh; myDoubleSeries1[0] = cHigh; } Print(" "); //Print("myDoubleSeries0 " +myDoubleSeries[0]); //Print("myDoubleSeries1 " +myDoubleSeries1[0]); } protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { // Print(" "); // Print("myDoubleSeries1 " +myDoubleSeries[0]); base.OnRender(chartControl, chartScale); SharpDX.Direct2D1.StrokeStyleProperties ssPropsWick = new SharpDX.Direct2D1.StrokeStyleProperties(); ssPropsWick.DashStyle = DashStyleSolidLine; SharpDX.Direct2D1.StrokeStyle solidLine = new SharpDX.Direct2D1.StrokeStyle(RenderTarget.Factory, ssPropsWick); SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode; RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.Aliased; // Loop through each Plot Values on the chart for (int seriesCount = 0; seriesCount < Values.Length; seriesCount++) { Print("seriesCount " + seriesCount); Print("Values.Length " + Values.Length); float startX = 0; float endX = 0; double plotValue = 0; for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++) { Print("myDoubleSeries1.GetValueAt(index) " + myDoubleSeries1.GetValueAt(barIndex) + " " + Time[0]); plotValue = myDoubleSeries1.GetValueAt(barIndex); } // get the starting and ending bars from what is rendered on the chart startX = chartControl.GetXByBarIndex(ChartBars, seriesCount); endX = chartControl.GetXByBarIndex(ChartBars, Values.Length); // get the value at the last bar on the chart (if it has been set) if (Values[seriesCount].IsValidDataPointAt(ChartBars.ToIndex)) { //double plotValue = cHigh; // double plotValue = MyPlot1[0]; // double plotValue = myDoubleSeries1.GetValueAt(seriesCount); // Print(" "); // Print("plotValue " + plotValue); // convert the plot value to the charts "Y" axis point float chartScaleYValue = chartScale.GetYByValue(plotValue); // calculate the x and y values for the line to start and end SharpDX.Vector2 startPoint = new SharpDX.Vector2(startX, chartScaleYValue); SharpDX.Vector2 endPoint = new SharpDX.Vector2(endX, chartScaleYValue); // draw a line between the start and end point at each plot using the plots SharpDX Brush color and style RenderTarget.DrawLine(startPoint, endPoint, Plots[seriesCount].BrushDX, Plots[seriesCount].Width, solidLine); } } solidLine.Dispose(); RenderTarget.AntialiasMode = oldAntialiasMode; } #region Properties [Browsable(false)] [XmlIgnore] public Series<double> MyPlot1 { get { return Values[0]; } } [NinjaScriptProperty] [Display(Name="DashStyle Solid Line", Description="DashStyle Solid Line.", Order=0, GroupName="Plots Lines")] public SharpDX.Direct2D1.DashStyle DashStyleSolidLine { get; set; } #endregion } } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private _OnRenderPlot[] cache_OnRenderPlot; public _OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return _OnRenderPlot(Input, dashStyleSolidLine); } public _OnRenderPlot _OnRenderPlot(ISeries<double> input, SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { if (cache_OnRenderPlot != null) for (int idx = 0; idx < cache_OnRenderPlot.Length; idx++) if (cache_OnRenderPlot[idx] != null && cache_OnRenderPlot[idx].DashStyleSolidLine == dashStyleSolidLine && cache_OnRenderPlot[idx].EqualsInput(input)) return cache_OnRenderPlot[idx]; return CacheIndicator<_OnRenderPlot>(new _OnRenderPlot(){ DashStyleSolidLine = dashStyleSolidLine }, input, ref cache_OnRenderPlot); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } #endregion
Comment
-
Originally posted by NinjaTrader_ChelseaB View PostHello PaulMohn,
If you are trying to loop over the visible bars, this would be:
for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
{
Print( myDoubleSeries1.GetValueAt(index) );
}
What is the line of code causing the error?
The line of code under the last print appears to be:
float chartScaleYValue = chartScale.GetYByValue(plotValue);
Is this the code causing the error?
Code:#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.DrawingTools; using SharpDX; using SharpDX.Direct2D1; using SharpDX.DirectWrite; #endregion //This namespace holds Indicators in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Indicators { public class _OnRenderPlot : Indicator { int cBar; double cHigh; // Defines the Series object private Series<double> myDoubleSeries, myDoubleSeries1; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Indicator here."; Name = "_OnRenderPlot"; Calculate = Calculate.OnEachTick; IsOverlay = true; 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; // Adds a blue Dash-Line style plot with 5pixel width and 70% opacity AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Dash, 5, 70), PlotStyle.Hash, "MyPlot1"); } else if (State == State.Configure) { } else if (State == State.DataLoaded) { // Create a new Series object and assign it to the variable myDoubleSeries declared in the ‘Variables’ region above myDoubleSeries = new Series<double>(this); myDoubleSeries1 = new Series<double>(this); } } protected override void OnBarUpdate() { if (CurrentBar <21) return; if (Open[0] < Close[0]) { cBar = CurrentBar; cHigh = High[0]; MyPlot1[0] = cHigh; myDoubleSeries[0] = cHigh; } else { //MyPlot1[0] = double.NaN; } if ((CurrentBar - cBar) < 4) { MyPlot1[0] = cHigh; myDoubleSeries1[0] = cHigh; } Print(" "); //Print("myDoubleSeries0 " +myDoubleSeries[0]); //Print("myDoubleSeries1 " +myDoubleSeries1[0]); } protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { // Print(" "); // Print("myDoubleSeries1 " +myDoubleSeries[0]); base.OnRender(chartControl, chartScale); SharpDX.Direct2D1.StrokeStyleProperties ssPropsWick = new SharpDX.Direct2D1.StrokeStyleProperties(); ssPropsWick.DashStyle = DashStyleSolidLine; SharpDX.Direct2D1.StrokeStyle solidLine = new SharpDX.Direct2D1.StrokeStyle(RenderTarget.Factory, ssPropsWick); SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode; RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.Aliased; // Loop through each Plot Values on the chart // for (int seriesCount = 0; seriesCount < Values.Length; seriesCount++) // { // Print("seriesCount " + seriesCount); // Print("Values.Length " + Values.Length); float startX = 0; float endX = 0; double plotValue = 0; for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++) { Print("myDoubleSeries1.GetValueAt(index) " + myDoubleSeries1.GetValueAt(barIndex) + " " + Time[0]); plotValue = myDoubleSeries1.GetValueAt(barIndex); // get the starting and ending bars from what is rendered on the chart startX = chartControl.GetXByBarIndex(ChartBars, ChartBars.FromIndex); endX = chartControl.GetXByBarIndex(ChartBars, ChartBars.ToIndex); Print("startX " + startX + " " + Time[0]); Print("endX " + endX + " " + Time[0]); // get the value at the last bar on the chart (if it has been set) // if (Values[seriesCount].IsValidDataPointAt(ChartBars.ToIndex)) // { //double plotValue = cHigh; // double plotValue = MyPlot1[0]; // double plotValue = myDoubleSeries1.GetValueAt(seriesCount); // Print(" "); // Print("plotValue " + plotValue); // convert the plot value to the charts "Y" axis point float chartScaleYValue = chartScale.GetYByValue(plotValue); Print("chartScaleYValue " + chartScaleYValue + " " + Time[0]); // calculate the x and y values for the line to start and end SharpDX.Vector2 startPoint = new SharpDX.Vector2(startX, chartScaleYValue); SharpDX.Vector2 endPoint = new SharpDX.Vector2(endX, chartScaleYValue); Print("startPoint " + startPoint + " " + Time[0]); Print("endPoint " + endPoint + " " + Time[0]); Print("DrawLine0 " + RenderTarget.DrawLine(startPoint, endPoint, Plots[barIndex].BrushDX, Plots[barIndex].Width, solidLine) + " " + Time[0]); // draw a line between the start and end point at each plot using the plots SharpDX Brush color and style RenderTarget.DrawLine(startPoint, endPoint, Plots[barIndex].BrushDX, Plots[barIndex].Width, solidLine); Print("DrawLine1 " + RenderTarget.DrawLine(startPoint, endPoint, Plots[barIndex].BrushDX, Plots[barIndex].Width, solidLine) + " " + Time[0]); } // } solidLine.Dispose(); RenderTarget.AntialiasMode = oldAntialiasMode; } #region Properties [Browsable(false)] [XmlIgnore] public Series<double> MyPlot1 { get { return Values[0]; } } [NinjaScriptProperty] [Display(Name="DashStyle Solid Line", Description="DashStyle Solid Line.", Order=0, GroupName="Plots Lines")] public SharpDX.Direct2D1.DashStyle DashStyleSolidLine { get; set; } #endregion } } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private _OnRenderPlot[] cache_OnRenderPlot; public _OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return _OnRenderPlot(Input, dashStyleSolidLine); } public _OnRenderPlot _OnRenderPlot(ISeries<double> input, SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { if (cache_OnRenderPlot != null) for (int idx = 0; idx < cache_OnRenderPlot.Length; idx++) if (cache_OnRenderPlot[idx] != null && cache_OnRenderPlot[idx].DashStyleSolidLine == dashStyleSolidLine && cache_OnRenderPlot[idx].EqualsInput(input)) return cache_OnRenderPlot[idx]; return CacheIndicator<_OnRenderPlot>(new _OnRenderPlot(){ DashStyleSolidLine = dashStyleSolidLine }, input, ref cache_OnRenderPlot); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } #endregion
RenderTarget.DrawLine(startPoint, endPoint, Plots[barIndex].BrushDX,
Plots[barIndex].Width, solidLine);
Code:myDoubleSeries1.GetValueAt(index) 18105 02/08/2024 00:00:07 startX 14 02/08/2024 00:00:07 endX 788 02/08/2024 00:00:07 chartScaleYValue 12826 02/08/2024 00:00:07 startPoint X:14 Y:12826 02/08/2024 00:00:07 endPoint X:788 Y:12826 02/08/2024 00:00:07 Indicator '_OnRenderPlot': Error on calling 'OnRender' method on bar 117497: Index was outside the bounds of the array.
Comment
-
Hello PaulMohn,
If you comment that line and the lines below does the error stop?
RenderTarget.DrawLine(startPoint, endPoint, Plots[barIndex].BrushDX,Plots[barIndex].Width, solidLine);
barIndex is being assigned as the bar index.
for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
For the Plots collection you will need a plot series index.
Trying using 0 instead.
Plots[0].BrushDX Plots[0].WidthChelsea B.NinjaTrader Customer Service
- Likes 1
Comment
-
Hello PaulMohn,
If you comment that line and the lines below does the error stop?
Yes in this version
Code:#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.DrawingTools; using SharpDX; using SharpDX.Direct2D1; using SharpDX.DirectWrite; #endregion //This namespace holds Indicators in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Indicators { public class _OnRenderPlot : Indicator { int cBar; double cHigh; // Defines the Series object private Series<double> myDoubleSeries, myDoubleSeries1; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Indicator here."; Name = "_OnRenderPlot"; Calculate = Calculate.OnEachTick; IsOverlay = true; 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; // Adds a blue Dash-Line style plot with 5pixel width and 70% opacity AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Dash, 5, 70), PlotStyle.Hash, "MyPlot1"); } else if (State == State.Configure) { } else if (State == State.DataLoaded) { // Create a new Series object and assign it to the variable myDoubleSeries declared in the ‘Variables’ region above myDoubleSeries = new Series<double>(this); myDoubleSeries1 = new Series<double>(this); } } protected override void OnBarUpdate() { if (CurrentBar <21) return; if (Open[0] < Close[0]) { cBar = CurrentBar; cHigh = High[0]; MyPlot1[0] = cHigh; myDoubleSeries[0] = cHigh; } else { //MyPlot1[0] = double.NaN; } if ((CurrentBar - cBar) < 4) { MyPlot1[0] = cHigh; myDoubleSeries1[0] = cHigh; } Print(" "); //Print("myDoubleSeries0 " +myDoubleSeries[0]); //Print("myDoubleSeries1 " +myDoubleSeries1[0]); } protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { // Print(" "); // Print("myDoubleSeries1 " +myDoubleSeries[0]); base.OnRender(chartControl, chartScale); SharpDX.Direct2D1.StrokeStyleProperties ssPropsWick = new SharpDX.Direct2D1.StrokeStyleProperties(); ssPropsWick.DashStyle = DashStyleSolidLine; SharpDX.Direct2D1.StrokeStyle solidLine = new SharpDX.Direct2D1.StrokeStyle(RenderTarget.Factory, ssPropsWick); SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode; RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.Aliased; // Loop through each Plot Values on the chart // for (int seriesCount = 0; seriesCount < Values.Length; seriesCount++) // { // Print("seriesCount " + seriesCount); // Print("Values.Length " + Values.Length); float startX = 0; float endX = 0; double plotValue = 0; for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++) { Print("myDoubleSeries1.GetValueAt(index) " + myDoubleSeries1.GetValueAt(barIndex) + " " + Time[0]); plotValue = myDoubleSeries1.GetValueAt(barIndex); // get the starting and ending bars from what is rendered on the chart startX = chartControl.GetXByBarIndex(ChartBars, ChartBars.FromIndex); endX = chartControl.GetXByBarIndex(ChartBars, ChartBars.ToIndex); Print("startX " + startX + " " + Time[0]); Print("endX " + endX + " " + Time[0]); // get the value at the last bar on the chart (if it has been set) // if (Values[seriesCount].IsValidDataPointAt(ChartBars.ToIndex)) // { //double plotValue = cHigh; // double plotValue = MyPlot1[0]; // double plotValue = myDoubleSeries1.GetValueAt(seriesCount); // Print(" "); // Print("plotValue " + plotValue); // convert the plot value to the charts "Y" axis point // float chartScaleYValue = chartScale.GetYByValue(plotValue); // Print("chartScaleYValue " + chartScaleYValue + " " + Time[0]); // // calculate the x and y values for the line to start and end // SharpDX.Vector2 startPoint = new SharpDX.Vector2(startX, chartScaleYValue); // SharpDX.Vector2 endPoint = new SharpDX.Vector2(endX, chartScaleYValue); // Print("startPoint " + startPoint + " " + Time[0]); // Print("endPoint " + endPoint + " " + Time[0]); // // draw a line between the start and end point at each plot using the plots SharpDX Brush color and style // RenderTarget.DrawLine(startPoint, endPoint, Plots[barIndex].BrushDX, // Plots[barIndex].Width, solidLine); } // } solidLine.Dispose(); RenderTarget.AntialiasMode = oldAntialiasMode; } #region Properties [Browsable(false)] [XmlIgnore] public Series<double> MyPlot1 { get { return Values[0]; } } [NinjaScriptProperty] [Display(Name="DashStyle Solid Line", Description="DashStyle Solid Line.", Order=0, GroupName="Plots Lines")] public SharpDX.Direct2D1.DashStyle DashStyleSolidLine { get; set; } #endregion } } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private _OnRenderPlot[] cache_OnRenderPlot; public _OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return _OnRenderPlot(Input, dashStyleSolidLine); } public _OnRenderPlot _OnRenderPlot(ISeries<double> input, SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { if (cache_OnRenderPlot != null) for (int idx = 0; idx < cache_OnRenderPlot.Length; idx++) if (cache_OnRenderPlot[idx] != null && cache_OnRenderPlot[idx].DashStyleSolidLine == dashStyleSolidLine && cache_OnRenderPlot[idx].EqualsInput(input)) return cache_OnRenderPlot[idx]; return CacheIndicator<_OnRenderPlot>(new _OnRenderPlot(){ DashStyleSolidLine = dashStyleSolidLine }, input, ref cache_OnRenderPlot); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } #endregion [I][/I]
Comment
-
Hello PaulMohn,
...
For the Plots collection you will need a plot series index.
Trying using 0 instead.
Plots[0].BrushDX Plots[0].Width
It plots the solid line with this, thanks
Code:#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.DrawingTools; using SharpDX; using SharpDX.Direct2D1; using SharpDX.DirectWrite; #endregion //This namespace holds Indicators in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Indicators { public class _OnRenderPlot : Indicator { int cBar; double cHigh; // Defines the Series object private Series<double> myDoubleSeries, myDoubleSeries1; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Indicator here."; Name = "_OnRenderPlot"; Calculate = Calculate.OnEachTick; IsOverlay = true; 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; // Adds a blue Dash-Line style plot with 5pixel width and 70% opacity AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Dash, 5, 70), PlotStyle.Hash, "MyPlot1"); } else if (State == State.Configure) { } else if (State == State.DataLoaded) { // Create a new Series object and assign it to the variable myDoubleSeries declared in the ‘Variables’ region above myDoubleSeries = new Series<double>(this); myDoubleSeries1 = new Series<double>(this); } } protected override void OnBarUpdate() { if (CurrentBar <21) return; if (Open[0] < Close[0]) { cBar = CurrentBar; cHigh = High[0]; MyPlot1[0] = cHigh; myDoubleSeries[0] = cHigh; } else { //MyPlot1[0] = double.NaN; } if ((CurrentBar - cBar) < 4) { MyPlot1[0] = cHigh; myDoubleSeries1[0] = cHigh; } Print(" "); //Print("myDoubleSeries0 " +myDoubleSeries[0]); //Print("myDoubleSeries1 " +myDoubleSeries1[0]); } protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { // Print(" "); // Print("myDoubleSeries1 " +myDoubleSeries[0]); base.OnRender(chartControl, chartScale); SharpDX.Direct2D1.StrokeStyleProperties ssPropsWick = new SharpDX.Direct2D1.StrokeStyleProperties(); ssPropsWick.DashStyle = DashStyleSolidLine; SharpDX.Direct2D1.StrokeStyle solidLine = new SharpDX.Direct2D1.StrokeStyle(RenderTarget.Factory, ssPropsWick); SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode; RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.Aliased; // Loop through each Plot Values on the chart // for (int seriesCount = 0; seriesCount < Values.Length; seriesCount++) // { // Print("seriesCount " + seriesCount); // Print("Values.Length " + Values.Length); float startX = 0; float endX = 0; double plotValue = 0; for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++) { Print("myDoubleSeries1.GetValueAt(index) " + myDoubleSeries1.GetValueAt(barIndex) + " " + Time[0]); plotValue = myDoubleSeries1.GetValueAt(barIndex); // get the starting and ending bars from what is rendered on the chart startX = chartControl.GetXByBarIndex(ChartBars, ChartBars.FromIndex); endX = chartControl.GetXByBarIndex(ChartBars, ChartBars.ToIndex); Print("startX " + startX + " " + Time[0]); Print("endX " + endX + " " + Time[0]); // get the value at the last bar on the chart (if it has been set) // if (Values[seriesCount].IsValidDataPointAt(ChartBars.ToIndex)) // { //double plotValue = cHigh; // double plotValue = MyPlot1[0]; // double plotValue = myDoubleSeries1.GetValueAt(seriesCount); // Print(" "); // Print("plotValue " + plotValue); // convert the plot value to the charts "Y" axis point float chartScaleYValue = chartScale.GetYByValue(plotValue); Print("chartScaleYValue " + chartScaleYValue + " " + Time[0]); // calculate the x and y values for the line to start and end SharpDX.Vector2 startPoint = new SharpDX.Vector2(startX, chartScaleYValue); SharpDX.Vector2 endPoint = new SharpDX.Vector2(endX, chartScaleYValue); Print("startPoint " + startPoint + " " + Time[0]); Print("endPoint " + endPoint + " " + Time[0]); // draw a line between the start and end point at each plot using the plots SharpDX Brush color and style RenderTarget.DrawLine(startPoint, endPoint, Plots[0].BrushDX, Plots[0].Width, solidLine); } // } solidLine.Dispose(); RenderTarget.AntialiasMode = oldAntialiasMode; } #region Properties [Browsable(false)] [XmlIgnore] public Series<double> MyPlot1 { get { return Values[0]; } } [NinjaScriptProperty] [Display(Name="DashStyle Solid Line", Description="DashStyle Solid Line.", Order=0, GroupName="Plots Lines")] public SharpDX.Direct2D1.DashStyle DashStyleSolidLine { get; set; } #endregion } } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private _OnRenderPlot[] cache_OnRenderPlot; public _OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return _OnRenderPlot(Input, dashStyleSolidLine); } public _OnRenderPlot _OnRenderPlot(ISeries<double> input, SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { if (cache_OnRenderPlot != null) for (int idx = 0; idx < cache_OnRenderPlot.Length; idx++) if (cache_OnRenderPlot[idx] != null && cache_OnRenderPlot[idx].DashStyleSolidLine == dashStyleSolidLine && cache_OnRenderPlot[idx].EqualsInput(input)) return cache_OnRenderPlot[idx]; return CacheIndicator<_OnRenderPlot>(new _OnRenderPlot(){ DashStyleSolidLine = dashStyleSolidLine }, input, ref cache_OnRenderPlot); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators._OnRenderPlot _OnRenderPlot(SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(Input, dashStyleSolidLine); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , SharpDX.Direct2D1.DashStyle dashStyleSolidLine) { return indicator._OnRenderPlot(input, dashStyleSolidLine); } } } #endregion
Not sure how to get the solid plot over the bars at their respective Y to X axis coordinates only.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Day_Storay1, Today, 09:06 PM
|
0 responses
6 views
0 likes
|
Last Post
by Day_Storay1
Today, 09:06 PM
|
||
Started by Rudmax, 07-02-2024, 05:40 AM
|
59 responses
1,053 views
1 like
|
Last Post
by QuantVPS
Today, 08:48 PM
|
||
Started by tomtom2000, Today, 12:56 PM
|
3 responses
18 views
0 likes
|
Last Post
by jabowery
Today, 07:45 PM
|
||
Started by stoner, 01-16-2020, 08:07 AM
|
20 responses
514 views
0 likes
|
Last Post
by IanS00
Today, 07:17 PM
|
||
Started by jmschmidt357, 09-06-2024, 04:51 PM
|
4 responses
52 views
0 likes
|
Last Post
by jabowery
Today, 07:13 PM
|
Comment