Trader’s Tech – Writing Your Own EA Part 98 – Price/Trade Size Validation

Forex Robot 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 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.

Our old friend (here and here) is PcntTradeSize(). If you would like to download it, look at my GitHub here.

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.

Tim

The following two tabs change content below.
Winners Edge Trading was founded in 2009 and is working to create the most current and useful Forex information and training available on the internet.

Latest posts by admin (see all)

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news