Okay, in this video we are going to create a simple Expert Advisor that actually works.
To do that please click on the little button here or press F4 in Metatrader and that will bring up the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this one: “SimpleBuyStop”, “Continue”, “Continue” and “Finish” and now you can delete everything above the “OnTick” function and the comment lines here.
This “OnTick” function will be called every time the price changes and we need to include a library called: “Trade.mqh” that contains a class called: “CTrade” and we are going to create an instance called: “trade”.
We need to get the Ask price, when you double-click on a currency pair like this one you will see two lines, one is for the Ask and one is for the Bid price.
This is how we do it, you don’t need to understand everything in this line right now, just take the free course, I have got dozens of other videos where you can learn all the details but for now I want to create something quick and simple that you can copy and use right away.
Let’s get the account balance by using “AccountInfoDouble” and the equity by using “AccountInfoDouble”, the balance will tell you your account balance and the equity is what’s left when you subtract all the open positions from the account balance and if equity and balance are equal and “OrdersTotal” equals 0 (zero) – that would be true when no other pending orders exist – we will place a buy stop that is done by “trade.BuyStop” for 10 micro lot. The Buy Stop will be placed 100 points above the Ask price for the current currency pair on the chart, we have not set a Stop Loss and the Take Profit will be 300 points above the current Ask price. We start the order right away, we don’t set an expiration date and we don’t need a comment and that’s it. Oh… I missed a 0 (zero) here, this is a complete Expert Advisor and if you are done please click on the “Compile” button here or press F7 on your keyboard and if you don’t have any errors this “Compile” button will translate your MQL5 code from the “.mq5” file here and create an “.ex5” file that is a compiled MQL5 Expert Advisor.
And if you click the little button over here or press F4 in the Metaeditor you will go back to Metatrader, you will see your new self-coded Expert Advisor in the “Navigator” window.
Now let’s click on: “File/ Strategy Tester” or press CTRL and R on your keyboard and in the Strategy Tester please select the “SimpleBuyStop.ex5” file and start your test.
Here we have our Buy Stop, I will zoom in a little bit and for the first few days nothing happens but now our trade has been opened and we make the first few profits here. I will try to speed up the process for you and now you see it actually works and it is making a profit right now!
In the next video we are going to do a few statistics and compare the results for different currency pairs.