myeatrade

MQL4 Reference

MQL4 Trading functions (1)

A group of functions intended for trading management.

Trading functions of OrderSend, OrderClose, OrderCloseBy, OrderDelete, and OrderModify cannot be called from custom indicators.
Trading functions can be used in experts and scripts. Trading functions can be called only if the "Allow live trading" property of this particular expert is checked.

To trade from experts and scripts, only one thread was provided that was launched in the program trade context (context of automated trading from experts and scripts). This is why, if this context is occupied with an expert trading operation, another expert or script cannot call trading functions at that moment due to error 146 (ERR_TRADE_CONTEXT_BUSY). To check whether it is possible to trade or not, one has to use the IsTradeAllowed() function. To make a clear sharing of access to the trading context, one can use a semaphore on the basis of a global variable the value of which must be changed using the GlobalVariableSetOnCondition() function.

 

OrderSelect()

bool OrderSelect(int index, int select, int pool=MODE_TRADES)

The function selects an order for further processing. It returns TRUE if the function succeeds. It returns FALSE if the function fails. To get the error information, one has to call the GetLastError() function.

The pool parameter is ignored if the order is selected by the ticket number. The ticket number is a unique order identifier. To find out from what list the order has been selected, its close time must be analyzed. If the order close time equals to 0, the order is open or pending and taken from the terminal open positions list. One can distinguish an open position from a pending order by the order type. If the order close time does not equal to 0, the order is a closed order or a deleted pending order and was selected from the terminal history. They also differ from each other by their order types.

Parameters:

index  - Order index or order ticket depending on the second parameter. 
select - Selecting flags. It can be any of the following values:
         SELECT_BY_POS - index in the order pool,
         SELECT_BY_TICKET - index is order ticket. 
pool   - Optional order pool index. Used when the selected parameter is SELECT_BY_POS. It can be any of the following values:
         MODE_TRADES (default)- order selected from trading pool(opened and pending orders),
         MODE_HISTORY - order selected from history pool (closed and canceled order). 

Sample:

  if(OrderSelect(12470, SELECT_BY_TICKET)==true)
    {
     Print("order #12470 open price is ", OrderOpenPrice());
     Print("order #12470 close price is ", OrderClosePrice());
    }
  else
    Print("OrderSelect returned the error of ",GetLastError());

OrderClose()

bool OrderClose(int ticket, double lots, double price, int slippage, 
                color Color=CLR_NONE)

Closes opened order. If the function succeeds, the return value is true. If the function fails, the return value is false.

To get the detailed error information, call GetLastError().

Parameters:

ticket   - Unique number of the order ticket. 
lots     - Number of lots. 
price    - Preferred closing price. 
slippage - Value of the maximum price slippage in points. 
Color    - Color of the closing arrow on the chart. If the parameter is missing or has CLR_NONE value closing arrow will not be drawn on the chart. 

Sample:

  if(iRSI(NULL,0,14,PRICE_CLOSE,0)>75)
    {
     OrderClose(order_id,1,Ask,3,Red);
     return(0);
    }

OrderCloseBy()

bool OrderCloseBy(int ticket, int opposite, color Color=CLR_NONE)

Closes an opened order by another opposite opened order. If the function succeeds, the return value is true. If the function fails, the return value is false. To get the detailed error information, call GetLastError().

Parameters:

ticket   - Unique number of the order ticket. 
opposite - Unique number of the opposite order ticket.  
Color    - Color of the closing arrow on the chart. If the parameter is missing or has CLR_NONE value closing arrow will not be drawn on the chart. 

Sample:

  if(iRSI(NULL,0,14,PRICE_CLOSE,0)>75)
    {
     OrderCloseBy(order_id,opposite_id);
     return(0);
    }

OrderClosePrice()

double OrderClosePrice()

Returns close price for the currently selected order.

Note: The order must be previously selected by the OrderSelect() function. 

Sample:

  if(OrderSelect(ticket,SELECT_BY_POS)==true)
    Print("Close price for the order ",ticket," = ",OrderClosePrice());
  else
    Print("OrderSelect failed error code is",GetLastError());

OrderCloseTime()

datetime OrderCloseTime()

Returns close time for the currently selected order. If order close time is not 0 then the order selected and has been closed and retrieved from the account history. Open and pending orders close time is equal to 0.

Note: The order must be previously selected by the OrderSelect() function.

Sample:

  if(OrderSelect(10,SELECT_BY_POS,MODE_HISTORY)==true)
    {
     datetime ctm=OrderOpenTime();
     if(ctm>0) Print("Open time for the order 10 ", ctm);
     ctm=OrderCloseTime();
     if(ctm>0) Print("Close time for the order 10 ", ctm);
    }
  else
    Print("OrderSelect failed error code is",GetLastError());

OrderComment()

string OrderComment()

Returns comment for the selected order.

Note: The order must be previously selected by the OrderSelect() function.

Sample:

  string comment;
  if(OrderSelect(10,SELECT_BY_TICKET)==false)
    {
     Print("OrderSelect failed error code is",GetLastError());
     return(0);
    }
  comment = OrderComment();
  // ...

OrderCommission()

double OrderCommission()

Returns calculated commission for the currently selected order.

Note: The order must be previously selected by the OrderSelect() function.

Sample:

  if(OrderSelect(10,SELECT_BY_POS)==true)
    Print("Commission for the order 10 ",OrderCommission());
  else
    Print("OrderSelect failed error code is",GetLastError());

OrderDelete()

bool OrderDelete(int ticket, color Color=CLR_NONE)

Deletes previously opened pending order. If the function succeeds, the return value is true. If the function fails, the return value is false. To get the detailed error information, call GetLastError().

Parameters:

ticket   - Unique number of the order ticket. 
Color    - Color of the closing arrow on the chart. If the parameter is missing or has CLR_NONE value closing arrow will not be drawn on the chart. 

Sample:

  if(Ask>var1)
    {
     OrderDelete(order_ticket);
     return(0);
    }

OrderExpiration()

datetime OrderExpiration()

Returns expiration date for the selected pending order.

Note: The order must be previously selected by the OrderSelect() function. 

Sample:

  if(OrderSelect(10, SELECT_BY_TICKET)==true)
    Print("Order expiration for the order #10 is ",OrderExpiration());
  else
    Print("OrderSelect returned error of ",GetLastError());

OrderLots()

double OrderLots()

Returns amount of lots for the selected order.

Note: The order must be previously selected by the OrderSelect() function. 

Sample:

  if(OrderSelect(10,SELECT_BY_POS)==true)
    Print("lots for the order 10 ",OrderLots());
  else
    Print("OrderSelect returned error of ",GetLastError());

OrderMagicNumber()

double OrderMagicNumber()

Returns an identifying (magic) number for the currently selected order.

Note: The order must be previously selected by the OrderSelect() function. 

Sample:

  if(OrderSelect(10,SELECT_BY_POS)==true)
    Print("Magic number for the order 10 ", OrderMagicNumber());
  else
    Print("OrderSelect returned error of ",GetLastError());

OrderModify()

bool OrderModify(int ticket, double price, double stoploss, 
                 double takeprofit, datetime expiration, 
                 color arrow_color=CLR_NONE)

Modification of characteristics for the previously opened position or pending orders. If the function succeeds, the returned value will be TRUE. If the function fails, the returned value will be FALSE. To get the detailed error information, call GetLastError() function.

Notes: Open price and expiration time can be changed only for pending orders.

If unchanged values are passed as the function parameters, the error 1 (ERR_NO_RESULT) will be generated.

Pending order expiration time can be disabled in some trade servers. In this case, when a non-zero value is specified in the expiration parameter, the error 147 (ERR_TRADE_EXPIRATION_DENIED) will be generated.

Parameters:

ticket      - Unique number of the order ticket. 
price       - New open price of the pending order. 
stoploss    - New StopLoss level. 
takeprofit  - New TakeProfit level. 
expiration  - Pending order expiration time. 
arrow_color - Arrow color for StopLoss/TakeProfit modifications in the chart. If the parameter is missing or has CLR_NONE value, the arrows will not be shown in the chart. 

Sample:

  if(TrailingStop>0)
    {
     OrderSelect(12345,SELECT_BY_TICKET);
     if(Bid-OrderOpenPrice()>Point*TrailingStop)
       {
        if(OrderStopLoss()<Bid-Point*TrailingStop)
          {
           OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Blue);
           return(0);
          }
       }
    }
This article url: http://www.myeatrade.com/459/

Please login to post comment, if you haven't account you can register free