• Introduction to Creating a Price Range for Buy Trades (00:00 – 00:17) Introduction to creating a price range for buy trades in MQL5, allowing positions to open within two defined limits.
  • Opening MetaEditor and Creating a New Expert Advisor File (00:17 – 00:48) Instructions on opening MetaEditor and creating a new expert advisor file named “Simple Buy Range.”
  • Setting Up the Code Structure (00:48 – 01:01) Deleting unnecessary code above the ‘on tick’ function.
  • Including the ‘trade.mqh’ File and Creating an Instance of CTrade (01:01 – 01:16) Including the ‘trade.mqh’ file for simplified trading functions and creating an instance of the CTrade class.
  • Defining Upper and Lower Limits as User-Defined Variables (01:16 – 01:59) Creating user-defined variables for the upper and lower limits of the price range.
  • Calculating the Ask Price (01:59 – 02:19) Calculating the ask price using ‘SymbolInfoDouble’ and ‘NormalizeDouble’.
  • Checking Entry Conditions for No Open Positions and Price Range (02:19 – 02:47) Checking if there are no open positions and if the price is within the allowed range.
  • Opening a New Position and Outputting Information (02:47 – 03:16) Opening a new position with ‘’ and outputting the upper and lower limits, ask price, and a Boolean value for the price range.
  • Creating a Function to Check if Price is Within Range (03:16 – 04:17) Creating a function to return a Boolean value indicating if the ask price is within the defined range.
  • Compilation and Testing in MetaTrader (04:17 – 05:00) Compiling the code and instructions on testing the expert advisor in MetaTrader using the strategy tester.
  • Demonstration of the Expert Advisor in MetaTrader (05:00 – 05:50) Demonstrating the expert advisor in MetaTrader, showing how it opens buy trades when the price is within the range.


In this video we are going to create a price range for buy trades, it is allowed to open positions within the two defined limits here, so let’s find out how to do that with mql5.
These are two user-defined variables and our expert advisor is going to open trades within these two boundaries so let’s 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 buy range, 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 statement to include the file trade dot mqh, it comes with mql5 and it includes some simplified trade functions so let’s create an instance of the class ctrade, we will call that trade and we are going to use it to open a position later on.
I also want to define an upper limit that is done by using the input modifier here because with the prefix input you can create a user-defined variable and that can be changed in the strategy tester without any further coding, it’s a double variable, I will call it upper limit and the initial value will be ten.
Let’s repeat that for the lower limit, that will be zero and we also need to create a global variable for the ask price because we are going to use the ask price in two different functions.
Inside of the on tick function, we 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 – and with normalize double and underscore digits we make sure that we calculate the right number of digits behind the dot.
For the entry conditions, I would like to know if the return value for positions total equals zero, if this is true we have no open positions and I would also like to know if the price is in the allowed range and only if this is true we use trade dot buy and open a new position for ten micro lot.
To see what’s going on we will use the comment statement that will output the upper and the lower limit, the ask price and a Boolean value for price is within range.
To get the value true or false we need to create this function because it doesn’t exist right now.
The return type of the function is bool, so what does that mean?
The bool type can either have the value true or false that is equal to one for true or zero for false.
First, we create a variable, this one is Boolean, the name is return value and the initial value is false and now we check if the ask price is below the upper limit and if the ask price is above the lower limit and only if both conditions are true we will set the return value to true.
Finally, we use the return statement to return the value to the main module and 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.
This video was actually requested from one of the premium course members so if you have a request and if you are a premium course member just send me an email, for now, we click on the compile button or press F7.
And if we don’t see any errors we can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader, we click on view, strategy tester or press control and r, please pick the new file, simple buy range dot ex5, mark the option for the visual mode here and start a test.
Here we are!
The expert advisor says that the price is within the range so we see that it is opening buy trades. Let’s speed that up a little bit! And now the price is outside of the range, we don’t see any further buy trades so our expert advisor working as expected and in this little video you have learned how you can create user-defined input variables that can be adjusted by the user and you have coded it yourself with a few lines of mql5 code.