MQL5 Tutorial – Simple Standalone Random Entry EA (00:00 – 00:02)
- Introduction to creating a Standalone Expert Advisor that trades random entries in MQL5.
Creating the Expert Advisor in MetaEditor (00:02 – 00:41)
- Instructions on opening MetaEditor in MetaTrader and creating a new Expert Advisor file named ‘SimpleStandaloneRandomEA’.
Initial Code Setup (00:41 – 00:52)
- Removing unnecessary lines and setting up the initial code in MetaEditor.
Including Trade.mqh and Initializing CTrade (00:52 – 01:04)
- Including ‘Trade.mqh’ to create an instance of the ‘CTrade’ class for opening positions.
Calculating Ask and Bid Prices (01:04 – 01:33)
- Using ‘SymbolInfoDouble’ to calculate Ask and Bid prices and normalizing them with ‘NormalizeDouble’ and ‘_Digits’.
Setting Up Price Array (01:33 – 01:57)
- Creating a price information array and sorting it from the current candle downwards using ‘ArraySetAsSeries’.
Filling Price Array with Data (01:57 – 02:08)
- Filling the price array with data for the current symbol and selected period on the chart using ‘CopyRates’.
Creating Signal Variable (02:08 – 02:22)
- Creating a string variable ‘signal’ to calculate the signal later on.
Random Number Generation for Signals (02:22 – 02:41)
- Using ‘MathSrand’ and ‘GetTickCount’ to initialize the random generator and ‘MathRand’ to calculate a random number.
Assigning Buy or Sell Signals (02:41 – 03:16)
- Assigning ‘buy’ or ‘sell’ to the signal variable based on the random number generated.
Executing Trades Based on Signals (03:16 – 03:32)
- Using ‘trade.Sell’ or ‘trade.Buy’ to execute trades based on the calculated signal and the absence of open positions.
Displaying Signal on Chart (03:32 – 03:46)
- Using the ‘Comment’ function to output the current signal on the chart.
Compiling and Testing the Expert Advisor (03:46 – 04:16)
- Instructions for compiling the Expert Advisor and testing it in MetaTrader using the Strategy Tester.
Observing the Expert Advisor’s Performance (04:16 – 04:44)
- Watching the Expert Advisor’s performance in the strategy tester and discussing the results of random entry trading.
In this video we are going to create a Standalone Expert Advisor that is going to trade random entries. It creates buy and sell signals directly on the chart, here is our first open position and now we are going to find out how to create something like this in and MQL5.
Download “MQL5 TUTORIAL - SIMPLE STANDALONE RANDOM EXPERT ADVISOR”
To do that please click on the little button here or press F4 in your Metatrader, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this one: “SimpleStandaloneRandomEA”, click on “Continue”, “Continue” and “Finish” and now we are going to delete everything above the “OnTick” function and the two comment lines here.
We start by using the “include” function to include the file: “Trade.mqh” that one comes with MQL5 and it allows us to create an instance of the class “CTrade” that we will call: “trade” and we are going to use it later on to open new positions.
Inside of the “OnTick” function we want to calculate the Ask price and the Bid price that’s done by using “SymbolInfoDouble”, the first parameter is for the current currency pair on the chart, the second one will deliver the Ask price or the Bid price, it depends on if we use “SYMBOL_ASK” or “SYMBOL _BID” and “NormalizeDouble” and “_Digits” will make sure that we get the right amount of digits behind the dot.
Now we use “MqlRates” that structure stores the information about prices, volumes and spread for the candles. We sort the price info array (PriceInfo) downwards from the current candle by using “ArraySetAsSeries” and with “CopyRates” we fill our price info array (PriceInfo) for the current symbol and the currently selected period on the chart with price data from the current candle 0 (zero) for 3 candles.
Now let’s create a string variable called: “signal” that will be used to calculate the signal later on, right now it’s empty and here comes the interesting part; we use ”MathSrand” and “GetTickCount” to initialize the random generator because we want to calculate a random number. We use “MathRand” %2 because “MathRand” can return a pseudorandom integer within the range of 0 to 32,767 but we only need 2 random numbers, so let’s say if the random number is 0 (zero) that would be a buy signal and in that case we would assign the word: “buy” to our signal. Otherwise if the random number is 1 we suppose that this one is a sell signal so we assign the word: “sell” to our signal and whenever the signal equals sell and “PositionsTotal” is below 1 – in other words we don’t have any open positions – we use “trade.Sell” to sell 10 micro lot. Otherwise if the signal equals buy and we have no open positions we use “trade.Buy” to buy 10 micro lot.
Finally we use the “Comment” function to output the words: ”The current signal is:” followed by the calculated “signal” directly on our chart.
That’s it, please click on the “Compile” button or press F7, now you should see that it went well without any errors and in that case you can click here or press F4 to go back to Metatrader.
In Metatrader we click on: “View/ Strategy Tester” or we press CTRL and R, please select the “SimpleStandaloneRandomEA” file, enable the visualization here and start your test.
And now you should see the first trade on the chart, let’s speed that up a little bit here and you see that we have made several trades, in this case we have made a profit and if you have no idea which results you could expect from a random entry you should definitely try this out.
Okay. In this video you have learned how to create an MQL5 Expert Advisor that is going to trade random entries and output signals directly on the chart and you have coded it yourself with a few lines of MQL5 code.
Download “MQL5 TUTORIAL - SIMPLE STANDALONE RANDOM EXPERT ADVISOR”SimpleStandaloneRandomEA.txt – Downloaded 377 times – 1.38 KB