Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
How to draw a line using the PlotStyle from a Plot?
Collapse
X
-
Hello koganam,
Thanks for your post. This would require custom rendering to accomplish. Below is a modified snippet from the help guide which can demonstrate drawing from the last bar to the right side margin.
PlotStyle.Cross cannot be used as a SharpDX StrokeStyle, so additional vertical lines would need to be drawn the spanning from ChartBars.ToIndex to ChartPanel.X + CharPanel.W. You can draw small vertical lines on top of this bar spanning the distance between ChartBars.ToIndex and ChartBars.ToIndex-1 to offset each vertical line so they are distanced in the same fashion as other bars on the chart.Code:protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { base.OnRender(chartControl, chartScale); // get the starting and ending bars from what is rendered on the chart float startX = chartControl.GetXByBarIndex(ChartBars, ChartBars.ToIndex); float endX = (ChartPanel.X + ChartPanel.W); // 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 = Values[seriesCount].GetValueAt(ChartBars.ToIndex); // 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, Plots[seriesCount].StrokeStyle); } } }
With the small vertical lines drawn on top of the horizontal line, you will have achieved the same visual effect as having the PlotStyle.Cross plot extend to the right side margin.
Please let me know if I can be of further assistance.
-
Your response goes way further afield, and has a specificity that is way too narrow, relative to the question that I asked.Originally posted by NinjaTrader_Jim View PostHello koganam,
Thanks for your post. This would require custom rendering to accomplish. Below is a modified snippet from the help guide which can demonstrate drawing from the last bar to the right side margin.
PlotStyle.Cross cannot be used as a SharpDX StrokeStyle, so additional vertical lines would need to be drawn the spanning from ChartBars.ToIndex to ChartPanel.X + CharPanel.W. You can draw small vertical lines on top of this bar spanning the distance between ChartBars.ToIndex and ChartBars.ToIndex-1 to offset each vertical line so they are distanced in the same fashion as other bars on the chart.Code:protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { base.OnRender(chartControl, chartScale); // get the starting and ending bars from what is rendered on the chart float startX = chartControl.GetXByBarIndex(ChartBars, ChartBars.ToIndex); float endX = (ChartPanel.X + ChartPanel.W); // 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 = Values[seriesCount].GetValueAt(ChartBars.ToIndex); // 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, Plots[seriesCount].StrokeStyle); } } }
With the small vertical lines drawn on top of the horizontal line, you will have achieved the same visual effect as having the PlotStyle.Cross plot extend to the right side margin.
Please let me know if I can be of further assistance.
I have already been able to finagle the PlotStyle.Cross pretty much in the manner that you describe. However, I used that PlotStyle pretty much as an example for my question, not as the only specific style to be used. I wanted to know how to use the Plot's PlotStyle, whatever it may be. After all, you are rendering the chart with the various plot styles, per user choice.
I was asking how to do the same using whatever the user may have already chosen. I really did not want to have to think through and code for each specific choice in the enumeration. I am way too lazy for that.
Comment
-
Wondering if there is a way to add a desired PlotStyle to a existing or called Ray, and Referenced Ray instead of them using DashStyle to be able to call the string name or ray to hard code a PlotStyle TriangleUp/Down. Is it possible using these line types?
Comment
-
Hello LoganJKTrader,
I may not be understanding the question fully.
PlotSyles are specific for customizing plots. Plots also contains a Stroke which has a Brush (color) and DashStyle.
If you want to customize plotting code, you will have to override the indicator's OnRender method.
Drawing Objects may only have the Stroke (includes DashStyle) to customize.
A list of available drawing commands that would not require custom rendering code in OnRender can be found below. Maybe you are looking to use Draw.ArrowUp/Draw.ArrowDown?
If there are additional questions, could you further describe what you are trying to accomplish?
Comment
-
Yes I have a set of high and low rays which also have a reference ray bool, and then there is one and two StDvtn away as a target of the high and low levels, and the HL and the targets are similar in visual style and having them changed plotstyle will help to identify what it is that is and differ the look.Originally posted by NinjaTrader_Jim View PostHello LoganJKTrader,
I may not be understanding the question fully.
PlotSyles are specific for customizing plots. Plots also contains a Stroke which has a Brush (color) and DashStyle.
If you want to customize plotting code, you will have to override the indicator's OnRender method.
Drawing Objects may only have the Stroke (includes DashStyle) to customize.
A list of available drawing commands that would not require custom rendering code in OnRender can be found below. Maybe you are looking to use Draw.ArrowUp/Draw.ArrowDown?
https://ninjatrader.com/support/help...t8/drawing.htm
If there are additional questions, could you further describe what you are trying to accomplish?
Comment
-
Hello LoganJKTrader,
Ray's do not have a PlotStyle, We would only be able to customize the DashStyle, and we would us the following overloads for that purpose:
Draw.Ray(NinjaScriptBase owner, string tag, DateTime startTime, double startY, DateTime endTime, double endY, Brush brush, DashStyleHelper dashStyle, int width)
Draw.Ray(NinjaScriptBase owner, string tag, bool isAutoScale, int startBarsAgo, double startY, int endBarsAgo, double endY, Brush brush, DashStyleHelper dashStyle, int width, bool drawOnPricePanel)
Draw.Ray(NinjaScriptBase owner, string tag, DateTime startTime, double startY, DateTime endTime, double endY, Brush brush, DashStyleHelper dashStyle, int width, bool drawOnPricePanel)
https://ninjatrader.com/support/help...l?draw_ray.htm
If you wanted to make a custom Ray that looks like a PlotStyle, that would require custom rendering in a drawing tool. You could make a copy of the Lines DrawingTool if you wanted to make modifications for a customized Ray.
Comment
-
Originally posted by NinjaTrader_Jim View PostHello koganam,
Thanks for your post. This would require custom rendering to accomplish. Below is a modified snippet from the help guide which can demonstrate drawing from the last bar to the right side margin.
PlotStyle.Cross cannot be used as a SharpDX StrokeStyle, so additional vertical lines would need to be drawn the spanning from ChartBars.ToIndex to ChartPanel.X + CharPanel.W. You can draw small vertical lines on top of this bar spanning the distance between ChartBars.ToIndex and ChartBars.ToIndex-1 to offset each vertical line so they are distanced in the same fashion as other bars on the chart.Code:protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { base.OnRender(chartControl, chartScale); // get the starting and ending bars from what is rendered on the chart float startX = chartControl.GetXByBarIndex(ChartBars, ChartBars.ToIndex); float endX = (ChartPanel.X + ChartPanel.W); // 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 = Values[seriesCount].GetValueAt(ChartBars.ToIndex); // 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, Plots[seriesCount].StrokeStyle); } } }
With the small vertical lines drawn on top of the horizontal line, you will have achieved the same visual effect as having the PlotStyle.Cross plot extend to the right side margin.
Please let me know if I can be of further assistance.
Need:
I need a horizontal line without blanks and without the slanted connections line segments between the horizontal line segments.
I have this plot:
AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Dash, 5, 95), PlotStyle.Hash, "MyPlot1");
Problem:
It plots horizontal dashes (horizontal line segments spaced by blanks).
The PlotStyle.Line has no blanks but it connects the horizontal lines.
AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Solid, 5, 95), PlotStyle.Line, "MyPlot1");
The PlotStyle.HLine plots a horizontal line but across the whole X axis.
AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Dash, 5, 95), PlotStyle.HLine, "MyPlot1");
I need a solution with AddPlot, not with Draw.Line.Last edited by PaulMohn; 07-28-2024, 02:15 PM.
Comment
-
Those 5 objects in the AddPlot(); can be customized.Originally posted by PaulMohn View Post
Need:
I need a horizontal line without blanks and without the slanted connections line segments between the horizontal line segments.
I have this plot:
AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Dash, 5, 95), PlotStyle.Hash, "MyPlot1");
Problem:
It plots horizontal dashes (horizontal line segments spaced by blanks).
The PlotStyle.Line has no blanks but it connects the horizontal lines.
AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Dash, 5, 95), PlotStyle.Line, "MyPlot1");
The PlotStyle.HLine plots a horizontal line but across the whole X axis.
AddPlot(new Stroke(Brushes.Cyan, DashStyleHelper.Dash, 5, 95), PlotStyle.HLine, "MyPlot1");
I need a solution with AddPlot, not with Draw.Line.
Example AddPlot(new Stroke(Brushes.SkyBlue, DashStyleHelper.Solid, 7, 100), PlotStyle.Line, "_myplot10");
I am just showing examples, do not define any actual object change on the example. Search how to correctly make a custom AddPlot that has a solid line.
Comment
-
Originally posted by LoganJKTrader View Post
Those 5 objects in the AddPlot(); can be customized.
Example AddPlot(new Stroke(Brushes.SkyBlue, DashStyleHelper.Solid, 7, 100), PlotStyle.Line, "_myplot10");
I am just showing examples, do not define any actual object change on the example. Search how to correctly make a custom AddPlot that has a solid line.
Input 0
AddPlot(new Stroke(Brushes.Magenta, DashStyleHelper.Solid, 5, 95), PlotStyle.Line, "MyPlot1");
Output 0 (undesired)
Input 1
AddPlot(new Stroke(Brushes.Magenta, DashStyleHelper.Dash, 5, 95), PlotStyle.Hash, "MyPlot1");
Output 1 (undesired)
Input 2
AddPlot(new Stroke(Brushes.Magenta, DashStyleHelper.Dash, 5, 95), PlotStyle.Line, "MyPlot1");
Output 2 (undesired)
Desired output
081724:
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; #endregion //This namespace holds Indicators in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Indicators { public class _OnRenderPlot : Indicator { private int cBar; private double cHigh; 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; MaximumBarsLookBack = MaximumBarsLookBack.Infinite; ShowTransparentPlotsInDataBox = true; plot1Brush = Brushes.ForestGreen; // Adds a Cyan Dash-Line style plot with 5pixel width and 3% opacity to hide the plot AddPlot(new Stroke(Brushes.Transparent, DashStyleHelper.Dash, 5, 3), PlotStyle.Hash, "MyPlot1"); } else if (State == State.Configure) { } } protected override void OnBarUpdate() { if (CurrentBar <21) return; if (Open[0] < Close[0]) { cBar = CurrentBar; cHigh = High[0]; MyPlot1[0] = cHigh; } if ((CurrentBar - cBar) < 4) { MyPlot1[0] = cHigh; } } protected override void OnRender(ChartControl chartControl, ChartScale chartScale) { base.OnRender(chartControl, chartScale); SharpDX.Direct2D1.Brush plot1BrushDx; plot1BrushDx = plot1Brush.ToDxBrush(RenderTarget); SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode; RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.PerPrimitive; Vector2 point0 = new Vector2(); Vector2 point1 = new Vector2(); Vector2 point2 = new Vector2(); Vector2 point3 = new Vector2(); for (int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++) { double plotValue0 = 0, plotValue1 = 0; float valY0, xHeight0, x0, barWidth0, xWidth0, valY1, xHeight1, x1, barWidth1, xWidth1, outlineWidth; plotValue0 = MyPlot1.GetValueAt(barIndex); valY0 = chartScale.GetYByValue(plotValue0); x0 = chartControl.GetXByBarIndex(ChartBars, barIndex); barWidth0 = (float)chartControl.BarWidth; outlineWidth = ChartBars.Properties.ChartStyle.Stroke2.Width; // 1.0f; point0.X = x0 - barWidth0 - outlineWidth; point0.Y = valY0; point1.X = x0 + barWidth0 + outlineWidth; point1.Y = valY0; xHeight0 = 10.0f; xWidth0 = (point1.X - point0.X); bool Cond0 = ( plotValue1 != double.NaN && plotValue0 != double.NaN && ( plotValue1 != plotValue0 ) || (plotValue1 == plotValue0 )); if ( Cond0 ) { RenderTarget.DrawLine(point0, point1, plot1BrushDx, 4); } plotValue1 = MyPlot1.GetValueAt(barIndex-1); valY1 = chartScale.GetYByValue(plotValue1); x1 = chartControl.GetXByBarIndex(ChartBars, barIndex-1); point2.X = x1 + barWidth0 - outlineWidth; point2.Y = valY1; point3.X = x0 - barWidth0 + outlineWidth; point3.Y = valY1; xHeight1 = 10.0f; xWidth1 = (point3.X - point2.X); bool Cond1 = ( plotValue1 != double.NaN && plotValue0 != double.NaN && plotValue1 == plotValue0 && !( plotValue1 != plotValue0 )); if ( Cond1 ) { RenderTarget.DrawLine(point2, point3, plot1BrushDx, 4); } } RenderTarget.AntialiasMode = oldAntialiasMode; } #region Properties [Browsable(false)] [XmlIgnore] public Series<double> MyPlot1 { get { return Values[0]; } } [NinjaScriptProperty] [XmlIgnore] [Display(Name="Plot Brush", Order=1, GroupName="Parameters")] public System.Windows.Media.Brush plot1Brush { get; set; } [Browsable(false)] public string plot1BrushSerializable { get { return Serialize.BrushToString(plot1Brush); } set { plot1Brush = Serialize.StringToBrush(value); } } #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(System.Windows.Media.Brush plot1Brush) { return _OnRenderPlot(Input, plot1Brush); } public _OnRenderPlot _OnRenderPlot(ISeries<double> input, System.Windows.Media.Brush plot1Brush) { if (cache_OnRenderPlot != null) for (int idx = 0; idx < cache_OnRenderPlot.Length; idx++) if (cache_OnRenderPlot[idx] != null && cache_OnRenderPlot[idx].plot1Brush == plot1Brush && cache_OnRenderPlot[idx].EqualsInput(input)) return cache_OnRenderPlot[idx]; return CacheIndicator<_OnRenderPlot>(new _OnRenderPlot(){ plot1Brush = plot1Brush }, input, ref cache_OnRenderPlot); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators._OnRenderPlot _OnRenderPlot(System.Windows.Media.Brush plot1Brush) { return indicator._OnRenderPlot(Input, plot1Brush); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , System.Windows.Media.Brush plot1Brush) { return indicator._OnRenderPlot(input, plot1Brush); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators._OnRenderPlot _OnRenderPlot(System.Windows.Media.Brush plot1Brush) { return indicator._OnRenderPlot(Input, plot1Brush); } public Indicators._OnRenderPlot _OnRenderPlot(ISeries<double> input , System.Windows.Media.Brush plot1Brush) { return indicator._OnRenderPlot(input, plot1Brush); } } } #endregionLast edited by PaulMohn; 08-17-2024, 08:27 AM.
Comment
-
-
Comment
-
Ok thanks. What steps would you follow to do it with OnRender()?
https://ninjatrader.com/support/helpguides/nt8/NT%20HelpGuide%20English.html?using_sharpdx_for_cu stom_chart_rendering.htm
Not sure where to look for the relevant topics.
Comment
-
Hello PaulMohn,
Included with NinjaTrader is a SampleCustomRender that demonstrates rendering in OnRender().
To render a straight line this would be done with RenderTarget.DrawLine(), to render a non-straight line or shape this would be done with RenderTarget.DrawGeometry().
In post # 2 NinjaTrader_Jim has provided sample code of rendering a line.Chelsea B.NinjaTrader Customer Service
- Likes 1
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
601 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
347 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
103 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
559 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
558 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment