MQL5 TUTORIAL – 135 SIMPLE LOT SIZE ROUNDING

video
play-sharp-fill

 

Introduction to Lot Size Rounding in MQL5 (00:00 – 00:11)

  • Introduction to calculating a lot size based on the account balance and how to round up or down the result to make it fit in MQL5.

Opening MetaEditor for MQL5 Programming (00:11 – 00:20)

  • Instructions on how to open MetaEditor in MetaTrader for MQL5 programming, either by clicking an icon or pressing the F4 key.

Creating a New Expert Advisor (00:20 – 00:28)

  • Steps to create a new Expert Advisor from a template in MetaEditor, with the example file named ‘Simple Lot Size Rounding’.

Starting with the OnTick Function (00:28 – 00:35)

  • Beginning with the ‘OnTick’ function to calculate the account balance using the ‘AccountInfoDouble’ function.

Calculating Position Size and Rounding Variables (00:35 – 00:49)

  • Explanation of calculating the position size based on the account balance and creating variables for rounded values, both up and down.

Defining Rounding Functions (00:49 – 01:01)

  • Introduction to custom functions ‘RoundUp’ and ‘RoundDown’ for rounding the calculated lot size to a specified number of decimal places.

Formatting and Outputting Results (01:01 – 01:18)

  • Using ‘NormalizeDouble’ to format the final results and outputting them on the chart using the ‘Comment’ function.

Implementing Rounding Functions (01:18 – 01:35)

  • Detailed explanation of the ‘RoundUp’ function, including parameters for position size and decimal places.

Calculating Rounded Values (01:35 – 01:48)

  • Description of how to calculate the rounded values using ‘MathCeil’ and ‘MathFloor’ functions for rounding up and down, respectively.

Final Results and Adjustments for Account Types (01:48 – 02:20)

  • Observing the final rounded values and discussing how to adjust these for different account types to ensure correct position opening.

Support for Different Microlot Sizes (02:20 – 02:30)

  • Mentioning that not all account types support staggering in microlot sizes down to the second decimal place.

Premium Customer Requests and Course Information (02:30 – 02:46)

  • Acknowledging that the tutorial was a request from a premium customer and mentioning the availability of a premium course on their website.

Conclusion: Rounding Lot Sizes in MQL5 (02:46 – 03:07)

  • Concluding the video with a summary of how to round up or down lot sizes in MQL5 to fit specific account types, and highlighting the simplicity of the programming involved.

 

In this video, let’s take a look at how to calculate a lot size based on the account balance and how to round up or down the result to make it fit. So let’s take a look at how to do that with MQL Five. To do that, in MetaTrader please click on the icon up here or press the F Four key. Then you should end up in the MetaEditor. And here we click on File, New Expert Advisor from template, next, assign a name here. I have already done this. My file is called Simple Lot Size Rounding and we’ve already pre-programmed this. We’ll start with the on tick function. There we first want to calculate the account balance. This is done using the Account Info Double, Account Balance function and based on the account balance we want to calculate the position size. In our case, we use the gross balance value and divide it by 100,000. We also create two variables for the rounded values, one rounded up and one rounded down. We determine these with the help of the respective function, which we program ourselves.

 

The first function is called Round up, the second Round down and we pass the calculated lot size and the value for the decimal rounding as parameters. We want to round this to one decimal place. With Normalize Double, we can output the final results in a more formatted way. Although we have specified two decimal places here, we see that our results are truncated after the first digit. And using the Comment function, we output the whole thing on the chart. Let’s move on to the actual functions. To round up, we pass our input value. That’s the position size that we passed above and our rounding value. Let’s add the second parameter here for the decimal places. And to get the result, we multiply the rounding value by the result of this function. If you press F1 once and call that up in the help, you’ll see that there’s an integer value returned. And as parameters, we pass the input value and the rounding value to the MathCeil function. Then we use Return to return our result back to the function. The second function for rounding the value works similarly.

 

Here, however, we do not use MathCeil, but MathFloor. And here we also return the result to our main function. And this then results in the rounded up value 0.3 and the rounded down value 0.2 for a determined lot size of 0.25 lot. Depending on which account type you have, you can adjust this accordingly and thus pass the correct values so that a position can also be opened correctly. Because not all content types support the staggering in microlot sizes down to the second decimal place. By the way, this was again a request from a premium customer. And if you are a premium customer and have an idea for such a video, just send me an email. Otherwise, you can find the premium course on our website. And in this video, you learned how to easily round up or round down lot sizes so that that fits your account type. And you programmed that yourself with these few lines here in MQL5.