Function to get the last open type :

Code:
int lastype(){ // count(OP_BUY)
   int type=0;
   for(int j=OrdersTotal()-1;j>=0;j--){
      if(!OrderSelect(j,SELECT_BY_POS))continue;
      //if(OrderType()==ordertype)
         if(OrderMagicNumber()==Magic)
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_BUY || OrderType()==OP_SELL){type=OrderType();return(type);}
   }
   return(0);
}