getting total orders and one from history

 
void OnTick() 
   
   {  

    int count = 1;
    for (int i=OrdersTotal()-1; i >= 0; i--)
              if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && (OrderSymbol() == Symbol()))
    {    
            count= count + 5;
        }
    if(count>2)
    {return;}
    else
    {
     
     
    string Last;
 
for(int j=OrdersHistoryTotal(); j >= 0; j--) if (
        OrderSelect(j, SELECT_BY_POS, MODE_HISTORY)   // Only orders w/           // my magic number
    &&  OrderSymbol()       == Symbol()    
    &&  OrderMagicNumber()  == Magicnumber)  


can any one explain me why this code does not work...one receives the total number of open trades for a symbol so that teh robot doesnot send more....to continue if there are no open orders it scans the history for the last closed order for the symbol if it is in profit or loss

Reason: