Modifying the SL of multiple orders

szfxtrader
Trader
Posts: 377
Joined: Sun Feb 10, 2013 9:36 am

Modifying the SL of multiple orders

Post by szfxtrader »

Hello! I would like to modify the SL of some orders to the same level, if you know how I can do that, please let me know, thanks in advance.

Example:

Order 1 = 1.3050

Order 2 = 1.3000

Order 3 = 1.2990

Order 4 = 1.2970

Let's say the market price is now at 1.3090 and I would like to set the SL @ 1.3050 for the orders 2,3 and 4.
phil_trade

Re: Modifying the SL of multiple orders

Post by phil_trade »

szfxtrader wrote:Hello! I would like to modify the SL of some orders to the same level, if you know how I can do that, please let me know, thanks in advance.

Example:

Order 1 = 1.3050

Order 2 = 1.3000

Order 3 = 1.2990

Order 4 = 1.2970

Let's say the market price is now at 1.3090 and I would like to set the SL @ 1.3050 for the orders 2,3 and 4.

Hi

you can such functions to retrieve the SL you want and then modify your orders

Code: Select all

double ReturnHighOpenPrice(string strSymbol, int nMagic, int ticket)
{
	double OrderHighOpenPrice=0;
	for (int i=OrdersTotal()-1 ; i>=0 ; i--)
	{
		if (!OrderSelect(i,SELECT_BY_POS)) continue;
		if((OrderType() == OP_BUY)||(OrderType()==OP_SELL))
		if (OrderMagicNumber() == nMagic)
		if (OrderSymbol() == strSymbol)
		if(OrderOpenPrice()>=OrderHighOpenPrice)OrderHighOpenPrice=OrderOpenPrice();
	}
	if(ticket>0)OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES); //restore pointer for calling func
	return(OrderHighOpenPrice);
}


double ReturnLowOpenPrice(string strSymbol, int nMagic, int ticket)
{
	double OrderLowOpenPrice=9999;
	for (int i=OrdersTotal()-1 ; i>=0 ; i--)
	{
		if (!OrderSelect(i,SELECT_BY_POS)) continue;
		if((OrderType() == OP_BUY)||(OrderType()==OP_SELL))
		if (OrderMagicNumber() == nMagic)
		if (OrderSymbol() == strSymbol)
		if(OrderOpenPrice()<=OrderLowOpenPrice)OrderLowOpenPrice=OrderOpenPrice();
	}
	if(ticket>0)OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES);//restore pointer for calling func
	return(OrderLowOpenPrice);
}
szfxtrader
Trader
Posts: 377
Joined: Sun Feb 10, 2013 9:36 am

Re: Modifying the SL of multiple orders

Post by szfxtrader »

phil_trade wrote:
szfxtrader wrote:Hello! I would like to modify the SL of some orders to the same level, if you know how I can do that, please let me know, thanks in advance.

Example:

Order 1 = 1.3050

Order 2 = 1.3000

Order 3 = 1.2990

Order 4 = 1.2970

Let's say the market price is now at 1.3090 and I would like to set the SL @ 1.3050 for the orders 2,3 and 4.

Hi

you can such functions to retrieve the SL you want and then modify your orders

Code: Select all

double ReturnHighOpenPrice(string strSymbol, int nMagic, int ticket)
{
	double OrderHighOpenPrice=0;
	for (int i=OrdersTotal()-1 ; i>=0 ; i--)
	{
		if (!OrderSelect(i,SELECT_BY_POS)) continue;
		if((OrderType() == OP_BUY)||(OrderType()==OP_SELL))
		if (OrderMagicNumber() == nMagic)
		if (OrderSymbol() == strSymbol)
		if(OrderOpenPrice()>=OrderHighOpenPrice)OrderHighOpenPrice=OrderOpenPrice();
	}
	if(ticket>0)OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES); //restore pointer for calling func
	return(OrderHighOpenPrice);
}


double ReturnLowOpenPrice(string strSymbol, int nMagic, int ticket)
{
	double OrderLowOpenPrice=9999;
	for (int i=OrdersTotal()-1 ; i>=0 ; i--)
	{
		if (!OrderSelect(i,SELECT_BY_POS)) continue;
		if((OrderType() == OP_BUY)||(OrderType()==OP_SELL))
		if (OrderMagicNumber() == nMagic)
		if (OrderSymbol() == strSymbol)
		if(OrderOpenPrice()<=OrderLowOpenPrice)OrderLowOpenPrice=OrderOpenPrice();
	}
	if(ticket>0)OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES);//restore pointer for calling func
	return(OrderLowOpenPrice);
}

Thanks a lot for your reply! Could you also please let me know how I can change the SL to the same level? I am asking you it because I was able to find the highest or lowest level for the SL(Long/Short), but when the EA goes to update the orders using that number, it is sending me an error 130, thanks in advance!
dietcoke
Trader
Posts: 162
Joined: Tue Nov 15, 2011 9:59 pm

Re: Modifying the SL of multiple orders

Post by dietcoke »

szfxtrader wrote:

Thanks a lot for your reply! Could you also please let me know how I can change the SL to the same level? I am asking you it because I was able to find the highest or lowest level for the SL(Long/Short), but when the EA goes to update the orders using that number, it is sending me an error 130, thanks in advance!

error 130 usually occurs when your attempted stoploss is too close to the current price. the stop loss has to be bigger than the brokers STOPLEVEL
User avatar
mobthehop
Trader
Posts: 362
Joined: Wed Nov 16, 2011 12:16 am

Re: Modifying the SL of multiple orders

Post by mobthehop »

SZ - if I have need to (manually) adjust multiple orders of one pair I usually use one of these scripts (!) - maybe there is a snippet in there that helps...

dietcoke - thanks for the code - went straight into my snippet collection!

Mop
You do not have the required permissions to view the files attached to this post.
dietcoke
Trader
Posts: 162
Joined: Tue Nov 15, 2011 9:59 pm

Re: Modifying the SL of multiple orders

Post by dietcoke »

mobthehop wrote:SZ - if I have need to (manually) adjust multiple orders of one pair I usually use one of these scripts (!) - maybe there is a snippet in there that helps...

dietcoke - thanks for the code - went straight into my snippet collection!

Mop

Not mine, mop. It was Phils code
szfxtrader
Trader
Posts: 377
Joined: Sun Feb 10, 2013 9:36 am

Re: Modifying the SL of multiple orders

Post by szfxtrader »

Thanks for all the replies! I will be more clear.

Let's say I have 4 orders and 3 of those are protected by BE(+5 pips), the BE will be activated just after +25pis, then I want to change the SL of the 4 and 3 to the same level of the order 2. I've wrote the following code but it is not working properly. Thanks in advance for all the replies!

Code: Select all

void movebe()
{
   if(OrdersTotal()==0) return;
   
   for(i=0;i<=OrdersTotal();i++)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderSymbol()!=Symbol()) continue;
      if(OrderMagicNumber()!=MagicNumber) continue;
      
      if(OrderType()==OP_BUY && OrderStopLoss()>0)
      {
         buymovebe[i] = OrderStopLoss(); 
         int indexbuymovebe = ArrayMaximum(buymovebe);
         newbuybe = buymovebe[indexbuymovebe];
         if(OrderStopLoss() != newbuybe) OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(newbuybe,Digits),OrderTakeProfit(),0,Blue);
         return;
      }
      
      if(OrderType()==OP_SELL && OrderStopLoss()>0)
      {
         sellmovebe[i] = OrderStopLoss();
         int indexsellmovebe = ArrayMinimum(sellmovebe);
         newbuybe = sellmovebe[indexsellmovebe];
         if(OrderStopLoss() != newsellbe) OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(newsellbe,Digits),OrderTakeProfit(),0,Red);
         return;
      }
   }
}
dietcoke
Trader
Posts: 162
Joined: Tue Nov 15, 2011 9:59 pm

Re: Modifying the SL of multiple orders

Post by dietcoke »

szfxtrader wrote:Thanks for all the replies! I will be more clear.

Let's say I have 4 orders and 3 of those are protected by BE(+5 pips), the BE will be activated just after +25pis, then I want to change the SL of the 4 and 3 to the same level of the order 2. I've wrote the following code but it is not working properly. Thanks in advance for all the replies!
It's not clear from your description what you're trying to do?

Are you trying to set your SL for all 4 trades so the basket closes at breakeven?


when you post code, try warpping your code in code tags. its easier to read eg.

Code: Select all

void movebe()
{
   if(OrdersTotal()==0) return;
   
}
szfxtrader
Trader
Posts: 377
Joined: Sun Feb 10, 2013 9:36 am

Re: Modifying the SL of multiple orders

Post by szfxtrader »

dietcoke wrote:
szfxtrader wrote:Thanks for all the replies! I will be more clear.

Let's say I have 4 orders and 3 of those are protected by BE(+5 pips), the BE will be activated just after +25pis, then I want to change the SL of the 4 and 3 to the same level of the order 2. I've wrote the following code but it is not working properly. Thanks in advance for all the replies!
It's not clear from your description what you're trying to do?

Are you trying to set your SL for all 4 trades so the basket closes at breakeven?


when you post code, try warpping your code in code tags. its easier to read eg.

Code: Select all

void movebe()
{
   if(OrdersTotal()==0) return;
   
}
Thanks for your reply! I will try to explain it again. Let's say I have 4 long orders

Order 1 = 1.3050

Order 2 = 1.3000 - SL gain = 1.3005

Order 3 = 1.2990 - SL gain = 1.2990

Order 4 = 1.2970 - SL gain = 1.2975

Now the market price is 1.3030 and my highest SL gain level = 1.3005(order 1), I would like to change the SL gain of the order 3 and 4 for the same level(1.3005), I want to do that because if the markets moves back to the 1.3005, it will close the orders 2, 3 and 4, later on the EA will build a new long grid until it can close all the orders(including order 1 that was left opened). Thanks in advance for all replies!
User avatar
mobthehop
Trader
Posts: 362
Joined: Wed Nov 16, 2011 12:16 am

Re: Modifying the SL of multiple orders

Post by mobthehop »

SZ,

if you combine the code provided by dietcoke /Phil and the recoursing of orders from the scripts i posted it should work - but you probably want to add a check that the new SL is not in conflict with your brokers min stoplevel or freezelevel (see http://docs.mql4.com/constants/marketinfo) as also pointed out by dietcoke

Cheers
Post Reply

Return to “Coders Hangout”