# How to Loop through orders

I want to loop through hedge orders but with this function it's not the correct way. If you know the correct way to loop or how to assemble hedge/hedging again and again please assist.

```#property strict

int MagicNumber=834856;
double Filter=0;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int CountSymbol(string Pair)
{
int NoSymbol=0;
for(int a=OrdersTotal()-1;a>=0;a--)
{
if(OrderSymbol()==Pair)
NoSymbol++;
}
return(NoSymbol);

}

{
int Count=0;
for(int b=OrdersTotal()-1;b>=0;b--)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
Count++;
}
return(Count);
}

bool CountHedge()
{
bool Hedge=true;
bool SellLoop=true;

for(int p=OrdersTotal()-1;p>=0;p--)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
{
Hedge=false;
}
}

return(Hedge);
}

double Pips()
{
double PipPoint=0;
double Digit=MarketInfo(Symbol(),MODE_DIGITS);
if(Digit==1||Digit==3||Digit==5){PipPoint=Point*10;}
return(PipPoint);
}

{
double open=iOpen(Symbol(),0,0);
double Range=MathAbs(open-Bid);

datetime Expire=TimeCurrent()+(60*15);

int TimeFrame=Period();
switch(TimeFrame)
{
case PERIOD_M1:Filter=7*Pips();break;//5//10
case PERIOD_M5:Filter=8*Pips();break;//10
case PERIOD_M15:Filter=10*Pips();break;//8
case PERIOD_M30:Filter=10*Pips();break;//15
case PERIOD_H1:Filter=15*Pips();break;//20//25
}

double MAClose=iMA(Symbol(),0,7,0,MODE_LWMA,PRICE_CLOSE,0);

if(Range>Filter&&Open[0]>MAClose&&Bid>MAClose)
{
}

}

{
for(int c=OrdersTotal()-1;c>=0;c--)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
{
if(OrderOpenPrice()-Bid>4*Pips())
{
int HedgeOpenSell=OrderSend(Symbol(),OP_SELL,0.02,Bid,3,0,0,"2023",MagicNumber,0,clrSkyBlue);
}
}
}

}

void Sell()
{
double open=iOpen(Symbol(),0,0);
double Range=MathAbs(open-Bid);

datetime Expire=TimeCurrent()+(60*15);

int TimeFrame=Period();
switch(TimeFrame)
{
case PERIOD_M1:Filter=7*Pips();break;//5//10
case PERIOD_M5:Filter=8*Pips();break;//10
case PERIOD_M15:Filter=10*Pips();break;//8
case PERIOD_M30:Filter=10*Pips();break;//15
case PERIOD_H1:Filter=15*Pips();break;//20//25
}

double MAClose=iMA(Symbol(),0,7,0,MODE_LWMA,PRICE_CLOSE,0);

if(Range>Filter&&Open[0]<MAClose&&Bid<MAClose)
{
}

}

void SellHedge()
{
for(int e=OrdersTotal()-1;e>=0;e--)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
{
{
}
}
}

}

void OnTick()
{
if(CountSymbol(Symbol())<2)
{

else if(CountHedge()<2){SellHedge();}
}

}```

I'm not sure about the this function:

```bool CountHedge()
{
bool Hedge=true;
bool SellLoop=true;

for(int p=OrdersTotal()-1;p>=0;p--)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
{
Hedge=false;
}
}

return(Hedge);
}```

Scalper8:
bool CountHedge() {   bool Hedge=true;   bool BuyLoop=true;   bool SellLoop=true;      for(int p=OrdersTotal()-1;p>=0;p--)   {    if(OrderSelect(p,SELECT_BY_POS,MODE_TRADES))    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)    {     if(OrderType()==OP_BUY||OrderType()==OP_SELL)     Hedge=false;    }   }      return(Hedge); }

Please dont it like this:

```bool CountHedge()
{
bool SellLoop=true;

for(int p=OrdersTotal()-1;p>=0;p--)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
{
return(false);
}
}

return (true);
}
//------------------------------------------------
if(CountHedge()==false){
// then do this.....
}```

Scalper8:

I want to loop through hedge orders but with this function it's not the correct way. If you know the correct way to loop or how to assemble hedge/hedging again and again please assist.

I'm not sure about the this function:

```bool CountHedge()
{
bool SellLoop=true;

for(int p=OrdersTotal()-1;p>=0;p--)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
{
return(false);
}
}

return (true);
}
//------------------------------------------------
if(CountHedge()==false){
// then do this.....
}```

Busingye Tusasirwe #:

I've changed it but it doesn't loop my hedge orders

```bool CountHedge()
{
for(int p=OrdersTotal()-1;p>=0;p--)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
{
return(false);
}
}

return(true);
}```

I've added these no looping my hedge orders at all.

```void BuyHedge()
{
for(int c=OrdersTotal()-1;c>=0;c--)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
{
if(CountHedge()==false&&OrderOpenPrice()-Bid>4*Pips())
{
int HedgeOpenSell=OrderSend(Symbol(),OP_SELL,0.02,Bid,3,0,0,"2023",MagicNumber,0,clrSkyBlue);
}
}
}

}

void SellHedge()
{
for(int e=OrdersTotal()-1;e>=0;e--)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
{
{
}
}
}

}```

I removed the bool function and I used the count trades function to count the hedge trades, the hedge trades don't loop.

```void BuyHedge()
{
{
{
if(OrderOpenPrice()-Bid>4*Pips())
{
HedgeOpenSell=OrderSend(Symbol(),OP_SELL,0.02,Bid,3,0,0,"2023",MagicNumber,0,clrSkyBlue);
}
}
}

}

void SellHedge()
{
{
if(OrderType()==OP_SELL)
{
{
}
}
}

}```

```int total = OrdersTotal() - 1;
for(int i = total; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_TICKET))
switch(OrderType())
{
ResetLastError();
if(OrderOpenPrice() - Bid > 4 * Pips())
if(!OrderSend(Symbol(), OP_SELL, 0.02, Bid, 3, 0, 0, "2023", MagicNumber, 0, clrSkyBlue))
Print(_LastError);
break;
case OP_SELL :
ResetLastError();
if(OrderOpenPrice() - Bid > 4 * Pips())
if(!OrderSend(Symbol(), OP_SELL, 0.02, Bid, 3, 0, 0, "2023", MagicNumber, 0, clrSkyBlue))
Print(_LastError);
break;
default:
break;
}
}```

When selecting the loop it doesn't open my hedge and still no looping through my hedge orders. Ill Post the full code:

```#property strict

int MagicNumber=834856;
double Filter=0;
int HedgeOpenSell=0;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int CountSymbol(string Pair)
{
int NoSymbol=0;
for(int a=OrdersTotal()-1;a>=0;a--)
{
if(OrderSymbol()==Pair)
NoSymbol++;
}
return(NoSymbol);
}

{
int Count=0;
for(int b=OrdersTotal()-1;b>=0;b--)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
Count++;
}
return(Count);
}

double Pips()
{
double PipPoint=0;
double Digit=MarketInfo(Symbol(),MODE_DIGITS);
if(Digit==1||Digit==3||Digit==5){PipPoint=Point*10;}
return(PipPoint);
}

{
double open=iOpen(Symbol(),0,0);
double Range=MathAbs(open-Bid);

datetime Expire=TimeCurrent()+(60*15);

int TimeFrame=Period();
switch(TimeFrame)
{
case PERIOD_M1:Filter=7*Pips();break;//5//10
case PERIOD_M5:Filter=8*Pips();break;//10
case PERIOD_M15:Filter=10*Pips();break;//8
case PERIOD_M30:Filter=10*Pips();break;//15
case PERIOD_H1:Filter=15*Pips();break;//20//25
}

double MAClose=iMA(Symbol(),0,7,0,MODE_LWMA,PRICE_CLOSE,0);

if(Range>Filter&&Open[0]>MAClose&&Bid>MAClose)
{
}

}

{
for(int v=OrdersTotal()-1;v>=0;v--)
{
if(OrderSelect(v,SELECT_BY_TICKET))
{
{
if(OrderOpenPrice()-Bid>4*Pips())
{
HedgeOpenSell=OrderSend(Symbol(),OP_SELL,0.02,Bid,3,0,0,"2023",MagicNumber,0,clrSkyBlue);
}
}
}
}

}

void Sell()
{
double open=iOpen(Symbol(),0,0);
double Range=MathAbs(open-Bid);

datetime Expire=TimeCurrent()+(60*15);

int TimeFrame=Period();
switch(TimeFrame)
{
case PERIOD_M1:Filter=7*Pips();break;//5//10
case PERIOD_M5:Filter=8*Pips();break;//10
case PERIOD_M15:Filter=10*Pips();break;//8
case PERIOD_M30:Filter=10*Pips();break;//15
case PERIOD_H1:Filter=15*Pips();break;//20//25
}

double MAClose=iMA(Symbol(),0,7,0,MODE_LWMA,PRICE_CLOSE,0);

if(Range>Filter&&Open[0]<MAClose&&Bid<MAClose)
{
}

}

void SellHedge()
{
for(int v=OrdersTotal()-1;v>=0;v--)
{
if(OrderSelect(v,SELECT_BY_TICKET))
{
if(OrderType()==OP_SELL)
{
{
}
}
}
}

}

void OnTick()
{
if(CountSymbol(Symbol())<2)
{

}

}```

When reverting back to Selecting the order ticket the hedges open just need it to be looped that's all.

```//if(OrderSelect(v,SELECT_BY_TICKET))

//if(OrderSelect(v,SELECT_BY_TICKET))

Scalper8 #:

When selecting the loop it doesn't open my hedge and still no looping through my hedge orders. Ill Post the full code:

My code was just an example how to loop through orders. I don't check your logic or if parameters are correct. You should implement error handling in your code, debug and find what specific error / problem you have and then somebody will be able to help you.

I used the following function to loop the hedges orders but there are so many "infinite", if only I could Avoid opening so many and only 1 at a time.

```int CountTrades2()
{
int Count=0;
bool SellNow=true;
bool Now=false;

for(int b=OrdersTotal()-1;b>=0;b--)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
{
}

}
return(Count);
}```

I had to add a few functions and integer for the trades:

```int Direction=0;//Buy
int Direction2=0;//Sell

bool CountHedge()
{
bool Hedge=true;
bool SellLoop=true;

for(int p=OrdersTotal()-1;p>=0;p--)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
{
Hedge=false;
}
}

return(Hedge);
}```
```if(Range>Filter&&Open[0]>MAClose&&Bid>MAClose)
{
if(Direction!=1)
{
Direction=1;
}
}

for(int c=OrdersTotal()-1;c>=0;c--)
{
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
{
if(OrderOpenPrice()-Bid>4*Pips())
{
if(Direction2!=-1)
{
Direction2=-1;
int HedgeOpenSell=OrderSend(Symbol(),OP_SELL,0.02,Bid,3,0,0,"2023",MagicNumber,0,clrSkyBlue);
}
}
}
}

void OnTick()
{