Chuyển sang chế độ ngoại tuyến với ứng dụng Player FM !
MQL5 Tutorial – Advanced Library Expert Advisor
Manage episode 375398500 series 1376757
- Introduction to Expert Advisor with Self-Defined Functions (00:00 – 00:14)
- Introduction to an expert advisor that opens buy stops and calculates profit on a chart.
- Explanation of using self-defined functions compiled in a library.
- Understanding Libraries in MQL5 (00:14 – 00:27)
- Explanation of what a library is in MQL5 and its purpose.
- How to create a library for reusable functions.
- Opening MetaEditor and Creating a Library (00:27 – 00:41)
- Instructions on opening MetaEditor from MetaTrader.
- Steps to create a new library in MQL5.
- Setting Up the Library File (00:41 – 01:01)
- Explanation of import statements and the structure of the library file.
- Introduction to the functions ‘getProfitCurrencyPair’ and ‘openBuyStop.’
- Defining Functions in the Library (01:01 – 01:30)
- How to define custom functions in the library.
- Example of creating functions for opening buy stops and calculating profit.
- Initializing Variables and Importing Trade Functions (01:30 – 02:05)
- Setting up initial variables and importing necessary trade functions.
- Explanation of the ‘property library’ directive.
- Creating Self-Defined Functions (02:05 – 03:28)
- Detailed steps to create self-defined functions in the library.
- Example of a function to calculate profit for a currency pair.
- Implementing the Buy Stop Function (03:28 – 04:49)
- Creating a function to open buy stop orders.
- Explanation of parameters and conditions for the buy stop.
- Compiling and Testing the Library (04:49 – 05:32)
- Instructions on compiling the library in MetaEditor.
- How to test the library’s functionality in MetaTrader.
- Using the Library in an Expert Advisor (05:32 – 06:06)
- Demonstrating how to use the created library in an expert advisor.
- Visualizing the expert advisor’s behavior in MetaTrader.
- Conclusion and Summary (06:06 – 06:25)
- Recap of how to create and use a custom library in MQL5.
- Emphasis on the efficiency of using libraries for reusable code.
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.
The post MQL5 Tutorial – Advanced Library Expert Advisor appeared first on MQL5 Tutorial.
27 tập
Manage episode 375398500 series 1376757
- Introduction to Expert Advisor with Self-Defined Functions (00:00 – 00:14)
- Introduction to an expert advisor that opens buy stops and calculates profit on a chart.
- Explanation of using self-defined functions compiled in a library.
- Understanding Libraries in MQL5 (00:14 – 00:27)
- Explanation of what a library is in MQL5 and its purpose.
- How to create a library for reusable functions.
- Opening MetaEditor and Creating a Library (00:27 – 00:41)
- Instructions on opening MetaEditor from MetaTrader.
- Steps to create a new library in MQL5.
- Setting Up the Library File (00:41 – 01:01)
- Explanation of import statements and the structure of the library file.
- Introduction to the functions ‘getProfitCurrencyPair’ and ‘openBuyStop.’
- Defining Functions in the Library (01:01 – 01:30)
- How to define custom functions in the library.
- Example of creating functions for opening buy stops and calculating profit.
- Initializing Variables and Importing Trade Functions (01:30 – 02:05)
- Setting up initial variables and importing necessary trade functions.
- Explanation of the ‘property library’ directive.
- Creating Self-Defined Functions (02:05 – 03:28)
- Detailed steps to create self-defined functions in the library.
- Example of a function to calculate profit for a currency pair.
- Implementing the Buy Stop Function (03:28 – 04:49)
- Creating a function to open buy stop orders.
- Explanation of parameters and conditions for the buy stop.
- Compiling and Testing the Library (04:49 – 05:32)
- Instructions on compiling the library in MetaEditor.
- How to test the library’s functionality in MetaTrader.
- Using the Library in an Expert Advisor (05:32 – 06:06)
- Demonstrating how to use the created library in an expert advisor.
- Visualizing the expert advisor’s behavior in MetaTrader.
- Conclusion and Summary (06:06 – 06:25)
- Recap of how to create and use a custom library in MQL5.
- Emphasis on the efficiency of using libraries for reusable code.
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.
The post MQL5 Tutorial – Advanced Library Expert Advisor appeared first on MQL5 Tutorial.
27 tập
Tất cả các tập
×Chào mừng bạn đến với Player FM!
Player FM đang quét trang web để tìm các podcast chất lượng cao cho bạn thưởng thức ngay bây giờ. Đây là ứng dụng podcast tốt nhất và hoạt động trên Android, iPhone và web. Đăng ký để đồng bộ các theo dõi trên tất cả thiết bị.