myeatrade

MQL4 Reference

MQL4 Trading functions (2)

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.

 

OrderOpenPrice()

double OrderOpenPrice()

Returns open price for the currently selected order.

Order must be first selected by the OrderSelect() function.

Sample:

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

OrderOpenTime()

datetime OrderOpenTime()

Returns open time 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("open time for the order 10 ",OrderOpenTime());
  else
    Print("OrderSelect returned error of ",GetLastError());

OrderPrint()

void OrderPrint()

Prints information about the selected order in the log in the following format:

ticket number; open time; trade operation; amount of lots; open price; Stop Loss; Take Profit; close time; close price; commission; swap; profit; comment; magic number; pending order expiration date.

Order must be selected by the OrderSelect() function.

Sample:

  if(OrderSelect(10, SELECT_BY_TICKET)==true)
    OrderPrint();
  else
    Print("OrderSelect failed error code is",GetLastError());

OrderProfit()

double OrderProfit()

Returns the net profit value (without swaps or commissions) for the selected order. For open positions, it is the current unrealized profit. For closed orders, it is the fixed profit.

Returns profit 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("Profit for the order 10 ",OrderProfit());
  else
    Print("OrderSelect returned the error of ",GetLastError());

OrderSend()

int OrderSend(string symbol, int cmd, double volume, double price, 
              int slippage, double stoploss, double takeprofit, 
              string comment=NULL, int magic=0, datetime expiration=0, 
              color arrow_color=CLR_NONE)

The main function used to open a position or place a pending order.

Returns number of the ticket assigned to the order by the trade server or -1 if it fails. To get additional error information, one has to call the GetLastError() function.

Notes:

At opening of a market order (OP_SELL or OP_BUY), only the latest prices of Bid (for selling) or Ask (for buying) can be used as open price. If operation is performed with a security differing from the current one, the MarketInfo() function must be used with MODE_BID or MODE_ASK parameter for the latest quotes for this security to be obtained. Calculated or unnormalized price cannot be applied. If there has not been the requested open price in the price thread or it has not been normalized according to the amount of digits after decimal point, the error 129 (ERR_INVALID_PRICE) will be generated. If the requested open price is fully out of date, the error 138 (ERR_REQUOTE) will be generated independently on the slippage parameter. If the requested price is out of date, but present in the thread, the position will be opened at the current price and only if the current price lies within the range of price+-slippage.

StopLoss and TakeProfit levels cannot be too close to the market. The minimal distance of stop levels in points can be obtained using the MarketInfo() function with MODE_STOPLEVEL parameter. In the case of erroneous or unnormalized stop levels, the error 130 (ERR_INVALID_STOPS) will be generated.

At placing of a pending order, the open price cannot be too close to the market. The minimal distance of the pending price from the current market one in points can be obtained using the MarketInfo() function with the MODE_STOPLEVEL parameter. In case of false open price of a pending order, the error 130 (ERR_INVALID_STOPS) will be generated.

Applying of 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.

On some trade servers, the total amount of open and pending orders can be limited. If this limit has been exceeded, no new position will be opened (or no pending order will be placed) and trade server will return error 148 (ERR_TRADE_TOO_MANY_ORDERS).

Parameters:

symbol      - Symbol for trading. 
cmd         - Operation type. It can be any of the Trade operation enumeration. 
volume      - Number of lots. 
price       - Preferred price of the trade. 
slippage    - Maximum price slippage for buy or sell orders. 
stoploss    - Stop loss level. 
takeprofit  - Take profit level. 
comment     - Order comment text. Last part of the comment may be changed by server. 
magic       - Order magic number. May be used as user defined identifier. 
expiration  - Order expiration time (for pending orders only). 
arrow_color - Color of the opening arrow on the chart. If parameter is missing or has CLR_NONE value opening arrow is not drawn on the chart. 

Sample:

  int ticket;
  if(iRSI(NULL,0,14,PRICE_CLOSE,0)<25)
    {
     ticket=OrderSend(Symbol(),OP_BUY,1,Ask,3,Ask-25*Point,Ask+25*Point,"My order #2",16384,0,Green);
     if(ticket<0)
       {
        Print("OrderSend failed with error #",GetLastError());
        return(0);
       }
    }

OrdersHistoryTotal()

int OrdersHistoryTotal()

Returns the number of closed orders in the account history loaded into the terminal. The history list size depends on the current settings of the "Account history" tab of the terminal. 

Sample:

  // retrieving info from trade history
  int i,hstTotal=OrdersHistoryTotal();
  for(i=0;i<hstTotal;i++)
    {
     //---- check selection result
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
       {
        Print("Access to history failed with error (",GetLastError(),")");
        break;
       }
     // some work with order
    }

OrderStopLoss()

double OrderStopLoss()

Returns stop loss value 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("Stop loss value for the order 10 ", OrderStopLoss());
  else
    Print("OrderSelect failed error code is",GetLastError());

OrdersTotal()

int OrdersTotal()

Returns market and pending orders count. 

Sample:

  int handle=FileOpen("OrdersReport.csv",FILE_WRITE|FILE_CSV,"\t");
  if(handle<0) return(0);
  // write header
  FileWrite(handle,"#","open price","open time","symbol","lots");
  int total=OrdersTotal();
  // write open orders
  for(int pos=0;pos<total;pos++)
    {
     if(OrderSelect(pos,SELECT_BY_POS)==false) continue;
     FileWrite(handle,OrderTicket(),OrderOpenPrice(),OrderOpenTime(),OrderSymbol(),OrderLots());
    }
  FileClose(handle);

OrderSwap()

double OrderSwap()

Returns swap value for the currently selected order.

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

Sample:

  if(OrderSelect(order_id, SELECT_BY_TICKET)==true)
    Print("Swap for the order #", order_id, " ",OrderSwap());
  else
    Print("OrderSelect failed error code is",GetLastError());

OrderSymbol()

double OrderSymbol()

Returns the order symbol value for selected order.

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

Sample:

  if(OrderSelect(12, SELECT_BY_POS)==true)
    Print("symbol of order #", OrderTicket(), " is ", OrderSymbol());
  else
    Print("OrderSelect failed error code is",GetLastError());

OrderTakeProfit()

double OrderTakeProfit()

Returns take profit value for the currently selected order.

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

Sample:

  if(OrderSelect(12, SELECT_BY_POS)==true)
    Print("Order #",OrderTicket()," profit: ", OrderTakeProfit());
  else
    Print("OrderSelect() returns error - ",GetLastError());

OrderTicket()

int OrderTicket()

Returns ticket number for the currently selected order.

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

Sample:

  if(OrderSelect(12, SELECT_BY_POS)==true)
    order=OrderTicket();
  else
    Print("OrderSelect failed error code is",GetLastError());

OrderType()

int OrderType()

Returns order operation type for the currently selected order. It can be any of the following values:

  • OP_BUY – buying position,
  • OP_SELL – selling position,
  • OP_BUYLIMIT – buy limit pending position,
  • OP_BUYSTOP – buy stop pending position,
  • OP_SELLLIMIT – sell limit pending position,
  • OP_SELLSTOP – sell stop pending position.

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

Sample:

  int order_type;
  if(OrderSelect(12, SELECT_BY_POS)==true)
    {
     order_type=OrderType();
     // ...
    }
  else
    Print("OrderSelect() returned error - ",GetLastError());
This article url: http://www.myeatrade.com/460/

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