Function in MQL for enter a new position - The #1 Blog on trading, personal investing! Best Tips for Beginners

Header Ads

Function in MQL for enter a new position

In order to enter a new position OrderSend(...) function is used. It is sent to server side with its parameters described below and then it return with a ticket number which is a integer value so that user can check if the operation is successful. If it is not than user must handle with the error. You will see how to handle with errors in further sections.

int OrderSend(
   string symbol,           // symbol
   int cmd,                 // operation
   double volume,           // volume
   double price,            // price
   int slippage,            // slippage
   double stoploss,         // stop loss
   double takeprofit,       // take profit
   string comment = NULL,     // comment
   int magic = 0,             // magic number
   datetime expiration = 0,     // pending order expiration
   color arrow_color = clrNONE  // color
   );

Parameters

Symbol: [in]  Symbol for trading.
Cmd: [in]  Operation type. It can be any of the Trade Operations enumeration.

OP_BUY – Alış emri (tamsayı deÄŸeri 0).OP_SELL – Satış emri (tamsayı deÄŸeri 1).OP_BUYSTOP – Bekleyen alış emri (tamsayı deÄŸeri 2).OP_SELLSTOP – Bekleyen satış emri (tamsayı deÄŸeri 3).OP_BUYLIMIT – Alış limit emri (tamsayı deÄŸeri 4).OP_SELLLIMIT – Satış limit emri (tamsayı deÄŸeri 5).

Volume: [in]  Number of lots.
Price: [in]  Order price.
Slippage: [in]  Maximum price slippage for buy or sell orders.
Stoploss: [in]  Stop loss level.
Takeprofit: [in]  Take profit level.
Comment: =NULL [in]  Order comment text. Last part of the comment may be changed by server.
Magic: =0 [in]  Order magic number. May be used as user defined identifier.
Expiration: =0 [in]  Order expiration time (for pending orders only).
arrow_color: =clrNONE [in]  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.

Returned value

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.

OrderSend() function returns an integer value to check if the operation is successfull or not. If it is successfull than it returns an integere which is greater than 0 so that user then can be use this ticket number according to program algorithm. If the operation is not successfull than function returns -1 which is logical false that shows there is an error occured during the trade. And with GetLastError() function user can check what is the error. You will see error handling in Error Handling section.

For all possible order operation you will a sample as follows;

OrderSend(Symbol(),OP_BUY,lotSize,buyPrice,slippage,stopLoss,takeProfit,"Buy Order ",1,0,Green);
OrderSend(Symbol(),OP_SELL, lotSize,sellPrice, slippage, stopLoss, takeProfit,"Sell Order",2,0,Red);

OrderSend(Symbol(),OP_BUYSTOP, lotSize,expectedBuyPrice, slippage, stopLoss, takeProfit,"Pending Buy Order",3,0,Green);
OrderSend(Symbol(),OP_SELLSTOP, lotSize,expectedSellPrice, slippage, stopLoss, takeProfit,"Pendig Sell Order ",3,0,Green);

OrderSend(Symbol(),OP_BUYLIMIT, lotSize,expectedBuyLimit, slippage, stopLoss, takeProfit,"Buy Limit Order ",4,0,Green);
OrderSend(Symbol(),OP_SELLLIMIT, lotSize,expectedSellLimit, slippage, stopLoss, takeProfit,"Sell Limit Order ",5,gecerlilikTarihi,Red);