# loop very big

120

Heey gus!

I'm trying to make a Trailling Stop

Bad, is very large. Is there something I'm missing?

Example:

OrdenOpenPrice() is 1.00000

OrdenOpenPrice() is 1.20  (STOPLOSS())

OrderOpenPrice() is 1.50 (STOPLOSS())

thank you for help.

```AutoStop=20;
AutoStop2=30;
int cnt, total=OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
{

if(OrderType()==OP_SELL)
{
double stnewprice = OrderOpenPrice();
SLshell = stnewprice-AutoStop*MyPoint;
double SLshell2 = stnewprice-40*MyPoint;
if(OrderOpenPrice()-Bid > AutoStop*MyPoint) modify = OrderModify(OrderTicket(),OrderOpenPrice(),SLshell,0,0,clrGreen);

if(OrderOpenPrice()-Bid > AutoStop2*MyPoint) modify = OrderModify(OrderTicket(),OrderOpenPrice(),SLshell2,0,0,clrGreen);

}}} ```
16099

KeepMarcos:

OrdenOpenPrice() is 1.00000

OrdenOpenPrice() is 1.20  (STOPLOSS())

OrderOpenPrice() is 1.50 (STOPLOSS())

1. Can't be all three. Your statement is illogical.
2. ```SLshell = stnewprice-AutoStop*MyPoint;
double SLshell2 = stnewprice-40*MyPoint;
if(OrderOpenPrice()-Bid > AutoStop*MyPoint) modify = OrderModify(OrderTicket(),OrderOpenPrice(),SLshell,0,0,clrGreen);```
You only are checking against OOP You need to also check against the current SL to avoid Error=1
3. You have two separate values SLshell and SLshell2. Make up your mind which is the trailing.
4. ```double SL = ...
if(OrderOpenPrice() > SL         // sell order
&& OrderStopLoss() - SL > _Point // Prevent Error 1
){
modify = OrderModify(OrderTicket(),OrderOpenPrice(),SL,0,0,clrGreen);```
and Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
5. `   for(cnt=0;cnt<total;cnt++)`
In the presence of multiple orders (one EA multiple charts, multiple EA's, manual trading) you must count down when closing/deleting/modifying in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
19295
Vasyl Nosal
KeepMarcos:

Heey gus!

I'm trying to make a Trailling Stop

Bad, is very large. Is there something I'm missing?

Example:

OrdenOpenPrice() is 1.00000

OrdenOpenPrice() is 1.20  (STOPLOSS())

OrderOpenPrice() is 1.50 (STOPLOSS())

thank you for help.

Look at example. Maybe helps.

``` extern int TrailingStart =0;
extern int TrailingStop = 20;
extern int TrailingStep =10;

///////////////////////////////////////////////////////////////////////////
void Traling_Stop_f()
{
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS))
{
if(OrderSymbol() == Symbol())
{
if(OrderMagicNumber()==Magic)
{

{
if(Bid>=NormalizeDouble(OrderOpenPrice()+TrailingStart*point+TrailingStop*point,Digits))
{
if(NormalizeDouble(OrderStopLoss(),Digits)<NormalizeDouble(Bid-(TrailingStop+TrailingStep)*point,Digits))
{
if(NormalizeDouble(OrderStopLoss(),Digits)!=NormalizeDouble(Bid-TrailingStop*point,Digits))
{
RefreshRates();
if(OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Bid-TrailingStop*point,Digits), OrderTakeProfit(), 0, clrNONE))
continue;
}
}
}
}
else
if(OrderType()==OP_SELL)
{
{
{
{
RefreshRates();
if(OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Ask+TrailingStop*point,Digits), OrderTakeProfit(), 0, clrNONE))
continue;
}
}
}
}
}
}
}
}

}```
120

Thank you for responding eevviill and WHRoeder
Bad, I'm not change correctly =\

I need to change the stoploss when the price reaches 10 pips for example.

The code below is correct. bad when the price reaches another 10 pips can not change.

:(((((

Help-me people!!!

make 5 days in this code line. I can not change :(((('

*Sorry some error in English.

```AutoStop=20;
AutoStop2=30;
int cnt, total=OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
{

if(OrderType()==OP_SELL)
{
double stnewprice = OrderOpenPrice();
SLshell = stnewprice-AutoStop*MyPoint;
double SLshell2 = stnewprice-40*MyPoint;
if(OrderOpenPrice()-Bid > AutoStop*MyPoint) modify = OrderModify(OrderTicket(),OrderOpenPrice(),SLshell,0,0,clrGreen);```
120

```   int cnt, total=OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
{
{
double stnewprice = OrderOpenPrice();

if(stnewprice+20*MyPoint>stnewprice) modify = OrderModify(OrderTicket(),OrderOpenPrice(),SLbuy,0,0,clrGreen);
if(stnewprice+40*MyPoint> stnewprice) modify = OrderModify(OrderTicket(),OrderOpenPrice(),stnewprice+40*MyPoint,0,0,clrGreen);

}
```

this screen is my real doubts.

It is in an infinite loop. =(

19295
Vasyl Nosal
``` int cnt, total=OrdersTotal();
for(cnt=0;cnt<total;cnt++)```
`Why you use construction like this?`
`Why not`
`      for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)`
19295
Vasyl Nosal

Maybe like this

```double stnewprice = OrderStopLoss();

if( stnewprice<Bid-20*MyPoint)```
120

I do not have enough words to thank.

thank you eevviill.

tank you too WHRoader =)))))

120

CODE:

``` int cnt, total=OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
{
{  double st =  OrderStopLoss();
double stnewprice = OrderOpenPrice();

if(Ask+35*MyPoint>st);  modify = OrderModify(OrderTicket(),OrderOpenPrice(),st+35*MyPoint,0,0,clrLightGreen);

}```
19295
Vasyl Nosal
KeepMarcos:
I do not have enough words to thank.

thank you eevviill.

tank you too WHRoader =)))))

My pleasure.