From ae09bdeb3b151f06c15f659c1756fc49c1b7838f Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 11 Apr 2023 06:46:41 -0700 Subject: [PATCH] Minor visual tweaks and rendering improvements --- indicators/DailyCOL.cs | 79 ++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/indicators/DailyCOL.cs b/indicators/DailyCOL.cs index 1cc1218..232cfab 100644 --- a/indicators/DailyCOL.cs +++ b/indicators/DailyCOL.cs @@ -30,8 +30,6 @@ namespace NinjaTrader.NinjaScript.Indicators private static int DAILY_BARS = 1; private static int TEXT_PADDING = 5; // Pixels - private static string DAILY_OPEN_TAG = "DailyOpen@"; - private static string DATE = "{date}"; private static string LEVEL = "{level}"; @@ -56,6 +54,7 @@ namespace NinjaTrader.NinjaScript.Indicators IsSuspendedWhileInactive = true; NumberOfDays = 3; DailyOpenStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3); + DrawLabels = true; LabelText = DATE + " Daily COL @ " + LEVEL; DailyOpenFont = new SimpleFont("Arial", 13); DailyOpenFontColor = Brushes.LightGray; @@ -82,31 +81,42 @@ namespace NinjaTrader.NinjaScript.Indicators { base.OnRender(chartControl, chartScale); + SharpDX.Direct2D1.Brush dailyOpenBrush = DailyOpenStroke.Brush.ToDxBrush(RenderTarget); + TextFormat textFormat = DailyOpenFont.ToDirectWriteTextFormat(); SharpDX.Direct2D1.Brush textBrush = DailyOpenFontColor.ToDxBrush(RenderTarget); foreach (double dailyOpen in DailyOpens.Keys) { - if (DrawObjects[DAILY_OPEN_TAG + dailyOpen] == null) - { // TODO: Remove older draw objects? - Draw.HorizontalLine(this, DAILY_OPEN_TAG + dailyOpen, dailyOpen, - DailyOpenStroke.Brush, DailyOpenStroke.DashStyleHelper, (int)DailyOpenStroke.Width); + // TODO: Only need to render daily opens visible on the chart. + int dailyOpenY = chartScale.GetYByValue(dailyOpen); + RenderTarget.DrawLine(new SharpDX.Vector2(ChartPanel.X, dailyOpenY), + new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, dailyOpenY), + dailyOpenBrush, DailyOpenStroke.Width, DailyOpenStroke.StrokeStyle); + + if (DrawLabels) + { + string dailyOpenText = LabelText.Replace(DATE, DailyOpens[dailyOpen]).Replace(LEVEL, dailyOpen.ToString(dailyOpen % 1 == 0 ? "F0" : "F2")); + // TODO: Extract text rendering into a separate utility. + TextLayout textLayout = new TextLayout(Core.Globals.DirectWriteFactory, dailyOpenText, textFormat, 500, textFormat.FontSize); + SharpDX.Vector2 textOrigin = new SharpDX.Vector2(ChartPanel.W - textLayout.Metrics.Width - TEXT_PADDING, + ChartPanel.Y + (float)chartScale.GetYByValue(dailyOpen) - (float)textLayout.Metrics.Height - TEXT_PADDING); + RenderTarget.DrawTextLayout(textOrigin, textLayout, textBrush, SharpDX.Direct2D1.DrawTextOptions.NoSnap); + + textLayout.Dispose(); } - - string dailyOpenText = LabelText.Replace(DATE, DailyOpens[dailyOpen]).Replace(LEVEL, dailyOpen.ToString()); - // TODO: Extract text rendering into a separate utility. - TextLayout textLayout = new TextLayout(Core.Globals.DirectWriteFactory, dailyOpenText, textFormat, 500, textFormat.FontSize); - SharpDX.Vector2 textOrigin = new SharpDX.Vector2(ChartPanel.W - textLayout.Metrics.Width - TEXT_PADDING, - ChartPanel.Y + (float)chartScale.GetYByValue(dailyOpen) - (float)textLayout.Metrics.Height - TEXT_PADDING); - RenderTarget.DrawTextLayout(textOrigin, textLayout, textBrush, SharpDX.Direct2D1.DrawTextOptions.NoSnap); - - textLayout.Dispose(); } + dailyOpenBrush.Dispose(); textFormat.Dispose(); textBrush.Dispose(); } - + + public override string DisplayName + { + get { return Name; } + } + [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name = "Number of Days", Description = "Number of daily opens to consider", Order = 1, GroupName = "Daily COL")] @@ -118,21 +128,24 @@ namespace NinjaTrader.NinjaScript.Indicators public Stroke DailyOpenStroke { get; set; } - // TODO: Draw label toggle. + [NinjaScriptProperty] + [Display(Name = "Draw Labels", Order = 1, GroupName = "Labels")] + public bool DrawLabels + { get; set; } [NinjaScriptProperty] - [Display(Name = "Label Text", Order = 1, GroupName = "Labels")] + [Display(Name = "Label Text", Order = 2, GroupName = "Labels")] public string LabelText { get; set; } [NinjaScriptProperty] - [Display(Name = "Font", Description = "Font used to display daily COLs", Order = 2, GroupName = "Labels")] + [Display(Name = "Font", Description = "Font used to display daily COLs", Order = 3, GroupName = "Labels")] public SimpleFont DailyOpenFont { get; set; } [XmlIgnore] [NinjaScriptProperty] - [Display(Name = "Font Color", Description = "Color of the text used to label the daily COLs", Order = 3, GroupName = "Labels")] + [Display(Name = "Font Color", Description = "Color of the text used to label the daily COLs", Order = 4, GroupName = "Labels")] public Brush DailyOpenFontColor { get; set; } @@ -152,18 +165,18 @@ namespace NinjaTrader.NinjaScript.Indicators public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private DailyCOL[] cacheDailyCOL; - public DailyCOL DailyCOL(int numberOfDays, Stroke dailyOpenStroke, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor) + public DailyCOL DailyCOL(int numberOfDays, Stroke dailyOpenStroke, bool drawLabels, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor) { - return DailyCOL(Input, numberOfDays, dailyOpenStroke, labelText, dailyOpenFont, dailyOpenFontColor); + return DailyCOL(Input, numberOfDays, dailyOpenStroke, drawLabels, labelText, dailyOpenFont, dailyOpenFontColor); } - public DailyCOL DailyCOL(ISeries input, int numberOfDays, Stroke dailyOpenStroke, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor) + public DailyCOL DailyCOL(ISeries input, int numberOfDays, Stroke dailyOpenStroke, bool drawLabels, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor) { if (cacheDailyCOL != null) for (int idx = 0; idx < cacheDailyCOL.Length; idx++) - if (cacheDailyCOL[idx] != null && cacheDailyCOL[idx].NumberOfDays == numberOfDays && cacheDailyCOL[idx].DailyOpenStroke == dailyOpenStroke && cacheDailyCOL[idx].LabelText == labelText && cacheDailyCOL[idx].DailyOpenFont == dailyOpenFont && cacheDailyCOL[idx].DailyOpenFontColor == dailyOpenFontColor && cacheDailyCOL[idx].EqualsInput(input)) + if (cacheDailyCOL[idx] != null && cacheDailyCOL[idx].NumberOfDays == numberOfDays && cacheDailyCOL[idx].DailyOpenStroke == dailyOpenStroke && cacheDailyCOL[idx].DrawLabels == drawLabels && cacheDailyCOL[idx].LabelText == labelText && cacheDailyCOL[idx].DailyOpenFont == dailyOpenFont && cacheDailyCOL[idx].DailyOpenFontColor == dailyOpenFontColor && cacheDailyCOL[idx].EqualsInput(input)) return cacheDailyCOL[idx]; - return CacheIndicator(new DailyCOL(){ NumberOfDays = numberOfDays, DailyOpenStroke = dailyOpenStroke, LabelText = labelText, DailyOpenFont = dailyOpenFont, DailyOpenFontColor = dailyOpenFontColor }, input, ref cacheDailyCOL); + return CacheIndicator(new DailyCOL(){ NumberOfDays = numberOfDays, DailyOpenStroke = dailyOpenStroke, DrawLabels = drawLabels, LabelText = labelText, DailyOpenFont = dailyOpenFont, DailyOpenFontColor = dailyOpenFontColor }, input, ref cacheDailyCOL); } } } @@ -172,14 +185,14 @@ namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { - public Indicators.DailyCOL DailyCOL(int numberOfDays, Stroke dailyOpenStroke, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor) + public Indicators.DailyCOL DailyCOL(int numberOfDays, Stroke dailyOpenStroke, bool drawLabels, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor) { - return indicator.DailyCOL(Input, numberOfDays, dailyOpenStroke, labelText, dailyOpenFont, dailyOpenFontColor); + return indicator.DailyCOL(Input, numberOfDays, dailyOpenStroke, drawLabels, labelText, dailyOpenFont, dailyOpenFontColor); } - public Indicators.DailyCOL DailyCOL(ISeries input , int numberOfDays, Stroke dailyOpenStroke, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor) + public Indicators.DailyCOL DailyCOL(ISeries input , int numberOfDays, Stroke dailyOpenStroke, bool drawLabels, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor) { - return indicator.DailyCOL(input, numberOfDays, dailyOpenStroke, labelText, dailyOpenFont, dailyOpenFontColor); + return indicator.DailyCOL(input, numberOfDays, dailyOpenStroke, drawLabels, labelText, dailyOpenFont, dailyOpenFontColor); } } } @@ -188,14 +201,14 @@ namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { - public Indicators.DailyCOL DailyCOL(int numberOfDays, Stroke dailyOpenStroke, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor) + public Indicators.DailyCOL DailyCOL(int numberOfDays, Stroke dailyOpenStroke, bool drawLabels, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor) { - return indicator.DailyCOL(Input, numberOfDays, dailyOpenStroke, labelText, dailyOpenFont, dailyOpenFontColor); + return indicator.DailyCOL(Input, numberOfDays, dailyOpenStroke, drawLabels, labelText, dailyOpenFont, dailyOpenFontColor); } - public Indicators.DailyCOL DailyCOL(ISeries input , int numberOfDays, Stroke dailyOpenStroke, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor) + public Indicators.DailyCOL DailyCOL(ISeries input , int numberOfDays, Stroke dailyOpenStroke, bool drawLabels, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor) { - return indicator.DailyCOL(input, numberOfDays, dailyOpenStroke, labelText, dailyOpenFont, dailyOpenFontColor); + return indicator.DailyCOL(input, numberOfDays, dailyOpenStroke, drawLabels, labelText, dailyOpenFont, dailyOpenFontColor); } } }