Help with OrderSend( ) Function

New to mql4, I wrote the following code for practice, but can’t figure out why I get an Error 130

I think it has to do with unNormalized numbers. I’m using IBFX broker and they use the 5th decimal on the price.

[B]double Stoploss = 50;
double Takeprofit = 50;

int start()
{
int ticket = OrderSend(Symbol(),OP_BUY,1.0,Ask,3,Ask-StoplossPoint,
Ask+Takeprofit
Point,“Order #1”,1234,0,Red);

if(ticket == -1) Print(“error-”,GetLastError());

return(0);
}[/B]

I also tried setting SL and TP to 500 because of the 5th decimal but got the same error. Any suggestions? Thanks

Normalizing the SL/TP usually solves the problem. It still seems incredible that values like Bid, High, Low and Ask can’t be used as provided by the server, but have to be normalized in OrderSend() and OrderModify().

Ask-(Stoploss*Point)

use ()

whn i take a buy order with code ordersend on mql terminal…the order opens above the ask everytime,bt whn it comes to sell…its perfect…hw do i correct the code in order to open a buy order at exact point …

@ztory666 is on to it. You need to specify price value for things like stop loss and take profit, which means doing some pip-to-digit math. See if this helps:

double Stoploss = 50;
double Takeprofit = 50;

int start()
{
int ticket = OrderSend(Symbol(),OP_BUY,1.0,Ask,3,Ask-(Stoploss*.0001),
Ask+(Takeprofit*.0001),“Order #1”,1234,0,Red);

if(ticket == -1) Print(“error-”,GetLastError());

return(0);
}

It’s probably because it’s an market execution broker. Basically the price that you send to the broker is meaningless. You can just send the order like this

ticket = OrderSend(Symbol(),OP_BUY,1,0,0,0,0,“Order #1”,1234,0,clrRed);

You cannot set a takeprofit or a stoploss until after you know the price that you got your trade at, so you set it using OrderModify afterwards.

Obviously it is good practice to use Ask price for compatibility with brokers that still use instant execution.

can someone here help me out!!..whn take a buy order,the order opens above the ask price ,this happens everytime…bt whn i take a sell order it opens exact at market price bid perfectly…how do i correct the ordersend function fr a buy order…i m working on broker hotforex

@CFP you code is good.

The only issue could come from stops distance.
In your code:

  • StopLoss of 50 and Symbol with point 0.00001 means a 5-pip distance. Too close from open price and rejected in some symbols.

  • If you set a greater StopLoss as you mentioned (500), it shouldn’t complaint in most symbols, unless,

  • the Ask variable holds a price that is far from current market price. In that case, it is good practice to RefreshRates as follows:

      #property strict
      double Stoploss = 500;
      double Takeprofit = 500;
    
      void OnInit()
      {
         RefreshRates();
         int ticket = OrderSend(Symbol(),OP_BUY,1.0,Ask,3,Ask-Stoploss*Point,
            Ask+Takeprofit*Point,"Order #1",1234,0,clrRed);
    
         if(ticket == -1)
            Print("error-",GetLastError());
      }
    

You don’t have to modify the order to set SL and TP with OrderModify() function.
You don’t have to normalize these prices with NormalizeDouble() function.
Just in case I forgot something, in which symbol do you get that error?