// improved.mq4
// The code should be used for educational purpose only.
//--- int start() // Special function start {
double bid =MarketInfo("GBPUSD",MODE_BID); // Request for the value of Bid double ask =MarketInfo("GBPUSD",MODE_ASK); // Request for the value of Ask double point =MarketInfo("GBPUSD",MODE_POINT);//Request for Point function GetLastError() returned the value of 0, i.e., no errors were detected in the execution of the trade request by the client terminal.
Let's also consider some other common errors. For this, let's return to the idea of opening an order using a script in the same window, to which the script is attached.
Error 129. Invalid Price
In some cases, a simple error occurs - the wrong value of the two-way quote is specified as the open price. Market orders Buy are known (see Requirements and Limitations in Making Trades) to be opened at the Ask price. Below is shown what happens if we, by mistake, specify the Bid price in script mistaken.mq4:
//--- // mistaken.mq4
// The code should be used for educational purpose only.
//---
Before sending the trade request to the server, the client terminal analyzes whether the requested values of price and stop orders comply with the allowed values.
During this check, the requested open-order price will be detected as invalid, so the client terminal will not send the trade request to the server for execution, and function GetLastError() will return the value of 129 (see Error Codes). The execution of the script will result in appearance of the corresponding error message:
Fig. 83. Error 129 (invalid price) at the execution of mistaken.mq4.
Error 134. Not Enough Money for Making a Trade
A similar result (error 134) will be obtained, if there are not enough free money on the account to open an order. You can know about the amount of free money required to open 1 lot for buying of each symbol using the function MarketInfo(symbol_name, MODE_MARGINREQUIRED).
The required amount of free assets for opening a one-lot order is inversely proportional to the amount of the provided leverage. At the same time, the cost of 1 point in the deposit currency for a symbol does not relate to the provided leverage.
Table 3. Possible combinations of 1-lot cost and 1-point cost (deposit currency is US dollar).
The size of one standard lot for the same symbol may vary in different dealing centers.
Dealing Center 1 Dealing Center 2 Dealing Center 3
Opening and Placing Orders - Programming of Trade Operations - MQL4 Tutorial
Prices are given as of 16.12.2007.
Let's briefly consider some common methods of calculating the cost of 1 lot and of 1 point.
Dealing Center 1 (most common)
For the symbols that have USD reciprocally, the cost of 1 lot is equal to the current price of the corresponding two-way quote multiplied by 1000, whereas the cost of 1 point is equal to $10.
For the symbols that have USD as their numerator, the cost of 1 lot is equal to $1000.00, whereas the cost of 1 point is inversely proportional to the current quote and equal to 1/(Bid). For example, for USD/CHF, at Bid= 1.2466, the cost of 1 point is 1/1. 2466 = 8.02.
For cross rates, the cost of 1 lot is calculated in the same way as that of the numerator currency, whereas the cost of 1 point is calculated in the same way as that for the denominator currency. For example, for EUR/CHF, the cost of 1 lot is 129.40 (as for EUR/USD), whereas the cost of 1 lot is 8.02 (as for USD/CHF).
Dealing Center 2
In some dealing centers, considering the same rule of calculating costs, the values of costs can be different for some symbols. For example, the cost of 1 lot and the cost of 1 point may be proportionally increased or decreased. For example, this factor can be 0.75 for GBP/USD, whereas it is 2.0 for AUD/USD. Such representation of cost values does not result in any economical changes; in such cases, you just have to consider this special feature when calculating costs of your orders. You should also pay attention to the fact that the 1-lot costs for buying and selling of assets at cross rates are the same.
Dealing Center 3
there are also dealing centers that set the cost of 1 lot as $1000.00 for any symbol. At the same time, the cost of 1 point remains proportional to the current prices.
This implies setting a special leverage for each symbol.
Generally, there can exist other principles of building cost values. It is needless to say that, prior to start real trading, you should find out about the calculation method for any specific dealing center and consider this method in your coding.
Free Margin
At coding, it is very important to consider the principle of forming free assets. Free margin (assets) is the amount of money that is available for making trades.
Let's consider an example. Let Balance be 5000.00, there are no open orders in the terminal. Let's open a Buy order of 1 lot in dealing center 3. The following rule is stated in dealing center 3:
The terminal window will display the information about the opened order. Please note that the margin makes 1000.00, order profit is -30.00, therefore the amount of free assets (free margin) makes 5000-1000-30=3970.00:
Fig. 84. Order Buy in the terminal window.
After a Sell order of the same value has been opened, free margin will increase. The smaller integrated cost of one-direction market orders makes 1000.00, so the free margin will increase by 1000.00. In Fig. 85, you can see the situation where the differently directed orders cost the same value, so the entire sum of orders costs is released for trading.
Fig. 85. Orders Buy and Sell in the terminal window.
After a Sell order of smaller cost has been opened, free margin will increase, as well. In this case, the smaller integrated cost of one-direction market orders makes 700.00, so the free margin will increase by 700.00, whereas the margin makes the difference between the integrated costs of differently directed orders (Fig. 86).
Fig. 86. Orders Buy and Sell in the terminal window.
USD/CHF 1000.00 1000.00 8.02 1000.00 1000.00 8.02 1000.00 1000.00 8.02 EUR/CHF 1296.40 1296.20 8.02 1296.35 1296. 35 8.02 1000.00 1000.00 8.02
1-point cost of all symbols that are not quoted as related to USD always changes proportionally to the cost of the symbol specified reciprocally.
If differently directed market orders are opened for one symbol, the smaller integrated cost of one-direction orders is released for trading and increases the amount of free assets (this rule is not applicable for all dealing centers).
Page 5 of 11
Opening and Placing Orders - Programming of Trade Operations - MQL4 Tutorial
If one more order Sell of 0.1 lot is opened (cost 100.00), the smaller integrated cost of one-direction market orders makes 700.00 + 100. 00 = 800.00. Thus, the margin (as compared to the situation where only one order Buy is opened) decreases by 800.00. As compared to the situation shown in Fig. 86, the margin decreases, whereas the equity increases by 100.00 (see Fig. 87).
Fig. 87. Orders Buy and Sell in the terminal window.
Free Margins shown in Fig. 86 and Fig. 87 differ from each other by more than 100.00, since the integrated profit of open orders has changed with change in the current price (the difference makes 8.00).
If we make similar manipulations in another dealing center, it's easy to see that the above order of forming the value of free margin is not kept. For some dealing centers, the following rule is effective:
For example, if you have previously opened an order Buy of 4 lots for USD/JPY in dealing center 2, the amounts of equity and free margin will not change at opening of a 4-lot Sell order.
Fig. 88. The presence of differently directed orders does not release equity.
You can make calculations to know whether the current equity is enough for opening of an order. You can also use the function AccountFreeMarginCheck() that returns the value of free margin to remain after opening of a market order with certain amount of lots for a certain symbol. If the returned value is equal or more than 0, there are enough money on the account. If it is less than 0, then the order of this volume and for this symbol cannot be opened, the client terminal will return error 134.
In order to know the conditions offered by the dealing center and the amount of free margin required for opening of an order with the volume of 1 lot, you can use a simple script, conditions.mq4:
//--- // conditions.mq4
// The code should be used for educational purpose only.
//--- int start() // Special function start {
Alert(Symbol()," Sell = ",AccountFreeMargin()// At selling -AccountFreeMarginCheck(Symbol(),OP_SELL,1));
Alert(Symbol()," Buy = ",AccountFreeMargin() // At buying -AccountFreeMarginCheck(Symbol(),OP_BUY,1));
return; // Exit start() }
//---
Here, the expression of
AccountFreeMargin() - AccountFreeMarginCheck(Symbol(),OP_SELL,1)
allows us to calculate the difference between the available free margin and the free margin that will remain after opening of the order.
If we start this script for execution, when there are no market orders in the terminal, we can obtain the currently required amount of equity to be available and enough for opening of an order with the volume of 1 lot for buying and for selling:
Opening of any market orders does not release the equity or increase the free margin. Opening of market orders increases the equity by the amount that exceeds the integrated cost of differently directed market orders for a symbol (the rule does not apply in all dealing centers).
Page 6 of 11
Opening and Placing Orders - Programming of Trade Operations - MQL4 Tutorial
Fig. 89. 1-Lot cost for different symbols, obtained using conditions.mq4.
If we launch the script conditions.mq4 for execution in the window of the symbol, for which there are opened market orders, we can obtain other values, it depends on the calculation methods accepted in one or another dealing center.