In Part 97 we discussed the functions provided by MQL4 for Trade Size Validation. This time we’ll revisit and old friend for a more detailed look at validating your trade sizes.
PcntTradeSize() calculates your trade size as a percentage of the smaller of your account balance or account equity. Again, account balance is the total amount of cash in your trading account, account equity adjusts your balance for any open positions. We discussed the function in depth in the earlier referenced articles. What I want to discuss is the portion of the function that validates the trade size based upon MQL4’s function we discussed in the last installment.
After calculating the exact trade size based on the designated percentage, we use the following code to validate the trade size:
if(TradeSize > MarketInfo(symbol,MODE_MAXLOT)) TradeSize = MarketInfo(symbol,MODE_MAXLOT); if(TradeSize < MarketInfo(symbol,MODE_MINLOT)) TradeSize = MarketInfo(symbol,MODE_MINLOT); TradeSize = MathRound(TradeSize / MarketInfo(symbol,MODE_LOTSTEP) - 0.5); TradeSize = TradeSize * MarketInfo(symbol,MODE_LOTSTEP);
My first step was to be sure the trade size didn’t exceed the maximum, if so, I set it to the maximum.
Next, I checked the total against the minimum. Here’s something you should be careful about. If the trade size is less than the minimum, I up it to the minimum. That was pure laziness on my part. What really should happen is that we have a lot of bells and whistles sound that you are not able to make the trade because your trade size was too small for this trading platform. But, for me, I just know that this happens and I’m careful about it. Be sure to be careful with this yourself. If you’re trading a small account with a broker that has a “large” minimum trade size, this could mess you up. Be smart. You should NEVER trade with a trade size that exceeds your designated risk parameters. Enough said.
The next thing we do, is 4/5 round the trade size to the nearest lot step size. Again, this could result in a slightly higher risk than you intended. If you prefer to always round down, remove the first line (TradeSize = MathRound(TradeSize / MarketInfo(symbol,MODE_LOTSTEP) – 0.5);.) The result will be your trade size will always rounded down to the next allowable lot step.
And Voila! Your trade size is validated. Next time we’ll look at validating your entry, SL and TP Prices. Thanks for your attention and please follow me on Twitter and LinkedIn.
Latest posts by admin (see all)
- Forex Trading in Summertime, what the charts can tell you. - September 22, 2017
- The Secret of Taking Profit and Why it is Important - September 20, 2017
- Fading the Momentum in Forex Trading - September 14, 2017
Winner’s Edge Trading, as seen on: