Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

//

//

#region Using declarations


using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.Xml.Serialization;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
#endregion

// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
///
/// </summary>
[Description("The Hurst Envelope is described in J.M Hursts 'The Profit Magic of
Stock Transaction Timing'")]
public class HurstEnvelope : Indicator
{
#region Variables
private int period
= 10;
private double offsetMultiplier = 1.5;
private DataSeries diff;
#endregion

/// <summary>
/// This method is used to configure the indicator and is called once
before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.Transparent, "Midline"));
Add(new Plot(Color.Gray, "Upper"));
Add(new Plot(Color.Gray, "Lower"));

diff = new DataSeries(this);

Overlay = true;
PriceTypeSupported = false;
}

/// <summary>
/// Called on each bar update event (incoming tick).
/// </summary>
protected override void OnBarUpdate()
{
if (CurrentBar < Period)
return;
diff.Set(High[0] - Low[0]);

double middle = TMA(Median, Period)[0];


double offset = TMA(diff, Period)[0] * offsetMultiplier;
double upper = middle + offset;
double lower = middle - offset;

KeltnerChannel env = KeltnerChannel(Median, offsetMultiplier,


(period/2));

Midline.Set(middle);
Upper.Set(upper);
Lower.Set(lower);

DrawLine("UPPER", true, (period/2), upper, 0, env.Upper[0],


Color.Cyan, DashStyle.Dash, 1);
DrawLine("LOWER", true, (period/2), lower, 0, env.Lower[0],
Color.Cyan, DashStyle.Dash, 1);

#region Properties
/// <summary>
/// </summary>
[Description("Numbers of bars used for calculations")]
[Category("Parameters")]
public int Period
{
get { return period; }
set { period = Math.Max(1, value); }
}

/// <summary>
/// </summary>
[Description("How much to expand the upper and lower band from the
normal offset")]
[Category("Parameters")]
[Gui.Design.DisplayNameAttribute("Offset multiplier")]
public double OffsetMultiplier
{
get { return offsetMultiplier; }
set { offsetMultiplier = Math.Max(0.01, value); }
}

/// <summary>
/// </summary>
[Browsable(false)]
[XmlIgnore()]
public DataSeries Midline
{
get { return Values[0]; }
}

/// <summary>
/// </summary>
[Browsable(false)]
[XmlIgnore()]
public DataSeries Upper
{
get { return Values[1]; }
}
/// <summary>
/// </summary>
[Browsable(false)]
[XmlIgnore()]
public DataSeries Lower
{
get { return Values[2]; }
}
#endregion
}
}

#region NinjaScript generated code. Neither change nor remove.


// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
public partial class Indicator : IndicatorBase
{
private HurstEnvelope[] cacheHurstEnvelope = null;

private static HurstEnvelope checkHurstEnvelope = new HurstEnvelope();

/// <summary>
/// The Hurst Envelope is described in J.M Hursts 'The Profit Magic of
Stock Transaction Timing'
/// </summary>
/// <returns></returns>
public HurstEnvelope HurstEnvelope(double offsetMultiplier, int period)
{
return HurstEnvelope(Input, offsetMultiplier, period);
}

/// <summary>
/// The Hurst Envelope is described in J.M Hursts 'The Profit Magic of
Stock Transaction Timing'
/// </summary>
/// <returns></returns>
public HurstEnvelope HurstEnvelope(Data.IDataSeries input, double
offsetMultiplier, int period)
{
checkHurstEnvelope.OffsetMultiplier = offsetMultiplier;
offsetMultiplier = checkHurstEnvelope.OffsetMultiplier;
checkHurstEnvelope.Period = period;
period = checkHurstEnvelope.Period;

if (cacheHurstEnvelope != null)
for (int idx = 0; idx < cacheHurstEnvelope.Length; idx++)
if (Math.Abs(cacheHurstEnvelope[idx].OffsetMultiplier -
offsetMultiplier) <= double.Epsilon && cacheHurstEnvelope[idx].Period == period &&
cacheHurstEnvelope[idx].EqualsInput(input))
return cacheHurstEnvelope[idx];

HurstEnvelope indicator = new HurstEnvelope();


indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
indicator.Input = input;
indicator.OffsetMultiplier = offsetMultiplier;
indicator.Period = period;
indicator.SetUp();
HurstEnvelope[] tmp = new HurstEnvelope[cacheHurstEnvelope == null ?
1 : cacheHurstEnvelope.Length + 1];
if (cacheHurstEnvelope != null)
cacheHurstEnvelope.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheHurstEnvelope = tmp;
Indicators.Add(indicator);

return indicator;
}

}
}

// This namespace holds all market analyzer column definitions and is required. Do
not change it.
namespace NinjaTrader.MarketAnalyzer
{
public partial class Column : ColumnBase
{
/// <summary>
/// The Hurst Envelope is described in J.M Hursts 'The Profit Magic of
Stock Transaction Timing'
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.HurstEnvelope HurstEnvelope(double offsetMultiplier, int
period)
{
return _indicator.HurstEnvelope(Input, offsetMultiplier, period);
}

/// <summary>
/// The Hurst Envelope is described in J.M Hursts 'The Profit Magic of
Stock Transaction Timing'
/// </summary>
/// <returns></returns>
public Indicator.HurstEnvelope HurstEnvelope(Data.IDataSeries input, double
offsetMultiplier, int period)
{
return _indicator.HurstEnvelope(input, offsetMultiplier, period);
}

}
}

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// The Hurst Envelope is described in J.M Hursts 'The Profit Magic of
Stock Transaction Timing'
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.HurstEnvelope HurstEnvelope(double offsetMultiplier, int
period)
{
return _indicator.HurstEnvelope(Input, offsetMultiplier, period);
}

/// <summary>
/// The Hurst Envelope is described in J.M Hursts 'The Profit Magic of
Stock Transaction Timing'
/// </summary>
/// <returns></returns>
public Indicator.HurstEnvelope HurstEnvelope(Data.IDataSeries input, double
offsetMultiplier, int period)
{
if (InInitialize && input == null)
throw new ArgumentException("You only can access an indicator with
the default input/bar series from within the 'Initialize()' method");

return _indicator.HurstEnvelope(input, offsetMultiplier, period);


}

}
}
#endregion

You might also like