MQL5 Tutorial – Advanced Library Expert Advisor


What you see here is an Expert Advisor that is opening buy stops and we have a calculated profit on the chart and it uses two self defined functions that we have compiled in a so called library.
You can think of a library as a file that contains self defined functions that you can use again and again.
Now how can we create such a library in MQL5?
To do that please click on the little button here in your Meta Trader and now you should see the MetaEditor and this is our main module and we use these import statements here to import the file MyLibrary.ex5 and in that file we have a function called GetProfitCurrencyPair and another function called OpenBuyStop.
And in the OnTick function we check if we have no open orders and if that is the case we want to OpenBuyStop so we call the self defined function OpeBuyStop from our library.
And to calculate the profit we use the function GetProfitCurrencyPair and that’s also self defined in our library.
And to create your own library please click on File, New, Library, Continue, this one is called myLibrary and now you can click on Finish.
And the first thing we need is property library, without that you cannot compile the file and to be able to open the buy stops we need to import the trade functions from the built in trade.mqh file.
And to get the profit of the current currency pair we need to create our self defined function and this extension 2export” is used because only a function with such a modifier is accessible for our MQL 5 programs.
And in the function we declare a variable called ProfitThisCurrencyPair; it contains no value here and now we go through all the positions using this For Loop and we use PositionGetSymbol for the current counter and if the symbol on the chart equals the symbol of the currency pair from our position, we use PositionGetDouble, position_profit to calculate the position profit and position get double, position_swap to calculate the swap.
Just add the two values up to get the profit of the currency pair.
Let’s end the For Loop and we use normalize double and two digits to calculate the profit for the currency pair and format it for two digits behind the dot, and afterwards we use return to return the profit for the currency pair back to the main module.
Let’s add another function for the buy stop, it’s called OpenBuyStop and it also has the export extension and to open the buy stop we want to know the ask price, the balance and the equity.
You can get the ask price by using SymbolInfoDouble for the current symbol and you use symbol_ask and with NormalizeDouble and _Digits we make sure that we have three digits for this currency pair or five digits for other currency pairs and to get the balance of the equity we use AccountInfoDouble, Account_Balance or AccountInfoDouble, Account_Equity.
And if equity and balance are equal, we want to open a buy stop by using trade.buystop for 10 micro lots.
The buy stop will be one hundred points above the ask price of the current symbol.
We haven’t defined a stop loss, the take profit will be three hundred points above the ask price and we use Order_Time GTC because that is good until the order is cancelled so it will not expire and because of that we use a zero for the date/time and the last parameter would be for the comment, we don’t need that so we also use zero here.
Let’s add the last bracket to close the function and press this little button or F7 on your keyboard to compile your library.
I made a mistake here, of course I need to create an instance of Ctrade now that’s done and I recompile and this time it worked without any errors.
So let’s click on the button here our press F4 to go back to MetaTrader.
And in MetaTrader you want to click on View, Strategy Tester or press control and R, and in the Strategy Tester please select the file simpleLibraryMainModule.ex5 and please remember that you also have to compile the main module each time you create a library or you change a library.
Now let’s click on start and here we go.
I will speed up the process a little bit and now you know how to create an Expert Advisor that is able to import your self-written library and you have created it with a few lines of MQL5 code.

Learn how to create your own MQL5 trading robot for $1
Sign Up Now