Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

ALGLIB - Numerical Analysis Library - Library for MetaTrader 5 | MT5 EA Download - MetaTrader 5 Resources

author EAcpu | 5 reads | 0 comments |

ALGLIB - Numerical Analysis Library - library for MetaTrader 5

Sergei Bochkanov. ALGLIB project website - http://www.alglib.net/ . Library dates back to 1999 .

Do you need to perform a Fast Fourier Transform or solve a system of differential equations? Are you performing complex data analysis trying to collect all methods in one place as source code? Then the ALGLIB numerical method library is for you!

ALGLIB is one of the best multi-language algorithm libraries available. The following are the ALGLIB functions mentioned on the official website:

ALGLIB is a cross-platform numerical analysis and data processing library. It supports multiple programming languages ​​(C++, C#, Pascal, VBA) and multiple operating systems (Windows, Linux, Solaris). ALGLIB functionality includes:

Why choose ALGLIB? Because it is:

The ALGLIB library is continuously enhanced, with new features and improvements regularly implemented based on user input. The latest version is 4.0.

Furthermore, the library contains a large number of test cases covering the main functionality of the proposed method. This will enable you to test and report detected bugs to the project's authors.

CAlglib class static functions should be used to work with the library - all library functions were moved to the CAlglib system class as static functions.

The testclasses.mq5 and testinterfaces.mq5 test case scripts are attached together with the simple usealglib.mq5 demo script. Included files with the same names (testclasses.mqh and testinterfaces.mqh) are used to start test cases. They should be placed in \MQL5\Scripts\Alglib\Testcases\.

Notes: The testclasses.mq5 script execution takes quite a long time (about 8 minutes).

Here are more details about the ALGLIB MQL5 porting library package:

The package contains the following classes:

Library functions have detailed comments on their use.

//+------------------------------------------------------------------+
//|Calculation of distribution moments: mean, variance, |
//|skewness, kurtosis.                                              |
//|Input parameters: |
//|
//| N - N>=0, sample size: |
//| * If given, only the first N elements of X are |
//| Processed |
//| * If not given, automatically determined from |
//| Size of X |
//|Output parameters |
//| average - the average value.                                            |
//| Variance - Variance.                                        |
//| Skewness - Skewness (if variance <> 0; otherwise zero).      |
//| kurtosis - kurtosis (if variance <> 0; otherwise zero).      |
//+------------------------------------------------------------------+
static boolean CBaseStat::SampleMoments( const double &cx[], const integer , double & means,
Double & variance, double & skewness, double & kurtosis)
  { // - - Check if (!CAp::assert(n>= 0 , __function__ + ":error variable" ))
return ( false ); // - - see if (!CAp::Assert(CAp::Len(cx)>=n, __function__ + ":length(x)  ))
return ( false ); // - - see if (!CAp::Assert(CApServ::IsFiniteVector(cx,n), __function__ + ":x is not a finite vector" ))
return ( false ); //--- create variable double v= 0 ;
Double v1= 0 ;
Double v2= 0 ;
Double standard deviation = 0 ; //--- initialization, special case 'N=0' mean = 0 ;
  variance = 0 ;
  Skewness = 0 ;
  kurtosis = 0 ; // - - check if (n<= 0 )
return ( true ); // - - means for ( integer i = 0 ; i  //--- Variance (using modified two-pass algorithm) if (n!= 1 )
    {
// - - calculated as ( integer i = 0 ; i for ( integer i = 0 ;i  1);
// - - Calculate standard deviation = mathematical square root (variance);
    }
else variance= null_VALUE ; //--- skewness and kurtosis if (standard equipment!= 0 )
    {
// - - calculated as ( integer i = 0 ; i(v); Skewness+=v2*v; Kurtosis+=CMath::Sqr(v2); } //--- Change value skewness=skewness/n; Kurtosis=kurtosis/n- 3 ; } //--- Return ( true ) on successful execution; }

CAlglib class static functions should be used to work with the library. The following is the source code of usealglib.mq5 function script, which is used to calculate some statistical trading parameters:

//+------------------------------------------------------------------+
//| Using Alglib.mq5 |
//| Copyright 2012, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#propertycopyright "Copyright 2012, MetaQuotes Software Corp."
#Attribute link "https://www.mql5.com"
#Attribute version "1.00"
//+------------------------------------------------------------------+
//|Connection library |
//+------------------------------------------------------------------+
#include
#include <Deal\DealInfo.mqh>
#include
//+------------------------------------------------------------------+
//|Script program startup function |
//+------------------------------------------------------------------+
blank start ()
  { //--- Object to access transaction data CDealInfo transaction; //--- Object to store profit/loss of each transaction CArrayDouble *profit = new CArrayDouble; //--- Object to store balance CArrayDouble *balance_total = new CArrayDouble; //--- Initial balance doubled balance = 0 ; //--- Receive history history selection ( 0 , time current ()); //--- Transaction total integer total transaction total = historical transaction total (); //--- receive transaction profit and balance data for ( integer i = 0 ; i<total transaction count; i++)
    {
//--- Go to transaction with index i deal.SelectByIndex(i);
//--- receive initial balance if (deal.DealType()== DEAL_TYPE_BALANCE )
        {
if ( normalized double (transaction.Profit()+transaction.Swap(), 2 )>= 0.0 )
if (balance == 0.0 )
              Balance = Transaction.Profit();
        }
//--- receive profit and balance if (deal.DealType()== DEAL_TYPE_BUY || deal.DealType()== deal type_sell )
if (transaction.Entry()== DEAL_ENTRY_OUT ||transaction.Entry()== DEAL_ENTRY_INOUT )
          {
            Profit.Add( Normalized Double (deal.Profit()+deal.Swap()+deal.Commission(), 2 ));
            Balance_total.Add(balance);
            Balance = Balance + Normalized Double (deal.Profit()+deal.Swap()+deal.Commission(), 2 );
          }
    }
  Balance_total.Add(balance_total.At(balance_total.Total()- 1 )+profit.At(balance_total.Total()- 1 )); //--- Copy the data on the balance to a double array double arr_balance[];
Array resize (arr_balance,balance_total.Total());
for ( integer i = 0 ;i  //--- Copy profit data to double array arr_profit[];
Array resize (arr_profit,profit.Total());
for ( integer i = 0 ;i<profit.Total();i++)
      arr_profit[i]=profit.At(i); //--- Linear regression //--- The number of independent variables integer nvars= 1 ; //--- The sample size integer npoints=balance_total.Total(); //--- Create the parameter matrix of linear regression CMatrixDouble xy(npoints,nvars+ 1 ); for ( integer i = 0 ;i  0, i); Array used to store regression results double lr_coeff[] ; double lr_values []; array resize(lr_values, n points); //--- Calculate linear regression ratios CAlglib:: LRBuild (xy,npoints,nvars,info,lm,ar); //--- Receive linear regression ratios CAlglib::LRUnpack(lm,lr_coeff,nvars); //--- Receive the recovered linear regression value for ( integer i = 0 ;i  0 | ); for ( integer i = 0 ; i  1 ;i++) HPR[i]=balance_total.At(i+ 1 )/balance_total.At(i); //--- Calculate the standard deviation and mathematical expectation double AHPR, SD according to HPR; CAlglib::SampleMoments(HPR,AHPR,SD,tmp2,tmp3); Standard deviation = mathematical square root (SD); //--- Calculate LR Correlation double lr_corr = CAlglib::PearsonCorr2(arr_balance,lr_values); //--- receive LR standard error double lr_stand_err = 0 ; order ( integer i = 0 ; i Doubled delta = mathematical antibody (arr_balance[i]-lr_values[i]); lr_stand_err=lr_stand_err+delta*delta; } lr_stand_err= Mathematical square root (lr_stand_err/(npoints- 2 )); //--- Calculate the Sharpe ratio of doubled Sharpe_ratio=(AHPR- 1 )/SD; // - - Print print format ( "--------------------------------------------------------" ); print format ( "Correlation function: y = %.2fx + %.2f" , lr_coeff[ 0 ], lr_coeff[ 1 ]); // - - Parameter print format ( "Expected return = %.2f" , exp_payoff); print format ( "AHPR = %.4f" , AHPR); print format ( "Sharpe ratio = %.2f" , sharp ratio); print format ( "LR correlation = %.2f" , lr_corr); print format ( "LR standard error = %.2f" , lr_stand_err); print format ( "--------------------------------------------------------" ); //--- delete object delete profit; delete total balance; } //+--------------------------------------------------------------------------------+

We receive the following results:

ALGLIB - Numerical Analysis Library - library for MetaTrader 5


Attachment download

📎 alglib.mqh (2420.29 KB)

📎 alglibinternal.mqh (579.73 KB)

📎 alglibmisc.mqh (119.42 KB)

📎 ap.mqh (89.98 KB)

📎 arrayresize.mqh (4.88 KB)

📎 bitconvert.mqh (13.49 KB)

📎 dataanalysis.mqh (1124.84 KB)

📎 delegatefunctions.mqh (21.25 KB)

📎 diffequations.mqh (32.41 KB)

📎 fasttransforms.mqh (92.23 KB)

📎 integration.mqh (116.61 KB)

📎 interpolation.mqh (1430.87 KB)

📎linalg.mqh (1455.11 KB)

📎 matrix.mqh (45.38 KB)

📎 optimization.mqh (2246.73 KB)

📎 solvers.mqh (295.08 KB)

📎 specialfunctions.mqh (235.55 KB)

📎 statistics.mqh (407.4 KB)

📎 TestClasses.mq5 (30.42 KB)

📎 TestClasses.mqh (3032.44 KB)

📎 TestInterfaces.mq5 (17.49 KB)

📎 TestInterfaces.mqh (679.69 KB)

📎UseAlglib.mq5 (6.2 KB)

Source: MQL5 #1146

Verification code Refresh