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


RAD - Rapid Application Development library - library for MetaTrader 4

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

votes: 26
2016.10.27 13:16
2016.11.22 07:32
rad_lib.mqh (85.36 KB)view

This library includes numerous functions, which can speed up the development in the MQL4 by saving from distracting on the mechanics and allowing to focus on the logic. For example, to open an order for 0.4 lot, it is sufficient to call the OpenBuy(0.4); function. Naturally, such functions have more than one parameter, the other parameters are set to default. If necessary, they can be filled as well.

The library features the RAD_onTick(), RAD_onInit(), RAD_onDeinit() handlers. They must be called in experts or indicators in the corresponding functions and should be placed and their beginning. It also features configuration of the default values, for example, setting the magic number with RAD_setMagic(...), the symbol with the RAD_setSymbol(...) and slippage with the RAD_setSlippage(...). It also includes the extended functionality for working with strings, arrays and additional mathematical operations. The RAD_DebugEnable() is activated for debugging the application. Inserting the RAD_DEBUG_ASSERT(...) to code sections will print the specified lines to the console, inserting the RAD_DEBUG_SECTION{....} will run the code in brackets when the debug mode is active, and with the logging function ( RAD_LogEnabled() ) enabled, it will output it to the log file. Description of the remaining multitude of functions can be found in the file itself.

Here is an example of code for an abstract expert advisor:

//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                              |
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      ""
#property version   "1.00"
#property strict

#include <RAD_lib.mqh>

input int Magic=2143658709;
input int FastPeriod =  14;
input int SlowPeriod =  20;
input double Risk=10;
input int Stoploss=60;
input int Takeprofit=30;
int OnInit()

   RAD_onInit(); //initialize the library
   RAD_setProgramName("Mashka Expert"); //set the expert name
   RAD_setLogName(RAD_ProgramName()+"_logger.log"); //specify name of the file for logging

   RAD_setEquityBreakdown(40); //set the allowed drawdown level by equity. Checked at every tick 
   RAD_setMagic(Magic); //set the standard value of magic number

   RAD_DebugEnable(); //enable debug mode
   RAD_LogEnable(); //start logging

void OnDeinit(const int reason)
   RAD_onDeinit(); //so far the handler of this event is empty, but let it be there for future

void OnTick()
   RAD_onTick(); //handling the internal affairs of the library

                 //a new bar arrived

      double fast = iMA(NULL, 0, FastPeriod, 0, MODE_EMA, PRICE_CLOSE, 0);//fast МА
      double slow = iMA(NULL, 0, SlowPeriod, 0, MODE_EMA, PRICE_CLOSE, 0);//slow МА
      double lot=LotOnRisk(Risk);

      RAD_ASSERT(0,"fast:"+fast+"  slow:"+slow+"  lot:"+lot);

      //conditions for opening BUY, otherwise SELL
         RAD_ASSERT(1,"Open BUY order");
         if(CountOrders(omBuy|omPoolTrades)==0) //checking if a BUY order had already been opened
            OpenBuy(lot,Stoploss,Takeprofit); //open a BUY order
         RAD_ASSERT(2,"Open SELL order");
         if(CountOrders(omSell|omPoolTrades)==0) //checking if a SELL order had already been opened
            OpenSell(lot,Stoploss,Takeprofit); //open a SELL order



Naturally, such an EA will not bring profits, because we all know this strategy based on MAs :)

This is only an example of the code. It shows that the library requires no value checks, no calculation of stop levels, there is no need to write 100-fold nested order checks, all this has been written for you in the library. Another code example that works with orders:

CountOrders(omBuy | omPoolTrades) //returns the number of BUY orders with any magic number and symbol in the Trades pool
CountOrders(omSells | omPoolHistory, 123, "EURUSD") //returns the number of SELL, SELL_STOP, SELL_LIMIT type orders with magic number equal to 123 and symbol equal to EURUSD in the History pool
CountOrders(omBuy | omBuyStop | omPoolTrades, 123) //returns the number of BUY and BUY_STOP orders with the magic number equal to 123 and any symbol in the Trades pool
CloseOrders(omBuys) //closes all BUY, BUY_STOP, BUY_LIMIT orders with any magic number and symbol and returns their number
CloseOrders(omAll) //closes all orders of all types available on the account

//checking if the order in the Trades pool belongs to BUY or SELL types, 123 magic number and EURUSD symbol
if(isOrderFilter(ticket, omActive | omPoolTrades, 123, "EURUSD"))

//checking if the order in the any pool belongs to BUY type, 123 magic number and any symbol
if(isOrderFilter(ticket, omBuy | omPoolAll, 123, NULL))

The code looks easy and at the same time it is functional and versatile.

Extended work with arrays. Now you will not have to repeatedly write the code for adding an element to array, now it is sufficient to call something like:

int a[];

ArrayAdd(a, 12);
ArrayAdd(a, 24);
ArrayAdd(a, 55);
ArrayAdd(a, 75);  //the array will look as [12,24,55,75]

Deleting elements from array is also uncomplicated:

ArrayDel(a, 2); //deletes the third element from the array, resulting in [12, 24,75]

Or like this:

ArrayDel(a, 1, 3); //deletes three elements starting from position 1. This results in [12]

This is implemented using the function templates for the option of transferring by value and by link

If it is necessary to download the page from the Internet, simple call:

string page;
int size;

size = DownloadPage("", page, 2, 5); //downloads the Google page, using 2 attempts at failure with a delay of 5 seconds

The same is with the file, only the second parameter is the filename.

The horrible expressions of MarketInfo(symbol, MODE_MAXLOT) are wrapped into normal functions => MarketInfoMaxLot(Symbol())

The mathematical apparatus has also been expanded. Searching for the maximum from multiple values no longer looks like a bunch of nested MathMax(MathMax(MathMax...))). It now features up to 5 arguments implemented using function templates, and can now be called as:

double max;

max = MathMax(12.4, 55.432, 128e-4, 8003.44);

And generation of random numbers in the range of -34000 to 125000 looks the following way:

double random;

random = MathRandomDouble(-34000.0, 125000.0); //generation of a random number from -34000 to 125000
random = MathDiscrete(random, 100.0); //sampling of the random number by 100 can be added, if necessary

But then, the code is open. Look, add, suggest ideas on the structure of the library or its functionality, as well as general comments.

Translated from Russian by MetaQuotes Software Corp.
Original code:

Indicator of the options levels Indicator of the options levels

The indicator is designed for calculating and displaying the options levels on the screen.

Trade panel with autopilot Trade panel with autopilot

An example of creating a trade panel using the MasterWindows library.

SAW_system_1 SAW_system_1

The SAW_system_1 Expert Advisor places pending orders on the basis of the volatility for the last N days.

Fishing Fishing

The Expert Advisor opens positions after the price passes the specified distance. 1 step up - sells, 1 step down - buys.