Add True Range Adjusted EMA (TRAdj EMA) indicator

This commit is contained in:
moshferatu 2024-07-27 07:21:02 -07:00
parent c6880cbcc4
commit 8142723b6a

149
indicators/TRAdjEMA.cs Normal file
View File

@ -0,0 +1,149 @@
#region Using declarations
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using System;
using System.ComponentModel.DataAnnotations;
using System.Windows.Media;
#endregion
namespace NinjaTrader.NinjaScript.Indicators
{
public class TRAdjEMA : Indicator
{
private MAX maxRange;
private MIN minRange;
private Series<double> trueRange;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"True Range Adjusted EMA";
Name = "TRAdj EMA";
Calculate = Calculate.OnPriceChange;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
PaintPriceMarkers = true;
ScaleJustification = ScaleJustification.Right;
IsSuspendedWhileInactive = true;
Period = 40;
TrueRangeLookback = 40;
Multiplier = 10;
AddPlot(new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3), PlotStyle.Line, "TRAdj EMA");
}
else if (State == State.DataLoaded)
{
trueRange = new Series<double>(this);
maxRange = MAX(trueRange, TrueRangeLookback);
minRange = MIN(trueRange, TrueRangeLookback);
}
else if (State == State.Historical)
{
SetZOrder(-1); // Display behind bars on chart.
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < 1)
return;
trueRange[0] = Math.Max(High[0] - Low[0], Math.Max(Math.Abs(High[0] - Close[1]), Math.Abs(Low[0] - Close[1])));
if (CurrentBar < Math.Max(Period, TrueRangeLookback))
Value[0] = Close[0];
else
{
double trAdj = (trueRange[0] - minRange[0]) / (maxRange[0] - minRange[0]);
double multiplier1 = 2.0 / (Period + 1);
double multiplier2 = trAdj * Multiplier;
double rate = multiplier1 * (1.0 + multiplier2);
Value[0] = Value[1] + rate * (Close[0] - Value[1]);
}
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Period", GroupName = "TRAdj EMA", Order = 1)]
public int Period { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "True Range Lookback", GroupName = "TRAdj EMA", Order = 2)]
public int TrueRangeLookback { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Multiplier", GroupName = "TRAdj EMA", Order = 3)]
public int Multiplier { get; set; }
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private TRAdjEMA[] cacheTRAdjEMA;
public TRAdjEMA TRAdjEMA(int period, int trueRangeLookback, int multiplier)
{
return TRAdjEMA(Input, period, trueRangeLookback, multiplier);
}
public TRAdjEMA TRAdjEMA(ISeries<double> input, int period, int trueRangeLookback, int multiplier)
{
if (cacheTRAdjEMA != null)
for (int idx = 0; idx < cacheTRAdjEMA.Length; idx++)
if (cacheTRAdjEMA[idx] != null && cacheTRAdjEMA[idx].Period == period && cacheTRAdjEMA[idx].TrueRangeLookback == trueRangeLookback && cacheTRAdjEMA[idx].Multiplier == multiplier && cacheTRAdjEMA[idx].EqualsInput(input))
return cacheTRAdjEMA[idx];
return CacheIndicator<TRAdjEMA>(new TRAdjEMA(){ Period = period, TrueRangeLookback = trueRangeLookback, Multiplier = multiplier }, input, ref cacheTRAdjEMA);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.TRAdjEMA TRAdjEMA(int period, int trueRangeLookback, int multiplier)
{
return indicator.TRAdjEMA(Input, period, trueRangeLookback, multiplier);
}
public Indicators.TRAdjEMA TRAdjEMA(ISeries<double> input , int period, int trueRangeLookback, int multiplier)
{
return indicator.TRAdjEMA(input, period, trueRangeLookback, multiplier);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.TRAdjEMA TRAdjEMA(int period, int trueRangeLookback, int multiplier)
{
return indicator.TRAdjEMA(Input, period, trueRangeLookback, multiplier);
}
public Indicators.TRAdjEMA TRAdjEMA(ISeries<double> input , int period, int trueRangeLookback, int multiplier)
{
return indicator.TRAdjEMA(input, period, trueRangeLookback, multiplier);
}
}
}
#endregion