Trader’s Tech – Writing Your Own EA Part 99 – 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 98 we wrapped our discussion of trade size validation. Now we’ll talk about price validation.

As with trade size validation, each broker has limitations as to how close to the current market levels that you can place orders. Some require that you place your entry at least ten or more pips from the market. I don’t understand their justification in this, unless they’re just trying to keep you from using limit orders for scalping or something. Up until recently, placing a live order using the MT4 platform was tedious at best. Maybe the brokers were concerned that trade management software could be written that would place/close orders faster than the order could be handed off to avoid counter party risk.

In any case, as before, you can find out programmatically how far the order must be using the MarketInfo() function. For convenience, I’ll show it once again:

double MarketInfo(string symbol, int type);


The values for the integer type that pertain to price validation are:

MODE_FREEZELEVEL – This is the distance in points between the execution price and the current market price inside of which no modifications can be made to the trade (no closes or deletions either.) I’ve never seen this number anything other than 0, but it’s possible that some brokers may increase it during news events.

MODE_STOPLEVEL – This is the most important number for our purposes. This is the smallest distance (in points) that order levels can be placed to the current market level. Use this value to validate your trade price levels to be sure your order is not kicked out for invalid levels. The execution price, stop loss and take profit must conform.

You will not be able to place a pending order any closer to the current market than this amount. Your stop losses and take profits must also be this distance from the market. As far as I can tell, that includes stop loss or take profit levels in a pending order. For example, if the stop level is 70 points (or 7 pips), and you place a sell stop order 20 pips above the market, but place your take profit on your sell stop order within 7 pips of the current market price, the order will be rejected. If you place the order without a take profit and attempt to add the take profit later, the modification will be rejected until such time as the market moves more than the 7 pips away from your take profit location. Annoying, I know. But we’re programmers and we’ll figure out a way around everything, right? What a pain.

If you must place this order, just keep checking the price on each tick until it moves past the stop level. Then modify your order with your TP. I don’t have code written for this, but I know it can be done. It would be pretty elaborate, though. As is, I just reject the order if any of the values are unacceptable.

As we all know, MT4 is kinda quirky. One of the quirks requires you to be very careful how you present prices in order functions. This brings us to the last price validation issue. When you put price information (or any other double value, like trade size), in an order function (OrderSend(), OrderModify(), etc.), you have to use the NormalizeDouble() function to round the value. Well, there may be instances where it’s not necessary, but I’ve saved myself some trouble by doing it all the time.

double PriceValue=1.30205;

Using the predefined variable Digits will round the value to the number of digits for price for this pair. If you are opening an order for a pair other than the pair in this particular chart, use MarketInfo(symbol,MODE_DIGITS) instead (where symbol is a string designating the pair you are using.)

And that should cover price and trade size validation. Thanks for your attention and please follow me on Twitter and LinkedIn.


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.

Winner’s Edge Trading, as seen on:

Winner's Edge Trading in the news