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 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 be 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.
Latest posts by admin (see all)
- AUDUSD approaching major resistance, prepare to sell - May 18, 2017
- Online Forex Trading: The Benefits and Dangers - May 16, 2017
- How To Trade The Fractal Indicator - April 3, 2017
Winner’s Edge Trading, as seen on: