Add customizable labels and perform drawing in OnRender()

This commit is contained in:
moshferatu 2023-03-17 10:53:34 -07:00
parent 89042c2e5b
commit a7a10dd909
2 changed files with 203 additions and 143 deletions

203
indicators/DailyCOL.cs Normal file
View File

@ -0,0 +1,203 @@
#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.DirectWrite;
#endregion
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class DailyCOL : Indicator
{
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}";
private Dictionary<double, string> DailyOpens = new Dictionary<double, string>(); // Open -> Date
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Plots the open of daily candles.";
Name = "Daily COL";
Calculate = Calculate.OnPriceChange;
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;
NumberOfDays = 3;
DailyOpenStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3);
LabelText = DATE + " Daily COL @ " + LEVEL;
DailyOpenFont = new SimpleFont("Arial", 13);
DailyOpenFontColor = Brushes.LightGray;
}
else if (State == State.Configure)
{
// TODO: Force ETH?
AddDataSeries(Instrument.FullName, new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = 1440 },
NumberOfDays, Bars.TradingHours.Name, Bars.IsResetOnNewTradingDay);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress == DAILY_BARS && IsFirstTickOfBar)
{
double dailyOpen = Open[0];
string day = Time[0].ToString("MM/dd");
DailyOpens[dailyOpen] = day;
}
}
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
{
base.OnRender(chartControl, chartScale);
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);
}
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();
}
textFormat.Dispose();
textBrush.Dispose();
}
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Number of Days", Description = "Number of daily opens to consider", Order = 1, GroupName = "Daily COL")]
public int NumberOfDays
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Daily Open", Description = "Daily open level drawn on the chart", Order = 2, GroupName = "Daily COL")]
public Stroke DailyOpenStroke
{ get; set; }
// TODO: Draw label toggle.
[NinjaScriptProperty]
[Display(Name = "Label Text", Order = 1, GroupName = "Labels")]
public string LabelText
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Font", Description = "Font used to display daily COLs", Order = 2, 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")]
public Brush DailyOpenFontColor
{ get; set; }
[Browsable(false)]
public string DailyOpenFontColorSerialization
{
get { return Serialize.BrushToString(DailyOpenFontColor); }
set { DailyOpenFontColor = Serialize.StringToBrush(value); }
}
}
}
#region NinjaScript generated code. Neither change nor remove.
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)
{
return DailyCOL(Input, numberOfDays, dailyOpenStroke, labelText, dailyOpenFont, dailyOpenFontColor);
}
public DailyCOL DailyCOL(ISeries<double> input, int numberOfDays, Stroke dailyOpenStroke, 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))
return cacheDailyCOL[idx];
return CacheIndicator<DailyCOL>(new DailyCOL(){ NumberOfDays = numberOfDays, DailyOpenStroke = dailyOpenStroke, LabelText = labelText, DailyOpenFont = dailyOpenFont, DailyOpenFontColor = dailyOpenFontColor }, input, ref cacheDailyCOL);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.DailyCOL DailyCOL(int numberOfDays, Stroke dailyOpenStroke, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor)
{
return indicator.DailyCOL(Input, numberOfDays, dailyOpenStroke, labelText, dailyOpenFont, dailyOpenFontColor);
}
public Indicators.DailyCOL DailyCOL(ISeries<double> input , int numberOfDays, Stroke dailyOpenStroke, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor)
{
return indicator.DailyCOL(input, numberOfDays, dailyOpenStroke, labelText, dailyOpenFont, dailyOpenFontColor);
}
}
}
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)
{
return indicator.DailyCOL(Input, numberOfDays, dailyOpenStroke, labelText, dailyOpenFont, dailyOpenFontColor);
}
public Indicators.DailyCOL DailyCOL(ISeries<double> input , int numberOfDays, Stroke dailyOpenStroke, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor)
{
return indicator.DailyCOL(input, numberOfDays, dailyOpenStroke, labelText, dailyOpenFont, dailyOpenFontColor);
}
}
}
#endregion

View File

@ -1,143 +0,0 @@
#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;
#endregion
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class DailyOpenLevels : Indicator
{
private static int DAILY_BARS = 1;
private HashSet<double> DailyOpens = new HashSet<double>();
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Plots the open of daily candles.";
Name = "Daily COL";
Calculate = Calculate.OnPriceChange;
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;
LookbackPeriod = 3;
DailyOpenStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3);
}
else if (State == State.Configure)
{
AddDataSeries(Instrument.FullName, new BarsPeriod { BarsPeriodType = BarsPeriodType.Day, Value = 1 }, LookbackPeriod, Bars.TradingHours.Name, Bars.IsResetOnNewTradingDay);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress == DAILY_BARS)
{
DailyOpens.Add(Opens[DAILY_BARS][0]);
}
else
{
foreach (double dailyOpen in DailyOpens)
{
if (DrawObjects["Open@" + dailyOpen] == null)
Draw.HorizontalLine(this, "Open@" + dailyOpen, dailyOpen, DailyOpenStroke.Brush, DailyOpenStroke.DashStyleHelper, (int)DailyOpenStroke.Width);
}
}
}
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Lookback Period", Description = "Number of daily opens to consider", Order = 1, GroupName = "Parameters")]
public int LookbackPeriod
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Daily Open", Description = "Daily open level drawn on the chart", Order = 2, GroupName = "Parameters")]
public Stroke DailyOpenStroke
{ get; set; }
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private DailyOpenLevels[] cacheDailyOpenLevels;
public DailyOpenLevels DailyOpenLevels(int lookbackPeriod, Stroke dailyOpenStroke)
{
return DailyOpenLevels(Input, lookbackPeriod, dailyOpenStroke);
}
public DailyOpenLevels DailyOpenLevels(ISeries<double> input, int lookbackPeriod, Stroke dailyOpenStroke)
{
if (cacheDailyOpenLevels != null)
for (int idx = 0; idx < cacheDailyOpenLevels.Length; idx++)
if (cacheDailyOpenLevels[idx] != null && cacheDailyOpenLevels[idx].LookbackPeriod == lookbackPeriod && cacheDailyOpenLevels[idx].DailyOpenStroke == dailyOpenStroke && cacheDailyOpenLevels[idx].EqualsInput(input))
return cacheDailyOpenLevels[idx];
return CacheIndicator<DailyOpenLevels>(new DailyOpenLevels(){ LookbackPeriod = lookbackPeriod, DailyOpenStroke = dailyOpenStroke }, input, ref cacheDailyOpenLevels);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.DailyOpenLevels DailyOpenLevels(int lookbackPeriod, Stroke dailyOpenStroke)
{
return indicator.DailyOpenLevels(Input, lookbackPeriod, dailyOpenStroke);
}
public Indicators.DailyOpenLevels DailyOpenLevels(ISeries<double> input , int lookbackPeriod, Stroke dailyOpenStroke)
{
return indicator.DailyOpenLevels(input, lookbackPeriod, dailyOpenStroke);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.DailyOpenLevels DailyOpenLevels(int lookbackPeriod, Stroke dailyOpenStroke)
{
return indicator.DailyOpenLevels(Input, lookbackPeriod, dailyOpenStroke);
}
public Indicators.DailyOpenLevels DailyOpenLevels(ISeries<double> input , int lookbackPeriod, Stroke dailyOpenStroke)
{
return indicator.DailyOpenLevels(input, lookbackPeriod, dailyOpenStroke);
}
}
}
#endregion