MQL5 Tutorial – Simple Buy Trade with MQL5



  1. Introduction to Creating a Simple Buy Trade in MQL5 (00:00 – 00:08) Introduction to the concept of creating multiple positions in MQL5 using a hedging account type.
  2. Opening Meta Editor and Starting a New Expert Advisor (00:08 – 00:21) Instructions on opening the Meta Editor and starting a new expert advisor for a simple buy trade.
  3. Setting Up a Trading Request and Result for Buy Order (00:21 – 00:33) Explaining how to create a trading request and result for opening a buy order in Netting Mode.
  4. Initializing the Trading Request and Creating Expert Advisor (00:33 – 00:45) Steps to initialize the trading request and create a new expert advisor named “Simple Buy Trade.”
  5. Getting the Ask Price and Account Details (00:45 – 00:58) Instructions on how to get the ask price and details like balance and equity of the account.
  6. Opening a Buy Trade Based on Equity and Balance (00:58 – 01:09) Coding the condition to open a buy trade if the equity is equal to or greater than the account balance.
  7. Compiling the Code and Fixing Errors (01:09 – 01:22) Compiling the code, fixing an error, and including a necessary library for the CTrade object.
  8. Testing the Expert Advisor in MetaTrader (01:22 – 01:36) Testing the expert advisor in MetaTrader’s strategy tester for the year 2016 and observing the first buy trade.
  9. Observing Multiple Trades and Profit Generation (01:36 – 01:49) Speeding up the test to observe multiple trades being opened and profit generation.
  10. Reviewing the Net Profit and Test Graph (01:49 – 02:09) Reviewing the net profit of $894 and the graph of the testing period.


We want to create a simple buy trade.
If you look over here you can see that hedging is now possible with MQL 5 and we want to use this type of account to create not only one, but more positions.
So let’s start by hitting this little button here (or the F4 key) to bring up the Meta Editor.
This is how to open a buy order in Netting Mode, you have to create a trading request, you will get a trading result afterwards.
Before you open the trade you have to “zeromemory” the request that means basically you empty the memory from old data that you might have used before to open another trade.
Now we would like to use the simplified version so we click on new Expert Advisor and we will call that simple buy trade.
Please click next, next, ready and you can delete all the stuff above the OnTick function.
This is a comment and this can also be deleted. Before we can open our buy trade, we need to know the ask price.
This is how we can get it in MQL 5.
We would also like to see how our equity and our balance looks, so let’s get the balance and the equity of our account, all these are double values.
Double is used for floating point values and that’s actually what we need.
Now we will say “IF” equity is equal or greater our balance, we want to open a buy trade.
Let’s compile that or we have to change this one to greater or equal.
And we will get an error because I have forgotten to import something; we need to include a library and to declare a CTrade.
This is called trade and here we are going to use it to buy.
So now it compiles without any errors.
Let’s see if it works. Let’s change this value to 2016 and start the test.
And here we are, it bought.
Let’s speed it up a little bit, and when the equity becomes greater than the balance that means we are making a profit.
It actually starts to open more trades, so this is full speed and here are all trades we have opened.
We made a net profit of 894 bucks, and this is the graph of our testing.

Download “MQL5 Simple Buy Trade Expert Advisor from MQL5 Tutorial”

SimpleBuyTrade.txt – Downloaded 742 times – 360.00 B