OrderSend trouble on Oanda

 
I'm moving my MT4 to Oanda but having trouble placing orders (OrderSend).  I've read through the various threads here and I still can't seem to find a solution. Of course Oanda can not provide technical support MT4. But Oanda says they do support EAs on MT4.  What do you think I'm doing wrong?

Here is the striped down code I'm working with for testing. I get error 4051.
string buyOrSell = "Buy";
int lot = 1.0;
double SL = 1.3040; // Reasonable value
double TP = 1.3090; // Reasonable value
int Magic = 0;
int ticket;

int start()
{
   if (buyOrSell=="Buy" ) 
      ticket=OrderSend(Symbol(),OP_BUY,lot,Ask,3,SL,TP,"My Order",Magic,3,Green);
   if (buyOrSell=="Sell") 
      ticket=OrderSend(Symbol(),OP_SELL,lot,Bid,3,SL,TP,"My Order",Magic,3,Red);
   if(ticket<0)
    {
     Print("OrderSend failed with error: ",GetLastError()); // error 4051
     return(0);
    }
   Print("=== All Done ===");
   return(0);
}

//  case 4051: error_string="invalid function parameter value"; 
 
OrderSend(Symbol(),OP_BUY,lot,Ask,3,SL,TP,"My Order",Magic,3,Green);

https://docs.mql4.com/constants/errors

ERR_INVALID_FUNCTION_PARAMVALUE    4051    Invalid function parameter value

https://docs.mql4.com/trading/OrderSend 

expiration - Order expiration time (for pending orders only).

Highlighted in Red... Expiration could be your problem.

Added: Lot should also be Double_Type. 

 

Excellent. the https://docs.mql4.com/constants/errors is a great help. Your right I changed lot to double type. (A rookie mistake). Now I am stuck on error 130, "ERR_INVALID_STOPS". I have tried both 4 decimal places and 5 decimal places. The current price of EUR/USD is 1.3070. This Stop should work.

Current code.

string buyOrSell = "Buy";
double lot = 1.0;
double SL = 1.30400; // Reasonable value
double TP = 1.30900; // Reasonable value
int Magic = 0;
int ticket;

int start()
{
Print("bid: "+Bid);
   if (buyOrSell=="Buy" ) 
      ticket=OrderSend(Symbol(),OP_BUY,lot,Ask,3,SL,TP,"My Order",Magic,0,Green);
   if (buyOrSell=="Sell") 
      ticket=OrderSend(Symbol(),OP_SELL,lot,Bid,3,SL,TP,"My Order",Magic,0,Red);
   if(ticket<0)
    {
     Print("OrderSend failed with error: ",GetLastError()); // error 130
     return(0);
    }
   Print("=== All Done ===");
   Print("=== All Done ===");
   return(0);
}
 
Chances are that the broker does-not accept the StopLoss and TakeProfit with OrderSend. Try sending the order with SL=0 and Tp=0. Then OrderModify. Example.
 

Yes. That's the answer. For Oanda Set Stop Loss and Take Profit to 0 when doing OrderSend.

Thanks! 

 
Adjust for 4/5 digits. Adjust for ECN. https://www.mql5.com/en/forum/141509
 

 Thanks everyone for the help. Here is what I found works with Oanda.

 

// Code tested good on Oanda Demo account February 2013

string buyOrSell = "Sell";
double lot = 0.1;    // Equals 10,000 Units in Oanda
double SL = 1.312;   // 3 digits works
double TP = 1.30800; // 5 digits works
int Magic = 070156;  // a Magic number works
int ticket;

int start()
{
   if (buyOrSell=="Buy" ) 
      ticket=OrderSend(Symbol(),OP_BUY,lot,Ask,3,0,0,"My Order",Magic,0,Green); // can not set SL or TP here
      
   if (buyOrSell=="Sell") 
      ticket=OrderSend(Symbol(),OP_SELL,lot,Bid,3,0,0,"My Order",Magic,0,Red); // can not set SL or TP here
      
   if(ticket<0)
   {
      Print("OrderSend failed with error: ",GetLastError());
      return(0);
   }
   
   if(ticket>-1)
   {
      if(OrderSelect(ticket, SELECT_BY_TICKET))
      {
         OrderModify(ticket,OrderOpenPrice(),SL,TP,0,Orange); // must set SL and TP here through OrderModify
      }
   }
   
   Print("=== All Done ===");
   return(0);
}
 
MisterDog:

 Thanks everyone for the help. Here is what I found works with Oanda.

You might want to do a little more than this . . .

   if(ticket<0)
   {
      Print("OrderSend failed with error: ",GetLastError());
      return(0);
   }

  .  .  . all that gives you is the Error code,  what other information do you want printed to the log so you can determine the cause of the error ?  the order type for example ?  what else do you need ?  take the opportunity and print it to the log.

 
RaptorUK:

You might want to do a little more than this . . .

  .  .  . all that gives you is the Error code,  what other information do you want printed to the log so you can determine the cause of the error ?  the order type for example ?  what else do you need ?  take the opportunity and print it to the log.

 


This sounds like good advice. But I'm stuck 1 step short of this. When I OrderSend from an EA I keep getting the error...

 ERR_OBJECT_ALREADY_EXISTS   4200   Object exists already.

 And I can't figure out what this means.

And when I OrderSend from a Script there is no error, it works fine. 

 

Two erors I can spot at a first glance;

1. int lot=1.0;   (I am sure conversion will take place but you will lose the decimals. So change it to double)

2. expiration  in the OrderSend is set to 3 (Is that correct. Usually we set it to 0 when no expiration i needed)

 

I  hope this  will help

 
MisterDog:


This sounds like good advice. But I'm stuck 1 step short of this. When I OrderSend from an EA I keep getting the error...

 ERR_OBJECT_ALREADY_EXISTS   4200   Object exists already.

 And I can't figure out what this means.

And when I OrderSend from a Script there is no error, it works fine. 

It's nothing to do with OrderSend()  your error reporting is broken . . .  and the way you draw Objects is also broken.  If an Object already exists how can you create it again ?  check first using ObjectFind() if it doesn't exist then create it.

Reason: