OrderSend error 138 only in tester, not in live demo

 

I've got OrderSend 138 only if I run it in tester, not in live demo. I've try to adjust slippage point to no avail. Anyone get any idea why this happen?

double price = iClose(Symbol(), PERIOD_H1, 1);
   if (up > down && up > sideway){
      double stoploss = NormalizeDouble(price-piploss*Point, Digits);
      double takeprofit = NormalizeDouble(price+pipgain*Point, Digits);
      int ticket = OrderSend(Symbol(), OP_BUY, 1, price, 5, stoploss, takeprofit, "buy", 16384, 0, clrGreen);
      if (ticket<0){
         Print("Order send error: ", GetLastError());
      } else{
         Print("Order success");
      }
   } else if (down > up && down > sideway) {
      double stoploss = NormalizeDouble(price+piploss*Point, Digits);
      double takeprofit = NormalizeDouble(price-pipgain*Point, Digits);
      int ticket = OrderSend(Symbol(), OP_SELL, 1, price, 5, stoploss, takeprofit, "sell", 16384, 0, clrGreen);
      if (ticket<0){
         Print("Order send error: ", GetLastError());
      } else{
         Print("Order success");
      }
   } else {
      Print("sideway");
   }
     }
 
double price = iClose(Symbol(), PERIOD_H1, 1);
//
//
int ticket = OrderSend(Symbol(), OP_BUY, 1, price, 5, stoploss, takeprofit, "buy", 16384, 0, clrGreen);

price is unlikely to be a valid entry price for either a buy or a sell.

Buys are opened at the current Ask and Sells at the current Bid.

I am surprised that you only get this problem in the tester

 
Erland Devona: I've got OrderSend 138 only if I run it in tester, not in live demo. I've try to adjust slippage point to no avail. Anyone get any idea why this happen?

Buy orders open at the Ask price, not the Bid price. The iClose function returns a Bid price.

The reason you are not getting an error on the live account is because the broker is probably using Market Execution which simply ignores your requested opening price and opens at the current market price. On other brokers that use Instant Execution, your code would give the same error.

Remember the following rules:

  • Buy Order, opens at Ask, closes at Bid (spread is calculated when you open)
  • Sell Order, opens at Bid, closes at Ask (spread is calculated when you close)
 
The iClose function is also returning the Bid price at the close of the last H1 bar, so not likely to be current anyway.
 
Erland Devona:

I've got OrderSend 138 only if I run it in tester, not in live demo. I've try to adjust slippage point to no avail. Anyone get any idea why this happen?

double price = iClose(Symbol(), PERIOD_H1, 1);
   if (up > down && up > sideway){
      double stoploss = NormalizeDouble(price-piploss*Point, Digits);
      double takeprofit = NormalizeDouble(price+pipgain*Point, Digits);
      int ticket = OrderSend(Symbol(), OP_BUY, 1, Ask, 5, stoploss, takeprofit, "buy", 16384, 0, clrGreen);
      if (ticket<0){
         Print("Order send error: ", GetLastError());
      } else{
         Print("Order success");
      }
   } else if (down > up && down > sideway) {
      double stoploss = NormalizeDouble(price+piploss*Point, Digits);
      double takeprofit = NormalizeDouble(price-pipgain*Point, Digits);
      int ticket = OrderSend(Symbol(), OP_SELL, 1, Bid, 5, stoploss, takeprofit, "sell", 16384, 0, clrGreen);
      if (ticket<0){
         Print("Order send error: ", GetLastError());
      } else{
         Print("Order success");
      }
   } else {
      Print("sideway");
   }
     }
Reason: