ninjatrader/indicators/DailyCOL.cs

217 lines
9.5 KiB
C#
Raw Normal View History

#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);
DrawLabels = true;
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);
SharpDX.Direct2D1.Brush dailyOpenBrush = DailyOpenStroke.Brush.ToDxBrush(RenderTarget);
TextFormat textFormat = DailyOpenFont.ToDirectWriteTextFormat();
SharpDX.Direct2D1.Brush textBrush = DailyOpenFontColor.ToDxBrush(RenderTarget);
foreach (double dailyOpen in DailyOpens.Keys)
{
// 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();
}
}
dailyOpenBrush.Dispose();
textFormat.Dispose();
textBrush.Dispose();
}
public override string DisplayName
{
get { return Name; }
}
[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; }
[NinjaScriptProperty]
[Display(Name = "Draw Labels", Order = 1, GroupName = "Labels")]
public bool DrawLabels
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Label Text", Order = 2, GroupName = "Labels")]
public string LabelText
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "Font", Description = "Font used to display daily COLs", Order = 3, 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 = 4, 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, bool drawLabels, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor)
{
return DailyCOL(Input, numberOfDays, dailyOpenStroke, drawLabels, labelText, dailyOpenFont, dailyOpenFontColor);
}
public DailyCOL DailyCOL(ISeries<double> input, int numberOfDays, Stroke dailyOpenStroke, bool drawLabels, 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].DrawLabels == drawLabels && 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, DrawLabels = drawLabels, 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, bool drawLabels, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor)
{
return indicator.DailyCOL(Input, numberOfDays, dailyOpenStroke, drawLabels, labelText, dailyOpenFont, 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, drawLabels, labelText, dailyOpenFont, dailyOpenFontColor);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.DailyCOL DailyCOL(int numberOfDays, Stroke dailyOpenStroke, bool drawLabels, string labelText, SimpleFont dailyOpenFont, Brush dailyOpenFontColor)
{
return indicator.DailyCOL(Input, numberOfDays, dailyOpenStroke, drawLabels, labelText, dailyOpenFont, 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, drawLabels, labelText, dailyOpenFont, dailyOpenFontColor);
}
}
}
#endregion