OrderSend is the function name. The function returns the ticket number 'ticket' is the unique number of an order that is assigned to the order by the trade server, or -1, if the trade request has been rejected by either the server or the client terminal. In order to get information about the reasons for rejection of the trade request, you should use the function GetLastError below we will consider some of the most common errors.
Each symbol corresponds with the value of a string variable. If the order is being opened for a foregone symbol, then this parameter can be specified explicitly: However, if you are going to use the Expert Advisor in the window of any other symbol, you can use the standard function Symbol.
This function returns a string value that corresponds with the name of the symbol, in the window of which the EA or script is being executed. The type of operation can be specified as a predefined constant or its value, and according to the type of the trade. For market orders, you must always check the account for the sufficiency. For pending orders, the amount of lots is not limited. It is specified according to the requirements and limitations accepted for making trades see Order Characteristics and Rules for Making Trades.
If the price requested for opening of market orders has not been found in the price thread or if it has considerably outdated, the trade request is rejected. However, if the price is outdated, but present in the price thread and if its deviation from the current price ranges within the value of slippage, this trade request will be accepted by the client terminal and sent to the trade server.
This parameter is not processed for placing of pending orders. It is set according to the requirements and limitations accepted for making trades see Order Characteristics and Rules for Making Trades , Requirements and Limitations in Making Trades. The last part of the comment can be modified by the trade server. It can be used as the user-defined order identifier. In some cases, it is the only information that helps you to find out about that the order belongs to one or another program that has opened it.
The parameter is set by the user; its value can be the same or other than the value of this parameter of other orders. As soon as this day comes, the pending order will be closed automatically on the server side. On some trade servers, there may be a prohibition for setting the expiration date for pending orders. In this case, if you try to set a non-zero value of the parameter, the request will be rejected. On some trade servers, there can be a limit set for the total amount of opened and pending orders.
If this limit is exceeded, any trade request that implies opening a market order or placing a pending order will be rejected by the trade server. The function OrderSend may at first seem to be too intricate. However, all considered parameters are quite simple, helpful and can be successfully used in your trading. In order to see this for ourselves, let's consider the simplest variation of how the trade function OrderSend is used for opening a market order. First of all, we should note that function OrderSend has predefined parameters see Function Call and Function Description and Operator 'return'.
This means that this function can be used in a simplified mode using the minimum required set of parameters. These parameters are as follows:. Let our script imply the possibility to open an order in any symbol window. In this case, we will substitute the standard function Symbol as this parameter;. Below is the simplest script, simpleopen.
If you launch this script for execution, it will work, in the majority of cases. The script consists of one special function that contains the order-opening function OrderSend and the operator 'return'.
Let's describe the execution algorithm for program lines and events related to that. At the moment of attaching the script to the symbol window, the client terminal is passing the control just by launching it o the special function start here we should briefly remind that the start of a script is launched at the moment of attaching the script to the symbol window, whereas the start of an EA is launched at the moment when the nearest tick incomes for the symbol.
Within the framework of execution of the special function start , the control is passed to the line that calls the order opening function:. The value of StopLoss, in this case, will be: The function formed a trade request for opening of an order and passed this request to the client terminal. The function passed the control to the client terminal simultaneously with passing of the trade request, so the program execution was stopped. The client terminal checked the received trade request.
It didn't detect any incorrect parameters, so it sent the request to the server. The server received the trade request, checked it, didn't detect any incorrect parameters, and decided to execute the request. The server executed the request by making a transaction in its database and sent the information about that executed request to the client terminal.
The client terminal received the information about that the last trade request had been executed, displayed this event in the terminal window and in the symbol window, and returned the control to the program. Once having received the control, the program continued working from the location, from which the control had previously been passed to the client terminal and to which it had been returned later. The execution of the operator 'return' results in exiting the function start and, therefore, in termination of the program execution it should be reminded that scripts complete their work after they have being executed - the control is returned to the client terminal.
Thus, the script has fulfilled its intended purpose: The use of scripts is very convenient, if you need to perform a small one-time operation; in this case, the use of a script is quite reasonable.
According to step 4. Order placed by script simpleopen. The events are not always ordered as shown above. It is possible that the trade request is rejected by the client terminal or by the server. Let's try to make some experiments, for example, change the symbol name: We will obtain a program with the limited field of use:.
A disadvantage of such programs is their functional limitation. In this case, once having attached the script to the symbol window, the user is just waiting for order opening. However, the order is not opened. The user is not aware of the reason why it is so: In order to provide the user and, which is also very important, the program with the information about the events related to the execution of the trade request, it is necessary to process the errors.
A very important property of the client terminal is that, if an error occurs during the execution of an application, the client terminal does not stop the execution of the program. Errors are usually caused by the imperfection of the algorithm used in the application. In some cases, errors are caused by some external as related to the program factors. The internal causes of errors are any violations of the MQL4 requirements or of trading rules, for example, using invalid prices.
The external causes are those that are not related to the application program, for example, interrupted connection. If an error occurs at the execution of a program, the program will continue running, while the client terminal will generate the error code value available to the program through the function GetLastError. The subsequent GetLastError call will return 0. Hereinafter, we will identify all occurring errors by this code. Several errors can occur during the execution of a program; function GetLastError allows us to get the code value for only one of them, the latest error, this is why every time when we need this information, it is recommended to use function GetLastError immediately after the program lines, in which the error may occur.
The last considered script does not analyze errors, so the user remains ignorant about the results of the execution of order-opening function. In the simple variation of using the function GetLastError , the program can analyze an error and just inform the user about it. If you launch script confined. Function GetLastError returns the code of the last error, whereas Alert is used to display this value on the screen. Error code obtained at the execution of script confined.
You can find in Appendixes codes of errors that can occur at the execution of a program. In this case, error invalid stop orders occurred. This means that the values of formal parameters used in the function OrderSend don't comply with the limitations specified in Requirements and Limitations in Making Trades.
Upon a closer view, we can see the reason that caused the error: In this case, an algorithmic error occurred. In order to correct it, you should use the correct values of symbol prices. You can obtain these values using the function MarketInfo.
The above error does not occur at the execution of this script, so its execution will result in displaying the corresponding message: This means that the function GetLastError returned the value of 0, i. 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.
In some cases, a simple error occurs - the wrong value of the two-way quote is specified as the open price. Below is shown what happens if we, by mistake, specify the Bid price in script mistaken. 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 see Error Codes. The execution of the script will result in appearance of the corresponding error message:. Error invalid price at the execution of mistaken.
A similar result error will be obtained, if there are not enough free money on the account to open an order. 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. Possible combinations of 1-lot cost and 1-point cost deposit currency is US dollar. 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.
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.More...