MQL5 TUTORIAL BASICS – 50 SIMPLE RANDOM ENTRY EXPERT ADVISOR

video
play-sharp-fill

 

  • Introduction to Mass Testing with Random Entries (00:00 – 00:12) Introduction to mass testing in MQL5 using buy and sell signals created by a random generator.
  • Explanation of Visual Mode Testing and Quick Test Example (00:12 – 00:34) Explanation of visual mode testing in MetaTrader and a quick test example for the year 2018, showing varying profit and loss results.
  • Demonstration of Different Results with Random Entries (00:34 – 01:31) Repeating the test with the same settings, demonstrating different results due to random generated entries.
  • Setting Up for Mass Testing in MQL5 (01:31 – 02:44) Instructions on setting up for mass testing, including modifying the source code and using an external variable for mass testing.
  • Enabling Optimization and Adjusting Settings for Mass Testing (02:44 – 03:26) Enabling optimization in the MetaTrader strategy tester and adjusting settings for mass testing.
  • Viewing Optimization Results and Analyzing Profitability (03:26 – 03:52) Viewing optimization results, analyzing the number of profitable outcomes compared to losses.
  • Exporting and Analyzing Results in a Spreadsheet (03:52 – 05:00) Exporting results to XML, opening in a spreadsheet, and analyzing the profit column to calculate the overall result.
  • Conclusion on the Power of Automated Trading and Mass Testing (05:00 – 05:54) Concluding remarks on the power of automated trading and the ability to perform hundreds or thousands of tests with mass testing.
  • Encouragement to Try Mass Testing with MQL5 Code (05:54 – 06:00) Encouraging viewers to try mass testing with random based entries using MQL5 code.

 

In this video we are going to trade random entries, you might find random entries very strange but I promise that you might find the results very interesting, so let’s find out how to do that with mql5.
To get started please click on the little icon here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file simple random entry ea, click on continue, continue and finish.
Now you can delete everything above the on tick function and the two comment lines here, we start with an include function to include the file trade dot mqh, this one comes with simplified trading functions. Now we create an instance of ctrade that will be called trade and in the next step we will create an input variable that will be an integer variable called mass test and the first initial value is one, that one will become interesting in the next video when we talk about mass testing.
Inside of the on tick function we want to calculate the ask price that is done by using symbol info double for the current symbol on the chart, we use symbol underscore ask – all in capital letters – to calculate the ask price and with normalized double and underscore digits we are going to automatically calculate the right number of digits behind the dot that depends on the currency pair we use because some currency pairs have three digits behind the dot while others have five digits behind the dot.
Let’s repeat that process, this time we want to calculate the bid price, everything is the same except of this parameter, this time we are using symbol underscore bid – all in capital letters – and for the signal we create a string variable that is called signal, so far we have no value because that’s what we are going to calculate now.
Our random generator needs an initial value and we use get tick count to do that, get tick count returns the number of milliseconds that have passed since the system was started, math srand sets the starting point for the pseudorandom integers, so let’s actually calculate a random number by using math rand and this expression here will create either zero or a one and when the random number equals zero we want to set our signal to buy, otherwise, if the random number equals one our signal should have the value sell because now we want to sell.
So if the signal equals sell and positions total is below one – that would mean we have a sell signal and no open positions – and now we use trade dot sell to sell ten micro lot.
Otherwise, if the signal equals buy and we have no open positions we use trade dot buy and buy ten micro lot.
Finally, we want to create a chart output that is done by using the comment function and it will output the text the signal is now followed by the current signal.
That’s about it, if you have no clue what all the code here does or if you think that this was too fast for you maybe you want to watch one of the other videos from this basic video series first or maybe even the premium course that you can buy on our website might be interesting for you.
For now, please click on the compile button, you shouldn’t get any errors here and if this is the case you can click on a little button here or press a F4 to go back to Metatrader.
In Metatrader we pick an empty chart, right-click, select templates, save template and save it as tester dot tpl because this is what we are going to see in the backtest.
Now you want to click on view, strategy tester or press control and r, we want to do a single test, please pick the new file simple random entry ea dot ex5, mark the visual mode here and start a test.
Here is our little expert advisor, it is running and it constantly produces buy and sell signals, so let’s set that one to full speed, here we have some results so let’s stop that for now, repeat the test, start it again and this time we have different results.
Let’s do another one, I don’t use the visual mode here to save some time and this time we should see the third result. All results are different that’s because of the random entry and in the next video we are going to do mass testing but for now, you know that random entries do work, our expert advisor is able to trade buy and sell trades that have been created by a random generator and you have coded it yourself with a few lines of mql5 code.