2023-03-17 17:53:34 +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 DailyCOL : 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 -> 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);
|
2023-04-11 13:46:41 +00:00
|
|
|
DrawLabels = true;
|
2023-03-17 17:53:34 +00:00
|
|
|
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);
|
|
|
|
|
2023-04-11 13:46:41 +00:00
|
|
|
SharpDX.Direct2D1.Brush dailyOpenBrush = DailyOpenStroke.Brush.ToDxBrush(RenderTarget);
|
|
|
|
|
2023-03-17 17:53:34 +00:00
|
|
|
TextFormat textFormat = DailyOpenFont.ToDirectWriteTextFormat();
|
|
|
|
SharpDX.Direct2D1.Brush textBrush = DailyOpenFontColor.ToDxBrush(RenderTarget);
|
|
|
|
|
|
|
|
foreach (double dailyOpen in DailyOpens.Keys)
|
|
|
|
{
|
2023-04-11 13:46:41 +00:00
|
|
|
// 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();
|
2023-03-17 17:53:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 13:46:41 +00:00
|
|
|
dailyOpenBrush.Dispose();
|
2023-03-17 17:53:34 +00:00
|
|
|
textFormat.Dispose();
|
|
|
|
textBrush.Dispose();
|
|
|
|
}
|
2023-04-11 13:46:41 +00:00
|
|
|
|
|
|
|
public override string DisplayName
|
|
|
|
{
|
|
|
|
get { return Name; }
|
|
|
|
}
|
|
|
|
|
2023-03-17 17:53:34 +00:00
|
|
|
[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; }
|
|
|
|
|
2023-04-11 13:46:41 +00:00
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "Draw Labels", Order = 1, GroupName = "Labels")]
|
|
|
|
public bool DrawLabels
|
|
|
|
{ get; set; }
|
2023-03-17 17:53:34 +00:00
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
2023-04-11 13:46:41 +00:00
|
|
|
[Display(Name = "Label Text", Order = 2, GroupName = "Labels")]
|
2023-03-17 17:53:34 +00:00
|
|
|
public string LabelText
|
|
|
|
{ get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
2023-04-11 13:46:41 +00:00
|
|
|
[Display(Name = "Font", Description = "Font used to display daily COLs", Order = 3, GroupName = "Labels")]
|
2023-03-17 17:53:34 +00:00
|
|
|
public SimpleFont DailyOpenFont
|
|
|
|
{ get; set; }
|
|
|
|
|
|
|
|
[XmlIgnore]
|
|
|
|
[NinjaScriptProperty]
|
2023-04-11 13:46:41 +00:00
|
|
|
[Display(Name = "Font Color", Description = "Color of the text used to label the daily COLs", Order = 4, GroupName = "Labels")]
|
2023-03-17 17:53:34 +00:00
|
|
|
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;
|
2023-04-11 13:46:41 +00:00
|
|
|
public DailyCOL DailyCOL(int numberOfDays, Stroke dailyOpenStroke, bool drawLabels, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor)
|
2023-03-17 17:53:34 +00:00
|
|
|
{
|
2023-04-11 13:46:41 +00:00
|
|
|
return DailyCOL(Input, numberOfDays, dailyOpenStroke, drawLabels, labelText, dailyOpenFont, dailyOpenFontColor);
|
2023-03-17 17:53:34 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 13:46:41 +00:00
|
|
|
public DailyCOL DailyCOL(ISeries<double> input, int numberOfDays, Stroke dailyOpenStroke, bool drawLabels, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor)
|
2023-03-17 17:53:34 +00:00
|
|
|
{
|
|
|
|
if (cacheDailyCOL != null)
|
|
|
|
for (int idx = 0; idx < cacheDailyCOL.Length; idx++)
|
2023-04-11 13:46:41 +00:00
|
|
|
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))
|
2023-03-17 17:53:34 +00:00
|
|
|
return cacheDailyCOL[idx];
|
2023-04-11 13:46:41 +00:00
|
|
|
return CacheIndicator<DailyCOL>(new DailyCOL(){ NumberOfDays = numberOfDays, DailyOpenStroke = dailyOpenStroke, DrawLabels = drawLabels, LabelText = labelText, DailyOpenFont = dailyOpenFont, DailyOpenFontColor = dailyOpenFontColor }, input, ref cacheDailyCOL);
|
2023-03-17 17:53:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
|
|
|
|
{
|
|
|
|
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
|
|
|
|
{
|
2023-04-11 13:46:41 +00:00
|
|
|
public Indicators.DailyCOL DailyCOL(int numberOfDays, Stroke dailyOpenStroke, bool drawLabels, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor)
|
2023-03-17 17:53:34 +00:00
|
|
|
{
|
2023-04-11 13:46:41 +00:00
|
|
|
return indicator.DailyCOL(Input, numberOfDays, dailyOpenStroke, drawLabels, labelText, dailyOpenFont, dailyOpenFontColor);
|
2023-03-17 17:53:34 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 13:46:41 +00:00
|
|
|
public Indicators.DailyCOL DailyCOL(ISeries<double> input , int numberOfDays, Stroke dailyOpenStroke, bool drawLabels, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor)
|
2023-03-17 17:53:34 +00:00
|
|
|
{
|
2023-04-11 13:46:41 +00:00
|
|
|
return indicator.DailyCOL(input, numberOfDays, dailyOpenStroke, drawLabels, labelText, dailyOpenFont, dailyOpenFontColor);
|
2023-03-17 17:53:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Strategies
|
|
|
|
{
|
|
|
|
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
|
|
|
|
{
|
2023-04-11 13:46:41 +00:00
|
|
|
public Indicators.DailyCOL DailyCOL(int numberOfDays, Stroke dailyOpenStroke, bool drawLabels, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor)
|
2023-03-17 17:53:34 +00:00
|
|
|
{
|
2023-04-11 13:46:41 +00:00
|
|
|
return indicator.DailyCOL(Input, numberOfDays, dailyOpenStroke, drawLabels, labelText, dailyOpenFont, dailyOpenFontColor);
|
2023-03-17 17:53:34 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 13:46:41 +00:00
|
|
|
public Indicators.DailyCOL DailyCOL(ISeries<double> input , int numberOfDays, Stroke dailyOpenStroke, bool drawLabels, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor)
|
2023-03-17 17:53:34 +00:00
|
|
|
{
|
2023-04-11 13:46:41 +00:00
|
|
|
return indicator.DailyCOL(input, numberOfDays, dailyOpenStroke, drawLabels, labelText, dailyOpenFont, dailyOpenFontColor);
|
2023-03-17 17:53:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|