//+------------------------------------------------------------------+
//|                                     cmkm-2011- thhudu@yahoo.com  |
//|                           Code Base on ForexPrediction.mq4       |
//+------------------------------------------------------------------+
#property copyright "cmkm-2011- thhudu@yahoo.com"

#property indicator_separate_window

extern int Conner=1;
int Size =10;
int Sell_Col=Red;
int Buy_Col=Lime;
string Font = "Comic Sans MS";

int S,B,rB,rS,NoStopLoss;

//-----------
int deinit() {
   ObjectDelete("Sell_orders");
   ObjectDelete("Buy_orders");
   ObjectDelete("NoStopLoss");
   ObjectDelete("StopLossOK");
   return (0);
}
//-----------
int init() {
IndicatorShortName("| Stoploss manager | ");
   return (0);
}
//-----------
int start() {
      int ord_total = OrdersTotal();     

      int  ord_Buy  = 0,BuyLimit = 0,BuyStop = 0; //Buy
      int  SL_Buy   = 0,SL_BLi= 0,SL_BX= 0;       //Stoploss Buy
      
      int  ord_Sell = 0,SellLimit = 0,SellStop = 0; //Sell
      int  SL_Sell  = 0,SL_SLi= 0,SL_SX = 0;        //Stoploss Sell
  
      for (int pos = ord_total - 1; pos >= 0; pos--) {
         if (OrderSelect(pos, SELECT_BY_POS)) {
            if (OrderSymbol() == Symbol()) {
               if (OrderType() == OP_BUY)     ord_Buy ++;
               if (OrderType() == OP_BUYLIMIT) BuyLimit ++;
               if (OrderType() == OP_BUYSTOP)  BuyStop  ++;
               
               if (OrderType() == OP_SELL)    ord_Sell ++;
               if (OrderType() == OP_SELLLIMIT) SellLimit ++;
               if (OrderType() == OP_SELLSTOP)  SellStop  ++;
                 
               if (OrderStopLoss()!=0&&OrderType() == OP_BUY)    SL_Buy ++;
               if (OrderStopLoss()!=0&&OrderType() == OP_BUYLIMIT) SL_BLi ++;
               if (OrderStopLoss()!=0&&OrderType() == OP_BUYSTOP)  SL_BX  ++;
               
               if (OrderStopLoss()!=0&&OrderType() == OP_SELL)    SL_Sell ++;
               if (OrderStopLoss()!=0&&OrderType() == OP_SELLLIMIT) SL_SLi ++;
               if (OrderStopLoss()!=0&&OrderType() == OP_SELLSTOP)  SL_SX  ++; 
            }}}
      
      NoStopLoss= (BuyLimit-SL_BLi)+(BuyStop-SL_BX)+(SellStop-SL_SX)+(SellLimit-SL_SLi);

  if ((ord_Buy-SL_Buy)>(ord_Sell-SL_Sell)){
        rB = (ord_Buy-SL_Buy)-(ord_Sell-SL_Sell);            //residual Buy
        
         NoStopLoss= MathAbs(rB-(SellStop-SL_SX)
                         +(SellLimit-SL_SLi))+(BuyLimit-SL_BLi)+(BuyStop-SL_BX);}
                  
  if ((ord_Sell-SL_Sell)>(ord_Buy-SL_Buy)){
        rS = (ord_Sell-SL_Sell)-(ord_Buy-SL_Buy);            //residual Sell
        
        NoStopLoss= MathAbs(rS-(BuyStop-SL_BX))
                         +(BuyLimit-SL_BLi)+(SellStop-SL_SX)+(SellLimit-SL_SLi);}
          
      S = ord_Sell+SellLimit+SellStop;
      B = ord_Buy+BuyLimit+BuyStop;
       
//show StopLoss status
 if (NoStopLoss==0){
          ObjectDelete("NoStopLoss");
          setLabel("StopLossOK","* ALL  StopLoss   OK !",White,Conner,5,5,false,Size, Font);}
      else {  ObjectDelete("StopLossOK");
               setLabel("NoStopLoss","** NO StopLoss = "+NoStopLoss+"  *",OrangeRed,Conner,5,5,false,Size+1, Font);
             // Alert("** No StopLoss : "+NoStopLoss);  }\
             }
   
   setLabel("Buy_orders", "Buy  : "+B,Buy_Col,Conner,5,22,false,Size, Font);
   setLabel("Sell_orders","Sell : "+S,Sell_Col,Conner,5,38,false,Size, Font);
   
   return (0);
}

//+------------------------------------------------------------------+
void setLabel(string name, string text, color col, int corner,
            int x, int y, bool back = false, int fontsize = 12, 
            string fontname = "MS Sans Serif") {
   if (ObjectFind(name)==-1){
      // aaa
      ObjectCreate(name, OBJ_LABEL, WindowOnDropped(),1, 0, 0);
      ObjectSetText(name, text, fontsize, fontname, col);
      ObjectSet(name, OBJPROP_CORNER, corner);
      ObjectSet(name, OBJPROP_BACK,back);
      ObjectSet(name, OBJPROP_XDISTANCE, x);
      ObjectSet(name, OBJPROP_YDISTANCE, y);          
   }else{
      ObjectSetText(name, text, fontsize, fontname, col);
      ObjectSet(name, OBJPROP_CORNER, corner);
      ObjectSet(name, OBJPROP_BACK,back);
      ObjectSet(name, OBJPROP_XDISTANCE, x);
      ObjectSet(name, OBJPROP_YDISTANCE, y); 
   }  
}