MQL5 TUTORIAL – SIMPLE DYNAMIC POSITION SIZE
Audio into Text Transcription
In this video, we are going to open lots and lots of trades to show you how to set a Dynamic Position Size. Let’s speed up the process here, now you should see the Dynamic Position Size is changing. Now its eight micro lots, let us speed it up a little bit more, now it’s nine micro lots, now its seven micro lots and right now we are down to six micro lots.
How can we create an Expert Advisor that is able to calculate a Dynamic Position Size based on the equity?
To do that please click the little button here in the Metatrader or press the F4 key on your keyboard.
And now you should see the Metaeditor and here you want to click on “File/ New/ Experts Advisor” from template, I will call this one: Simple Dynamic Lot Size, Continue, Continue and Finish. And now you can delete everything above the OnTick Function and the two comment lines here.
First, we need to include the file: Trade.mqh. This file is included in the MQL5 and now we want to create an instance of CTrade.
We will call it Trade. In the first step, we need to get the Account Equity, that is done by using the AccountInfoDouble, we are using the term ACCOUNT_EQUITY. For the Account Balance, you could use ACCOUNT_BALANCE and here are several other Account Properties you could use but for now we need the Equity.
To set the position size dynamically we created a double variable called: DynamicPositionSize and the lot size should be the Equity divided by one Million. With NormalizeDouble and the two digits here we want to create an output that only has two digits behind the dot. For some reason that doesn’t work always. Let’s speed it up a little bit, it seems to work right now.
Now we need to get the Ask price, that can be done with SymbolInfoDouble, for the current Symbol, and here we use SYMBOL_ASK and for the Bid price you would use SYMBOL_BID and with NormalizeDouble and _Digits we ensure that the Ask price is calculated with the right number of digits here because there are three digit and five digit currency pairs.
Now if we have below 100 positions and below 100 orders we want to create a Buy Stop with our Dynamic Position Size. The Buy Stop will be placed 100 points above the Ask price for the current currency pair, we haven’t defined a Stop Loss and the Take Profit will be 300 points above the current Ask price. We use the current time without any expiration and we don’t need a comment.
In the last step, we are going to use the Comment function to output the text: Dynamic Position Size and the Calculated Position Size.
In the last step, we use a bracket to close our module and now you can click on “Compile” or press the F7 key on your keyboard and that worked without any errors. Now click here or press F4 to go back to Metatrader. In Metatrader we want to click on “View/ Strategy Tester”, select the file: SimpleDynamicLotSize.ex5 and click on start.
Now you should see that the lot size for your position size is calculated based on your Equity.
Okay, in this little video, you have learned how to calculate a Dynamic Position Size in MQL5 and you have done it yourself with a few lines of MQL5 code.
Download “MQL5 Tutorial - Simple Position Size”SimpleDynamicLotSize.txt – Downloaded 737 times – 825 B