//+------------------------------------------------------------------+
//|                                                       NoLoss.mq4 |
//|                              Copyright © 2013, Khlistov Vladimir |
//|                                         http://cmillion.narod.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013, Khlistov Vladimir"
#property link      "http://cmillion.narod.ru"
#property indicator_chart_window
extern color ColorMarket = Gold;
extern color ColorSL     = Lime;
extern int   Width     = 2;
extern color ColorBuy  = DodgerBlue;
extern int   WidthBuy  = 1;
extern color ColorSell = Red;
extern int   WidthSell = 1;
extern int   Magic     = -1; //если -1, то все ордера на данной валютной паре, иначе только с магиком
//+------------------------------------------------------------------+
int start()
{
   double profit, Profit, ProfitS, ProfitB;
   int b,s,tip,bo,so;
   double price,price_b,price_s,lot,lot_s,lot_b,price_bo,price_so,lot_so,lot_bo,NLb,NLs,NL;
   double TICKVALUE=MarketInfo(Symbol(),MODE_TICKVALUE);
   double LOTSIZE = MarketInfo(Symbol(),MODE_LOTSIZE);
   int n,Ticket[100];ArrayInitialize(Ticket,0);
   for (int i=0; i<OrdersTotal(); i++)
   {  
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {  
         if (OrderSymbol()==Symbol() && (Magic == OrderMagicNumber() || Magic == -1))
         {
            profit = OrderProfit()+OrderSwap()+OrderCommission();
            Profit += profit;
            price = OrderOpenPrice();
            lot = OrderLots();
            tip = OrderType();
            if (tip==OP_BUY ) 
            {
               ProfitB += profit;
               price_b += price*lot;
               b++;  
               lot_b+=lot;
            }
            if (tip==OP_SELL) 
            {
               ProfitS += profit;
               price_s += price*lot;
               s++;  
               lot_s+=lot;
            }
            Ticket[n]=OrderTicket();
            drawtext(Ticket[n],OrderOpenPrice(),lot,colorType(tip));
            n++;
            if (tip==OP_BUYSTOP || tip==OP_BUYLIMIT) 
            {
               price_bo += price*lot;
               lot_bo+=lot;
               bo++;
            }
            if (tip==OP_SELLSTOP || tip==OP_SELLLIMIT) 
            {
               price_so += price*lot;
               lot_so+=lot;
               so++;
            }
         }  
      }  
   }
   
   for(i=ObjectsTotal()-1; i>=0; i--) 
   {
      string Obj_Name=ObjectName(i);
      if (ObjectType(Obj_Name)!=OBJ_TEXT) continue;
      int Tik=StrToInteger(Obj_Name);
 
      if (Tik<1) continue;
      
      bool cont=false;
      for (int j=0; j<n; j++)
      {  
         if (Tik==Ticket[j]) {cont=true;break;}
      }
      if (cont) continue;
      ObjectDelete(Obj_Name);
   }
   
   
   ObjectDelete("NoLoss__NLb");
   if (b!=0) 
   {  
      NLb = price_b/lot_b;
      ObjectCreate("NoLoss__NLb",OBJ_ARROW,0,Time[0],NLb,0,0,0,0);                     
      ObjectSet   ("NoLoss__NLb",OBJPROP_ARROWCODE,6);
      ObjectSet   ("NoLoss__NLb",OBJPROP_COLOR, ColorBuy);
      ObjectSet   ("NoLoss__NLb",OBJPROP_WIDTH, WidthBuy);
   }
   ObjectDelete("NoLoss__NLs");
   if (s!=0) 
   {  
      NLs = price_s/lot_s;
      ObjectCreate("NoLoss__NLs",OBJ_ARROW,0,Time[0],NLs,0,0,0,0);                     
      ObjectSet   ("NoLoss__NLs",OBJPROP_ARROWCODE,6);
      ObjectSet   ("NoLoss__NLs",OBJPROP_COLOR, ColorSell);
      ObjectSet   ("NoLoss__NLs",OBJPROP_WIDTH, WidthSell);
   }
   ObjectDelete("NoLoss__NL");
   if (b!=0 && s!=0 && lot_b!=lot_s) 
   {  
      NL = NormalizeDouble(Bid - Profit/TICKVALUE/(lot_b-lot_s)*Point,Digits);
      ObjectCreate("NoLoss__NL",OBJ_ARROW,0,Time[0],NL,0,0,0,0);                     
      ObjectSet   ("NoLoss__NL",OBJPROP_ARROWCODE,6);
      ObjectSet   ("NoLoss__NL",OBJPROP_COLOR, ColorMarket);
      ObjectSet   ("NoLoss__NL",OBJPROP_WIDTH, Width);
   }
   ObjectDelete("NoLoss_с отложками");
   if (bo+b!=0 && so+s!=0 && lot_b+lot_bo!=lot_s+lot_so) 
   {  
      NL = NormalizeDouble((price_bo+price_b-price_so-price_s)/(lot_b+lot_bo-lot_s-lot_so),Digits);
      ObjectCreate("NoLoss_с отложками",OBJ_ARROW,0,Time[0],NL,0,0,0,0);                     
      ObjectSet   ("NoLoss_с отложками",OBJPROP_ARROWCODE,6);
      ObjectSet   ("NoLoss_с отложками",OBJPROP_COLOR, ColorSL);
      ObjectSet   ("NoLoss_с отложками",OBJPROP_WIDTH, Width);
   }
   //DrawLABEL("NoLoss__TIME",3,5,15,Gray,TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
   DrawLABEL("NoLoss__1",3,5,30,colorPrice(ProfitB),StringConcatenate("Buy ",b,"  Profit ",DoubleToStr(ProfitB,2),"  Lot ",DoubleToStr(lot_b,2)));
   DrawLABEL("NoLoss__2",3,5,45,colorPrice(ProfitS),StringConcatenate("Sell ",s,"  Profit ",DoubleToStr(ProfitS,2),"  Lot ",DoubleToStr(lot_s,2)));
   DrawLABEL("NoLoss__b",3,5,60,colorPriceo(lot_so),StringConcatenate("Отложек sell ",so,"   lot ",DoubleToStr(lot_so,2)));
   DrawLABEL("NoLoss__s",3,5,75,colorPriceo(lot_bo),StringConcatenate("Отложек buy ",bo,"   lot ",DoubleToStr(lot_bo,2)));
    
   
   double Margin0; 
   if (lot_b!=lot_s) Margin0 = Bid - AccountFreeMargin() / (MarketInfo(Symbol(), MODE_TICKVALUE) * (lot_b-lot_s))*Point; 
   drawHLINE("NoLoss__Margin 0", Margin0, Chocolate);
   //DrawLABEL("Level Margin 0",3,5,75,Chocolate,StringConcatenate("Margin 0 ",DoubleToStr(Margin0,Digits)));
   
   double MarginP;
   if (lot_b!=lot_s) MarginP = AccountEquity() / (MarketInfo(Symbol(), MODE_TICKVALUE) * (lot_b-lot_s));
   
   double MarginCall = Bid - MarginP*Point;
   drawHLINE("NoLoss__Margin Call", MarginCall, Red);
   DrawLABEL("NoLoss__Level Margin Call",3,5,15,Red,StringConcatenate("Margin Call ",DoubleToStr(MarginCall,Digits),"   ",DoubleToStr(MathAbs(MarginP),0)," п"));
}
//+------------------------------------------------------------------+
int deinit()
{
   for(int k=ObjectsTotal()-1; k>=0; k--) 
   {
      string Obj_Name=ObjectName(k);
      if (StringFind(Obj_Name,"NoLoss__",0)!=-1) ObjectDelete(Obj_Name);
   }
   return(0);
}
//+------------------------------------------------------------------+
int drawHLINE(string NameLine, double price,color c)
{
   ObjectCreate(NameLine, OBJ_HLINE, 0, 0, price);
   ObjectSet(NameLine, OBJPROP_COLOR, c);
   ObjectSet(NameLine, OBJPROP_STYLE, 2);
   ObjectSet(NameLine, OBJPROP_WIDTH, 1);
}
//--------------------------------------------------------------------
int drawtext(string NameLine, double Y1, double l,color c)
{
   //NameLine=StringConcatenate("NoLoss__",NameLine);
   int i;
   if (c==ColorBuy) i=WindowFirstVisibleBar()-WindowFirstVisibleBar()/10;
   else i=WindowFirstVisibleBar()-WindowFirstVisibleBar()/5;
   ObjectDelete (NameLine);
   ObjectCreate (NameLine, OBJ_TEXT,0,Time[i],Y1,0,0,0,0);
   ObjectSetText(NameLine, DoubleToStr(l,2),8,"Arial");
   ObjectSet    (NameLine, OBJPROP_COLOR, c);
}
//--------------------------------------------------------------------
void DrawLABEL(string name, int CORNER, int X, int Y, color clr, string Name)
{
   if (ObjectFind(name)==-1)
   {
      ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
      ObjectSet(name, OBJPROP_CORNER, CORNER);
      ObjectSet(name, OBJPROP_XDISTANCE, X);
      ObjectSet(name, OBJPROP_YDISTANCE, Y);
   }
   ObjectSetText(name,Name,10,"Arial",clr);
}
//+------------------------------------------------------------------+
color colorType(int t) 
{ 
   if (t==OP_BUY) return(ColorBuy);
   if (t==OP_SELL) return(ColorSell);
   if (t==OP_BUYLIMIT) return(ColorBuy);
   if (t==OP_SELLLIMIT) return(ColorSell);
   if (t==OP_BUYSTOP) return(ColorBuy);
   if (t==OP_SELLSTOP) return(ColorSell);
} 
//-------------------------------------------------------------------- 
color colorPriceo(double p) 
{ 
   if (p==0) return(Gray);
   if (p>0) return(ColorSL);
   if (p<0) return(Red);
} 
//-------------------------------------------------------------------- 
color colorPrice(double p) 
{ 
   if (p==0) return(Gray);
   if (p>0) return(ColorMarket);
   if (p<0) return(Red);
} 
//-------------------------------------------------------------------- 

