ninjatrader/indicators/DailyLevels.cs

293 lines
16 KiB
C#
Raw Permalink Normal View History

2023-09-06 19:36:57 +00:00
#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 DailyLevels : Indicator
{
private static int DAILY_BARS = 1;
private static int TEXT_PADDING = 5; // Pixels
private static string DATE = "{date}";
private static string LEVEL = "{level}";
private Dictionary<double, string> DailyOpens = new Dictionary<double, string>(); // Open -> Month / Day
private Dictionary<double, string> DailyCloses = new Dictionary<double, string>(); // Close -> Month / Day
private Dictionary<string, DateTime> DailyCloseTimes = new Dictionary<string, DateTime>(); // Month / Day -> Time
2023-09-06 19:36:57 +00:00
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Plots levels from daily candles.";
Name = "Daily Levels";
Calculate = Calculate.OnPriceChange;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = ScaleJustification.Right;
IsSuspendedWhileInactive = true;
NumberOfDays = 3;
DailyOpenStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3);
ShowDailyOpens = true;
2023-09-06 19:36:57 +00:00
DailyCloseStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3);
ShowDailyCloses = false;
2023-09-06 19:36:57 +00:00
DrawLabels = true;
DailyOpenLabelText = DATE + " Daily Open @ " + LEVEL;
DailyCloseLabelText = DATE + " Daily Close @ " + LEVEL;
DailyLevelFont = new SimpleFont("Arial", 13);
DailyLevelFontColor = Brushes.LightGray;
}
else if (State == State.Configure)
{
// Using 1440 minute bars rather than daily bars.
// The desired number of days was being ignored when requesting daily bars.
2023-09-06 19:36:57 +00:00
AddDataSeries(Instrument.FullName, new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = 1440 },
NumberOfDays, Bars.TradingHours.Name, Bars.IsResetOnNewTradingDay);
}
else if (State == State.Historical)
{
SetZOrder(-1); // Display behind bars on chart.
}
2023-09-06 19:36:57 +00:00
}
protected override void OnBarUpdate()
{
if (BarsInProgress == DAILY_BARS && IsFirstTickOfBar)
{
double dailyOpen = Open[0];
string day = Time[0].ToString("MM/dd");
DailyOpens[dailyOpen] = day;
if (CurrentBar >= 1) // Make sure there is a prior day to reference.
{
double dailyClose = Close[1];
string previousDay = Time[1].ToString("MM/dd");
DailyCloses[dailyClose] = previousDay;
}
// Candle times in NT are associated with the close rather than the open.
// For the daily bars, this will be the end of the market session.
DailyCloseTimes[day] = Time[0];
2023-09-06 19:36:57 +00:00
}
}
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
{
base.OnRender(chartControl, chartScale);
TextFormat textFormat = DailyLevelFont.ToDirectWriteTextFormat();
SharpDX.Direct2D1.Brush textBrush = DailyLevelFontColor.ToDxBrush(RenderTarget);
if (ShowDailyOpens)
2023-09-06 19:36:57 +00:00
{
SharpDX.Direct2D1.Brush dailyOpenBrush = DailyOpenStroke.Brush.ToDxBrush(RenderTarget);
2023-09-06 19:36:57 +00:00
foreach (double dailyOpen in DailyOpens.Keys)
2023-09-06 19:36:57 +00:00
{
// Subtracting a day and adding a minute to the daily closing time results in the candle associated with the session open.
// Basing the open / close times on the chart bars would be better, but using the daily bars as that's where the levels come from.
int dailyOpenBarIndex = ChartBars.GetBarIdxByTime(ChartControl, DailyCloseTimes[DailyOpens[dailyOpen]].AddDays(-1).AddMinutes(1));
int dailyOpenX = chartControl.GetXByBarIndex(ChartBars, dailyOpenBarIndex);
int dailyOpenY = chartScale.GetYByValue(dailyOpen);
RenderTarget.DrawLine(new SharpDX.Vector2(dailyOpenX, dailyOpenY),
new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, dailyOpenY),
dailyOpenBrush, DailyOpenStroke.Width, DailyOpenStroke.StrokeStyle);
if (DrawLabels && (dailyOpenX < ChartPanel.X + ChartPanel.W))
{
string dailyOpenText = DailyOpenLabelText.Replace(DATE, DailyOpens[dailyOpen])
.Replace(LEVEL, dailyOpen.ToString(dailyOpen % 1 == 0 ? "F0" : "F2"));
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();
}
2023-09-06 19:36:57 +00:00
}
dailyOpenBrush.Dispose();
}
2023-09-06 19:36:57 +00:00
if (ShowDailyCloses)
2023-09-06 19:36:57 +00:00
{
SharpDX.Direct2D1.Brush dailyCloseBrush = DailyCloseStroke.Brush.ToDxBrush(RenderTarget);
2023-09-06 19:36:57 +00:00
foreach (double dailyClose in DailyCloses.Keys)
2023-09-06 19:36:57 +00:00
{
int dailyCloseBarIndex = ChartBars.GetBarIdxByTime(ChartControl, DailyCloseTimes[DailyCloses[dailyClose]]);
int dailyCloseX = chartControl.GetXByBarIndex(ChartBars, dailyCloseBarIndex);
int dailyCloseY = chartScale.GetYByValue(dailyClose);
RenderTarget.DrawLine(new SharpDX.Vector2(dailyCloseX, dailyCloseY),
new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, dailyCloseY),
dailyCloseBrush, DailyCloseStroke.Width, DailyCloseStroke.StrokeStyle);
if (DrawLabels && (dailyCloseX < ChartPanel.X + ChartPanel.W))
{
string dailyCloseText = DailyCloseLabelText.Replace(DATE, DailyCloses[dailyClose])
.Replace(LEVEL, dailyClose.ToString(dailyClose % 1 == 0 ? "F0" : "F2"));
TextLayout textLayout = new TextLayout(Core.Globals.DirectWriteFactory, dailyCloseText, textFormat, 500, textFormat.FontSize);
SharpDX.Vector2 textOrigin = new SharpDX.Vector2(ChartPanel.W - textLayout.Metrics.Width - TEXT_PADDING,
ChartPanel.Y + (float)chartScale.GetYByValue(dailyClose) - (float)textLayout.Metrics.Height - TEXT_PADDING);
RenderTarget.DrawTextLayout(textOrigin, textLayout, textBrush, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
textLayout.Dispose();
}
2023-09-06 19:36:57 +00:00
}
dailyCloseBrush.Dispose();
2023-09-06 19:36:57 +00:00
}
textFormat.Dispose();
textBrush.Dispose();
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Number of Days", Description = "Number of days to consider", Order = 1, GroupName = "Daily Levels")]
public int NumberOfDays
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Show Daily Opens", Description = "Show daily opens on the chart", Order = 2, GroupName = "Daily Levels")]
public bool ShowDailyOpens
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Daily Open", Description = "Daily open level drawn on the chart", Order = 3, GroupName = "Daily Levels")]
2023-09-06 19:36:57 +00:00
public Stroke DailyOpenStroke
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Show Daily Closes", Description = "Show daily closes on the chart", Order = 4, GroupName = "Daily Levels")]
public bool ShowDailyCloses
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Daily Close", Description = "Daily close level drawn on the chart", Order = 5, GroupName = "Daily Levels")]
2023-09-06 19:36:57 +00:00
public Stroke DailyCloseStroke
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Draw Labels", Description = "Show labels on the chart", Order = 1, GroupName = "Labels")]
2023-09-06 19:36:57 +00:00
public bool DrawLabels
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Daily Open Label Text", Description = "Text displayed for daily opens", Order = 2, GroupName = "Labels")]
2023-09-06 19:36:57 +00:00
public string DailyOpenLabelText
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Daily Close Label Text", Description = "Text displayed for daily closes", Order = 3, GroupName = "Labels")]
2023-09-06 19:36:57 +00:00
public string DailyCloseLabelText
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Font", Description = "Font used to display daily levels", Order = 4, GroupName = "Labels")]
2023-09-06 19:36:57 +00:00
public SimpleFont DailyLevelFont
{ get; set; }
[XmlIgnore]
[NinjaScriptProperty]
[Display(Name = "Font Color", Description = "Color of the text used to label the daily levels", Order = 5, GroupName = "Labels")]
2023-09-06 19:36:57 +00:00
public Brush DailyLevelFontColor
{ get; set; }
[Browsable(false)]
public string DailyOpenFontColorSerialization
{
get { return Serialize.BrushToString(DailyLevelFontColor); }
set { DailyLevelFontColor = Serialize.StringToBrush(value); }
}
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private DailyLevels[] cacheDailyLevels;
public DailyLevels DailyLevels(int numberOfDays, bool showDailyOpens, Stroke dailyOpenStroke, bool showDailyCloses, Stroke dailyCloseStroke, bool drawLabels, string dailyOpenLabelText, string dailyCloseLabelText, SimpleFont dailyLevelFont, Brush dailyLevelFontColor)
2023-09-06 19:36:57 +00:00
{
return DailyLevels(Input, numberOfDays, showDailyOpens, dailyOpenStroke, showDailyCloses, dailyCloseStroke, drawLabels, dailyOpenLabelText, dailyCloseLabelText, dailyLevelFont, dailyLevelFontColor);
2023-09-06 19:36:57 +00:00
}
public DailyLevels DailyLevels(ISeries<double> input, int numberOfDays, bool showDailyOpens, Stroke dailyOpenStroke, bool showDailyCloses, Stroke dailyCloseStroke, bool drawLabels, string dailyOpenLabelText, string dailyCloseLabelText, SimpleFont dailyLevelFont, Brush dailyLevelFontColor)
2023-09-06 19:36:57 +00:00
{
if (cacheDailyLevels != null)
for (int idx = 0; idx < cacheDailyLevels.Length; idx++)
if (cacheDailyLevels[idx] != null && cacheDailyLevels[idx].NumberOfDays == numberOfDays && cacheDailyLevels[idx].ShowDailyOpens == showDailyOpens && cacheDailyLevels[idx].DailyOpenStroke == dailyOpenStroke && cacheDailyLevels[idx].ShowDailyCloses == showDailyCloses && cacheDailyLevels[idx].DailyCloseStroke == dailyCloseStroke && cacheDailyLevels[idx].DrawLabels == drawLabels && cacheDailyLevels[idx].DailyOpenLabelText == dailyOpenLabelText && cacheDailyLevels[idx].DailyCloseLabelText == dailyCloseLabelText && cacheDailyLevels[idx].DailyLevelFont == dailyLevelFont && cacheDailyLevels[idx].DailyLevelFontColor == dailyLevelFontColor && cacheDailyLevels[idx].EqualsInput(input))
2023-09-06 19:36:57 +00:00
return cacheDailyLevels[idx];
return CacheIndicator<DailyLevels>(new DailyLevels(){ NumberOfDays = numberOfDays, ShowDailyOpens = showDailyOpens, DailyOpenStroke = dailyOpenStroke, ShowDailyCloses = showDailyCloses, DailyCloseStroke = dailyCloseStroke, DrawLabels = drawLabels, DailyOpenLabelText = dailyOpenLabelText, DailyCloseLabelText = dailyCloseLabelText, DailyLevelFont = dailyLevelFont, DailyLevelFontColor = dailyLevelFontColor }, input, ref cacheDailyLevels);
2023-09-06 19:36:57 +00:00
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.DailyLevels DailyLevels(int numberOfDays, bool showDailyOpens, Stroke dailyOpenStroke, bool showDailyCloses, Stroke dailyCloseStroke, bool drawLabels, string dailyOpenLabelText, string dailyCloseLabelText, SimpleFont dailyLevelFont, Brush dailyLevelFontColor)
2023-09-06 19:36:57 +00:00
{
return indicator.DailyLevels(Input, numberOfDays, showDailyOpens, dailyOpenStroke, showDailyCloses, dailyCloseStroke, drawLabels, dailyOpenLabelText, dailyCloseLabelText, dailyLevelFont, dailyLevelFontColor);
2023-09-06 19:36:57 +00:00
}
public Indicators.DailyLevels DailyLevels(ISeries<double> input , int numberOfDays, bool showDailyOpens, Stroke dailyOpenStroke, bool showDailyCloses, Stroke dailyCloseStroke, bool drawLabels, string dailyOpenLabelText, string dailyCloseLabelText, SimpleFont dailyLevelFont, Brush dailyLevelFontColor)
2023-09-06 19:36:57 +00:00
{
return indicator.DailyLevels(input, numberOfDays, showDailyOpens, dailyOpenStroke, showDailyCloses, dailyCloseStroke, drawLabels, dailyOpenLabelText, dailyCloseLabelText, dailyLevelFont, dailyLevelFontColor);
2023-09-06 19:36:57 +00:00
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.DailyLevels DailyLevels(int numberOfDays, bool showDailyOpens, Stroke dailyOpenStroke, bool showDailyCloses, Stroke dailyCloseStroke, bool drawLabels, string dailyOpenLabelText, string dailyCloseLabelText, SimpleFont dailyLevelFont, Brush dailyLevelFontColor)
2023-09-06 19:36:57 +00:00
{
return indicator.DailyLevels(Input, numberOfDays, showDailyOpens, dailyOpenStroke, showDailyCloses, dailyCloseStroke, drawLabels, dailyOpenLabelText, dailyCloseLabelText, dailyLevelFont, dailyLevelFontColor);
2023-09-06 19:36:57 +00:00
}
public Indicators.DailyLevels DailyLevels(ISeries<double> input , int numberOfDays, bool showDailyOpens, Stroke dailyOpenStroke, bool showDailyCloses, Stroke dailyCloseStroke, bool drawLabels, string dailyOpenLabelText, string dailyCloseLabelText, SimpleFont dailyLevelFont, Brush dailyLevelFontColor)
2023-09-06 19:36:57 +00:00
{
return indicator.DailyLevels(input, numberOfDays, showDailyOpens, dailyOpenStroke, showDailyCloses, dailyCloseStroke, drawLabels, dailyOpenLabelText, dailyCloseLabelText, dailyLevelFont, dailyLevelFontColor);
2023-09-06 19:36:57 +00:00
}
}
}
#endregion