Articles on MQL4
Orders in MetaTrader4

Orders in MetaTrader4


for example, forex

The work of expert or script, that uses trading functions, needs recieving the current data about open and pending orders – opening level, values of Stop Loss and Take Profit, the current profit (or loss) for the specific order, the number of open orders. Sometimes such systems of automatic trading are witten where the length of time of holding an open position is important, and in such cases it is also necessary to know the time of opening the specific position. For diversification purposes such systems are created that trade using similar algorithms, but different instruments, in such cases it is important to know for which instrument the specific order is open. Or two or more mechanical trading systems (MTS), which are also called trading robots, are working with one instrument. In such case it is necessary to differ somehow the orders opened by different MTS for the same instrument. There are MTS in which signals for opening are of different strength of value (and that’s why orders of larger or smaller size of position are opened). Also there are MTS that require opening orders in one direction under several different from each other conditions, for example in case of a break of resistance and a bounce off of a support level. It is also necessary to differ somehow orders opened in one MTS but in different situations, to add comments. Besides there are systems in which pending orders that haven’t worked for a certain period of time after placing, should be deleted, as it is considered that conditions of their (orders) existance don’t correspond the current situation any more. For example we place a pending order on the day when good economic reports are expected, and it is necessary to delete it if it doesn’t work in the first minutes after the news comes. In general there are a lot of reasons for opening, closing, deleting and holding orders and practically all these variants are realized in the trading platform МТ4. Let’s call the tab «Terminal» (Ctrl+T) in the terminal and choose the tab «Trade». In the picture You may see the typical state of the terminal on a trading account on which several MTS are working simultaneously.

Pic. 1
Pic. 1

The first column contains the numbers of tickets for each order, the second column shows the time of opening the order, then the column of the order’s type follows (buy or sell), then the column showing the size of the open position, the opening price, the level of Stop Loss, the level of Take Profit, the current market price, at which the order can be closed at the given moment(Ask or Bid depending on the type of the order), total swaps for the given order (positive or negative), the current profit on the given order, and the last column contains commentaries to the order. Below the line cotaining data on the current Balance, Equity, Margin, free margin there is a line with the information about the only pending order with the ticket 3313320. There are no data about swap and current profit for pending orders, other parameters are also known. Besides, MagicNumber of each order isn’t shown in the table. MagicNumber – it is usually a unique whole number which is marked by the expert advisor at the opening of an order, so that this expert advisor can differ its orders from the orders opened by other advisors or by hand (for them MagicNumber=0). In order to see the MagicNumber of a specific order You need to point the mouse cursor at the order needed and a pop-up help with the number of ticket, MagicNumber (id 1005 in the picture) and a comment to the order will appear.

Pic. 2
Pic. 2

Thus MagicNumber for the order with the ticket 3313336 is 1005 and the comment is «sell stop». Let’s create a script which outputs all the attributes of the orders on the current trading account in the file *.csv and in the log journal. In order to do this we need to go through all the orders and request for these attributes. We’ll do it with the help of the cycle for(), operators Print() and FileWrite(). In the picture from the terminal we can see that orders in МТ4 can be presented as a list. We can see that the number of orders is 13 (12 open orders + 1 pending order). The function OrdersTotal() returns the information of the total number of orders. Actually, the list of orders is stored in the terminal, at that it should be remembered that the number of position in the list starts with zero and ends with OrdersTotal()-1. In the given picture the last order in the list will have the number of position 12, not 13. In order to address the order it is necessary at first to select it with the function OrderSelect(). In case of valid trial of selection the function returns value true , otherwise false.

Pic. 3
Pic. 3

I launched the script on a demo-account and we can compare the position of orders in the terminal (that we can see) and the position of orders in the memory of the terminal (the result of script’s work).

Pic. 4
Pic. 4

It can be seen that orders in the terminal’s memory are arranged not in the way as we see. But if the code is written correctly it doesn’t matter. I added a pending order on USDCAD 0.1 Buy Stop 1.1490 and launched the updated script on the terminal. Please note that the file’s name consists of the number of account on which the script was executed, and after it ends working values of balance, equity, margin and free funds on the account are output in the log journal by the function Print(). Also the number of open sell and buy orders and pending orders of four types is calculated. It is also possible to make a printout of these calculated values.

Pic. 5
Pic. 5

So we have such table in Microsoft Excel. The order, placed by hand (USDCAD), has MagicNumber equal to zero. It can be seen that on one of the symbols (EURUSD) orders with different MagicNumber(1002, 1003, 1005, 1006, 1007) are open, and that on two different symbols (EURJPY and GBPJPY) there are orders with the same MagicNumber(1009). МТ4 gives possibility to test a lot of MTS on one trading account МТС, at that each MTS can differ its orders from orders of other MTS and from orders placed by hand or on other symbol.

<#IMG6>
Ultimately I would like to remeber the most common error in the work with the list of orders– usually processing is started not with the zero position in the list but with the first one, and consequently it is ended beyond the list of orders. Eventually it gives a double mistake – the order with zero position is not processed and an attempt is made to process a nonexistent order on the position OrdersTotal().

For the script refer to OrderList here


Go to article «Closing and deleting orders».

+7 (495) 710-76-76
© 1998—2008 «Alpari»

close

Your Personal Area

For alpari.classic enter your account number (a letter and 4 figures) and the code word for the Personal Area.

For alpari.micro account: enter your login (6 figures) and the password for MT.

Open an account!Forgotten your password?