Buy/sell order without SL and TP

 

Some brokers don't like that you send a buy or sell order with SL and TP in the same line of code. It has to be done in two steps.

How can i do that?

 

if you need to do it on the same tick you can do the following:

int ticket = OrderSend( Symbol(), type, lot, open_price, slippage, 0, 0, );
if ( ticket > 0 )
{
   if ( OrderSelect( ticket, SELECT_BY_TICKET ) )
   {
      OrderModify( ticket, open_price, newSL, newTP );
   }
}
//you might need additional checks and error handling too...
 

hasayama,

This is fine thanks!

 

Hi,

I have created this code.

void SendOrder() {

double newSL = 20;
double newTP = 20;  
point=MarketInfo(Symbol(),MODE_POINT);
expiration=CurTime()+PERIOD_D1*60;

int ticket = OrderSend( Symbol(), OP_SELLSTOP, 1.0, Bid, 0, 0, 0, "some comment",16384,expiration,Green);

   if(ticket<=0) {
      Print("Error = ",GetLastError());
   } else {
      Print("ticket = ",ticket); 
      break;
   }

   if ( ticket > 0 ) {
      if (OrderSelect( ticket, SELECT_BY_TICKET)) {
         OrderModify( ticket, Bid, newSL, newTP );
      }
   }
}

But i get this error 'break' - 'break' or 'continue' used within some cycle only

and this error - ')' - wrong parameters count in this line below
OrderModify( ticket, Bid, newSL, newTP );

 

Break can be used only inside "for" or "while" loops and "switch" structure. Just delete break statement from the code, it is useless here.

OrderModify has more input params than you specify: ticket, price, sl, tp, expiration and color. Learn more about it in meta editor. Just place your cursor inside OrderModify word and press F1, it will open meta editor help with accurate descriptions of all functions.

I believe, you need to use the following call of OrderModify:

OrderModify( ticket, Bid, newSL, newTP, expiration )
 

Just noticed - newSL and newTP are defined in a wrong way. You need to do the following:

int TP = 20;
int SL = 20;

double point = MarketInfo( Symbol(), MODE_POINT );
int stop_level = MarketInfo( Symbol(), MODE_STOPLEVEL );
int digits = MarketInfo( Symbol(), MODE_DIGITS );

int useTP = 0, useSL = 0;

if ( TP <= stop_level ) { useTP = stop_level + 1; }
else { useTP = TP; }

if ( SL <= stop_level ) { useSL = stop_level + 1; }
else { useSL = SL; }

double newTP = NormalizeDouble( Bid - useTP*point, digits ); //for Sell
double newSL = NormalizeDouble( Ask + useSL*point, digits ); //for Sell

//OR

double newTP = NormalizeDouble( Ask + useTP*point, digits ); //for Buy
double newSL = NormalizeDouble( Bid - useSL*point, digits ); //for Buy

//your code continues...
 

I works now

Thanks hasayama

If someone is interested in the code that works, here it is.

void SendOrder(){

double newSL = 20;
double newTP = 20;  
point=MarketInfo(Symbol(),MODE_POINT);
expiration=CurTime()+PERIOD_D1*60;

int ticket = OrderSend( Symbol(), OP_SELLSTOP, 1.0, Bid, 0, 0, 0, "some comment", 16384, expiration, Green);

   if(ticket<=0){
      Print("Error = ",GetLastError());
   } else {
      Print("ticket = ",ticket); 
   }

   if ( ticket > 0 ) {
      if (OrderSelect( ticket, SELECT_BY_TICKET)) { OrderModify( ticket, Bid, newSL, newTP, expiration, Green);
      }
   }
}
Reason: