使用计量经济学方法分析图表

30 十二月 2013, 08:35
2
1 319

K. Boulding

1. 时间序列的特性

3. 回报指标

• Pt - 是在时间 t
处的价格序列的值；
• Pt-1 - 是在时间 t-1
处的价格序列的值；
• pt = ln(Pt)  - 是自然对数 Pt

```//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,      // size of the array price[]
const int prev_calculated,  // number of bars available at the previous call
const int begin,            // index of the array price[] the reliable data starts from
const double& price[])      // array for the calculation itself

{
//---
int start;
if(prev_calculated<2) start=1;  // start filling ReturnsBuffer[] from the 1-st index, not 0.
else start=prev_calculated-1;   // set 'start' equal to the last index in the arrays
for(int i=start;i<rates_total;i++)
{
ReturnsBuffer[i]=MathLog(price[i]/price[i-1]);
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+```

4. 统计检验

```class Complex
{
public:
double            re,im; //re -real component of the complex number, im - imaginary
public:
void              Complex(){};                                   //default constructor
void              setComplex(double rE,double iM){re=rE; im=iM;}; //set method (1-st variant)
void              setComplex(double rE){re=rE; im=0;};           //set method (2-nd variant)
void              ~Complex(){};                                  //destructor
void              opEqual(const Complex &y){re=y.re;im=y.im;};   //operator=
void              opPlus(const Complex &x,const Complex &y);      //operator+
void              opPlusEq(const Complex &y);                    //operator+=
void              opMinus(const Complex &x,const Complex &y);     //operator-
void              opMult(const Complex &x,const Complex &y);      //operator*
void              opMultEq(const Complex &y);                    //operator*= (1-st variant)
void              opMultEq(const double y);                       //operator*= (2-nd variant)
void              conjugate(const Complex &y);                   //conjugation of complex numbers
double            norm();                                       //normalization
};```

1. getComplexArr（从一个复数数组返回一个二维实数数组）；
2. setComplexArr（从一个一维实数数组返回一个复数数组）；
3. setComplexArr2（从一个二维实数数组返回一个复数数组）。

```class CFFT
{
public:
Complex           Input[];  //input array of complex numbers
Complex           Output[]; //output array of complex numbers
public:
bool              Forward(const uint N);                                   //direct Fourier transformation
bool              InverseT(const uint N,const bool Scale=true);            //weighted reverse Fourier transformation
bool              InverseF(const uint N,const bool Scale=false);           //non-weighted reverse Fourier transformation
void              setCFFT(Complex &data1[],Complex &data2[],const uint N); //set method(1-st variant)
void              setCFFT(Complex &data1[],Complex &data2[]);              //set method (2-nd variant)
protected:
void              Rearrange(const uint N);                                 // regrouping
void              Perform(const uint N,const bool Inverse);                // implementation of transformation
void              Scale(const uint N);                                     // weighting
};```

`int nFFT=pow(2,nextpow2(ArraySize(res))+1);      //power rate of two`

```void autocorr(double &ACF[],double &res[],int nLags)  //1-st variant of function
/* selective autocorrelation function (ACF)  for unidimensional stochastic time series
ACF - output array of calculated values of the autocorrelation function;
res - array of observation of stochastic time series;
nLags - maximum number of lags the ACF is calculated for.
*/
{
Complex Data1[],Data21[], //input arrays of complex numbers
Data2[],Data22[],         //output arrays of complex numbers
cData[];                  //array of conjugated complex numbers
double rA[][2];           //auxiliary two-dimensional array of real numbers

int nFFT=pow(2,nextpow2(ArraySize(res))+1);      //power rate of two
ArrayResize(rA,nFFT);ArrayResize(Data1,nFFT);    //correction of array sizes
ArrayResize(Data2,nFFT);ArrayResize(Data21,nFFT);
ArrayResize(Data22,nFFT);ArrayResize(cData,nFFT);

double rets1[];                   //an auxiliary array for observing the series
double m=mean(res);               //arithmetical mean of the array res
ArrayResize(rets1,nFFT);          //correction of array size
for(int t=0;t<ArraySize(res);t++) //copy the initial array of observation
// to the auxiliary one with correction by average
rets1[t]=res[t]-m;
setComplexArr(Data1,rets1); //set input array of complex numbers
CFFT F,F1;                  //initialize instances of the CFFT class
F.setCFFT(Data1,Data2);     //initialize data-members for the instance F
F.Forward(nFFT);            //perform direct Fourier transformation
for(int i=0;i<nFFT;i++)
{
Data21[i].opEqual(F.Output[i]);//assign the values of the F.Output array to the Data21 array;
cData[i].conjugate(Data21[i]); //perform conjugation for the array Data21
Data21[i].opMultEq(cData[i]);  //multiplication of the complex number by the one adjacent to it
//results in a complex number that has only real component not equal to zero
}
F1.setCFFT(Data21,Data22);   //initialize data-members for the instance F1
F1.InverseT(nFFT);           //perform weighter reverse Fourier transformation
getComplexArr(rA,F1.Output); //get the result in double format after
//weighted reverse Fourier transformation
for(int i=0;i<nLags+1;i++)
{
ACF[i]=rA[i][0];          //in the output ACF array save the calculated values
//of autocorrelation function
ACF[i]=ACF[i]/rA[0][0];   //normalization relatively to the first element
}
}```

```void lbqtest(bool &H[],double &rets[])
/* Function that implements the Q test of Ljung-Box-Pierce
H - output array of logic values, that confirm or disprove
the zero hypothesis on the specified lag;
rets - array of observations of the stochastic time series;
*/
{
double lags[3]={10.0,15.0,20.0};       //specified lags
int maxLags=20;                        //maximum number of lags
double ACF[]; ArrayResize(ACF,21);     //epmty ACF array
double acf[]; ArrayResize(acf,20);     //alternate ACF array
autocorr(ACF,rets,maxLags);            //calculated ACF array
for(int i=0;i<20;i++) acf[i]=ACF[i+1]; //remove the first element - one, fill
//alternate array
double alpha[3]={0.05,0.05,0.05};      //array of levels of significance of the test

/*Calculation of array of Q statistics for selected lags according to the formula:
L
|----|
\
Q = T(T+2) || (rho(k)^2/(T-k)),
/
|----|
k=1
where: T is range, L is the number of lags, rho(k) is the value of ACF at the k-th lag.
*/

double idx[]; ArrayResize(idx,maxLags);     //auxiliary array of indexes
int len=ArraySize(rets);                    //length of the array of observations
int arrLags[];ArrayResize(arrLags,maxLags); //auxiliary array of lags
double stat[]; ArrayResize(stat,maxLags);   //array of Q statistics
double sum[]; ArrayResize(sum,maxLags);     //auxiliary array po sums
double iACF[];ArrayResize(iACF,maxLags);    //auxiliary ACF array
for(int i=0;i<maxLags;i++)
{                                         //fill:
arrLags[i]=i+1;                          //auxiliary array of lags
idx[i]=len-arrLags[i];                   //auxiliary array of indexes
iACF[i]=pow(acf[i],2)/idx[i];            //auxiliary ACF array
}
cumsum(sum,iACF);                           //sum the auxiliary ACF array
//by progressive total
for(int i=0;i<maxLags;i++)
stat[i]=sum[i]*len*(len+2);              //fill the array Q statistics
double stat1[];                             //alternate of the array of Q statistics
ArrayResize(stat1,ArraySize(lags));
for(int i=0;i<ArraySize(lags);i++)
stat1[i]=stat[lags[i]-1];                //fill the alternate array of specified lags

double pValue[ArraySize(lags)];             //array of 'p' values
for(int i=0;i<ArraySize(lags);i++)
{
pValue[i]=1-gammp(lags[i]/2,stat1[i]/2); //calculation of 'p' values
H[i]=alpha[i]>=pValue[i];                //estimation of zero hypothesis
}
}
```

结论

#
文件
路径
1
2
3
4

1. Analysis of Financial Time Series, Ruey S. Tsay, 2nd Edition, 2005. - 638 pp.
2. Applied Econometric Time Series,Walter Enders, John Wiley & Sons, 2nd Edition, 1994. - 448 pp.
3. Bollerslev, T., R. F. Engle, and D. B. Nelson. "ARCH Models."Handbook of Econometrics. Vol. 4, Chapter 49, Amsterdam:Elsevier Science B.V.
4. Box, G. E. P., G. M. Jenkins, and G. C. Reinsel. Time Series Analysis:Forecasting and Control.3rd ed. Upper Saddle River, NJ:Prentice-Hall, 1994.
5. Numerical Recipes in C, The Art of Scientific Computing, 2nd Edition, W.H. Press, B.P. Flannery, S. A. Teukolsky, W. T. Vetterling, 1993. - 1020 pp.
6. Gene H. Golub, Charles F. Van Loan.Matrix computations, 1999.
7. Porshnev S. V. "Computing mathematics.Series of lectures", S.Pb, 2004.

complex_class.mqh (5.86 KB)
fft_class.mqh (6.56 KB)
garchtest.mq5 (24.67 KB)
garchtest_html.mq5 (22.81 KB)

| 30 11月 2014 在 08:14

| 2 9月 2015 在 09:59

MQL5 向导：如何创建交易信号模块

MQL5 向导：如何创建风险和资金管理模块

MQL5 向导的交易策略生成器极大简化了交易理念的检验过程。本文介绍了如何创建自定义风险和资金管理模块以及如何在 MQL5 向导中启用该模块。我们将使用一个资金管理算法作为示例，在该算法中交易量规模取决于上一笔交易的结果。本文还将介绍为 MQL5 向导创建的类的说明的结构和格式。