MQL4 TUTORIAL BASICS – 50 SIMPLE RANDOM ENTRY EXPERT ADVISOR

video
play-sharp-fill

In this video we are going to create random based entry signals, that might sound a little strange but I promise that the results will be very interesting, so let’s find out how to do that with mql4.
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 by creating an integer variable with the modifier extern; that will allow the user to change the setting for this variable and it will become interesting in the next video when we are going to create mass tests.
Inside of the on tick function, we start by creating a signal but we don’t assign any value here because we are going to calculate that now.
First, we start with this function, it is called math srand and when you mark it and press F1 you will learn that it sets the starting point for generating a series of pseudorandom integer numbers and to get a unique starting value we use get tick count as a parameter because get tick count returns the number of milliseconds that have passed since we have started our system and that number will always be unique and therefore the combination of these two functions will always create a unique series of random numbers.
To calculate our random number we use math rand, that’s a different one, this one is with an additional s and math rand returns pseudorandom integer within the range of zero to thirty-two thousand but with this expression, we will get two different values, either a zero or a one and if our random number equals zero we want to buy so we assign the value buy to our signal.
Otherwise, if the random number equals one, we want to sell so now we assign the value sell to our signal.
And if our signal equals buy and the value for orders total is zero – that would mean that we have no open orders and we have a buy signal – and now we use order send to buy ten micro lot.
Otherwise, if the signal equals sell and orders total is also zero – that would mean we have no open orders and a sell signal – and now we use order send to sell ten micro lot.
Finally, we use the comment function to output the text the current signal is followed by the calculated signal.
That’s about it.
If you don’t understand what all the code here does or if this was too fast for you maybe you want to watch one of the other videos in this basic video series or maybe even the premium course on our website might be interesting for you.
For now, please click on the compile button, you shouldn’t get any errors and if that is the case you can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader you want to right-click into an empty chart, click on template, save template and save the template as tester dot tpl, you can overwrite the old value because tester dot tpl is what we will see when we start the strategy test and to do that please click on view, strategy tester or press control and r on your keyboard, please pick the new file, simple random entry ea dot ex4, mark the visual mode here and start a test.
Here we are!
The expert advisors running, let’s go to full speed and you should get some results here so basically the expert adviser is working but it’s a random entry so let’s stop that, disable the visual mode, start a new test.
In my case we have made a net profit of five hundred twenty-three dollars and forty cents now I will repeat the test and because it’s a random entry we should now get a different result.
We can already see that this time the profit was only eighty-three dollars and fifty-four cents but it is still a random based entry and it is a profit.
Let’s do another one!
And this time we made seven hundred and twenty-nine dollars profit so it is possible to use a random based entry to generate profits and in the next video we are going to do mass testing but for now you know how to create a random based entry signal that can be automatically traded in your expert advisor and you have coded it yourself with a few lines of mql4 code.