namespace NinjaTrader.NinjaScript.Indicators
{
public class ASR3 : Indicator
{
private bool direction;
private double currHigh;
private double currLow;
private double prevHigh1;
private double prevHigh2;
private double prevHigh3;
private double prevHigh4;
private double prevHigh5;
private double prevLow1;
private double prevLow2;
private double prevLow3;
private double prevLow4;
private double prevLow5;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"";
Name = "ASR 3";
Calculate = Calculate.OnBarClose;
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;
Period = 55;
Deviations = 1;
AddPlot(Brushes.Green, "Upper");
AddPlot(Brushes.Red, "Lower");
AddPlot(Brushes.Orange, "Middle");
}
else if (State == State.Configure)
{
}
}
protected override void OnBarUpdate()
{
double dev = StdDev(Input, Period)[0] * Deviations;
if(CurrentBar == 0)
{
currHigh = Input[0];
currLow = Input[0];
prevHigh1 = Input[0];
prevHigh2 = Input[0];
prevHigh3 = Input[0];
prevHigh4 = Input[0];
prevHigh5 = Input[0];
prevLow1 = Input[0];
prevLow2 = Input[0];
prevLow3 = Input[0];
prevLow4 = Input[0];
prevLow5 = Input[0];
return;
}
else if(CurrentBar == 1)
{
if(Input[0] >= Input[1])
{
direction = true;
}
else if(Input[0] < Input[1])
{
direction = false;
}
}
if(Input[0] > currHigh && direction == true)
{
currHigh = Input[0];
}
else if(Input[0] < currLow && direction == false)
{
currLow = Input[0];
}
if(direction == true && Input[0] < currHigh - dev)
{
direction = false;
prevLow5 = prevLow4;
prevLow4 = prevLow3;
prevLow3 = prevLow2;
prevLow2 = prevLow1;
prevLow1 = currLow;
currLow = Input[0];
}
if(direction == false && Input[0] > currLow + dev)
{
direction = true;
prevHigh5 = prevHigh4;
prevHigh4 = prevHigh3;
prevHigh3 = prevHigh2;
prevHigh2 = prevHigh1;
prevHigh1 = currHigh;
currHigh = Input[0];
}
Upper[0] = (prevHigh5 + prevHigh4 + prevHigh3 + prevHigh2 + prevHigh1) / 5;
Lower[0] = (prevLow5 + prevLow4 + prevLow3 + prevLow2 + prevLow1) / 5;
Middle[0] = (Upper[0] + Lower[0]) / 2;
}
region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Period", Order=1, GroupName="Parameters")]
public int Period
{ get; set; }
[NinjaScriptProperty]
[Range(0.01, double.MaxValue)]
[Display(Name="Deviations", Order=2, GroupName="Parameters")]
public double Deviations
{ get; set; }
[Browsable(false)]
[XmlIgnore]
public Series<double> Upper
{
get { return Values[0]; }
}
[Browsable(false)]
[XmlIgnore]
public Series<double> Lower
{
get { return Values[1]; }
}
[Browsable(false)]
[XmlIgnore]
public Series<double> Middle
{
get { return Values[2]; }
}
#endregion
}
}
Comment