ninjatrader/indicators/ADR.cs

210 lines
7.2 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.Data;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.NinjaScript;
#endregion
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class DailyRange
{
public int StartBarIndex { get; set; }
public int EndBarIndex { get; set; }
public double UpperLevel { get; set; }
public double LowerLevel { get; set; }
}
public class ADR : Indicator
{
private const int PrimaryBars = 0;
private double CurrentDailyHigh = double.MinValue;
private double CurrentDailyLow = double.MaxValue;
private List<double> DailyHighs = new List<double>();
private List<double> DailyLows = new List<double>();
private List<DailyRange> DailyRanges = new List<DailyRange>();
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Average Daily Range (ADR)";
Name = "ADR";
Calculate = Calculate.OnPriceChange;
IsOverlay = true;
ScaleJustification = ScaleJustification.Right;
IsSuspendedWhileInactive = false;
RangePeriod = 5;
ADRHighStroke = new Stroke(Brushes.Yellow, 3);
ADRLowStroke = new Stroke(Brushes.Yellow, 3);
}
else if (State == State.Historical)
{
SetZOrder(-1); // Display behind bars on chart.
}
}
protected override void OnBarUpdate()
{
if (PrimaryBars != BarsInProgress)
return;
if (Bars.IsFirstBarOfSession && IsFirstTickOfBar)
{
if (CurrentDailyHigh > double.MinValue)
{
DailyHighs.Add(CurrentDailyHigh);
DailyLows.Add(CurrentDailyLow);
}
if (DailyHighs.Count < RangePeriod)
return;
if (DailyRanges.Count > 0)
DailyRanges[DailyRanges.Count - 1].EndBarIndex = CurrentBar - 1;
double totalRange = 0;
for (int i = (DailyHighs.Count - 1); i >= (DailyHighs.Count - RangePeriod); i--)
totalRange += DailyHighs[i] - DailyLows[i];
double averageRange = totalRange / RangePeriod;
DailyRanges.Add(new DailyRange
{
StartBarIndex = CurrentBar,
UpperLevel = Close[1] + (averageRange / 2),
LowerLevel = Close[1] - (averageRange / 2),
});
CurrentDailyHigh = High[0];
CurrentDailyLow = Low[0];
}
else
{
if (High[0] > CurrentDailyHigh)
CurrentDailyHigh = High[0];
if (Low[0] < CurrentDailyLow)
CurrentDailyLow = Low[0];
}
}
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
{
base.OnRender(chartControl, chartScale);
foreach (var Range in DailyRanges)
{
int startX = chartControl.GetXByBarIndex(ChartBars, Range.StartBarIndex);
int endX = Range.EndBarIndex == 0 ? ChartPanel.X + ChartPanel.W : chartControl.GetXByBarIndex(ChartBars, Range.EndBarIndex);
DrawHorizontalLine(startX, endX, chartScale.GetYByValue(Range.UpperLevel), ADRHighStroke);
DrawHorizontalLine(startX, endX, chartScale.GetYByValue(Range.LowerLevel), ADRLowStroke);
}
}
private void DrawHorizontalLine(int startX, int endX, int y, Stroke stroke)
{
SharpDX.Direct2D1.Brush dxBrush = stroke.Brush.ToDxBrush(RenderTarget);
RenderTarget.DrawLine(
new SharpDX.Vector2(startX, y),
new SharpDX.Vector2(endX, y),
dxBrush, stroke.Width, stroke.StrokeStyle
);
dxBrush.Dispose();
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Range Period", GroupName = "Average Daily Range", Order = 1)]
public int RangePeriod
{ get; set; }
[NinjaScriptProperty]
[Display(Name = "ADR High", Order = 2, GroupName = "Average Daily Range")]
public Stroke ADRHighStroke { get; set; }
[NinjaScriptProperty]
[Display(Name = "ADR Low", Order = 3, GroupName = "Average Daily Range")]
public Stroke ADRLowStroke { get; set; }
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private ADR[] cacheADR;
public ADR ADR(int rangePeriod, Stroke aDRHighStroke, Stroke aDRLowStroke)
{
return ADR(Input, rangePeriod, aDRHighStroke, aDRLowStroke);
}
public ADR ADR(ISeries<double> input, int rangePeriod, Stroke aDRHighStroke, Stroke aDRLowStroke)
{
if (cacheADR != null)
for (int idx = 0; idx < cacheADR.Length; idx++)
if (cacheADR[idx] != null && cacheADR[idx].RangePeriod == rangePeriod && cacheADR[idx].ADRHighStroke == aDRHighStroke && cacheADR[idx].ADRLowStroke == aDRLowStroke && cacheADR[idx].EqualsInput(input))
return cacheADR[idx];
return CacheIndicator<ADR>(new ADR(){ RangePeriod = rangePeriod, ADRHighStroke = aDRHighStroke, ADRLowStroke = aDRLowStroke }, input, ref cacheADR);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.ADR ADR(int rangePeriod, Stroke aDRHighStroke, Stroke aDRLowStroke)
{
return indicator.ADR(Input, rangePeriod, aDRHighStroke, aDRLowStroke);
}
public Indicators.ADR ADR(ISeries<double> input , int rangePeriod, Stroke aDRHighStroke, Stroke aDRLowStroke)
{
return indicator.ADR(input, rangePeriod, aDRHighStroke, aDRLowStroke);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.ADR ADR(int rangePeriod, Stroke aDRHighStroke, Stroke aDRLowStroke)
{
return indicator.ADR(Input, rangePeriod, aDRHighStroke, aDRLowStroke);
}
public Indicators.ADR ADR(ISeries<double> input , int rangePeriod, Stroke aDRHighStroke, Stroke aDRLowStroke)
{
return indicator.ADR(input, rangePeriod, aDRHighStroke, aDRLowStroke);
}
}
}
#endregion