How to send a limit buy order in MT5?

To add comments, please log in or register
nicolasxu
312
nicolasxu  

I placed a limit buy order using following code:

 

uint limitBuy(double amount, ulong magicNumber, double price, double tp, double sl) {
   printf("limit buying %G lot, at %G price, TP %G, SL %G", amount, price, tp, sl);
   
   MqlTradeRequest request = {0};
   request.action = TRADE_ACTION_DEAL; // or TRADE_ACTION_PENDING;
   request.type   = ORDER_TYPE_BUY_LIMIT;
   request.magic  = magicNumber;
   request.symbol = Symbol();
   request.volume = amount;
   request.price  = price;
   request.sl     = sl;
   request.tp     = tp;
   
   MqlTradeResult result = {0};
   
   ResetLastError();

    if(!OrderSend(request,result)) {
      Print(__FUNCTION__,":",result.retcode);
      return 0; 
    }
    //--- write the server reply to log  
    Print(__FUNCTION__,":",result.comment);
    if(result.retcode==10016) Print(result.bid,result.ask,result.price);
    //--- return code of the trade server reply
    return result.retcode;   
   
}

 

the price crossed my limit price several times, but the order was not triggered. Is my above code correct to place a limit order?

I am placing order in strategy tester. If the code is correct, what other reason can result failing to execute the order?

 

 

Thanks!

To add comments, please log in or register