scanning historical trades to prevent opening multiple trades of the same type

 
void hedger()
{
   int buy_trades_number = _get_number_of_trades(OP_BUY);
   int sell_trades_number = _get_number_of_trades(OP_SELL);
   int numberofhedgedbuys = _get_number_of_trades_hedge_buy(OP_BUY);
   int numberofhedgedsells = _get_number_of_trades_hedge_sell(OP_SELL);
   for(int n=OrdersTotal()-1; n>=0; n--) 
     {
      OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY);
      if((OrderMagicNumber()!=333 || OrderMagicNumber()!=334)) continue;

     }

   
 

            if (buy_trades_number == 6 && numberofhedgedbuys == 0)
               {OrderSend(Symbol(),OP_SELL,50,Bid,2,10 * Point * mp,0,"hedged",333,0,clrRed);}
               
            if (sell_trades_number == 6 && numberofhedgedsells == 0)
               {OrderSend(Symbol(),OP_BUY,50,Ask,2,10 * Point * mp,0,"hedged",334,0,clrGreen);}
               

     
   

}

I have a separate function, seen below, that applies a trailing stop loss to these 50 lot trades. The problem is that once the trailing stop loss closes out one of these trades, it automatically opens up another. So I had an idea to use OrdersHistoryTotal() in order to scan the last 20 historical trades and make sure there wasnt one with a magic number of 333 or 334 before placing a 50 lot trade. Any ideas on how to do this guys? I suck at creating loops.


void tStop(string symb,int stop, int MN)// Symbol + stop in pips + magic number
  {
   double bsl=NormalizeDouble(MarketInfo(symb,MODE_BID)-stop*MarketInfo(symb,MODE_POINT),MarketInfo(symb,MODE_DIGITS));
   double ssl=NormalizeDouble(MarketInfo(symb,MODE_ASK)+stop*MarketInfo(symb,MODE_POINT),MarketInfo(symb,MODE_DIGITS));

   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==MN)
            if(OrderSymbol()==symb)

               if(OrderMagicNumber()==MN && OrderType()==OP_BUY && (OrderStopLoss()<bsl || OrderStopLoss()==0))
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),bsl,OrderTakeProfit(),0,clrNONE))
                    {
                     Print(symb+" Buy's Stop Trailled to "+(string)bsl);
                       }else{
                     Print(symb+" Buy's Stop Trail ERROR");
                    }

               if(OrderMagicNumber()==MN && OrderType()==OP_SELL && (OrderStopLoss()>ssl || OrderStopLoss()==0))
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),ssl,OrderTakeProfit(),0,clrNONE))
                    {
                     Print(symb+" Sell's Stop Trailled to "+(string)ssl);
                       }else{
                     Print(symb+" Sell's Stop Trail ERROR");
                    }
     }
  }

trailing stop loss code^

int _get_number_of_trades(int ttype){ //function that returns number of currently opene trades on current pair
   int tCount = 0;
   
   for(int cnt=0;cnt<OrdersTotal();cnt++){
      bool result = OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
      
      if(OrderType() == ttype && OrderMagicNumber()!=333 && OrderMagicNumber()!=334) tCount++;
   }
   
   return(tCount);
}  

int _get_number_of_trades_hedge_buy(int ttype){ //function that returns number of currently opene trades on current pair
   int tCount = 0;
   
   for(int cnt=0;cnt<OrdersTotal();cnt++){
      bool result = OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
      
      if(OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES))
        if(OrderMagicNumber() == 333)
           tCount++;
   }
   
   return(tCount);
}

int _get_number_of_trades_hedge_sell(int ttype){ //function that returns number of currently opene trades on current pair
   int tCount = 0;
   
   for(int cnt=0;cnt<OrdersTotal();cnt++){
      bool result = OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
      
      if(OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES))
        if(OrderMagicNumber() == 334)
           tCount++;
   }
   
   return(tCount);
}  

how I count my buys and sells code^

void onTick()
{
     hedger();
     tStop(Symbol(),100,666);
     tStop(Symbol(),100,667);
}

how my onTick code looks^

 
   int maxEntriesToLookAt=10;
   bool found=false;

   for(int n=OrdersHistoryTotal()-1; n>=0; n--) 
     {
      if(n<OrdersHistoryTotal()-maxEntriesToLookAt) break;
      if(OrderSelect(n,SELECT_BY_POS,MODE_HISTORY) && (OrderMagicNumber()==333 || OrderMagicNumber()==334)) { found=true; break; }
     }
   if(found) Print("Magic order exists in recent history!");
I'd put the first loop like this, if you intended to look at the recent history to locate one of these orders (not sure if I got you.)
 
lippmaje:
I'd put the first loop like this, if you intended to look at the recent history to locate one of these orders (not sure if I got you.)
void hedger()
{
   int buy_trades_number = _get_number_of_trades(OP_BUY);
   int sell_trades_number = _get_number_of_trades(OP_SELL);
   int numberofhedgedbuys = _get_number_of_trades_hedge_buy(OP_BUY);
   int numberofhedgedsells = _get_number_of_trades_hedge_sell(OP_SELL);
   int maxEntriesToLookAt=10;
   bool found=false;

   for(int n=OrdersHistoryTotal()-1; n>=0; n--) 
     {
      if(n<OrdersHistoryTotal()-maxEntriesToLookAt) break;
      if(OrderSelect(n,SELECT_BY_POS,MODE_HISTORY) && (OrderMagicNumber()==333 || OrderMagicNumber()==334)) { found=true; break; }
     }
   if(found) Print("Magic order exists in recent history!");

   
 

            if (buy_trades_number == 6 && numberofhedgedbuys == 0)
               {OrderSend(Symbol(),OP_SELL,50,Bid,2,28 * Point * mp,0,"hedged",333,0,clrRed);}
               
            if (sell_trades_number == 6 && numberofhedgedsells == 0)
               {OrderSend(Symbol(),OP_BUY,50,Ask,2,28 * Point * mp,0,"hedged",334,0,clrGreen);}
               

     
   

}

Yes! exactly what I was looking for, however, this doesnt work although seemingly it should. I also tried reversing the conditions by using != and sticking in a continue but that didnt work. Do you see the error here that I dont see?


essentially what Im trying to do is see if there is an order in the last 10 trades with a magic number of 333 or 334 and if there is, I want to break out of the function

 
lippmaje:
I'd put the first loop like this, if you intended to look at the recent history to locate one of these orders (not sure if I got you.


void hedger()
{
   int buy_trades_number = _get_number_of_trades(OP_BUY);
   int sell_trades_number = _get_number_of_trades(OP_SELL);
   int numberofhedgedbuys = _get_number_of_trades_hedge_buy(OP_BUY);
   int numberofhedgedsells = _get_number_of_trades_hedge_sell(OP_SELL);

   
   int found=0;

   for(int n=OrdersHistoryTotal()-1; n>=0; n--) 
     {
      if(n<OrdersHistoryTotal()-maxEntriesToLookAt) break;
      if(OrderSelect(n,SELECT_BY_POS,MODE_HISTORY) && (OrderMagicNumber()==333 || OrderMagicNumber()==334)) { found=1; break; }
     }
   if(found) Print("Magic order exists in recent history!");
   else;

   
 

            if (buy_trades_number == 7 && numberofhedgedbuys == 0 && found==0)
               {OrderSend(Symbol(),OP_SELL,50,Bid,2,28 * Point * mp,0,"hedged",333,0,clrRed);}
               
            if (sell_trades_number ==7 && numberofhedgedsells == 0 && found==0)
               {OrderSend(Symbol(),OP_BUY,50,Ask,2,28 * Point * mp,0,"hedged",334,0,clrGreen);}
               

     
   

}

does this look fine to you? Or is it bad code practice?


 
marth tanaka:

...
essentially what Im trying to do is see if there is an order in the last 10 trades with a magic number of 333 or 334 and if there is, I want to break out of the function

This is easy. I wonder why you didn't see it:

if(found) return; // was: Print("Magic order exists in recent history!");

Or, to take your else construct into account:

   if(found) Print("Magic order exists in recent history!");
   else
   {
      if (buy_trades_number == 7 && numberofhedgedbuys == 0)
         {OrderSend(Symbol(),OP_SELL,50,Bid,2,28 * Point * mp,0,"hedged",333,0,clrRed);}

      if (sell_trades_number == 7 && numberofhedgedsells == 0)
         {OrderSend(Symbol(),OP_BUY,50,Ask,2,28 * Point * mp,0,"hedged",334,0,clrGreen);}
   }


Note there's some really helpful tool to fix the code indentation (formatting of white space etc to improve readability without changing the logic), it's called Styler. You'll find it in the Tools menu of the editor.

Reason: