If not, will the bar type be extendable like it was in 6.5?
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Hollow Renko Bars
Collapse
X
-
Real Hollow Bars
Ok, I decided to play with the ChartStyles to figure out why hollow bars are not possible, and I ended up fixing the OpenClose style to properly allow hollow bars (see attached image). Below is my proposed OpenCloseStyle code that implements a few changes to allow the hollow bars.
The major changes are the following:
* only use one brush, saving a resource
* enable Pen2 so a different outline can be used for up/down bars
* updates to helper methods
* update PaintBars() to handle everything
* generally tighten up the PaintBars() code a bit
When coloring bars, the order of precedence is to use the set CandleOutlineColor if not empty, then the set BarColor if not empty, and then the default up/down Pen color.
To generate hollow bars, the up/down colors can be set to Transparent, and the two Pen color control the painting. If a study then overrides the colors, that would properly recolor the bars.
This same pattern can be applied to the CandleStyle as well, and I would be happy to do so if Ninja takes and runs with this code (as I pray they do after some review).
Code:public class OpenCloseStyle : ChartStyle { private static bool registered = Chart.ChartStyle.Register(new OpenCloseStyle()); private SolidBrush brush = new SolidBrush(Color.LightGreen); /// <summary> /// </summary> public OpenCloseStyle() : base(ChartStyleType.OpenClose) { this.DownColor = Color.Red; this.UpColor = Color.LightGreen; } /// <summary> /// </summary> /// <returns></returns> public override object Clone() { OpenCloseStyle ret = new OpenCloseStyle(); ret.BarWidth = BarWidth; ret.DownColor = DownColor; ret.Pen = Gui.Globals.Clone(Pen); ret.Pen2 = Gui.Globals.Clone(Pen2); ret.UpColor = UpColor; return ret; } /// <summary> /// </summary> /// <returns></returns> public override string DisplayName { get { return "Open/Close"; } } /// <summary> /// </summary> public override void Dispose() { base.Dispose(); brush.Dispose(); } /// <summary> /// </summary> /// <param name="barWidth"></param> /// <returns></returns> public override int GetBarPaintWidth(int barWidth) { // middle line + 2 * half of the body width + 2 * border line return (int) (1 + 2 * (barWidth) + 2 * Pen.Width); } /// <summary> /// </summary> /// <param name="propertyDescriptor"></param> /// <param name="chartStyle"></param> /// <param name="attributes"></param> /// <returns></returns> public override PropertyDescriptorCollection GetProperties(PropertyDescriptor propertyDescriptor, ChartStyle chartStyle, Attribute[] attributes) { PropertyDescriptorCollection properties = base.GetProperties(propertyDescriptor, chartStyle, attributes); // here is how you change the display name of the property on the properties grid Gui.Design.DisplayNameAttribute.SetDisplayName(properties, "BarWidthUI", "\r\r\rBar width"); Gui.Design.DisplayNameAttribute.SetDisplayName(properties, "DownColor", "\r\r\rColor for down bars"); Gui.Design.DisplayNameAttribute.SetDisplayName(properties, "Pen", "\r\r\rUp bars outline"); Gui.Design.DisplayNameAttribute.SetDisplayName(properties, "Pen2", "\r\r\rDown bars outline"); Gui.Design.DisplayNameAttribute.SetDisplayName(properties, "UpColor", "\r\r\rColor for up bars"); return properties; } /// <summary> /// </summary> public override bool IsTransparent { get { return UpColor == Color.Transparent && DownColor == Color.Transparent && Pen.Color == Color.Transparent && Pen2.Color == Color.Transparent; } } /// <summary> /// </summary> /// <param name="chartControl"></param> /// <param name="graphics"></param> /// <param name="bars"></param> /// <param name="panelIdx"></param> /// <param name="fromIdx"></param> /// <param name="toIdx"></param> /// <param name="bounds"></param> /// <param name="max"></param> /// <param name="min"></param> public override void PaintBars(ChartControl chartControl, Graphics graphics, Data.Bars bars, int panelIdx, int fromIdx, int toIdx, Rectangle bounds, double max, double min) { int BoxSize = bars.Period.Value; double TickSize = bars.Instrument.MasterInstrument.TickSize; double offset = BoxSize * TickSize; int barWidthValue = bars.BarsData.ChartStyle.BarWidthUI; for (int idx = fromIdx; idx <= toIdx; idx++) { int barWidth = GetBarPaintWidth(barWidthValue); double closeValue = bars.GetClose(idx); int close = chartControl.GetYByValue(bars, closeValue); double openValue = bars.GetOpen(idx); int open = chartControl.GetYByValue(bars, openValue); int x = chartControl.GetXByBarIdx(bars, idx); double previousOpenVal = (idx != 0 ? bars.GetOpen(idx - 1) : openValue); double previousCloseVal = (idx != 0 ? bars.GetClose(idx - 1) : closeValue); bool upTrend = (previousOpenVal < previousCloseVal); bool upBar = upTrend; // flip open if direction is reverse of trend if (upTrend && closeValue < openValue) { upBar = false; open = chartControl.GetYByValue(bars, openValue - offset); } else if (!upTrend && closeValue > openValue) { upBar = true; open = chartControl.GetYByValue(bars, openValue + offset); } Pen pen = upBar ? Pen : Pen2; Color orgPenColor = pen.Color; Color barColor = chartControl.GetBarColor(bars, idx); Color olColor = chartControl.GetCandleOutlineColor(bars, idx); if (olColor != Color.Empty) pen.Color = olColor; else if (barColor != Color.Empty) pen.Color = barColor; if (open == close) graphics.DrawLine(pen, x - barWidth / 2, close, x + barWidth / 2, close); else { brush.Color = barColor != Color.Empty ? barColor : upBar ? UpColor : DownColor; graphics.FillRectangle(brush, x - barWidth / 2 + 1, Math.Min(close, open) + 1, barWidth - 1, Math.Max(open, close) - Math.Min(close, open) - 1); graphics.DrawRectangle(pen, x - (barWidth / 2) + (pen.Width / 2), Math.Min(close, open), barWidth - pen.Width, Math.Max(open, close) - Math.Min(close, open)); } pen.Color = orgPenColor; } } }
Comment
-
Hollow Candlesticks
Ok, the following is the same method applied to CandleStyle:
Code:public class CandleStyle : ChartStyle { private static bool registered = Chart.ChartStyle.Register(new CandleStyle()); private SolidBrush brush = new SolidBrush(Color.LightGreen); /// <summary> /// </summary> public CandleStyle() : base(ChartStyleType.CandleStick) { this.DownColor = Color.Red; this.UpColor = Color.LightGreen; } /// <summary> /// </summary> /// <returns></returns> public override object Clone() { CandleStyle ret = new CandleStyle(); ret.BarWidth = BarWidth; ret.DownColor = DownColor; ret.Pen = Gui.Globals.Clone(Pen); ret.Pen2 = Gui.Globals.Clone(Pen2); ret.UpColor = UpColor; return ret; } /// <summary> /// </summary> /// <returns></returns> public override string DisplayName { get { return "Candle Stick"; } } /// <summary> /// </summary> public override void Dispose() { base.Dispose(); brush.Dispose(); } /// <summary> /// </summary> /// <param name="barWidth"></param> /// <returns></returns> public override int GetBarPaintWidth(int barWidth) { // middle line + 2 * half of the body width + 2 * border line return (int) (1 + 2 * (barWidth) + 2 * Pen.Width); } /// <summary> /// </summary> /// <param name="propertyDescriptor"></param> /// <param name="chartStyle"></param> /// <param name="attributes"></param> /// <returns></returns> public override PropertyDescriptorCollection GetProperties(PropertyDescriptor propertyDescriptor, ChartStyle chartStyle, Attribute[] attributes) { PropertyDescriptorCollection properties = base.GetProperties(propertyDescriptor, chartStyle, attributes); // here is how you change the display name of the property on the properties grid Gui.Design.DisplayNameAttribute.SetDisplayName(properties, "BarWidthUI", "\r\r\rBar width"); Gui.Design.DisplayNameAttribute.SetDisplayName(properties, "DownColor", "\r\r\rColor for down bars"); Gui.Design.DisplayNameAttribute.SetDisplayName(properties, "Pen", "\r\r\rUp bars outline"); Gui.Design.DisplayNameAttribute.SetDisplayName(properties, "Pen2", "\r\r\rDown bars outline"); Gui.Design.DisplayNameAttribute.SetDisplayName(properties, "UpColor", "\r\r\rColor for up bars"); return properties; } /// <summary> /// </summary> public override bool IsTransparent { get { return UpColor == Color.Transparent && DownColor == Color.Transparent && Pen.Color == Color.Transparent && Pen2.Color == Color.Transparent; } } /// <summary> /// </summary> /// <param name="chartControl"></param> /// <param name="graphics"></param> /// <param name="bars"></param> /// <param name="panelIdx"></param> /// <param name="fromIdx"></param> /// <param name="toIdx"></param> /// <param name="bounds"></param> /// <param name="max"></param> /// <param name="min"></param> public override void PaintBars(ChartControl chartControl, Graphics graphics, Data.Bars bars, int panelIdx, int fromIdx, int toIdx, Rectangle bounds, double max, double min) { int barWidthValue = bars.BarsData.ChartStyle.BarWidthUI; int barWidth = GetBarPaintWidth(barWidthValue); for (int idx = fromIdx; idx <= toIdx; idx++) { double closeValue = bars.GetClose(idx); int close = chartControl.GetYByValue(bars, closeValue); int high = chartControl.GetYByValue(bars, bars.GetHigh(idx)); int low = chartControl.GetYByValue(bars, bars.GetLow(idx)); double openValue = bars.GetOpen(idx); int open = chartControl.GetYByValue(bars, openValue); int x = chartControl.GetXByBarIdx(bars, idx); bool upBar = closeValue >= openValue; Pen pen = upBar ? Pen : Pen2; Color orgPenColor = pen.Color; Color barColor = chartControl.GetBarColor(bars, idx); Color olColor = chartControl.GetCandleOutlineColor(bars, idx); if (olColor != Color.Empty) pen.Color = olColor; else if (barColor != Color.Empty) pen.Color = barColor; if (open == close) graphics.DrawLine(pen, x - barWidth / 2, close, x + barWidth / 2, close); else { brush.Color = barColor != Color.Empty ? barColor : upBar ? UpColor : DownColor; graphics.FillRectangle(brush, x - barWidth / 2 + 1, Math.Min(close, open) + 1, barWidth - 1, Math.Max(open, close) - Math.Min(close, open) - 1); graphics.DrawRectangle(pen, x - (barWidth / 2) + (pen.Width / 2), Math.Min(close, open), barWidth - pen.Width, Math.Max(open, close) - Math.Min(close, open)); } if (high < Math.Min(open, close)) graphics.DrawLine(pen, x, high, x, Math.Min(open, close)); if (low > Math.Max(open, close)) graphics.DrawLine(pen, x, low, x, Math.Max(open, close)); pen.Color = orgPenColor; } } }
Not sure hollow candles are a big deal, but having two outline pens does make for nicer candles on black background for sure.Last edited by aslane; 11-23-2009, 08:05 AM.
Comment
-
Originally posted by aslane View PostOk, the following is the same method applied to CandleStyle
Where would one put this code to make it work?
Thanks for the killer work!
Comment
-
Well I was hoping Ninja would take a run with this code, but perhaps after they get thru the Beta.
Anyway, to use this code now, you need to open the file "My Documents\NinjaTrader 7\bin\Custom\Type\@ChartStyles.cs", and replace the appropriate class (CandleStyle and/or OpenCloseStyle) in that file with the code provided here. You then need to open some indicator/strategy in NT and compile it in order to compile the changes into NT. Finally, you will need to restart NT to get it to load.
Before you start, I would suggest making a copy of the @ChartStyles.cs file first in case something goes wrong.
I have only done this on NT7B4, and the code likely does not work on NT6.5.
Another thing to note, is every time you install a new version of NT, you will have to patch the @ChartStyles.cs file again, as this file gets overwritten with a new install. Again, once it is working save a copy off somewhere.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Tburg1073, Today, 08:36 AM
|
0 responses
5 views
0 likes
|
Last Post
by Tburg1073
Today, 08:36 AM
|
||
Started by alexstox, 10-16-2018, 03:29 PM
|
10 responses
333 views
0 likes
|
Last Post
by Tburg1073
Today, 08:32 AM
|
||
Started by smartromain, 03-13-2024, 01:42 AM
|
6 responses
98 views
0 likes
|
Last Post
by smartromain
Today, 08:30 AM
|
||
Started by Jonker, 04-27-2024, 01:19 PM
|
4 responses
29 views
0 likes
|
Last Post
by Jonker
Today, 08:26 AM
|
||
Started by Yasharandom, Today, 07:45 AM
|
0 responses
5 views
0 likes
|
Last Post
by Yasharandom
Today, 07:45 AM
|
Comment