... Log in Sign up >> WebTerminal Documentation Calendar CodeBase Articles Freelance Market

MetaTrader 5 / Libraries

ALGLIB ‐ Library ‐ library for MetaTrader 5

Sergey Bochkanov 22947 (98)

Real author:

Sergey Bochkanov. ALGLIB project website ‐ http://www.alglib.net/. The library dates back to 1999. 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, , 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 (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.mq5 and testinterfaces.mq5 test cases scripts are attached together with a simple usealglib.mq5 demo script. Include files of the same name (testclasses.mqh and testinterfaces.mqh) are used to launch test cases. They should be placed to \MQL5\Scripts\Alglib\Testcases\.

Note: testclasses.mq5 script execution takes quite a long time (about 30 minutes).

Below is more detailed information about ALGLIB MQL5 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:

1. CHighQualityRand ‐ high quality random number generator. 2. CNearestNeighbor ‐ KD‐trees generation and solution.

ap.mqh The package also contains additional classes necessary for other packages.

bitconvert.mqh С++ basic classes and functions that are absent in MQL5 as built‐in:

1. BitConverter ‐ class for transforming int and double type figures into an array of bits and vice Packages Description versa. 2. CInfOrNaN ‐ NaNs and plus/minus infinity verification and generation class. 3. ArrayReverse ‐ array elements reverse function. 4. GetSelectionString ‐ function for receiving the string generated from char type array elements. 5. MathSign ‐ number sign calculation function. 6. MathSinh ‐ hyperbolic sine calculation function. 7. MathCosh ‐ hyperbolic sine calculation function. 8. MathTanh ‐ hyperbolic tangent calculation function. complex.mqh Defining complex structures for working with complex numbers. dataanalysis.mqh Classes of data analysis:

1. CBdSS ‐ error functions calculation. 2. CDForest ‐ working with the forests of solution trees. 3. CKMeans ‐ clustering using k‐means++ algorithm. 4. CLDA ‐ linear discriminant analysis. 5. CLinReg ‐ linear regression. 6. CMLPBase ‐ multilayer perceptron (neural networks). 7. CLogit ‐ multinomial logit regression. 8. CMarkovCPD ‐ Markov chains for population data. 9. CMLPTrain ‐ training of multilayer perceptron. 10. CMLPE ‐ ensembles of neural networks. 11. CPCAnalysis ‐ principal components method. 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:

1. CODESolver ‐ solving ordinary differential equations. fasttransforms.mqh Fast transformation classes:

1. CFastFourierTransform ‐ fast Fourier transform. 2. CConv ‐ convolution. 3. CCorr ‐ cross‐correlation. 4. CFastHartleyTransform ‐ fast Hartley transform. integration.mqh Classes for numerical integration:

1. CGaussQ ‐ Gaussian quadrature. 2. CGaussKronrodQ ‐ Gauss–Kronrod quadrature formulas. 3. CAutoGK ‐ adaptive integrator. interpolation.mqh Interpolation, approximation and numerical differentiation classes:

1. CIDWInt ‐ interpolation and approximation by the inverse average weighted distance. 2. CRatInt ‐ rational interpolation. 3. CPolInt ‐ polynomial interpolation. 4. CSpline1D ‐ one‐dimensional spline interpolation. 5. CLSFit ‐ approximation using linear or non‐ method. 6. CPSpline ‐ parametric spline interpolation. 7. CSpline2D ‐ two‐dimensional spline interpolation. linalg.mqh Classes for calculation of some linear algebra operations:

1. COrtFac ‐ QR/LQ decompositions, decompositions of Hessenberg, as well as of bi‐ and tridiagonal matrices. 2. CEigenVDetect ‐ finding the eigenvalues ​​and eigenvectors. 3. CMatGen ‐ random matrices generation. 4. CTrFac ‐ LU and Cholesky decompositions. 5. CRCond ‐ estimation of a matrix dependence value. 6. CMatInv ‐ matrix inversion. 7. CBdSingValueDecompose ‐ singular decomposition of a bidiagonal matrix. 8. CSingValueDecompose ‐ matrix singular decomposition. 9. CFbls ‐ quick basic linear solutions. 10. CMatDet ‐ calculation of a matrix determinant. 11. CSpdGEVD ‐ finding the eigenvalues ​​and eigenvectors in generalized symmetric matrices. 12. CInverseUpdate ‐ matrices inversion and update. 13. CSchur ‐ Issai Schur decomposition. matrix.mqh Classes of matrices: integer, continuous and complex. optimization.mqh Dimensional and multidimensional optimization classes:

1. CMinCG ‐ optimization using conjugate gradient method. 2. CMinBLEIC ‐ optimization with linear constraints in the form of equations and inequations. Packages Description 3. CMinLBFGS ‐ optimization using successive generation and classification of the function's quadratic model. 4. CMinQP ‐ quadratic programming with linear constraints in the form of equations and inequations. 5. CMinLM ‐ optimization using Levenberg‐Marquardt algorithm. 6. CMinComp ‐ backward compatibility functions.

solvers.mqh Classes for solving linear and non‐linear equation systems:

1. CDenseSolver ‐ solving linear equation systems. 2. CNlEq ‐ solving non‐linear equation systems.

specialfunctions.mqh Classes of distribution functions, integrals and polynomials:

1. CGammaFunc ‐ Gamma function. 2. CIncGammaF ‐ incomplete Gamma function. 3. CBetaF ‐ Beta function. 4. CIncBetaF ‐ incomplete beta function. 5. CPsiF ‐ psi‐function. 6. CAiryF ‐ Airy function. 7. CBessel ‐ Bessel functions of integral order. 8. CJacobianElliptic ‐ Jacobian elliptic functions. 9. CDawson ‐ Dawson integral. 10. CTrigIntegrals ‐ trigonometric integrals. 11. CElliptic ‐ elliptic integrals of the first and second kind. 12. CExpIntegrals ‐ exponential integrals. 13. CFresnel ‐ Fresnel integrals. 14. CHermite ‐ Hermite polynomials. 15. CChebyshev ‐ Chebyshev polynomials. 16. CLaguerre ‐ Laguerre polynomials. 17. CLegendre ‐ Legendre polynomials. 18. CChiSquareDistr ‐ chi‐square distribution. 19. CBinomialDistr ‐ binomial distribution. 20. CNormalDistr ‐ normal distribution. 21. CPoissonDistr ‐ Poisson distribution. 22. CStudenttDistr ‐ Student t‐distribution. 23. CFDistr ‐ F‐distribution.

statistics.mqh Classes of statistical data analysis:

1. CBaseStat ‐ basic statistical methods. 2. CCorrTests ‐ test for correlation ratio significance. 3. CJarqueBera ‐ JarqueBera croterion. 4. CMannWhitneyU ‐ Mann‐Whitney U‐criterion. 5. CSignTest ‐ sign test. 6. CStudentTests ‐ Student t‐tests. 7. CVarianceTests ‐ F‐test and chi‐square test. 8. CWilcoxonSignedRank ‐ Wilcoxon W‐criterion.

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)

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 tading parameters:

//+------+ //| UseAlglib.mq5 | //| Copyright 2012, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------+ #property copyright "Copyright 2012, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" //+------+ //| Connecting the libraries | //+------+ #include #include #include //+------+ //| Script program start function | //+------+ void OnStart() { //--- object to access data on deals CDealInfo deal; //--- object for storing profit/loss of each deal CArrayDouble *profit=new CArrayDouble; //--- objects for storing the balance CArrayDouble *balance_total=new CArrayDouble; //--- initial balance double balance=0; //--- receive history HistorySelect(0,TimeCurrent()); //--- total amount of deals int deals_total=HistoryDealsTotal(); //--- receive data on deals' profit and balance for(int i=0;i=0.0) if(balance==0.0) balance=deal.Profit(); } //--- receive profit and balance if(deal.DealType()==DEAL_TYPE_BUY || deal.DealType()==DEAL_TYPE_SELL) if(deal.Entry()==DEAL_ENTRY_OUT || deal.Entry()==DEAL_ENTRY_INOUT) { profit.Add(NormalizeDouble(deal.Profit()+deal.Swap()+deal.Commission(),2)); balance_total.Add(balance); balance=balance+NormalizeDouble(deal.Profit()+deal.Swap()+deal.Commission(),2); } } balance_total.Add(balance_total.At(balance_total.Total()-1)+profit.At(balance_total.Total()-1)); //--- copy data on the balance to double type array double arr_balance[]; ArrayResize(arr_balance,balance_total.Total()); for(int i=0;i

We receive the following result:

Translated from Russian by MetaQuotes Software Corp. Original code: https://www.mql5.com/ru/code/1146

Elmo de Moraes: The code could be updated to the last version 3.8.2

Rashid Umarov: Automatically generated CHM‐help for ALGLIB

Tom Sasson: Thank you @Rashid Umarov I recommend all to go to the website, because this thread has been updated for 3 years, but the website keeps updating.

LydiaMW: We've done a complete overhaul of the GPL C++‐based version of the ALGLIB library, releasing it as ALGLIB++. This tracks the latest version of ALGLIB, which is at 3.16.0, as of 2019/12. Much has been added in the Optimization and Interpolation modules since the earlier versions that MQL5 is synched up to (. point‐ cloud splines(!), more inverse distance weighted interpolation methods, numerous additional optimization methods, etc.) ALGLIB++ is a derivative of ALGLIB that is being used as an intermediate form in a longer‐term reengineering/refactoring process, in which it will be recoded into native C++ (similar to where it was before Version 3), the extra layer and duplication being removed in preparation of providing more direct support for multi‐threading, as well as additional tests and modules and eventually a scripting language front end. The different language versions of ALGLIB were all generated from a common core, with the C++ version providing limited (but unofficial) support for the C90 dialect of C. This feature made it necessary to simulate, within C, features that would otherwise be native to C++, and then to provide a C++ wrapper on top of this. Correspondingly, there are two separate name‐spaces: alglib_impl, which contains the C version, and alglib, which contains the C++ wrappers. ALGLIB++ has retained most of this structure and as much of the original coding as possible, but has reduced or eliminated much of the global infrastructure as a first step for its elimination and replacement by multi‐threaded native C++ code and has significantly simplified the C++ wrapper interface. As such, it represents an intermediate form bridging between ALGLIB, itself, and the future library that ALGLIB++ is being transformed into. Many issues that lead to a growth of complexity in ALGLIB, since (and prior to) the versions adapted by MQL5, have been resolved, with a resulting simplification in structure and reduction in complexity. In its present form, it should prove easier to adapt to MQL5, by those who are currently maintaining the MQL5 version of ALGLIB. Included with the distribution is a complete reformatting of the manual for ALGLIB++ from the ALGLIB C++ original. The sections on the packages and subpackages, however, are compatible with both versions of ALGLIB, and the layout and content should both be easily adapted to the MQL5 version. MQL5 gets a shout‐out in the "References & Related Links" section of the manual. The latest version may be found at , at present, it is undergoing continual reengineering and refactoring at a fairly rapid pace, and this will remain the case for a while longer. The timestamp of the latest revision is always kept in the README file. The recoded version will eventually be expanded into a larger library that will include more features and applications dedicated to Machine Learning, advanced Digital Signal Processing, Graphics and Sound Processing and Natural Language Processing. We are also preparing a C++ translation of LAPACK, to be called LAPACK++, for future integration into ALGLIB++. Other libraries, including MKL (which has neural net routines, by the way) are also under consideration for future integration.

LydiaMW: A few additional notes to the developers of the MQL5 version of ALGLIB: When you adapted ALGLIB to MQL5, you ran into difficulties with the "RCOMM" routines. These are effectively multi‐threaded routines, where the thread‐switching is written directly into the code by: (1) caching thread‐local variables with each entry and exit ‐ and iteration routines carry out potentially millions and billions of call/return cycles! (2) implementing thread‐switching by jumping out of the routine to "pause" on an event, and jumping back into the routine to "resume" after the event ‐ which means a lot of goto's and multiple entry points. (3) decimating the control‐flow structure of these routines in order to put the entry points on the top level in the function body. When adapting ALGLIB to MQL5 you kept the control flow decimation in place, actually shipping off the code snippets into separate routines. This solution ‐ as you eventually found out ‐ is not maintainable; which has made keeping up with the ALGLIB updates impossible. ALGLIB, itself, got a little behind the curve as well ‐ their RCOMM routines sometimes leave orphan structures and variables from earlier versions. In ALGLIB++, the control flow structure is reintegrated: entry points jump back into the middle of a loop or other control flow structure where they left off. There is no caching of thread‐local variables; for the time being they've been made static (at the cost of thread‐safety ‐ but multi‐threading is not formally a part of GPL ALGLIB). Like ALGLIB, this means there are a lot of goto statements to do all the jumping back and forth; but they are more cleanly organized. The repairs made in ALGLIB++, by themselves, speed up the routines by about 20‐25%, based on our test results. There are two ways to deal with this architecture to get rid of the goto statements and more properly handle the thread‐local variables: (1) Implement actual thread‐switching, which means that the RCOMM routines send out "event" messages (which is what the needf, ... algpowerup flags actually are), and the caller has to set up an event‐handler to receive and process the messages. Not sure if MQL5 can produce and handle user‐defined events, however. (2) Use function pointers for the events. Bear in mind that the RCOMM routines may be nested; and in some cases, they can relay events on to their caller obtained from the RCOMM routines that they themselves call. The ALGLIB developer(s) decided not to use function pointers or multi‐threading, when they implemented ALGLIB, because when ALGLIB was first released there was no formal support for multi‐ threaded programming in any widely‐distributed and widely‐used language, and not all languages that ALGLIB was targeted to had the same or similar equivalent to the function pointers of C and C++. But MQL5 has function pointers. If you adapt ALGLIB to MQL5, it will be much easier to do this from ALGLIB++, instead, because of the reintegration of the control flow structures. The ideal solution, however, is to use thread‐switching. Eventually, either function pointers or thread‐switching will be used in ALGLIB++; we haven't decided yet which way to go. Read more comments and leave your own ones >>

Download MetaTrader 5!