ninjatrader/indicators/SuperSmoother.cs

134 lines
4.2 KiB
C#
Raw Permalink Normal View History

#region Using declarations
using System;
using System.ComponentModel.DataAnnotations;
using System.Windows.Media;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
#endregion
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
[CategoryOrder("SuperSmoother", 1)]
[CategoryOrder("Plots", 2)]
public class SuperSmoother : Indicator
{
private double a;
private double b;
private double c1;
private double c2;
private double c3;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Credit to John F. Ehlers";
Name = "SuperSmoother";
Calculate = Calculate.OnPriceChange;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = ScaleJustification.Right;
IsSuspendedWhileInactive = true;
Period = 10;
AddPlot(new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 2), PlotStyle.Line, "SuperSmoother");
}
else if (State == State.Configure)
{
a = Math.Exp(-1.414 * Math.PI / Period);
b = 2 * a * Math.Cos(1.414 * 180 / Period);
c2 = b;
c3 = -a * a;
c1 = 1 - c2 - c3;
}
else if (State == State.Historical)
{
SetZOrder(-1); // Display behind bars on chart.
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < 3) // Need at least 3 bars.
{
Value[0] = Input[0];
return;
}
Value[0] = c1 * (Input[0] + Input[1]) / 2 + c2 * Value[1] + c3 * Value[2];
}
public override string DisplayName
{
get { return Name; }
}
#region Properties
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(Name = "Period", Order = 1, GroupName = "SuperSmoother")]
public int Period { get; set; }
#endregion
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private SuperSmoother[] cacheSuperSmoother;
public SuperSmoother SuperSmoother(int period)
{
return SuperSmoother(Input, period);
}
public SuperSmoother SuperSmoother(ISeries<double> input, int period)
{
if (cacheSuperSmoother != null)
for (int idx = 0; idx < cacheSuperSmoother.Length; idx++)
if (cacheSuperSmoother[idx] != null && cacheSuperSmoother[idx].Period == period && cacheSuperSmoother[idx].EqualsInput(input))
return cacheSuperSmoother[idx];
return CacheIndicator<SuperSmoother>(new SuperSmoother(){ Period = period }, input, ref cacheSuperSmoother);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.SuperSmoother SuperSmoother(int period)
{
return indicator.SuperSmoother(Input, period);
}
public Indicators.SuperSmoother SuperSmoother(ISeries<double> input , int period)
{
return indicator.SuperSmoother(input, period);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.SuperSmoother SuperSmoother(int period)
{
return indicator.SuperSmoother(Input, period);
}
public Indicators.SuperSmoother SuperSmoother(ISeries<double> input , int period)
{
return indicator.SuperSmoother(input, period);
}
}
}
#endregion