Real author:
Sergey Bochkanov. ALGLIB project website – The library dates back to 1999.
Note: The Expert Advisor works with MetaTrader 4 build 555 and higher.
The archive must be unpacked to: terminal_data_folder.The codes of the library are located in terminal_data_folder\Math\Alglib\
Examples of test scripts are located in terminal_data_folder\MQL4\Scripts\Alglib\
ALGLIB is one of the largest and most complete mathematical libraries
Do you need to make a fast Fourier transform or to solve a differential equation system? Do you perform a complex data analysis trying to gather all methods in one place as a source code? Then ALGLIB library of numerical methods is for you!
ALGLIB is currently one of the best libraries of the multi-language algorithms. Below are ALGLIB features mentioned on the official website:
ALGLIB is a cross-platform numerical analysis and data processing library. It supports several programming languages (C++, C#, Pascal, VBA) and several operating systems (Windows, Linux, Solaris). ALGLIB features include:
- Linear algebra (direct algorithms, EVD/SVD)
- Solvers (linear and nonlinear)
- Interpolation
- Optimization
- Fast Fourier transforms
- Numerical integration
- Linear and nonlinear least-squares fitting
- Ordinary differential equations
- Special functions
- Statistics (descriptive statistics, hypothesis testing)
- Data analysis (classification/regression, including neural networks)
- Multiple precision versions of linear algebra, interpolation optimization and others algorithms (using MPFR for floating point computations)
Why to choose ALGLIB? Because it is:
- portable. It can be compiled almost anywhere using almost any compiler (see compatibility matrix for more info).
- easy to use. It supports many programming languages. If you use one language, you don’t need to study another (FORTRAN, for example) to compile and link an external library.
- open source. It can be used for free under GPL 2+.
- suited for commercial users too. Those who want to use ALGLIB in commercial applications can buy commercial license without copyleft requirement.
ALGLIB library is being constantly enhanced, new functions and improvements according to users’ comments are implemented regularly. The latest version is 3.6.0.
Besides, the library contains the large collection of test cases covering the major part of the proposed methods’ functionality. That will allow you to carry the tests and report detected errors to the project’s authors.
CAlglib class static functions should be used to work with the library – all library functions are moved to CAlglib system class as static functions.
testclasses.mq4 and testinterfaces.mq5 test cases scripts are attached together with a simple usealglib.mq4 demo script. Include files of the same name (testclasses.mqh and testinterfaces.mqh) are used to launch test cases. They must be placed to \MQL4\Scripts\Alglib\Testcases\.
Note: testclasses.mq4 script execution takes quite a long time (about 30 minutes).
Below is more detailed information about ALGLIB MQL4 ported library packages:
Packages |
Description |
---|---|
alglib.mqh |
The main library package includes custom functions. These functions should be called for working with the library. |
alglibinternal.mqh |
Additional classes for the operation of other library packages. |
alglibmisc.mqh | The package contains the following classes:
|
ap.mqh | The package also contains additional classes necessary for other packages. |
bitconvert.mqh | Π‘++ basic classes and functions that are absent in MQL4 as built-in:
|
complex.mqh | Defining complex structures for working with complex numbers. |
dataanalysis.mqh | Classes of data analysis:
|
delegatefunctions.mqh | The package contains the classes created as replacement for the delegates. The objects of these classes are the functions optimized in several library methods. |
diffequations.mqh | The class for solving ordinary differential equations:
|
fasttransforms.mqh | Fast transformation classes:
|
integration.mqh | Classes for numerical integration:
|
interpolation.mqh | Interpolation, approximation and numerical differentiation classes:
|
linalg.mqh | Classes for calculation of some linear algebra operations:
|
matrix.mqh | Classes of matrices: integer, continuous and complex. |
optimization.mqh | Dimensional and multidimensional optimization classes:
|
solvers.mqh | Classes for solving linear and non-linear equation systems:
|
specialfunctions.mqh | Classes of distribution functions, integrals and polynomials:
|
statistics.mqh | Classes of statistical data analysis:
|
Code:
Library functions have detailed comments on their usage.
//+------------------------------------------------------------------+ //| Calculation of the distribution moments: mean, variance,Β Β Β Β Β Β Β Β | //| skewness, kurtosis.Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //| INPUT PARAMETERS:Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //|Β Β Β Β XΒ Β Β Β Β Β -Β Β sampleΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //|Β Β Β Β NΒ Β Β Β Β Β -Β Β N>=0, sample size:Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //|Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β * if given, only leading N elements of X areΒ Β Β Β | //|Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β processedΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //|Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β * if not given, automatically determined fromΒ Β Β Β | //|Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β size of XΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //| OUTPUT PARAMETERSΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //|Β Β Β Β MeanΒ Β Β Β -Β Β mean.Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //|Β Β Β Β Variance-Β Β variance.Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //|Β Β Β Β Skewness-Β Β skewness (if variance<>0; zero otherwise).Β Β Β Β Β Β | //|Β Β Β Β Kurtosis-Β Β kurtosis (if variance<>0; zero otherwise).Β Β Β Β Β Β | //+------------------------------------------------------------------+ static bool CBaseStat::SampleMoments(const double &cx[],const int n,double &mean, Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β double &variance,double &skewness,double &kurtosis) Β Β { //--- check Β Β if(!CAp::Assert(n>=0,__FUNCTION__+": the error variable")) Β Β Β Β Β Β return(false); //--- check Β Β if(!CAp::Assert(CAp::Len(cx)>=n,__FUNCTION__+": length(x)<n")) Β Β Β Β Β Β return(false); //--- check Β Β if(!CAp::Assert(CApServ::IsFiniteVector(cx,n),__FUNCTION__+": x is not finite vector")) Β Β Β Β Β Β return(false); //--- create variables Β Β double v=0; Β Β double v1=0; Β Β double v2=0; Β Β double stddev=0; //--- Init, special case 'N=0' Β Β mean=0; Β Β variance=0; Β Β skewness=0; Β Β kurtosis=0; //--- check Β Β if(n<=0) Β Β Β Β Β Β return(true); //--- Mean Β Β for(int i=0;i<n;i++) Β Β Β Β Β Β mean+=cx[i]; Β Β mean/=n; //--- Variance (using corrected two-pass algorithm) Β Β if(n!=1) Β Β Β Β { Β Β Β Β Β Β //--- calculation Β Β Β Β Β Β for(int i=0;i<n;i++) Β Β Β Β Β Β Β Β v1+=CMath::Sqr(cx[i]-mean); Β Β Β Β Β Β for(int i=0;i<n;i++) Β Β Β Β Β Β Β Β v2+=cx[i]-mean; Β Β Β Β Β Β v2=CMath::Sqr(v2)/n; Β Β Β Β Β Β variance=(v1-v2)/(n-1); Β Β Β Β Β Β //--- calculation Β Β Β Β Β Β stddev=MathSqrt(variance); Β Β Β Β } Β Β else Β Β Β Β Β Β variance=EMPTY_VALUE; //--- Skewness and kurtosis Β Β if(stddev!=0) Β Β Β Β { Β Β Β Β Β Β //--- calculation Β Β Β Β Β Β for(int i=0;i<n;i++) Β Β Β Β Β Β Β Β { Β Β Β Β Β Β Β Β v=(cx[i]-mean)/stddev; Β Β Β Β Β Β Β Β v2=CMath::Sqr(v); Β Β Β Β Β Β Β Β skewness+=v2*v; Β Β Β Β Β Β Β Β kurtosis+=CMath::Sqr(v2); Β Β Β Β Β Β Β Β } Β Β Β Β Β Β //--- change values Β Β Β Β Β Β skewness=skewness/n; Β Β Β Β Β Β kurtosis=kurtosis/n-3; Β Β Β Β } //--- successful execution Β Β return(true); Β Β }
CAlglib class static functions should be used to work with the library. Below is usealglib.mq5 function script’s source code for calculation of some statistical trading parameters:
//+------------------------------------------------------------------+ //|Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β UseAlglib.mq4 | //|Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Copyright 2012, MetaQuotes Software Corp. | //|Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //+------------------------------------------------------------------+ #property copyright "Copyright 2012, MetaQuotes Software Corp." #property linkΒ Β Β Β Β Β "" #property versionΒ Β "1.00" #property strict //+------------------------------------------------------------------+ //| Include librariesΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //+------------------------------------------------------------------+ #include <Math\Alglib\alglib.mqh> #include <Arrays\ArrayDouble.mqh> //+------------------------------------------------------------------+ //| Script program start functionΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | //+------------------------------------------------------------------+ void OnStart() Β Β { //--- object for storage of profit/loss data for each order Β Β CArrayDouble *profit=new CArrayDouble; //--- object for storage of balance data Β Β CArrayDouble *balance_total=new CArrayDouble; //--- initial balance Β Β doubleΒ Β Β Β Β Β Β Β balance=0; //--- historical data request //--- get total amount ot orders Β Β int orders_total=OrdersHistoryTotal(); //--- get profit data Β Β for(int i=0;i<orders_total;i++) Β Β Β Β { Β Β Β Β Β Β if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) Β Β Β Β Β Β Β Β { Β Β Β Β Β Β Β Β Print("Error in OrderSelect (",GetLastError(),")"); Β Β Β Β Β Β Β Β return; Β Β Β Β Β Β Β Β } Β Β Β Β Β Β int order_type=OrderType(); Β Β Β Β Β Β //--- initial balance Β Β Β Β Β Β if(order_type==6) // OP_BALANCE=6 Β Β Β Β Β Β Β Β { Β Β Β Β Β Β Β Β if(NormalizeDouble(OrderProfit()+OrderSwap(),2)>=0.0) Β Β Β Β Β Β Β Β Β Β Β Β if(balance==0.0) Β Β Β Β Β Β Β Β Β Β Β Β Β Β balance=OrderProfit(); Β Β Β Β Β Β Β Β } Β Β Β Β Β Β //--- add order profit Β Β Β Β Β Β if(order_type==OP_BUY || order_type==OP_SELL) Β Β Β Β Β Β Β Β { Β Β Β Β Β Β Β Β double order_profit=OrderProfit()+OrderSwap()+OrderCommission(); Β Β Β Β Β Β Β Β profit.Add(NormalizeDouble(order_profit,2)); Β Β Β Β Β Β Β Β balance_total.Add(balance); Β Β Β Β Β Β Β Β balance=balance+NormalizeDouble(order_profit,2); Β Β Β Β Β Β Β Β } Β Β Β Β } //--- Β Β balance_total.Add(balance_total.At(balance_total.Total()-1)+profit.At(balance_total.Total()-1)); //--- copy balance data to the array of double type Β Β double arr_balance[]; Β Β ArrayResize(arr_balance,balance_total.Total()); Β Β for(int i=0;i<balance_total.Total();i++) Β Β Β Β Β Β arr_balance[i]=balance_total.At(i); //--- copy profit data to the array of double type Β Β double arr_profit[]; Β Β ArrayResize(arr_profit,profit.Total()); Β Β for(int i=0;i<profit.Total();i++) Β Β Β Β Β Β arr_profit[i]=profit.At(i); //--- linear regression //--- number of independent variables Β Β int nvars=1; //--- number of points Β Β int npoints=balance_total.Total(); //--- creating parameters matrix, used for linear regression Β Β CMatrixDouble xy(npoints,nvars+1); Β Β for(int i=0;i<npoints;i++) Β Β Β Β { Β Β Β Β Β Β xy[i].Set(0,i); Β Β Β Β Β Β xy[i].Set(1,arr_balance[i]); Β Β Β Β } //--- variable for detecting calculations result (successful, unsuccessful) Β Β int info; //--- class objects necessary for storing data on calculations Β Β CLinearModelShell lm; Β Β CLRReportShellΒ Β Β Β ar; //--- arrays for storing regression results Β Β double lr_coeff[]; Β Β double lr_values[]; Β Β ArrayResize(lr_values,npoints); //--- calculate linear regression coefficients Β Β CAlglib::LRBuild(xy,npoints,nvars,info,lm,ar); //--- get linear regression coefficients Β Β CAlglib::LRUnpack(lm,lr_coeff,nvars); //--- get the recovered linear regression values Β Β for(int i=0;i<npoints;i++) Β Β Β Β Β Β lr_values[i]=lr_coeff[0]*i+lr_coeff[1]; //--- calculation of Expected Payoff Β Β double exp_payoff,tmp1,tmp2,tmp3; Β Β CAlglib::SampleMoments(arr_profit,exp_payoff,tmp1,tmp2,tmp3); //--- calculation of HPR array Β Β double HPR[]; Β Β ArrayResize(HPR,balance_total.Total()-1); Β Β for(int i=0;i<balance_total.Total()-1;i++) Β Β Β Β Β Β HPR[i]=balance_total.At(i+1)/balance_total.At(i); //--- calculation of the standard deviation and mean of the HPR Β Β double AHPR,SD; Β Β CAlglib::SampleMoments(HPR,AHPR,SD,tmp2,tmp3); Β Β SD=MathSqrt(SD); //--- calculation of LR Correlation Β Β double lr_corr=CAlglib::PearsonCorr2(arr_balance,lr_values); //--- calculation of LR Standard Error Β Β double lr_stand_err=0; Β Β for(int i=0;i<npoints;i++) Β Β Β Β { Β Β Β Β Β Β double delta=MathAbs(arr_balance[i]-lr_values[i]); Β Β Β Β Β Β lr_stand_err=lr_stand_err+delta*delta; Β Β Β Β } Β Β lr_stand_err=MathSqrt(lr_stand_err/(npoints-2)); //--- calculation of Sharpe Ratio Β Β double sharpe_ratio=(AHPR-1)/SD; //--- print Β Β PrintFormat("-----------------------------------------------"); Β Β PrintFormat("Linear regression: y = %.2fx + %.2f",lr_coeff[0],lr_coeff[1]); //--- parameters Β Β PrintFormat("Expected Payoff = %.2f",exp_payoff); Β Β PrintFormat("AHPR = %.4f",AHPR); Β Β PrintFormat("Sharpe Ratio = %.2f",sharpe_ratio); Β Β PrintFormat("LR Correlation = %.2f",lr_corr); Β Β PrintFormat("LR Standard Error = %.2f",lr_stand_err); Β Β PrintFormat("-----------------------------------------------"); //--- delete objects Β Β delete profit; Β Β delete balance_total; Β Β } //+------------------------------------------------------------------+
We will receive the following result (depening on results of your trading):
/ru/code/11077