2024-03-10 11:52:41 +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;
|
2024-03-12 15:10:59 +00:00
|
|
|
using NinjaTrader.Data;
|
2024-03-10 11:52:41 +00:00
|
|
|
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
|
|
|
|
{
|
2024-03-12 15:10:59 +00:00
|
|
|
public int StartBarIndex { get; set; }
|
|
|
|
public int EndBarIndex { get; set; }
|
2024-03-10 11:52:41 +00:00
|
|
|
public double UpperLevel { get; set; }
|
|
|
|
public double LowerLevel { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ADR : Indicator
|
|
|
|
{
|
|
|
|
private const int PrimaryBars = 0;
|
|
|
|
|
2024-03-12 15:10:59 +00:00
|
|
|
private double CurrentDailyHigh = double.MinValue;
|
|
|
|
private double CurrentDailyLow = double.MaxValue;
|
|
|
|
|
|
|
|
private List<double> DailyHighs = new List<double>();
|
|
|
|
private List<double> DailyLows = new List<double>();
|
2024-03-10 11:52:41 +00:00
|
|
|
|
|
|
|
private List<DailyRange> DailyRanges = new List<DailyRange>();
|
|
|
|
|
|
|
|
protected override void OnStateChange()
|
|
|
|
{
|
|
|
|
if (State == State.SetDefaults)
|
|
|
|
{
|
|
|
|
Description = @"Average Daily Range (ADR)";
|
|
|
|
Name = "ADR";
|
2024-03-12 15:10:59 +00:00
|
|
|
Calculate = Calculate.OnPriceChange;
|
2024-03-10 11:52:41 +00:00
|
|
|
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()
|
|
|
|
{
|
2024-03-12 15:10:59 +00:00
|
|
|
if (PrimaryBars != BarsInProgress)
|
2024-03-10 11:52:41 +00:00
|
|
|
return;
|
2024-03-12 15:10:59 +00:00
|
|
|
|
|
|
|
if (Bars.IsFirstBarOfSession && IsFirstTickOfBar)
|
|
|
|
{
|
|
|
|
if (CurrentDailyHigh > double.MinValue)
|
|
|
|
{
|
|
|
|
DailyHighs.Add(CurrentDailyHigh);
|
|
|
|
DailyLows.Add(CurrentDailyLow);
|
|
|
|
}
|
2024-03-10 11:52:41 +00:00
|
|
|
|
2024-03-12 15:10:59 +00:00
|
|
|
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),
|
|
|
|
});
|
2024-03-10 11:52:41 +00:00
|
|
|
|
2024-03-12 15:10:59 +00:00
|
|
|
CurrentDailyHigh = High[0];
|
|
|
|
CurrentDailyLow = Low[0];
|
|
|
|
}
|
|
|
|
else
|
2024-03-10 11:52:41 +00:00
|
|
|
{
|
2024-03-12 15:10:59 +00:00
|
|
|
if (High[0] > CurrentDailyHigh)
|
|
|
|
CurrentDailyHigh = High[0];
|
|
|
|
|
|
|
|
if (Low[0] < CurrentDailyLow)
|
|
|
|
CurrentDailyLow = Low[0];
|
|
|
|
}
|
2024-03-10 11:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
|
|
|
|
{
|
|
|
|
base.OnRender(chartControl, chartScale);
|
|
|
|
|
|
|
|
foreach (var Range in DailyRanges)
|
2024-03-12 15:10:59 +00:00
|
|
|
{
|
|
|
|
int startX = chartControl.GetXByBarIndex(ChartBars, Range.StartBarIndex);
|
|
|
|
int endX = Range.EndBarIndex == 0 ? ChartPanel.X + ChartPanel.W : chartControl.GetXByBarIndex(ChartBars, Range.EndBarIndex);
|
2024-03-10 11:52:41 +00:00
|
|
|
|
|
|
|
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
|