stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Modifying the SL of multiple orders
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=1591
Page 1 of 2
Author:  szfxtrader [ Sat Mar 09, 2013 3:14 pm ]
Post subject:  Modifying the SL of multiple orders

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.
Author:  phil_trade [ Sat Mar 09, 2013 4:55 pm ]
Post subject:  Re: Modifying the SL of multiple orders

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);
}
Author:  szfxtrader [ Sat Mar 09, 2013 11:03 pm ]
Post subject:  Re: Modifying the SL of multiple orders

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!
Author:  dietcoke [ Sat Mar 09, 2013 11:12 pm ]
Post subject:  Re: Modifying the SL of multiple orders

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
Author:  mobthehop [ Sun Mar 10, 2013 1:27 am ]
Post subject:  Re: Modifying the SL of multiple orders

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
Author:  dietcoke [ Sun Mar 10, 2013 11:23 am ]
Post subject:  Re: Modifying the SL of multiple orders

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
Author:  szfxtrader [ Sun Mar 10, 2013 11:28 am ]
Post subject:  Re: Modifying the SL of multiple orders

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;
      }
   }
}
Author:  dietcoke [ Sun Mar 10, 2013 12:49 pm ]
Post subject:  Re: Modifying the SL of multiple orders

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;
   
}
Author:  szfxtrader [ Sun Mar 10, 2013 3:05 pm ]
Post subject:  Re: Modifying the SL of multiple orders

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!
Author:  mobthehop [ Sun Mar 10, 2013 4:44 pm ]
Post subject:  Re: Modifying the SL of multiple orders

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
All times are UTC Page 1 of 2