MQL4 TUTORIAL BASICS - 50 SIMPLE RANDOM ENTRY EXPERT ADVISOR

Опубликовано: 01 Январь 1970
на канале: MQL4Tutorial
3,952
69

https://mql4tutorial.com

With MQL4 for Metatrader4 we create an Expert Advisor to trade random entries. These are buy and sell signals that have been created with a random generator and we use Metatrader to automatically trade them

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.