MQL4 Book  Programming of Trade Operations  Order Characteristics and Rules for Making Trades

Order Characteristics and Rules for Making Trades


Before we start to describe trade functions, we should consider parameters that characterize market prices, order types, their characteristics, as well as the rules for making trades.

Characteristics of Symbols


First of all, we should consider the principle used by brokerage companies to form prices of securities. This principle consists in that the broker offers to the trader a two-way quote for performing trades.

Two-way quote is a connected pair of market prices offered by the broker for buying and selling of assets for a security (symbol) at the present moment.

Bid is the lower of two prices offered by broker in a two-way quote for a security.

Ask is the higher of two prices offered by broker in a two-way quote for a security.

Point is the unit of price measurement for a symbol (the minimum possible price change, the last significant figure of the price value).

Spread is the difference between the larger and the smaller price in points in a two-way quote for a symbol.

Normally, spread is a fixed value. In MetaTrader 4, it is accepted to display in the symbol window the chart that reflects only Bid price changes:


Fig. 68. A normal price chart for a symbol.

Fig. 68 shows us a symbol window where we can see the changes of the Bid prices and the two-way quote - the line of the current Bid price (black, 1.3005) and the line of the current Ask price (red, 1.3007). It can be easily seen that, in this case, the broker offers a spread of 2 points. The history for the Ask price is not displayed in the chart, but it is implied and can be easily calculated for any moment of time.

Order Types and Characteristics


There are six order types in total: two types of market orders and four types of pending orders.

Buy is a market order that defines buying of assets for a symbol.

Sell is a market order that defines selling of assets for a symbol.

BuyLimit is a pending order to buy assets for a security at a price lower than the current one. The order will be executed (modified into market order Buy) if the Ask price reaches or falls below the price set in the pending order.

SellLimit is a pending order to sell assets for a security at a price higher than the current one. The order will be executed (modified into market order Sell) if the Bid price reaches or rises above the price set in the pending order.

BuyStop is a pending order to buy assets for a security at a price higher than the current one. The order will be executed (modified into market order Buy) if the Ask price reaches or rises above the price set in the pending order.

SellStop is a pending order to sell assets for a security at a price lower than the current one. The order will be executed (modified into market order Sell) if the Bid price reaches or falls below the price set in the pending order.

Lot is the volume of an order expressed in the amount of lots.

StopLoss is a stop order; it is a price set by trader, at which a market order will be closed if the symbol price moves in a direction that produces losses for the order.

TakeProfit is a stop order; it is a price set by trader, at which a market order will be closed if the symbol price moves in a direction that produces profits for the order.


Trading Requirements and Limitations


In order to form correct trade requests in your application programs (Expert Advisors and scripts), you should take the existing requirements and limitations into consideration. Let's consider them in more details.

All trades are performed at correct prices. The execution price for each trade is calculated on the basis of the correct price of a two-way quote.

The above rule is the common rule for all market participants and cannot be changed at the will of the developers of a trading platform or on the basis of an agreement between a broker and a trader. This means, for example, that a market order can only be opened at the current market price, but not at any other price. The correct-price calculation procedure for different trades is considered below.

When calculating correct prices, it is also necessary to consider the limitations of the service provider (dealing center). These limitations include the minimum distance and the freeze distance. These limitations mean that the broker needs some time for preparations for performing of new trades, whether converting a pending order into a market one or closing an order by stop order.

Dealing centers limit the value of the minimum permissible difference between the market price and the requested price of each stop orders of a market order, between the market price and the requested price of a pending order, as well as between the requested price of a pending order and the requested prices of its stop orders. This means, for example, that in a trade request for opening a market order you can specify only the stop-order price values that are not distant from the current price less than the prescribed minimum distance. A trade request containing stop-order prices that are closer to the market price than the minimum distance is considered by the client terminal as incorrect one. Different dealing centers can establish different, dealing center-specific limitations for the minimum allowed distance. As a rule, the value of such distance ranges between 1 and 15 points. For the most commonly used securities (EUR/USD, GBP/USD, EUR/CHF, etc.), this distance makes in most dealing centers 3-5 points. Different securities can have different minimum allowed distances, too. For example, this value can be 50-100 points for gold. The value of the minimum distance for any symbol can be changed by the broker at any time (this usually precedes the broadcasting of important commercial news). There are no limitations for the maximum distance.

Freeze distance limits the possibility to modify the requested prices of opening your pending orders, as well as the requested stop levels for market orders that are in the freeze area. This means, for example, that, if the market price is 1.3800, your pending order is placed to be opened at 1.3807 and the broker's prescription is 10, your pending order is in the freeze area, i.e., you cannot modify or delete it. At a calm market, brokers usually don't set freeze distance, i.e., its value = 0. However, during the period preceding important news or at high volatility, the broker may set a certain value of a freeze distance. In different conditions and for different brokers, this value may range from 1 to 30 points for basic symbols and take higher values for other symbols. Brokerage company can change the freeze-distance value at its own discretion at any time.

The limitations of price levels limited by the values of minimum distance and of freeze distance are calculated on the basis of correct prices.

Opening/Closing Market Orders


Opening a market order implies buying or selling some assets for a symbol at the current market prices (see Requirements and Limitations in Making Trades). To open a market order, use function OrderSend(); for closing it, use function OrderClose().

The correct open price of of market order Buy is the latest known market price Ask.
The correct open price of of market order Sell is the latest known market price Bid.
The limitation related to the position of stop levels of the market order to be opened is calculated on the basis of the correct market price used for closing the order.
Orders StopLoss and TakeProfit cannot be placed closer to the market price than the minimum distance.

For example, the minimum distance for EURUSD makes 5 points. Market order Sell is opened at Bid=1.2987. The price corresponding with the two-way quote used for closing this order Sell is Ask=1.2989. The following stop levels will be the closest to the current price at the order opening (see Fig. 69 and Requirements and Limitations in Making Trades):

StopLoss = Ask + minimum distance = 1.2989 + 0.0005 = 1.2994, and

TakeProfit = Ask - minimum distance = 1.2989 - 0.0005 = 1.2984.


Fig. 69. Market order Sell with Stop Levels being closest to the market price.

If at requesting for opening a market order Sell at Bid=1.2987 you use the stop-level values closer than the above ones (SL=1.2994 and ТР=1.2984), the trade request will be rejected by the client terminal. Besides, you should take into consideration that price slippages are possible during opening of orders, which results in opening of your order at a price other than that requested by a certain value you have specified in the request. If the same request has the specified values of stop levels closest to the requested open price, this request will also be rejected by the client terminal since, in this case, the request does not comply with the required minimum distance between the open price of your order and the requested price of one of the stop orders. This is why it is not recommended to use in trade requests for opening of market orders the stop-order values closest to the requested order open price. On the contrary, it is recommended to have some "free play", i.e., to specify such values of stop orders that would be distant from the requested open price of the order by 1-2 points farther than the value of the minimum allowed distance.

Market orders can be closed as a result of execution of the trade request given by trader or by program, as well as when the price reaches one of the prices specified in stop orders.

The correct close price of a market order Buy is the latest known market price Bid.
The correct close price of a market order Sell is the latest known market price Ask.

If we close order Sell (Fig. 69) at the current moment, it will be closed at Ask=1.2989, i.e., with a loss of 2 points. If we allow this order to remain open for a while and the Ask price falls down to 1.2984, the order will be closed at that price with the profit of 3 points. If the market price grows during this period of time and reaches Ask= 1.2994, the order will be closed at that price with a loss of 7 points.

If the application has formed a request for opening or closing a market order at a price that does not correspond with the latest known market price, the request will be rejected by the client terminal.

The limitation related to closing of market orders is calculated on the basis of the correct market price used for closing of the order.
Order cannot be closed, if the execution price of its StopLoss or TakeProfit is within the range of freeze distance from the market price.

For example, the order shown in Fig. 69 can be closed only if the brokers set the freeze-distance value 4 points or less as of the moment of closing. The open price of this order does not matter, in this case. The boarders of the freeze band for the order are calculated on the basis of the market price. So, if it is = 4, the price of the upper freeze boarder is equal to += 1.2989 + 0.0004 = 1.2993, whereas the price of the lower freeze boarder is, correspondingly, - = 1.2989 - 0.0004 = 1.2985. In these conditions, no stop order is in the freeze area, so the order can be closed, if the trader (or a program) sends a correct request to the server. If the broker has set it to =5 as of the current moment, the boarders of the freeze band will be, 1.2994 and 1.2984, respectively. In this case, each stop order gets into the freeze boarder, i.e., undergoes the limitation set by the broker, so the order cannot be closed at the trader's initiative or by request of the trading program. In this example, both stop orders undergo the limitation. In a general case, a market order cannot be closed at the initiative of the client terminal, if at least one stop level of this order is in the freeze area.

If two market orders are opened for one symbol simultaneously, one of them being Buy and another one being Sell, they can be closed in one of two ways: you can close them consecutively, one by one, using OrderClose(); or you can close one of them by the other one using OrderCloseBy(). In terms of saving money, the second way is more preferable, because you will save one spread in closing orders by each other. The use of trade functions is considered in more details below in this book.

Placing and Deleting Pending Orders


A pending order implies the requested order open price other than the current market price. To place pending orders, use function OrderSend(). Use function OrderDelete() to delete a pending order.

Pending orders SellLimit and BuyStop are placed at a price that is higher than the current market price, whereas BuyLimit and SellStop are placed at a price that is lower than the current market price.

The limitation related to the position of the pending order to be placed is calculated on the basis of the correct market price for conversion a pending order into a market order.
Pending orders BuyLimit, BuyStop, SellLimit and SellStop cannot be placed at a price that is closer to the market price than the minimum distance.

For example, in order to calculate the minimum allowed price for the order BuyStop, you should add the value of minimum distance to the latest known Ask price. If StopLevel= 5, then the minimum allowed price to place the pending order BuyStop will make 1.3003+0.0005 = 1.3008 (see Fig. 70). This means that order BuyStop can be placed at the current moment at the requested price of 1.3008 or at a higher price. In this example, BuyStop is placed at 1.3015, which is quite allowable.


Fig. 70. Pending orders are placed at a price lower or higher than the current price.

The requested price of pending order BuyStop is 1.3015.

The requested price of pending order SellLimit is 1.3012.

The requested price of pending order SellStop is 1.2995.

The requested price of pending order BuyLimit is 1.2993.

In the above example, all pending orders were placed at zero bar at the moment shown in Fig. 70, while the minimum distance for placing of pending orders made 5 points. Order SellStop is the closest to the market price. In this case, Bid = 1.3001 and the requested price of SellStop = 1.2995. Thus, the distance between the order and the correct price of the two-way quote (Bid) is 6 points (1.3001 - 1.2995), i.e., it is more than the minimum distance requires. This means that, at opening of the order (or all other orders in this example), the trade request was "approved" by the client terminal and sent to the server. The server also checked it for compliance with the requirements and decided to execute the request for placing of the pending order (see Requirements and Limitations in Making Trades).

The position of stop orders attached to pending orders is also limited by the minimum distance:

The limitation related to the position of stop orders of a pending order is calculated on the basis of the requested open price of the pending order and has no relation to market prices.
StopLoss and TakeProfit of a pending order cannot be placed closer to the requested price than at the minimum distance.
The positions of StopLoss and TakeProfit of pending orders are not limited by freeze distance.

In Fig. 71, we can see pending order SellLimit, the stop orders of which are as close to the requested order price as possible. In this case, the requested order price = 1.2944, StopLoss=1.2949, TakeProfit=1.2939. At the minimum distance of 5 points, these values are quite allowable.


Fig. 71. Pending order with stop orders as close to the order as possible.

In this example, pending order SellLimit was opened at 18:07. You can see in Fig. 71 that after that the market price then reached and crossed one of its stop orders, and then went down again. This event didn't influence the pending order in any way: a stop order can only close the market order, i.e., it becomes effective as soon as the pending order is modified into a market order. In this case, the pending order is not modified into a market one (since the Bid price has not reached the requested order open price), so the stop-order price crossing this level has not resulted in any changes.

The limitation related to the deletion of pending orders is calculated on the basis of the correct market price applicable for modification of a pending order into a market one.
Pending orders BuyLimit, BuyStop, SellLimit and SellStop cannot be deleted, if the requested open price of the order is within the range of the freeze distance from the market price.

Order SellLimit can be deleted at the moment shown in Fig. 71 as initiated by the client terminal, if only the value specified at this moment is equal to or less than 8 points. In this case, the upper boarder of the freeze band (to be calculated for SellLimit) will make: + = 1.2935 +0.0008 = 1.2943. The requested order open price makes 1.2944, i.e., the order is placed outside the freeze band and can be deleted. If the broker sets the value to more than 8 points, then the pending order SellLimit cannot be deleted and the client terminal rejects the trade request.

Modification of Pending Orders into Market Orders


Pending orders are automatically modified into market orders at the server, so no functions are provided to execute this operation (see Requirements and Limitations in Making Trades).

Pending orders BuyLimit and BuyStop are modified to market ones, if the last known Ask price reaches the requested pending order price.
Pending orders SellLimit and SellStop are modified to market ones, if the last known Bid price reaches the requested pending order price.

As to the pending orders shown in Fig. 70, we can say the following.

Pending order BuyStop is modified into market order Buy, if the current price Ask reaches the value of 1.3015.

Pending order SellLimit is modified into market order Sell, if the current price Bid reaches the value of 1.3012.

Pending order SellStop is modified into market order Sell, if the current price Bid reaches the value of 1.2995.

Pending order BuyLimit is modified into market order Buy, if the current price Ask reaches the value of 1.2993.

The subsequent events related to these orders are shown in Figures 72-74.


Fig. 72. Modification of pending orders into market ones.

In the further history, the 2 other pending orders were also modified into market ones.


Fig. 73. Modification of pending orders into market ones.



Fig. 74. Modified (market) orders are displayed in the terminal window.

Please note that Fig. 73 shows the opening of order Buy 4210322 (the former pending order BuyStop). As is easy to see, the bar formed at 18:55 does not touch the price of 1.3015. The highest price within this bar is 1.3013. At the same time, the Terminal window (Fig. 74) shows that the time of the pending order was modified into a market one within this specific bar, i.e., at 18:55.

Here we must emphasize once again that the symbol window displays only the price history for the lower price of the two-side quote, namely, it reflects the Bid history. The Ask history is not displayed. This is why you may think that the pending order was modified into the market one by error. However, there is no error here, in this case. At the moment, when the price Bid was equal to 1.3013, the price Ask was 1.3013 + 2 = 1.3015 (2 means the spread of 2 points). Thus, the market price still touched the requested order price, and that resulted in automated modification of the pending order into a market one. The order was modified on the server side. Immediately after that, the server passed the information about this to the client terminal that, in its turn, displayed that information in both the symbol window (graphically) and in the terminal window (as a text).

Similar observations relate to the modification of order BuyLimit 4210411. Although the graphical displaying of the price touches or is below the requested price of pending order BuyLimit at 16:37-16:39 and at 16:41 (Fig. 72), no market order is opened. In this case, the reason for it is the same: the market price Ask did not touch the requested order price. However, it touched that level within the next bar, at 16:42. This event resulted in modifying of the pending order into a market one, so BuyLimit in the symbol window was replaced with Buy, while a new market order appears in the terminal window.

In the above example, all orders were placed with zero stop orders (it means without stop orders). However, the availability of content (non-zero) values of stop orders will not influence the modification of pending orders into market ones in any way, since they can only be modified if the corresponding price of the two-way quote touches or crosses the requested price of the pending order.

A pending order is modified into a market one without any relation to the stop orders attached.

A pending order can be opened (modified into a market one) at a price that does not match with the requested open price of the pending order. This can happen at rapid change of the market price, i.e., in the conditions when the latest known price before opening of the order has not reached the requested price, but the next price (at which the order is opened) does not match with the order open price, but is beyond it (Fig. 75).


a) price gapped between two bars b) price gapped within one bar at its forming
Fig. 75. Pending order is modified into a market one at a gap.

In Fig. 75a, we can see a possible variation of opening the pending order BuyStop (it shows two positions of the order - before and after opening; in the reality, you can see either BuyStop or Buy order, but not both). The latest known price before the price jumped up had been 1.9584. At 19:15, some news was published, which results in that the symbol price had changed in a jump. The first known price after the news has been released is 1.9615. Normally, prices jump up or down as a result of important news. In such cases, the broker cannot open your order at the requested price, because there are no corresponding prices in the market at the moment. In this case, the pending order BuyStop was placed at the requested price of 1.9590, but it opened (modified to the market order) at the price of 1.9615. This resulted from the fact that there had not been any other prices within the range from 1.9584 to 1.9615.

As a result of the considered events, the market order Buy was opened at a price that was 25 points worse than that of the placed pending order BuyStop. A similar situation (receiving less profit than expected on the order) can take place for the order SellStop, if the price jumps down. However, if pending order BuyLimit or SellLimit get into the price gap, the corresponding market order can be opened at a price that is better for the trader than his or her requested price.

It must also be noted that a price gap (the difference between two nearest quotes that makes more than one point) occurs rather frequently and can arise at any time. If the price gap takes place between bars, i.e., a very different price incomes at the first tick of a new bar, you can see the price gap in the price chart (Fig. 75a). However, if the price gap happens within one bar, you cannot detect this gap visually (Fig. 75b). In this case, the gap is hidden within the bar (the candlestick). However, you cannot judge about the quotes history inside the bar just by its appearance or by any program characteristics available (however, you can detect the gap using an application program that wold calculate the difference between the prices of incoming quotes).

Modification of Market Orders


Trading platform MetaTrader 4 allows you to form trade requests to modify price levels of market and pending orders.

To modify orders of any types, including market orders, you should use function OrderModify().

Modification of a market order implies changing of the requested values of stop orders. You may not change open prices in market orders.

You cannot change the open price of a market order, since order opening is a fact. Therefore, there is no any programming method to do this. The only thing you can do with a market order is to close it. A market order can be closed as a result of the execution of a trade request formed by a trader or by a program, or if the market price reaches the requested price of one of stop orders.

Orders StopLoss and TakeProfit cannot be placed closer to the market price than at the minimum distance.
An order cannot be modified, if the execution price of its StopLoss or TakeProfit ranges within the freeze distance from the market price.

Please note that the position of the stop orders of a market order is limited as related to the current market price, but it has no relation to the order open price (see Requirements and Limitations in Making Trades). This means that the modification of the order may result in that the stop orders are placed above or below the market order open price.

Let's consider an example. A market order was opened before, its stop orders being closest to the market price (Fig. 69). After that, the market price changed (increased by 1 point). At the moment shown in Fig. 76, it became possible to change the value of TakeProfit. Order Sell is closed at the latest known price Ask. The distance between Ask=1.2990 and the preceding value TakeProfit=1.2984 made 6 points, i.e., exceeded the minimum allowed distance. Trader (or program) formed a trade request to change the value of TakeProfit, namely, to increase this value by 1 point. This resulted in making a trade by changing the position of the stop order of the market order (the preceding value of TakeProfit=1.2984, the new value = 1.2985).

If the trade request contained the instruction to modify order Sell so that the value of any stop orders were closer to the market price Ask than at the minimum distance, this trade request would be rejected by the client terminal and the trade would not be made.


Fig. 76. A modified order, its stop orders being closest to the market price.

The modification rule for market orders limits the stop-order approaching to the current price, but it does not limit the stop-order distance from the price. This is why stop orders can be placed at any distance from the current price, if this distance is larger than the limiting distance (if, as of the order modification moment, the stop order is outside the freeze band determined by the value). In Fig. 77, we can see the same order after one more modification: in this case, stop orders are well out of the range of the limiting minimum distance.


Fig. 77. A modified order, the stop orders of which are placed beyond the minimum distance.

Modification of Pending Orders


To modify any order types, including pending orders, we use function OrderModify().

Modification of a pending order implies the possibility to change the defined values of open prices of the pending order and its stop orders.
The limitation related to the position of the pending order to be modified is calculated on the basis of the correct market price of modifying the pending order into the market one.
The limitation related to the position of stop orders of a pending order is calculated on the basis of the requested open price of the pending order and has no relation to market prices.

Pending orders BuyLimit and BuyStop cannot be placed closer to the market price Ask than at the minimum distance StopLevel.
Pending orders SellLimit and SellStop cannot be placed closer to the market price Bid than at the minimum distance StopLevel.
StopLoss/TakeProfit of a pending order cannot be placed closer to the requested order open price than at the minimum distance StopLevel.
Pending orders BuyLimit and BuyStop cannot be modified, if the requested order open price ranges within the freeze distance from the market price Ask.
Pending orders SellLimit and SellStop cannot be modified, if the requested order open price ranges within the freeze distance from the market price Bid.
The positions of StopLoss and TakeProfit of pending orders are not limited by the freeze distance FreezeLevel.

Please note that the requested price of a pending order is limited as related to the market price, whereas its stop orders are limited by the requested open price of the pending order (see Requirements and Limitations in Making Trades).


For example, pending order BuyLimit is placed with the following parameters: requested price=1. 2969, StopLoss=1.2964, TakeProfit=1.2974. The current value of the market price (applied to modify the pending order into a market one) Ask=1.2983. Thus, the order is placed at a distance of 14 points (1.2983-1.2969) from the market price, which by far exceeds the minimum allowed distance. The stop orders are at the distance of 5 points from the requested price, which does not exceed the minimum distance, so it is allowable.


Fig. 78. Pending order BuyLimit with attached stop orders closest to the order.

If the trader needs to change the price of order BuyLimit, then, whatever direction he or she moves it in, in this case, it is necessary to simultaneously change the position of the corresponding stop order (or to delete it, i.e., to set zero value for it), too. Otherwise, the distance between the order and its stop order may turn out to be less that the minimum allowed one. The trader decided to modify the order so that it kept the distance between the order and its TakeProfit as 5 points, while the value of StopLoss remained as it was (Fig. 79).


Fig. 79. Modified order BuyLimit (the requested price and the TakeProfit level are changed).

If the trader needs to place pending order BuyLimit as close to the market price as possible, then in this case (Fig. 80), the minimum allowed value of the requested price Ask-5points = 1.2985-0.0005 = 1.2980. In this example, stop orders are placed outside the limiting minimum distance.


Fig. 80. Modified order BuyLimit closest to the market price.


Appendix named Requirements and Limitations in Making Trades contains a summary table that specifies the matched values of a two-way quote, which are used for opening, closing or modifying of orders, as well as other reference values that limit the making of trades.