I was researching standard deviation and found something called skewness and kurtosis, so I thought I would display them in the indicator to see if they make sense.
I was able to display the standard deviation and variance well, but the skewness and kurtosis do not display well.
Only the same values 0 and -3.84 are displayed.
I don't know why, so I would appreciate it if someone could help me.
Code: Select all
using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
namespace PowerLanguage.Indicator
{
public class Kurtosis : IndicatorObject
{
private VariableSeries<double> m_sdev;
private VariableSeries<double> m_variance;
private VariableSeries<double> m_skewness;
private VariableSeries<double> m_kurtosis;
private VariableSeries<double> m_mean;
private IPlotObject Plot1;
private IPlotObject Plot2;
private IPlotObject Plot3;
private IPlotObject Plot4;
double[] m_values;
private ISeries<double> stdevprice { get; set; }
public Kurtosis(object ctx) :
base(ctx)
{
length = 14;
}
[Input]
public int length { get; set; }
protected override void Create()
{
m_sdev = new VariableSeries<double>(this);
m_variance = new VariableSeries<double>(this);
m_skewness = new VariableSeries<double>(this);
m_kurtosis = new VariableSeries<double>(this);
m_mean = new VariableSeries<double>(this);
//Plot1 =
// AddPlot(new PlotAttributes("Std_Dev", 0, Color.Yellow,
// Color.Empty, 0, 0, true));
//Plot2 =
// AddPlot(new PlotAttributes("Variance", 0, Color.Cyan,
// Color.Empty, 0, 0, true));
//Plot3 =
// AddPlot(new PlotAttributes("Skew", 0, Color.LightSalmon,
// Color.Empty, 0, 0, true));
Plot4 =
AddPlot(new PlotAttributes("Kurt", 0, Color.OrangeRed,
Color.Empty, 0, 0, true));
m_values = new double[length];
}
protected override void StartCalc()
{
stdevprice = Bars.Close;
}
protected override void CalcBar()
{
for (int i = 0; i < length; ++i)
{
m_values[i] = Bars.Close[i];
}
m_mean.Value = m_values.Average();
m_sdev.Value = m_values.StandardDeviation();
m_variance.Value = m_values.Variance();
m_skewness.Value = length / ((length - 1) * (length - 2)) * m_values.Select(x => Math.Pow((x - m_mean.Value) / m_sdev.Value, 3)).Sum();
m_kurtosis.Value =
(length * (length + 1)) / ((length - 1) * (length - 2) * (length - 3)) *
m_values.Select(x => Math.Pow((x - m_mean[0]) / m_sdev[0], 4)).Sum() - (3 * Math.Pow(length - 1, 2)) / ((length - 2) * (length - 3));
//Plot1.Set(0, m_sdev[0]);
//Plot2.Set(0, m_variance[0]);
//Plot3.Set(0, m_skewness.Value);
Plot4.Set(0, m_kurtosis[0]);
}
}
}