Minor visual tweaks and rendering improvements

This commit is contained in:
moshferatu 2023-04-11 06:46:41 -07:00
parent a7a10dd909
commit ae09bdeb3b

View File

@ -30,8 +30,6 @@ namespace NinjaTrader.NinjaScript.Indicators
private static int DAILY_BARS = 1; private static int DAILY_BARS = 1;
private static int TEXT_PADDING = 5; // Pixels private static int TEXT_PADDING = 5; // Pixels
private static string DAILY_OPEN_TAG = "DailyOpen@";
private static string DATE = "{date}"; private static string DATE = "{date}";
private static string LEVEL = "{level}"; private static string LEVEL = "{level}";
@ -56,6 +54,7 @@ namespace NinjaTrader.NinjaScript.Indicators
IsSuspendedWhileInactive = true; IsSuspendedWhileInactive = true;
NumberOfDays = 3; NumberOfDays = 3;
DailyOpenStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3); DailyOpenStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3);
DrawLabels = true;
LabelText = DATE + " Daily COL @ " + LEVEL; LabelText = DATE + " Daily COL @ " + LEVEL;
DailyOpenFont = new SimpleFont("Arial", 13); DailyOpenFont = new SimpleFont("Arial", 13);
DailyOpenFontColor = Brushes.LightGray; DailyOpenFontColor = Brushes.LightGray;
@ -82,31 +81,42 @@ namespace NinjaTrader.NinjaScript.Indicators
{ {
base.OnRender(chartControl, chartScale); base.OnRender(chartControl, chartScale);
SharpDX.Direct2D1.Brush dailyOpenBrush = DailyOpenStroke.Brush.ToDxBrush(RenderTarget);
TextFormat textFormat = DailyOpenFont.ToDirectWriteTextFormat(); TextFormat textFormat = DailyOpenFont.ToDirectWriteTextFormat();
SharpDX.Direct2D1.Brush textBrush = DailyOpenFontColor.ToDxBrush(RenderTarget); SharpDX.Direct2D1.Brush textBrush = DailyOpenFontColor.ToDxBrush(RenderTarget);
foreach (double dailyOpen in DailyOpens.Keys) foreach (double dailyOpen in DailyOpens.Keys)
{ {
if (DrawObjects[DAILY_OPEN_TAG + dailyOpen] == null) // TODO: Only need to render daily opens visible on the chart.
{ // TODO: Remove older draw objects? int dailyOpenY = chartScale.GetYByValue(dailyOpen);
Draw.HorizontalLine(this, DAILY_OPEN_TAG + dailyOpen, dailyOpen, RenderTarget.DrawLine(new SharpDX.Vector2(ChartPanel.X, dailyOpenY),
DailyOpenStroke.Brush, DailyOpenStroke.DashStyleHelper, (int)DailyOpenStroke.Width); 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(); textFormat.Dispose();
textBrush.Dispose(); textBrush.Dispose();
} }
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty] [NinjaScriptProperty]
[Range(1, int.MaxValue)] [Range(1, int.MaxValue)]
[Display(Name = "Number of Days", Description = "Number of daily opens to consider", Order = 1, GroupName = "Daily COL")] [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 public Stroke DailyOpenStroke
{ get; set; } { get; set; }
// TODO: Draw label toggle. [NinjaScriptProperty]
[Display(Name = "Draw Labels", Order = 1, GroupName = "Labels")]
public bool DrawLabels
{ get; set; }
[NinjaScriptProperty] [NinjaScriptProperty]
[Display(Name = "Label Text", Order = 1, GroupName = "Labels")] [Display(Name = "Label Text", Order = 2, GroupName = "Labels")]
public string LabelText public string LabelText
{ get; set; } { get; set; }
[NinjaScriptProperty] [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 public SimpleFont DailyOpenFont
{ get; set; } { get; set; }
[XmlIgnore] [XmlIgnore]
[NinjaScriptProperty] [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 public Brush DailyOpenFontColor
{ get; set; } { get; set; }
@ -152,18 +165,18 @@ namespace NinjaTrader.NinjaScript.Indicators
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{ {
private DailyCOL[] cacheDailyCOL; 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<double> input, int numberOfDays, Stroke dailyOpenStroke, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor) public DailyCOL DailyCOL(ISeries<double> input, int numberOfDays, Stroke dailyOpenStroke, bool drawLabels, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor)
{ {
if (cacheDailyCOL != null) if (cacheDailyCOL != null)
for (int idx = 0; idx < cacheDailyCOL.Length; idx++) 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 cacheDailyCOL[idx];
return CacheIndicator<DailyCOL>(new DailyCOL(){ NumberOfDays = numberOfDays, DailyOpenStroke = dailyOpenStroke, LabelText = labelText, DailyOpenFont = dailyOpenFont, DailyOpenFontColor = dailyOpenFontColor }, input, ref cacheDailyCOL); return CacheIndicator<DailyCOL>(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 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<double> input , int numberOfDays, Stroke dailyOpenStroke, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor) public Indicators.DailyCOL DailyCOL(ISeries<double> 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 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<double> input , int numberOfDays, Stroke dailyOpenStroke, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor) public Indicators.DailyCOL DailyCOL(ISeries<double> 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);
} }
} }
} }