Nice big storm here last night, lots of rain, wind, and hail, but we survived. Once again, it's a beautiful morning.
If you’re new to this series and want to check it out from the beginning, you can find that here. And look here for a list of all the programming articles.
In Part 32 I revealed another super-secret include function PcntTradeSize(). In this installment, we'll discuss the ins and out of the function and maybe we'll get into a couple of other features that are used in the function.
The PcntTradeSize() function returns the lot size that risks the specified percentage of your account based upon the designated pip risk of the trade. The lot size is returned as a double value since most brokers designate lots with two decimal places. It adjusts the lot size so that it conforms to the minimum, maximum, and step of lot sizes for your broker's platform.
The way I wrote this function is that it will return the minimum size even if that exceeds the designated risk for the trade, so if that is not acceptable, you may want to modify the function. It also 4/5 rounds the lot size to conform to the minimum lot step. Some traders may prefer to round it down instead.
double PcntTradeSize(string symbol,int PipRisk,double Pcnt,int NumOfPos=1, bool FixedAmt=false,bool Diagnose=false)
symbol - string - the symbol of the pair that is going to be traded. If you want to use the symbol of the current chart, you can use Symbol().
PipRisk - int - this is the actual number of pips you are risking on the trade (not points.)
Pcnt - double - this is the percentage of your account you wish to risk on the trade. In other words, if you wish to risk 3% of your account on the trade, you would use 3 (not 0.03.)
NumOfPos - int - this would be the number of positions you will be putting on the trade. The function would simply divide the total position size by this number and adjust each position size to conform to the minimum, maximum, and step size allowed by your broker. You'll notice this variable has been initialized in the formal parameters of the function. That means you can leave this place empty when you call the function and it will default to the value specified.
FixedAmt - bool - this was actually added in a later version of the function in response to a special need. If true, it designates the Pcnt value as a fixed amount to risk rather than a percentage of the account size. It still calculates the lot size and adjusts it for the broker's min, max, and step.
It just starts with a flat dollar (euro, pound, whatever) value and performs the calculations from that point, skipping the calculation of the account percentage. Again, you can leave this parameter empty and it will default to false. Using that feature of MQL4 allows me to add parameters and additional functionality to the function without having to modify the calls in prior uses of the function.
Diagnose - bool - this puts the function in diagnostic mode. It's usually not necessary since the function is tested, but if I need to see what values are being passed to the function, I can set this to true and it will send diagnostic information to the experts log (using the Print statement.) Again, this will default to false if left empty.
And those are the ins and outs for the function PcntTradeSize().
If you have any ideas to modify this or the MAXover() function to make it even more super-cool, please let me know. I love new ideas! Next time we'll discuss the built-in function, MarketInfo() and if we have space, we'll talk about using the MAXover() and PcntTradeSize() functions in our EA. Then maybe we can get back to doing a little testing.
Thanks for your attention and please follow me on Twitter and LinkedIn.
Please leave a comment below if you have any questions!
Also, please give this strategy a 5 star if you enjoyed it!