# Trailing stop ONLY on the remaining open positions.

1639
2014.09.15 16:05
```//+------------------------------------------------------+
//Moving Average Trailing Stop Function                  |
//+------------------------------------------------------+
void MA_Trail()

{

double ATR = iATR(NULL,60,14,1);
double MA = iMA(NULL,60,MA_Period,0,1,0,1);

double BuyStopPriceMath = MA - ATR;
double SellStopPriceMath = MA + ATR;

double SellStopPrice = NormalizeDouble(SellStopPriceMath,5);

for(int b=OrdersTotal()-1; b>=0; b--)
{
if( OrderMagicNumber() == MagicNumber1 || OrderMagicNumber()== MagicNumber2 || OrderMagicNumber()== MagicNumber3 )
if(OrderSymbol() == Symbol())
{
if( OrderMagicNumber() == MagicNumber1 ){
if( OrderMagicNumber() == MagicNumber2 ){
if( OrderMagicNumber() == MagicNumber3 ){

if( OrderType() == OP_BUY && BuyStopPrice - OrderStopLoss() > Point / 2.)
{
if( OpenOrdersThisPair(Symbol()) == 3 ){
}

if( OpenOrdersThisPair(Symbol()) == 2 ){
}

if( OpenOrdersThisPair(Symbol()) == 1 ){
}
}

...```
Now, this works, but I feel it's inefficient how I've written it. Any ideas how I can just work on the open trades remaining within this for loop and trail the stop accordingly? I apologize if I am asking a stupid question as I have it working now, but I'm convinced I have written this poorly despite it working...

Cheers.
1639
2014.09.15 18:27
Not sure if this is 100% correct after-all. Can anyone tell me where I am going wrong. I am getting errors? Bascially, once first trade has hit its first target, I want the other 2 to move to break even. What this is doing though is also removing the take profit target too :s?
Moderator
9623
2014.09.15 21:05

Not checked or tested or even compiled, but this makes more sense to me

```   for(int b=OrdersTotal()-1; b>=0; b--)
{
if( OrderMagicNumber() == MagicNumber1 || OrderMagicNumber()== MagicNumber2 || OrderMagicNumber()== MagicNumber3 )
{
if(OrderSymbol() != Symbol())
continue;
if( OrderType() == OP_BUY && BuyStopPrice - OrderStopLoss() > Point / 2.)
{
if( OrderMagicNumber() == MagicNumber1 )
if( OrderMagicNumber() == MagicNumber2 )
if( OrderMagicNumber() == MagicNumber3 )
}
}
}```
Moderator
9623
2014.09.15 21:07
DomGilberto:
Not sure if this is 100% correct after-all. Can anyone tell me where I am going wrong. I am getting errors? Bascially, once first trade has hit its first target, I want the other 2 to move to break even. What this is doing though is also removing the take profit target too :s?
Maybe change btp1 etc to OrderTakeProfit()
1639
2014.09.15 21:31
```//+----------------------------------------------------------------------------------------------------------------------------------------+
//Moving Average Trailing Stop Function
//+----------------------------------------------------------------------------------------------------------------------------------------+
void MA_Trail()

{

double ATR = iATR(NULL,60,14,1);
double MA = iMA(NULL,60,MA_Period,0,1,0,1);

double BuyStopPriceMath = MA - ATR;
double SellStopPriceMath = MA + ATR;

double SellStopPrice = NormalizeDouble(SellStopPriceMath,5);

for(int b=OrdersTotal()-1; b>=0; b--)
{
if( OrderMagicNumber() == MagicNumber1 || OrderMagicNumber()== MagicNumber2 || OrderMagicNumber()== MagicNumber3 )
if(OrderSymbol() == Symbol())
{

{
if(OrderStopLoss() - BuyStopPrice > Point / 2.)continue;
if(BuyStopPrice - OrderStopLoss() > Point / 2.)

}

if(OrderType()==OP_SELL)
{
if(SellStopPrice - OrderStopLoss() > Point / 2. )continue;
if(OrderStopLoss() - SellStopPrice > Point / 2. )
bool SellModify = OrderModify(OrderTicket(),OrderOpenPrice(),SellStopPrice,OrderTakeProfit(),0,CLR_NONE);

}
}

}
}```
How come this works better? I have 3 open positions but I only use OrderModify once? Yet it moves all three of the positions trailing stops in unison?
1639
2014.09.15 21:44
```void Move_To_BreakEven()
{
for(int b=OrdersTotal()-1; b>=0; b--)
{
if( OrderMagicNumber()== MagicNumber2 || OrderMagicNumber()== MagicNumber3 )
if(OrderSymbol() == Symbol())
{

if(OrderType() == OP_BUY && Bid - btp1 > Point / 2. )
if(OrderStopLoss() - (OrderOpenPrice() + (PipsToLockIn*pips)) > Point / 2. )break;

{
}

if( OrderType() == OP_SELL && stp1 - Ask > Point / 2. )
if(OrderOpenPrice()-((PipsToLockIn*pips) - OrderStopLoss()) > Point / 2. )break;

{
Sell_StopLoss_Break_Even1 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(PipsToLockIn*pips),OrderTakeProfit(),0,CLR_NONE);
if(Sell_StopLoss_Break_Even1!=True)Print("SELL B/E Stop Loss Failed: ", GetLastError(), " On: ", OrderSymbol());
}

}

}
}```
Sorry for being a little bit spammy here, but would you also be kind enough to help me in understanding what I am doing wrong above?
Moderator
9623
2014.09.16 00:44
DomGilberto:
How come this works better? I have 3 open positions but I only use OrderModify once? Yet it moves all three of the positions trailing stops in unison?

It's because of

` if( OrderMagicNumber() == MagicNumber1 || OrderMagicNumber()== MagicNumber2 || OrderMagicNumber()== MagicNumber3 )`

When OrderMagicNumber1 is selected, it will modify that order

the the loop continues and when OrderMagicNumber2 is found and selected, it will modify that order

etc

Moderator
9623
2014.09.16 00:54
DomGilberto:
Sorry for being a little bit spammy here, but would you also be kind enough to help me in understanding what I am doing wrong above?

You have

```               if(OrderType() == OP_BUY && Bid - btp1 > Point / 2. )
if(OrderStopLoss() - (OrderOpenPrice() + (PipsToLockIn*pips)) > Point / 2. )break;

{
}
```

I think that you intend

```
if(OrderStopLoss() - (OrderOpenPrice() + (PipsToLockIn*pips)) > Point / 2. )break;
if(OrderType() == OP_BUY && Bid - btp1 > Point / 2. )
{
}
```

You also mentioned in an earlier post that the order's TP was being modified to 0

This may mean that the value of btpl etc is being set to 0 somewhere in your code

1639
2014.09.16 09:01
Ah yes of course! I am using the OR statement so it's looping through to find them all. Trailing seems to be up and running but this has got me trumped. All I want to do is simply store the first trade (1st closest target out of the 3 triggered ones) and use that first target price to trigger the other 2 to break even... I simply cannot get this working?

```//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{

if(IsNewCandle())
{
}

if( OpenOrdersThisPair(Symbol()) > 0 ){
MA_Trail();
Move_To_BreakEven(); <<<<<<<<<<<<<<<<<<
}

...

//+----------------------------------------------------------------------------------------------------------------------------------------+
// Move to break even function
//+----------------------------------------------------------------------------------------------------------------------------------------+

void Move_To_BreakEven()
{
for(int b=OrdersTotal()-1; b>=0; b--)
{
if( OrderMagicNumber()==MagicNumber1 && OrderMagicNumber()== MagicNumber2 && OrderMagicNumber()== MagicNumber3 )
if(OrderSymbol() == Symbol())
{

if(OrderMagicNumber() == MagicNumber1)TakeProfit_1 = NormalizeDouble(OrderTakeProfit(),Digits);
Print("TakeProfit_1 = ", TakeProfit_1);

if( OrderStopLoss() - (OrderOpenPrice() + (PipsToLockIn*pips)) > Point / 2. )break;
if( OrderType() == OP_BUY && Bid - TakeProfit_1 > Point / 2. )
if( OrderMagicNumber()== MagicNumber2 || OrderMagicNumber()== MagicNumber3 )
{
}

if(OrderOpenPrice()-((PipsToLockIn*pips) - OrderStopLoss()) > Point / 2. )break;
if( OrderType() == OP_SELL && TakeProfit_1 - Ask > Point / 2. )
if( OrderMagicNumber()== MagicNumber2 || OrderMagicNumber()== MagicNumber3 )
{
Sell_StopLoss_Break_Even1 = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(PipsToLockIn*pips),OrderTakeProfit(),0,CLR_NONE);
if(Sell_StopLoss_Break_Even1!=True)Print("SELL B/E Stop Loss Failed: ", GetLastError(), " On: ", OrderSymbol());
}

}

}
}```
1639
2014.09.17 16:54
```//+--------------------------------------
//Moving Average Trailing Stop Function
//+--------------------------------------
void MA_Trail()

{
ATR = iATR(NULL,60,14,1);
MA = iMA(NULL,60,MA_Period,0,1,0,1);

double BuyStopPriceMath = MA - ATR;
double SellStopPriceMath = MA + ATR;

double new_SSL = NormalizeDouble(SellStopPriceMath,Digits);

for(int b=OrdersTotal()-1; b>=0; b--)
{
if( OrderMagicNumber() == MagicNumber1 || OrderMagicNumber()== MagicNumber2 ||
OrderMagicNumber()== MagicNumber3 || OrderMagicNumber()== MagicNumber4 )
if(OrderSymbol() == Symbol())
{

if( OrderType()==OP_BUY && new_BSL > OrderStopLoss() - Point )
{
}

if( OrderType()==OP_SELL && OrderStopLoss() > new_SSL - Point )
{
bool SellModify = OrderModify(OrderTicket(),OrderOpenPrice(),new_SSL,OrderTakeProfit(),0,CLR_NONE);
}
}
}
}```
No idea what I am doing wrong here? There are 4 open orders that require their stops to be trailed. I get spammed with OrderModify error 1? (there are loads of prints but I took them out so its easier to read).

Any ideas? Still unresolved...
1639
2014.09.17 18:33
How can this not work? It's being called OnTick() if there are OpenOrdersThisPair > 0. This loop selects any one of those trades with the corresponding MagicNumber, if its a buy or sell, modify it and then loop again for the remaining MagicNumbers...?

I feel like there is one tiny thing I need to change but simply cannot see it... It's riddles with OrderModify error 1...
12