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 mql4.
To get started please click on a little icon here or press F4 on your keyboard, now you should see the Metaeditor 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.
This is one of the rare cases where mql4 and mql5 are almost similar. We will use something that is called account info double.
If you know mql5 you will already know how that works but for mql4 it use to be completely different but after an upgrade, it is possible to use account info double and get the required information.
We will start with account margin and as you see there are lots of more entries here, the procedure is the same for each of the property values, so let’s repeat that for the free margin and now you can see that this term here has a different color than this one and that is because the old way to get those values in mql4 included a function that was called account margin. So let’s change that to my account margin and now it’s black.
Let’s repeat the same thing for the initial account margin that will work but currently inside of the mql4 reference they say that it is not supported, that’s also true for the property account margin maintenance.
This is one of those cases where I don’t understand why they implement something that is not supported but I have included it anyhow because it works with mql5.
Now let’s continue with the account margin level, this is the calculated value here and it should be equal to what you see for the margin level here in Metatrader 4.
The next one would be the account margin maintenance, as I said this is not supported in Metatrader 4 but it is part of the language now.
The next property is one of the interesting ones, this is the stop out call. In my case, the stop out call would be at sixty percent of my account level. The next one is the stop out level, that’s when your broker is going to close everything and that would be reached when my account equity goes below forty percent.
Let’s include the account profit as you see this is another function, so let’s call that my account profit and now there is something that is called account info integer and to calculate the margin stop out mode we would use account info integer and here the parameter we have to pass is account margin so mode, but this would return an integer value and I would like to have that a little bit more readable, so let’s create a string variable called account margin stop out mode readable, we don’t assign a value here and if the account margin stop out mode would return a zero we assign the sentence, account stop out mode is in percent to our variable here.
Otherwise, if the return value for the account margin stop out mode is one that’s when we would assign the sentence account stop out mode is in money.
Finally, we want to add a comment statement to create an output that will show us all the calculated values on our chart.
Well, if this was too fast for you or if you don’t know what all the code here does maybe you want to watch one of the other videos in the basic video series first or maybe even the premium course on our website might be interesting for you. Actually, this was a suggestion of a premium course member and if you are a premium course member and have ideas for videos like this one, just send me an email.
Okay! For now, we can click on the compile button or press F7, I have messed up something up here! Oh, this one is wrong and here I have to remove the braces and now when I recompile I don’t get any errors and if this is the case we can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader we use one of the charts, now we pick the new file, simple margin monitor and drop it on a live chart because in the strategy tester you wouldn’t see very much because this expert advisor does not open any positions so your margin settings would never change but when you drop it on a live chart you should see all the values that we have calculated and in this little video you have learned how to get all the margin related information for your Metatrader 4 account and you have coded it yourself with a few lines of mql4 code.