Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2011.02.28 14:52
Libraries

sToken - library for MetaTrader 5

| English Русский 中文 Español Deutsch 日本語 Português

Views:
1404
Rating:
votes: 21
\MQL5\Scripts\
stoken.mq5 (22.71 KB)view

The script contains a class, that can be used to solve mathematical and logical expressions, defined as string.

The script has two classes: cTokenBase and cToken (a member of CTokenBase). The cTokenBase class must be configured before its use (see below).

The following MQL5 mathematical functions are supported: abs, arccos, arcsin, arctan, ceil, cos, exp, floor, log, log10, max, min, mod, pow, rand, round, sin, sqrt, tan.

Logical and math operations:  /, %, *, +, -, >, <, >=, <=, ==, !=, &&, ||.

The expression may contain numbers, including double numbers, user variables and user arrays. If you will use the user's variables and arrays, you need to add functions, that returns the values of these variables and arrays. The functions should be added into the cTokenBase class.

The user variables and arrays are named with letters (the case isn't important), the array elements are defined as follows: e[0], e[1], f[0], f[1].

Examples:

sToken demo expressions


sToken demo results


How to configure cTokenBase class

It's neccessary to register the name of the user variables and arrays. It must be done in the UsersVariables() function. The list of user variables/arrays contain the names, separated by ";".

      void UsersVariables()
     {
         UserVariables="a;b;c;d"; // list of user's variables
         UserArrays="e;f";       // list of user's arrays
      } 

2. You need to add a call of the corresponding function to the UserFunc(string FuncName) function:

      string UserFunc(string FuncName)
     {
         if(FuncName=="a")return(a()); 
         if(FuncName=="b")return(b());
         if(FuncName=="c")return(c());
         if(FuncName=="d")return(d());
         Alert("Function for "+FuncName+" variable is not defined");
         return("0");
      }

3. Add functions for all of the user's variables:

      string a()
      {
         return("1");
      }
      string b()
      {
         return("2");
      }
      string c()
      {
         return("3");
      }
      string d()
      {
         return("4");
      } 

4. Add call of the corresponding function (dependent on ArrName) to the UserArray(string ArrName,int aIndex) function. The index of array element is specified in aIndex variable.

      string UserArray(string ArrName,int aIndex){
         if(ArrName=="e")return(e(aIndex));
         if(ArrName=="f")return(f(aIndex));
         Alert("Function for "+ArrName+" array is not defined");
         return("0");
      }

5. Add functions for each of the arrays:

      string e(int Index)
      {
         string v[]={"1","2","3","4","5","6","7","8","9"};
         return(v[Index]);
      }
      string f(int Index)
      {
         string v[]={"10","20","30","40","50","60","70","80","90"};
         return(v[Index]);
      }

Use of the class

1. Declare an external input variable for the expresssion:

input string Expression="enter expression here";

2. Delcare a cToken class variable on the global level:

cToken token;

3. Initialize class with the expression.

   token.Init(Expression);

4. Call SolveExpression() method

double Value=token.SolveExpression();

The class can be used to solve several expressions, just use the several instances of cToken class:

cToken token1;
cToken token2;
cToken token3;
cToken token4;

In this case all the class instances will use the same user variables and arrays (declared in cTokenBase class)

You can add your functions.


Adding new functions to cToken class

  1. Register a new function. It must be done in the Init() function.
  2. Add the calculation code to the SolveFunc(string Func,string & aRes[]) function. The function arguments are located in aRes[] array. The number of arguments isn't limited.

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

downloadhistory.mq5 downloadhistory.mq5

The script downloads historical data (for the current symbol), available at trade server.

MQL5 Wizard - Trade Signals Based on Dark Cloud Cover/Piercing Line + RSI MQL5 Wizard - Trade Signals Based on Dark Cloud Cover/Piercing Line + RSI

Trade signals based on "Dark Cloud Cover/Piercing Line" candlestick pattern, confirmed by Relative Strength Index (RSI) indicator is considered. The code of the Expert Advisor based on this strategy can be generated automatically using the MQL5 Wizard.

CDownLoadHistory class CDownLoadHistory class

The CDownLoadHistory class provides the methods of historical data downloading.

MQL5 Wizard - Trade Signals Based on Bullish Engulfing/Bearish Engulfing + Stochastic MQL5 Wizard - Trade Signals Based on Bullish Engulfing/Bearish Engulfing + Stochastic

Trade signals based on "Bullish Engulfing/Bearish Engulfing" candlestick pattern, confirmed by Stochastic indicator is considered. The code of the Expert Advisor based on this strategy can be generated automatically using the MQL5 Wizard.