204 lines
8.8 KiB
C#
204 lines
8.8 KiB
C#
|
#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
|