MQL4 TUTORIAL – ADVANCED RANDOM ENTRY

video
play-sharp-fill

In this video we are going to create a Random Entry module, I’m using the Forex Trading Framework and the reason I like to use random entries is that most people think that any kind of entry like RSI, Bollinger Bands or MACD should be better than a random entry, but if you really want to find out, it makes sense to try it to measure the results and to compare that with your favorite entry and find out if one of the other entries actually has an advantage over the random entry.
Now how can we create an MQL4 Expert Advisor that is able to produce and trade random entries?
To do that please click on the little button here or press F4 on your keyboard and 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 file: “CheckEntry_Random”, click on “Continue”,” Continue” and ”Finish” and now you can remove everything that is above the “OnTick” function here and let’s also delete the two command lines.
Actually we need to replace the “OnTick” function with a string function that is called: Check Entry (CheckEntry) because that is the name of the function that I will call later on and here we need to create an empty string variable that will be called signal, it’s of the type string because it will contain a text like buy or sell but right now we don’t assign any value because we are going to calculate that later on.
Let’s initialize the random generator, that is done with the function “MathSrand”, let’s mark the function and hit F1 and you will see that it sets the starting point for generating a series of pseudorandom integers and we need a value to initialize it. That will be the function “GetTickCount”.
“GetTickCount” returns the number of milliseconds that elapsed since the system start, so that value is always different value, that’s actually what we want because with a random entry you want to have different entries each time you trade the same time span.
In the next step we want to create a random number 0 or 1; that can be done by using “MathRand” without the s in the middle.
Let’s see what the help has to say about “MathRand”, that returns a pseudorandom integer within the range of 0 to 32,767, we don’t need such a high value here, so we use “%2”, that will either give us a 0 or a 1, and if the random direction equals 0 we want to assign the word “buy” to our signal in the other case if the random direction equals 1 we assign the word “sell” to our signal.
In the last step we send the random entry to the main module by using the “return” function for our signal variable.
Now what’s a main module?
In my case it’s the Forex Trading Framework from February 2018.
A framework basically uses several functions to open and manage trades, you can use it to trade our random entry or Bollinger Bands or whatever entry you like. If you don’t have the Forex Trading Framework, you can download it on my website: forextradingframework.com or you could also use your own main module. So here’s the “include” function for our newly created Check Entry Random (CheckEntry_Random) module, I have to save this one, recompile the framework by clicking on the little button here or pressing F7 and that worked without any errors, so now we can click here or press F4 to go back to Metatrader.
In Metatrader you want to click on “View/ Strategy Tester” or press CTRL + R that will bring up the Strategy Tester panel here and here you need to select your main module, in my case the Forex Trading Framework, select a currency pair, we are going to trade from January 2017 to the end of March 2017, let’s enable the visualization mode here and start a new test.
Here is our first random entry, I will now do a full speed back test, let’s switch to the “graph” tab here, this is our balance curve, here you can see the current equities rising, we are now in the middle of March and in a few seconds our test should be finished. That happened right now, here is our end result; we made a total net profit of 1,059 Dollars.
Let’s remove the visual mode to speed things up and repeat the test; that should be much faster this time because Metatrader doesn’t have to render the chart.
Here is our result and this time we made 362 Dollars.
The reason why we have different results is because each and every time we trade we get different entry signals because we used a random generator and now you know how to create an entry module to trade random entries and you have coded it yourself with a few lines of MQL4 for codes.