Thanks again for the help.
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
#endregion
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// EMA20 of Srandard Error Indicator
/// </summary>
[Description("EMA20 of Srandard Error Indicator")]
public class StdErrorSmoothed2 : Indicator
{
#region Variables
private int period = 14;
private DataSeries y;
#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.Orange, "LinReg"));
Add(new Plot(Color.DarkViolet, "Upper"));
Add(new Plot(Color.DarkViolet, "Lower"));
y = new DataSeries(this);
Overlay = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// calculate Linear Regression
double sumX = (double) Period * (Period - 1) * 0.5;
double divisor = sumX * sumX - (double) Period * Period * (Period - 1) * (2 * Period - 1) / 6;
double sumXY = 0;
for (int count = 0; count < Period && CurrentBar - count >= 0; count++)
sumXY += count * Input[count];
y.Set(Input[0]);
double slope = ((double) Period * sumXY - sumX * SUM(y, Period)[0]) / divisor;
double intercept = (SUM(y, Period)[0] - slope * sumX) / Period;
double linReg = intercept + slope * (Period - 1);
double average = EMA(linReg, 20)[0];
// Calculate Standard Error
double sumSquares = 0;
for (int count = 0; count < Period && CurrentBar - count >= 0; count++)
{
double linRegX = intercept + slope * (Period - 1 - count);
double valueX = Input[count];
double diff = Math.Abs(valueX - linRegX);
sumSquares += diff * diff;
}
double stdErr = Math.Sqrt(sumSquares / Period);
Middle.Set(average);
Upper.Set(average + stdErr);
Lower.Set(average - stdErr);
}
#region Properties
/// <summary>
/// </summary>
[Description("Numbers of bars used for calculations")]
[GridCategory("Parameters")]
public int Period
{
get { return period; }
set { period = Math.Max(2, value); }
}
/// <summary>
/// Lower band of Standard Error
/// </summary>
[Browsable(false)]
[XmlIgnore]
public Data.DataSeries Lower
{
get { return Values[2]; }
}
[Browsable(false)]
[XmlIgnore]
public Data.DataSeries Middle
{
get { return Values[0]; }
}
/// <summary>
/// Upper band of Standard Error
/// </summary>
[Browsable(false)]
[XmlIgnore]
public Data.DataSeries Upper
{
get { return Values[1]; }
}
#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 StdErrorSmoothed2[] cacheStdErrorSmoothed2 = null;
private static StdErrorSmoothed2 checkStdErrorSmoothed2 = new StdErrorSmoothed2();
/// <summary>
/// EMA20 of Srandard Error Indicator
/// </summary>
/// <returns></returns>
public StdErrorSmoothed2 StdErrorSmoothed2(int period)
{
return StdErrorSmoothed2(Input, period);
}
/// <summary>
/// EMA20 of Srandard Error Indicator
/// </summary>
/// <returns></returns>
public StdErrorSmoothed2 StdErrorSmoothed2(Data.IDataSeries input, int period)
{
if (cacheStdErrorSmoothed2 != null)
for (int idx = 0; idx < cacheStdErrorSmoothed2.Length; idx++)
if (cacheStdErrorSmoothed2[idx].Period == period && cacheStdErrorSmoothed2[idx].EqualsInput(input))
return cacheStdErrorSmoothed2[idx];
lock (checkStdErrorSmoothed2)
{
checkStdErrorSmoothed2.Period = period;
period = checkStdErrorSmoothed2.Period;
if (cacheStdErrorSmoothed2 != null)
for (int idx = 0; idx < cacheStdErrorSmoothed2.Length; idx++)
if (cacheStdErrorSmoothed2[idx].Period == period && cacheStdErrorSmoothed2[idx].EqualsInput(input))
return cacheStdErrorSmoothed2[idx];
StdErrorSmoothed2 indicator = new StdErrorSmoothed2();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
indicator.Input = input;
indicator.Period = period;
Indicators.Add(indicator);
indicator.SetUp();
StdErrorSmoothed2[] tmp = new StdErrorSmoothed2[cacheStdErrorSmoothed2 == null ? 1 : cacheStdErrorSmoothed2.Length + 1];
if (cacheStdErrorSmoothed2 != null)
cacheStdErrorSmoothed2.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheStdErrorSmoothed2 = tmp;
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>
/// EMA20 of Srandard Error Indicator
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.StdErrorSmoothed2 StdErrorSmoothed2(int period)
{
return _indicator.StdErrorSmoothed2(Input, period);
}
/// <summary>
/// EMA20 of Srandard Error Indicator
/// </summary>
/// <returns></returns>
public Indicator.StdErrorSmoothed2 StdErrorSmoothed2(Data.IDataSeries input, int period)
{
return _indicator.StdErrorSmoothed2(input, period);
}
}
}
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// EMA20 of Srandard Error Indicator
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.StdErrorSmoothed2 StdErrorSmoothed2(int period)
{
return _indicator.StdErrorSmoothed2(Input, period);
}
/// <summary>
/// EMA20 of Srandard Error Indicator
/// </summary>
/// <returns></returns>
public Indicator.StdErrorSmoothed2 StdErrorSmoothed2(Data.IDataSeries input, 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.StdErrorSmoothed2(input, period);
}
}
}
#endregion
Comment