How to Loop through orders

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. 

#property strict

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

int CountTrades()
 {
  int Count=0;
  for(int b=OrdersTotal()-1;b>=0;b--)
   {
    if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
    if(OrderType()==OP_BUY||OrderType()==OP_SELL)
    Count++;
   }
  return(Count);
 }
 
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);
 }
 
double Pips()
 {
  double PipPoint=0;
  double Digit=MarketInfo(Symbol(),MODE_DIGITS);
  if(Digit==1||Digit==3||Digit==5){PipPoint=Point*10;}
  return(PipPoint);
 }  

void Buy()
 {
  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)
  {
   BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,3,0,0,"2023",MagicNumber,0,clrBlue);
  } 
  
 }
 
void BuyHedge()
 {
  for(int c=OrdersTotal()-1;c>=0;c--)
  {
   if(OrderSelect(BuyTrade,SELECT_BY_TICKET))
   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)
  {
   SellTrade=OrderSend(Symbol(),OP_SELL,0.01,Bid,3,0,0,"2023",MagicNumber,0,clrRed);
  } 
  
 }
 
void SellHedge()
 {
  for(int e=OrdersTotal()-1;e>=0;e--)
  {
   if(OrderSelect(SellTrade,SELECT_BY_TICKET))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   {
    if(Ask-OrderOpenPrice()>4*Pips())
    {
     int HedgeOpenBuy=OrderSend(Symbol(),OP_BUY,0.02,Ask,3,0,0,"2023",MagicNumber,0,clrMaroon);
    }
   }
  }   
  
 }  

void OnTick()
 {
  if(CountSymbol(Symbol())<2)
  {
   if(CountTrades()<1){Buy();}
   else if(CountHedge()<2){BuyHedge();}

   if(CountTrades()<1){Sell();}
   else if(CountHedge()<2){SellHedge();}
  }
   
 }

  I'm not sure about the this function:

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);
 }
Busingye Tusasirwe  
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 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)
   return(false);
   }
  }  
  
return (true); 
 }
//------------------------------------------------
if(CountHedge()==false){
// then do this.....
}
Busingye Tusasirwe  
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 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)
   return(false);
   }
  }  
  
return (true); 
 }
//------------------------------------------------
if(CountHedge()==false){
// then do this.....
}
Scalper8  
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(OrderSelect(p,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   {
    if(OrderType()==OP_BUY||OrderType()==OP_SELL)
    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(OrderSelect(BuyTrade,SELECT_BY_TICKET))
   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(OrderSelect(SellTrade,SELECT_BY_TICKET))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   {
    if(CountHedge()==false&&Ask-OrderOpenPrice()>4*Pips())
    {
     int HedgeOpenBuy=OrderSend(Symbol(),OP_BUY,0.02,Ask,3,0,0,"2023",MagicNumber,0,clrMaroon);
    }
   }
  }   
  
 }
Scalper8  

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(OrderSelect(BuyTrade,SELECT_BY_TICKET))
  {
   if(OrderType()==OP_BUY)
   {
    if(OrderOpenPrice()-Bid>4*Pips())
    {
     HedgeOpenSell=OrderSend(Symbol(),OP_SELL,0.02,Bid,3,0,0,"2023",MagicNumber,0,clrSkyBlue);
    }
   }
  }      
  
 }

void SellHedge()
 {
  if(OrderSelect(SellTrade,SELECT_BY_TICKET))
  {
   if(OrderType()==OP_SELL)
   {
    if(Ask-OrderOpenPrice()>4*Pips())
    {
     HedgeOpenBuy=OrderSend(Symbol(),OP_BUY,0.02,Ask,3,0,0,"2023",MagicNumber,0,clrMaroon);
    }
   }
  }   
  
 }


  

Marcin Madrzak  
int total = OrdersTotal() - 1;
for(int i = total; i >= 0; i--)
  {
   if(OrderSelect(i, SELECT_BY_TICKET))
      switch(OrderType())
        {
         case OP_BUY :
            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;
        }
  }
Scalper8  
Marcin Madrzak #:

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 BuyTrade=0;
int SellTrade=0;
int HedgeOpenSell=0;
int HedgeOpenBuy=0;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int CountSymbol(string Pair)
 {
  int NoSymbol=0;
  for(int a=OrdersTotal()-1;a>=0;a--)
  {
   if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Pair)
   NoSymbol++;
  }
  return(NoSymbol);
 }

int CountTrades()
 {
  int Count=0;
  for(int b=OrdersTotal()-1;b>=0;b--)
   {
    if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
    if(OrderType()==OP_BUY||OrderType()==OP_SELL)
    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);
 }  

void Buy()
 {
  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)
  {
   BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,3,0,0,"2023",MagicNumber,0,clrBlue);
  } 
  
 }
 
void BuyHedge()
 {
  for(int v=OrdersTotal()-1;v>=0;v--)
  {
   if(OrderSelect(v,SELECT_BY_TICKET))
   {
    if(OrderType()==OP_BUY)
    {
     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)
  {
   SellTrade=OrderSend(Symbol(),OP_SELL,0.01,Bid,3,0,0,"2023",MagicNumber,0,clrRed);
  } 
  
 }
 
void SellHedge()
 {
  for(int v=OrdersTotal()-1;v>=0;v--)
  {
   if(OrderSelect(v,SELECT_BY_TICKET))
   {
    if(OrderType()==OP_SELL)
    {
     if(Ask-OrderOpenPrice()>4*Pips())
     {
      HedgeOpenBuy=OrderSend(Symbol(),OP_BUY,0.02,Ask,3,0,0,"2023",MagicNumber,0,clrMaroon);
     }
    }
   }
  }    
  
 }  

void OnTick()
 {
  if(CountSymbol(Symbol())<2)
  {
   if(CountTrades()<1){Buy();}
   else if(CountTrades()<2){BuyHedge();}

   if(CountTrades()<1){Sell();}
   else if(CountTrades()<2){SellHedge();}
  }
   
 }
Scalper8  
Marcin Madrzak #:

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(BuyTrade,SELECT_BY_TICKET))

//if(OrderSelect(v,SELECT_BY_TICKET))
   if(OrderSelect(SellTrade,SELECT_BY_TICKET))
Marcin Madrzak  
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.

Scalper8  

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 BuyNow=true;
  bool SellNow=true;
  bool Now=false;
  
  for(int b=OrdersTotal()-1;b>=0;b--)
  {
   if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   {
    if(OrderType()==OP_BUY||OrderType()==OP_SELL)Now=false;
   }

  }
  return(Count);
 }
Scalper8  

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 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);
 }
if(Range>Filter&&Open[0]>MAClose&&Bid>MAClose)
  {
  if(Direction!=1)
  {
   Direction=1;
   BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,3,0,0,"2023",MagicNumber,0,clrBlue);
  } 
  } 

for(int c=OrdersTotal()-1;c>=0;c--)
  {
   if(OrderSelect(c,SELECT_BY_POS,MODE_TRADES))
   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()
 {
  if(CountTrades()<1){Buy();}
  else if(CountHedge()<2){BuyHedge();}
 }
Reason: