diff --git a/indicators/DailyLevels.cs b/indicators/DailyLevels.cs new file mode 100644 index 0000000..95e7ca8 --- /dev/null +++ b/indicators/DailyLevels.cs @@ -0,0 +1,249 @@ +#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 DailyOpens = new Dictionary(); // Open -> Date + private Dictionary DailyCloses = new Dictionary(); // Close -> Date + + 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); + DailyCloseStroke = new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3); + 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) + { + 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]; + double dailyClose = Close[0]; + string day = Time[0].ToString("MM/dd"); + DailyOpens[dailyOpen] = day; + DailyCloses[dailyClose] = day; + } + } + + protected override void OnRender(ChartControl chartControl, ChartScale chartScale) + { + base.OnRender(chartControl, chartScale); + + SharpDX.Direct2D1.Brush dailyOpenBrush = DailyOpenStroke.Brush.ToDxBrush(RenderTarget); + + TextFormat textFormat = DailyLevelFont.ToDirectWriteTextFormat(); + SharpDX.Direct2D1.Brush textBrush = DailyLevelFontColor.ToDxBrush(RenderTarget); + + foreach (double dailyOpen in DailyOpens.Keys) + { + 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 = 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(); + } + } + + SharpDX.Direct2D1.Brush dailyCloseBrush = DailyCloseStroke.Brush.ToDxBrush(RenderTarget); + + foreach (double dailyClose in DailyCloses.Keys) + { + int dailyCloseY = chartScale.GetYByValue(dailyClose); + RenderTarget.DrawLine(new SharpDX.Vector2(ChartPanel.X, dailyCloseY), + new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, dailyCloseY), + dailyCloseBrush, DailyCloseStroke.Width, DailyCloseStroke.StrokeStyle); + + if (DrawLabels) + { + 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(); + } + } + + dailyOpenBrush.Dispose(); + textFormat.Dispose(); + textBrush.Dispose(); + dailyCloseBrush.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 = "Daily Open", Description = "Daily open level drawn on the chart", Order = 2, GroupName = "Daily Levels")] + public Stroke DailyOpenStroke + { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Daily Close", Description = "Daily close level drawn on the chart", Order = 3, GroupName = "Daily Levels")] + public Stroke DailyCloseStroke + { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Draw Labels", Order = 1, GroupName = "Labels")] + public bool DrawLabels + { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Daily Open Label Text", Order = 2, GroupName = "Labels")] + public string DailyOpenLabelText + { get; set; } + + [NinjaScriptProperty] + [Display(Name = "DailyClose Label Text", Order = 3, GroupName = "Labels")] + public string DailyCloseLabelText + { get; set; } + + [NinjaScriptProperty] + [Display(Name = "Font", Description = "Font used to display daily levels", Order = 3, GroupName = "Labels")] + public SimpleFont DailyLevelFont + { get; set; } + + [XmlIgnore] + [NinjaScriptProperty] + [Display(Name = "Font Color", Description = "Color of the text used to label the daily levels", Order = 4, GroupName = "Labels")] + 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, Stroke dailyOpenStroke, Stroke dailyCloseStroke, bool drawLabels, string dailyOpenLabelText, string dailyCloseLabelText, SimpleFont dailyLevelFont, Brush dailyLevelFontColor) + { + return DailyLevels(Input, numberOfDays, dailyOpenStroke, dailyCloseStroke, drawLabels, dailyOpenLabelText, dailyCloseLabelText, dailyLevelFont, dailyLevelFontColor); + } + + public DailyLevels DailyLevels(ISeries input, int numberOfDays, Stroke dailyOpenStroke, Stroke dailyCloseStroke, bool drawLabels, string dailyOpenLabelText, string dailyCloseLabelText, SimpleFont dailyLevelFont, Brush dailyLevelFontColor) + { + if (cacheDailyLevels != null) + for (int idx = 0; idx < cacheDailyLevels.Length; idx++) + if (cacheDailyLevels[idx] != null && cacheDailyLevels[idx].NumberOfDays == numberOfDays && cacheDailyLevels[idx].DailyOpenStroke == dailyOpenStroke && 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)) + return cacheDailyLevels[idx]; + return CacheIndicator(new DailyLevels(){ NumberOfDays = numberOfDays, DailyOpenStroke = dailyOpenStroke, DailyCloseStroke = dailyCloseStroke, DrawLabels = drawLabels, DailyOpenLabelText = dailyOpenLabelText, DailyCloseLabelText = dailyCloseLabelText, DailyLevelFont = dailyLevelFont, DailyLevelFontColor = dailyLevelFontColor }, input, ref cacheDailyLevels); + } + } +} + +namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns +{ + public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase + { + public Indicators.DailyLevels DailyLevels(int numberOfDays, Stroke dailyOpenStroke, Stroke dailyCloseStroke, bool drawLabels, string dailyOpenLabelText, string dailyCloseLabelText, SimpleFont dailyLevelFont, Brush dailyLevelFontColor) + { + return indicator.DailyLevels(Input, numberOfDays, dailyOpenStroke, dailyCloseStroke, drawLabels, dailyOpenLabelText, dailyCloseLabelText, dailyLevelFont, dailyLevelFontColor); + } + + public Indicators.DailyLevels DailyLevels(ISeries input , int numberOfDays, Stroke dailyOpenStroke, Stroke dailyCloseStroke, bool drawLabels, string dailyOpenLabelText, string dailyCloseLabelText, SimpleFont dailyLevelFont, Brush dailyLevelFontColor) + { + return indicator.DailyLevels(input, numberOfDays, dailyOpenStroke, dailyCloseStroke, drawLabels, dailyOpenLabelText, dailyCloseLabelText, dailyLevelFont, dailyLevelFontColor); + } + } +} + +namespace NinjaTrader.NinjaScript.Strategies +{ + public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase + { + public Indicators.DailyLevels DailyLevels(int numberOfDays, Stroke dailyOpenStroke, Stroke dailyCloseStroke, bool drawLabels, string dailyOpenLabelText, string dailyCloseLabelText, SimpleFont dailyLevelFont, Brush dailyLevelFontColor) + { + return indicator.DailyLevels(Input, numberOfDays, dailyOpenStroke, dailyCloseStroke, drawLabels, dailyOpenLabelText, dailyCloseLabelText, dailyLevelFont, dailyLevelFontColor); + } + + public Indicators.DailyLevels DailyLevels(ISeries input , int numberOfDays, Stroke dailyOpenStroke, Stroke dailyCloseStroke, bool drawLabels, string dailyOpenLabelText, string dailyCloseLabelText, SimpleFont dailyLevelFont, Brush dailyLevelFontColor) + { + return indicator.DailyLevels(input, numberOfDays, dailyOpenStroke, dailyCloseStroke, drawLabels, dailyOpenLabelText, dailyCloseLabelText, dailyLevelFont, dailyLevelFontColor); + } + } +} + +#endregion