In this video, we are going to create an Expert Advisor. Let’s see how we can do that. We start by opening Metaeditor, which can be done by clicking a little icon or pressing F4. The code begins by including the Trade module, which provides functions for trading operations. We then create an instance of the CTrade class, which will allow us to execute buy and sell trades. An input variable named MassTest is defined with a default value of 1, which can be used for mass testing purposes. The OnTick function is where the main logic of the Expert Advisor is executed. First, we calculate the Ask price for the current symbol and normalize it to the number of digits specified by the _Digits variable. Similarly, we calculate and normalize the Ask prices for the GBPUSD and USDCAD currency pairs.
Next, we calculate the Bid price for the current symbol and normalize it, followed by the Bid prices for GBPUSD and USDCAD. A string variable named signal is created to store the trading signal. We initialize the random number generator using the current tick count and generate a random number between 0 and 1. If the random number is 0, we set the signal to “buy,” and if it is 1, we set the signal to “sell.” If the signal is “sell” and the total number of open positions is less than 10, we execute a sell trade for 0.10 lots, which is equivalent to 10 Micro Lots. The trade is executed at the Bid price with a take profit set 250 points below the Bid price. This process is repeated for the GBPUSD and USDCAD currency pairs. If the signal is “buy” and the total number of open positions is less than 10, we execute a buy trade for 0.10 lots at the Ask price with a take profit set 250 points above the Ask price. This is also repeated for the GBPUSD and USDCAD pairs.
Once the code is complete, we press F7 to compile it. If this was too fast for you or if you don’t understand what all the code is doing, you might want to check out the Premium course on our website or watch one of the basic videos first. After compiling, we go back to MetaTrader by pressing F4 or clicking on the icon. In MetaTrader, we press Control and R to start the strategy tester, select the Expert Advisor we just created, enable the visual mode, and start a strategy test. We should then see the Expert Advisor on the chart. If you are already a Premium course member and have an idea for a video like this one, feel free to send us an email. In this video, we have learned how to create an Expert Advisor that uses random signals to execute buy and sell trades with a few lines of MQL code. Thanks for watching, and I will see you in the next video.