Skewness, Kurtosis indicator  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
eunos64
Posts: 38
Joined: 15 Sep 2013
Has thanked: 13 times
Been thanked: 3 times

Skewness, Kurtosis indicator

Postby eunos64 » 23 Aug 2023

Hello.
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.
スクリーンショット 2023-08-23 200453.png
(25.71 KiB) Not downloaded yet

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]); } } }

eunos64
Posts: 38
Joined: 15 Sep 2013
Has thanked: 13 times
Been thanked: 3 times

Re: Skewness, Kurtosis indicator

Postby eunos64 » 23 Aug 2023

I saw an article on the web that Math.net.Numerics is useful, but I installed it in VisualStudio 2022 with NuGet, and when I try to declare the use of a namespace that contains the extended method I want to use in the "using" directive, Math.Net followed by "using" is not suggested as a candidate in Visualstudio's Intellisense. Is it not working?
Thank you in advance for your help.

HellGhostEvocatorX
Posts: 80
Joined: 10 Feb 2022
Has thanked: 52 times
Been thanked: 11 times

Re: Skewness, Kurtosis indicator  [SOLVED]

Postby HellGhostEvocatorX » 27 Aug 2023

Good day.

I added the example output windows to your code. Use this to understand your calculation. Unfortunately, I'm no math genius and don't know how the correct calculation of kurtosis looks like. But if you think the result is wrong, the calculation will probably be wrong.
Maybe the struct.double m_values ​​is also the problem?

What I have definitely noticed before is that the first part of your calculation is always zero why this is so was also not clear to me on a quick glance, but if I change the code as follows, the first part is no longer zero, whether I don't know if the total is correct now. However, it is also a good idea to pack the calculations into methods and individual calculations. Lately I've had problems with calculations in the calcBar method and have now started to write individual methods and only call the methods in the calcBar.

Code: Select all

using System; using System.Drawing; using System.Linq; //https://www.multicharts.com/discussion/viewtopic.php?t=54038 namespace PowerLanguage.Indicator { public class Kurtosis : IndicatorObject { private VariableSeries<double> m_sdev; private VariableSeries<double> m_variance; private VariableSeries<double> m_skewness; private VariableObject<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 VariableObject<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]; Output.WriteLine("m_values-----{0},{1}", m_values[i], Bars.FullSymbolData.Current); } Output.WriteLine("m_valuesxxx-----{0},{1}", m_values[0], Bars.FullSymbolData.Current); 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(); Output.WriteLine("m_mean-----{0},{1}", m_mean.Value, Bars.FullSymbolData.Current); Output.WriteLine("m_sdev-----{0},{1}", m_sdev.Value, Bars.FullSymbolData.Current); Output.WriteLine("length-----{0},{1}", length, Bars.FullSymbolData.Current); double xx = length * (length + 1); double yy = (length - 1) * (length - 2) * (length - 3); double zz = xx / yy; m_kurtosis.Value = (zz * 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))); Output.WriteLine("m_kurtosis-----{0},{1}", m_kurtosis.Value, Bars.FullSymbolData.Current); //Plot1.Set(0, m_sdev[0]); //Plot2.Set(0, m_variance[0]); //Plot3.Set(0, m_skewness.Value); Plot4.Set(0, m_kurtosis.Value); } } }

eunos64
Posts: 38
Joined: 15 Sep 2013
Has thanked: 13 times
Been thanked: 3 times

Re: Skewness, Kurtosis indicator

Postby eunos64 » 29 Aug 2023

Thank you HellGhostEvocatorX !!
It worked !!! :D
Mmmm, I didn't know there was a way to do this.
I had checked the contents of m_values to make sure everything was OK, but I couldn't imagine that the "length * (length + 1);" calculation wasn't working.
I'm not sure why, but it worked anyway, so thanks for your help.
Next time I will try to Evoke method X from the hellish CalcBar () as well.
I don't know if this indicator makes sense, but I will have uploaded it to "User Contributed Studies".

By the way, I looked up the formula on Wikipedia and had ChatGPT write the code for me.


Return to “MultiCharts .NET”