In this video we are going to create a simple margin monitor that is going to output all the margin related information on our chart, so let’s find out how to do that with mql5.
To get started please click on a 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 margin monitor, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
We will use account info double to get most of the information that we need, this is one of the rare cases where mql4 and mql5 are almost similar.
To get the desired information we need to create a variable and use account info double for a certain property, in this case, the account margin.
Let’s repeat that for the free account margin.
There is also a property that is called account margin initial. The documentation says that it is the initial margin and the amount reserved on an account to cover the margin of all pending orders. Well, if you see the return value is zero here, that’s also the case for account margin maintenance and as I have created an mql4 video for this topic I have seen that the mql4 reference says that this is not supported but maybe it depends on the account type, so let’s continue.
The next level will be the account margin level, and when you click on view, toolbox or press control and t you should see the same value for the margin level here. There is also something called account margin maintenance, they say it is for the minimum equity reserved on an account to cover the minimum of all open positions. Well, I have an account, I have open positions but the return value for account margin maintenance is zero, for me more interesting is the account margin stop out call, that’s the level when your broker will tell you that you don’t have enough money. For my account type, this is set to forty percent and there is the final stop out level for the account, that’s when your broker is going to close everything and for my account type, this will happen when my equity goes below ten percent.
I would like to add another value for my account profit to my margin monitor and that’s it for the account info double values, but there is also something that is called account info integer and we can use it to get the account margin mode, this is something that is different because Metatrader 4 didn’t know anything about margin modes. In Metatrader 5 we started by using the netting mode if you use an account with netting mode you are not able to trade several positions for the same currency pair as they say only one position can exist for one symbol, so what we are using is the hedging mode and there is also an exchange mode, I have never used that before.
I am not very good at remembering integer values so let’s create a string variable called account margin mode readable and depending on the return type of the integer value we will assign the text, netting mode, exchange mode or hedging mode.
You see that my margin monitor is showing that we are using the hedging mode here.
Another margin related value is the account margin stop out mode. We will get that by using account info integer for account margin so mode and this property has two return types, it can either be a stop out mode that is calculated in percents or it’s a money value, so let’s create a string variable to make those return types more readable and depending on the return type we will either create an output that is showing us that the account stop out mode is in percent or if the return value is one we will see that the account stop out mode is in money.
Finally, we will add a comment statement that will output all the calculated values on our chart.
If this was too fast for you or if you don’t understand what all the code here does 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 a suggestion from a premium course member and if you are already a premium course member and have an idea for such a video just send me an email.
For now, we click on the compile button here and I have messed up something! Yes, I missed a semicolon so let’s add that, recompile the code and this time I don’t have any errors here and if this is the case for you you can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader we simply use a live chart, scroll down, please pick the new file, simple margin monitor, that we have created and drop it on your chart. We don’t use the strategy tester because in this expert advisor we do not open any positions so your margin monitor would show you always the same values but when you drop it on a live chart you should see that all the margin related information appears on your chart and in this little video you have learned how to create a margin related monitor for Metatrader 5 and you have coded it yourself with a few lines of mql5 code.