Statistical_characteristics

Author: Copyright © 2017, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Statistical_characteristics
ÿþ//+------------------------------------------------------------------+

//|                                  Statistical characteristics.mq5 |

//|                              Copyright © 2017, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2017, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.00"

#property description "@8<5=5=85 AB0=40@B=>9 181;8>B5:8 !B0B8AB8:0: !B0B8AB8G5A:85 E0@0:B5@8AB8:8"

#property description "The use of standard library statistics: Statistical characteristics"

//--- ?>4:;NG05< DC=:F88 4;O @0AG5B0 =>@<0;L=>3> @0A?@545;5=8O

//--- include the functions for calculating the normal distribution

#include <Math\Stat\Normal.mqh>

#property indicator_separate_window 

#property indicator_buffers 4 

#property indicator_plots   1 

//--- plot Histogram 

#property indicator_label1  "Method Of Stat" 

#property indicator_type1   DRAW_LINE 

#property indicator_color1  clrBlue 

#property indicator_style1  STYLE_SOLID 

#property indicator_width1  1 

//--- indicator buffers 

double         meanBuffer[];                 // A@54=55 7=0G5=85 (1-K9 <><5=B)      // the mean (first moment)

double         varianceBuffer[];             // 48A?5@A8O (2->9 <><5=B)             // the variance (second moment)

double         skewnessBuffer[];             // :>MDD8F85=B 0A8<<5B@88 (3-89 <><5=B)// the skewness (third moment)

double         kurtosisBuffer[];             // :>MDD8F85=B M:AF5AA0 (4-K9 <><5=B)  // the kurtosis (fourth moment)

//--- method of stat 

enum methodOfStat

  {

   mean=0,        // A@54=55 7=0G5=85, mean 

   variance=1,    // 48A?5@A8O, variance 

   skewness=2,    // :>MDD8F85=B 0A8<<5B@88, skewness 

   kurtosis=3,    // :>MDD8F85=B M:AF5AA0, kurtosis 

  };

//--- input parameters

input int            Inpcount=10;            // :>;8G5AB2> M;5<5=B>2, the number of elements 

input methodOfStat   Inpmethod=mean;         // <5B>4, method

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- =N0=A: 1CD5@ "INDICATOR_DATA" 4>;65= 8<5BL 8=45:A "0"

//--- caveat: buffer "INDICATOR_DATA" must have an index of "0"

   if(Inpmethod==mean)

     {

      SetIndexBuffer(0,meanBuffer,INDICATOR_DATA);

      SetIndexBuffer(1,varianceBuffer,INDICATOR_CALCULATIONS);

      SetIndexBuffer(2,skewnessBuffer,INDICATOR_CALCULATIONS);

      SetIndexBuffer(3,kurtosisBuffer,INDICATOR_CALCULATIONS);

     }

   if(Inpmethod==variance)

     {

      SetIndexBuffer(0,varianceBuffer,INDICATOR_DATA);

      SetIndexBuffer(1,meanBuffer,INDICATOR_CALCULATIONS);

      SetIndexBuffer(2,skewnessBuffer,INDICATOR_CALCULATIONS);

      SetIndexBuffer(3,kurtosisBuffer,INDICATOR_CALCULATIONS);

     }

   if(Inpmethod==skewness)

     {

      SetIndexBuffer(0,skewnessBuffer,INDICATOR_DATA);

      SetIndexBuffer(1,meanBuffer,INDICATOR_CALCULATIONS);

      SetIndexBuffer(2,varianceBuffer,INDICATOR_CALCULATIONS);

      SetIndexBuffer(3,kurtosisBuffer,INDICATOR_CALCULATIONS);

     }

   if(Inpmethod==kurtosis)

     {

      SetIndexBuffer(0,kurtosisBuffer,INDICATOR_DATA);

      SetIndexBuffer(1,meanBuffer,INDICATOR_CALCULATIONS);

      SetIndexBuffer(2,varianceBuffer,INDICATOR_CALCULATIONS);

      SetIndexBuffer(3,skewnessBuffer,INDICATOR_CALCULATIONS);

     }

//--- :>@>B:>5 =08<5=>20=85 8=48:0B>@0 

//--- name for DataWindow and indicator subwindow label 

   IndicatorSetString(INDICATOR_SHORTNAME,EnumToString(Inpmethod)+"("+IntegerToString(Inpcount)+")");

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//--- check for rates total

   if(rates_total<Inpcount)

      return(0); // not enough bars for calculation

   int limit=Inpcount;

   if(prev_calculated==0)

      for(int i=0;i<Inpcount;i++)

        {

         meanBuffer[i]=0.0;

         varianceBuffer[i]=0.0;

         skewnessBuffer[i]=0.0;

         kurtosisBuffer[i]=0.0;

        }

   else

      limit=prev_calculated;

//---

   for(int i=limit;i<rates_total;i++)

     {

      double mean=0.0;

      double variance=0.0;

      double skewness=0.0;

      double kurtosis=0.0;

      //--- MathMoments: 00AAG8BK205B ?5@2K5 4 <><5=B0 (A@54=55, 48A?5@A8O, :>MDD8F85=B 0A8<<5B@88, :>MDD8F85=B M:AF5AA0) M;5<5=B>2 <0AA820

      //--- MathMoments: calculates the first 4 moments (mean, variance, skewness, kurtosis) of array elements

      if(MathMoments(close,mean,variance,skewness,kurtosis,i-Inpcount,Inpcount))

        {

         meanBuffer[i]=mean;

         varianceBuffer[i]=variance;

         skewnessBuffer[i]=skewness;

         kurtosisBuffer[i]=kurtosis;

        }

     }

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+

Comments