Menu

Ordersend forex

5 Comments

ordersend forex

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 forex 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 TradesRequirements 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, ordersend 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 startthe control is passed to the line that calls the order opening function:. In this case, the standard function Symbol will return the string value EURUSD. The value of StopLoss, in this case, will be: Ordersend 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, ordersend 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 forex 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 GetLastErrorthe 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 ordersend 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. 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. 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. For example, this factor can be 0. 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. 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.

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 Let's forex a Buy order of 1 lot in dealing center 3.

The following rule is stated in dealing center The terminal window will display the information about the opened forex. Please note that the margin makes 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 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 If one more order Sell of 0. Thus, the margin as compared to the situation where only one order Buy is opened decreases by As compared to the situation shown in Fig.

Free Margins shown in Fig. 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:. 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 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.

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:.

If we launch the script conditions. There are other limitations related to determining of values of parameters of function OrderSend. This are the maximum and the minimum order price step, the maximum and the minimum order price value, etc. Parts of information about the current symbol are stored in predefined variables.

It can be either value of those of request identifiers see Function MarketInfo Identifier. For the program to operate in a stable manner, with the minimal amount of rejected requests, you should update the parameters of information environment used by the program using the functions MarketInfo and RefreshRates before you execute the function OrderSend. The script consists of one special function start blocks In blockthe values are set, at which the order must be opened.

Block represents cycle operator whilein which all necessary calculations are performed. This cycle is included into the code to allow the program make several attempts to open the order.

In blockthe environment variables are updated. In blocksthe amount of lots and the requested prices of stop orders are calculated. In blockerrors are processed. In blockthe message is printed that the script has completed its operations. Let's consider some special features of a program code. It's easy to see that the trade request ordersend formed in block In blockthe amount of lots is calculated.

It also considers the situation when the available free margin is insufficient to open even an order with the minimum amount of lots. This is why, in blockafter printing the message about insufficient money, we exit cycle using the operator 'break'.

The ordersend is passed to blockand script completes its operations. The message in block 9 is unnecessary. It is given here just to help users of the code to find tails or heads in the script - when is the end of the program's operations and when is the pause caused by delays in the network or on the server.

If the free margin is sufficient for opening of the order, the control will be passed to block and then to block In those blocks, there is no cycle exit. This means that, for any minimum distance set by the broker, there will be corresponding stop levels found.

In block3 points were chosen for TP by design. The majority of brokers set the minimum distance as 5 points. In blockthe program will discover that the preset value is less than the allowed one.

The program will set such a value of the stop-order price that does not contradict forex limitation. In the first line of this block, the message is printed. The trade request is formed only in the second line. Why do we declare about forming a request before it is really formed? We could give the instruction first and then inform the user about it. The answer to this question is closely related to the technology of sending the request to the client terminal and then to the server see Fig.

In our case, the trade request is formed in the function OrderSend specified in the right part of the assignment operator. Thus, the only possibility to inform the user about the start of events related to the request is to show the message before the assignment operator, in the right part of which the trade function is specified. Sooner or later, the client terminal will pass the control back to the program, the assignment operator in block will be executed, which will result in that the 'ticket' variable will take a value, and the control will be passed further - to error-analyzing block If the order is opened on the server, the number ticket of the opened order will be assigned to the variable 'ticket'.

In this case, it means that the script has fulfilled its task and there is no need for the program to continue operations.

In blockwe use the operator 'break' to exit cycle while. The control is passed to block outside the cycleand the program completes its operations. However, if the attempt to open an order fails, the control will be passed to block for error analyzing.

Two types of errors are considered here: The variable 'Error' is assigned with the code of the last error, in this case, of the error that has been returned by the server or by the client terminal at execution of function OrderSend.

In the first operator 'switch' of blockovercomable errors are considered. Each error in this group is processed differently. For example, if the price has changed errorit is sufficient just to update the environment parameters using RefreshRates and repeat the attempt to open an order.

In this case, we should wait for a new tick to income there are no prices on the server at this time, either and, only after that, retry to open an order. This is why there is a waiting cycle in the block that processes error This waiting cycle will be interrupted as soon as a new tick incomes. We exit the operator switch using operator 'continue' that breaks the current iteration of the cycle while and starts a new one.

Critical errors are processed in another way. If such an error occurs, the program will just inform the user about it and terminate operations. For this purpose, we use the operator 'break' the last one in block that breaks the cycle whilewhich results in termination of the program. We should note particularly that, in this example, we don't consider all errors without exceptions, by design.

In this case, we are not aiming at providing the user with a ready-made program. It is very important that the programmer him or herself analyzes other errors and decides independently what else errors and in what way should be processed in the program. At the same time, some errors must not be processed, because the program is built in such a way that it does not imply occurrence of some errors, for example, in this case, of errors and In the above example, there is a small algorithmic error that cannot be found at neither compilation nor in the client terminal, nor on the server.

Suppose a normal minimum distance makes 5 points. Suppose that at the first execution in quick marketthis value is set as 20 points on the server. Also suppose that the formed trade request has been rejected due to error The program will track the new tick in block Within this period of time, the value of the minimum distance can be changed on the forex, for example, decreased to 10 points.

There is no crucial difference in programming between placing of pending orders and placing of market ones. You should only note the fact that the assets necessary to modify the pending order into a market one are checked for their sufficiency neither by the client terminal or by the server. They are not limited either. You can place a pending order for the amount that many times exceeds the amount of money available on your account.

Such an order can be kept for indefinite periods of time. When the market price reaches the level of the open price requested for the pending order, there will be a check made on the server. If there are enough money on the account for opening this order, it will be modified into a market one opened.

If not, it will be deleted. In MQL4, we have a very important feature - we can determine programmatically in the symbol window the coordinates of the location, at which an Expert Advisor or a script has been placed, if they have been attached using a mouse.

For example, we can obtain the ordinate value of attachment of the script using the function WindowPriceOnDropped. The function returns the value of the price in the point of the chart, in which the EA or the script has been dropped. The value will be true only, if the EA or the script has been moved using a mouse 'drag and drop'. This value is not defined for custom indicators. The structure of the script openbuystop. We will only turn our attention to basic differences between these programs.

The price, at the level of which the script has been attached to the symbol window, is determined in the line:. Subsequently, the value of this variable is kept unchanged during the entire period of operation of the program. This is necessary, if the script fails opening an order more than. At the same time, the script will every time calculate the requested value of the price close to the location forex the price level where user attached the script.

It is easy to see that, in the script openbuystop. If the calculated value of the variable Price does not comply with the requirements of placing of a pending Stop order see Order Characteristics and Rules for Making TradesRequirements and Limitations in Making Tradesthis value will be recalculated.

In the block of error processing, there are some small changes, as well: As related to the use of trade functions, we should pay attention to some more general limitations. For example, error occurs only, if several programs that form trade requests work in one symbol window.

In our opinion, this practice is allowable, but not reasonable. It would be much more efficient to create and use one trading program that would consider all special features of trading. If we use only one trading program, it is just impossible to form several trade request simultaneously.

Moreover, the entire algorithm could be organized much better in such a program: For performing of trades, it is more efficient to use a full-scaled Expert Advisor, whereas a script would be better used for one-time calculations or for displaying some useful information on the screen.

At the same time, if the trader does not use an Expert Advisor for automated trading, the use of scripts turns out to be more efficient than working with orders using the control panel of the client terminal.

Let's consider in more details what this function consists of. Opening Market Orders The function OrderSend may at first seem to be too intricate.

These parameters are as follows: Let's specify 2; stoploss - stop orders can be placed at a distance that is not closer than the minimum allowed distance, normally 5 points see Requirements and Limitations in Making Trades ; let's place stop orders at a distance of 15 points ordersend the close price, namely: Within the framework of execution of the special function startthe control is passed to the line that calls the order opening function: Execution of the function OrderSend: Please note that no actions were performed in the program starting from step 4.

The control in the program is passed to the next operator - the operator 'return'. We will obtain a program with the limited field of use: Error Processing 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.

Invalid Stop Orders The last considered script does not analyze errors, so the user remains ignorant about the results of the execution of order-opening function. Invalid Price In some cases, a simple error occurs - the wrong value of the two-way quote is specified as the open price. The execution of the script will result in appearance of the corresponding error message: Not Enough Money for Making a Trade A similar result error will be obtained, if there are not enough free money on the account to open an order.

The size of one standard lot for the same symbol may vary in different dealing centers. Order Characteristics and Rules for Making Trades Closing and Deleting Orders. Download MetaTrader 5 MetaTrader 5 Trading Platform MetaTrader 5 Tour.

About Terms and Conditions Privacy Policy. Common Way of Making Trades Order Characteristics and Rules for Making Trades Opening and Placing Orders Closing and Deleting Orders Modification of Orders.

Opening and Placing Orders Trade requests for opening and placing pending orders are formed using the function OrderSend. 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.

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.

ordersend forex

5 thoughts on “Ordersend forex”

  1. AdmS says:

    The Indian drinks the coffee down in one gulp, turns and blasts the buffalo with the shotgun, causing parts of the animal to splatter everywhere and then just walks out.

  2. andriajingram09 says:

    But the best guess appears that it would be about the JFK assassination.

  3. andriy85if says:

    Granville graduated from USMA in June 1946 as a second lieutenant.

  4. adamsit says:

    Project: GP15 Papanicolaou stain technique a method of polychromatic staining developed by George N.

  5. alexey1982 says:

    Understanding the needs, feelings, and responses of those we care about leads to stronger and more fulfilling relationships.

Leave a Reply

Your email address will not be published. Required fields are marked *

inserted by FC2 system