//+------------------------------------------------------------------+
//|                                             Exp - LineOfZERO.mq4 |
//|                                       Copyright © 2016, expforex |
//|                                        http://www.expforex.at.ua |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2016, expforex"
#property link      "http://www.expforex.at.ua"

#property indicator_chart_window

extern int OP=-1;
extern int Magic=-1;
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   SetHLine(Green,"LineofZERO",startZERO(OP));
//----
   return(0);
  }
//+------------------------------------------------------------------+



double startZERO(int OP=0)
  {
   double BuyLots=0;
   double SellLots=0;
   double BuyProfit=0;
   double SellProfit=0;
   int Total=OrdersTotal();
   for(int i=Total-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS))
        {
         if(OrderSymbol()!=Symbol()&&( OrderMagicNumber()!=Magic || Magic==-1)) continue;
         if(OrderType()==OP_BUY)
           {
            BuyLots=BuyLots+OrderLots();
            BuyProfit=BuyProfit+OrderProfit()+OrderCommission()+OrderSwap();
           }
         if(OrderType()==OP_SELL)
           {
            SellLots=SellLots+OrderLots();
            SellProfit=SellProfit+OrderProfit()+OrderCommission()+OrderSwap();
           }
        }
     }
   double Price=0;
   double TickValue=MarketInfo(Symbol(),MODE_TICKVALUE);
   if(BuyLots>0) double BuyLevel=NormalizeDouble(Bid-(BuyProfit/(TickValue*BuyLots)*Point),Digits); else BuyLevel=0;
   if(SellLots>0) double SellLevel=NormalizeDouble(Ask+(SellProfit/(TickValue*SellLots)*Point),Digits); else SellLevel=0;
   if((BuyLots-SellLots)>0) Price=NormalizeDouble(Bid-((BuyProfit+SellProfit)/(TickValue*(BuyLots-SellLots))*Point),Digits);
   if((SellLots-BuyLots)>0) Price=NormalizeDouble(Ask+((BuyProfit+SellProfit)/(TickValue*(SellLots-BuyLots))*Point),Digits);
   string Title="Уровень без убытка для "+Symbol();
   string ZeroLevel=" не существует";
   if(Price>0) ZeroLevel=" = "+DoubleToStr(Price,Digits);
   string Buy=" не существует";
   if(BuyLevel>0) Buy=" = "+DoubleToStr(BuyLevel,Digits);
   string Sell=" не существует";
   if(SellLevel>0) Sell=" = "+DoubleToStr(SellLevel,Digits);
   string Message="Уровень без убытка"+ZeroLevel+"\t\nУровень на покупку"+Buy+"\t\nУровень на продажу"+Sell;
 if (Price==0)Price=Ask;
   if(OP==OP_BUY)return(BuyLevel);
   if(OP==OP_SELL)return(SellLevel); 
   if(OP==-1)return(Price);


  }
  
  
  
  void SetHLine(color cl,string nm="",double p1=0,int st=0,int wd=1) 
  {
   if(nm=="") nm=DoubleToStr(Time[0],0);
   if(p1<=0) p1=Bid;
   if(ObjectFind(nm)<0) ObjectCreate(nm,OBJ_HLINE,0,0,0);
   ObjectSet(nm,OBJPROP_PRICE1,p1);
   ObjectSet(nm,OBJPROP_COLOR,cl);
   ObjectSet(nm,OBJPROP_STYLE,st);
   ObjectSet(nm,OBJPROP_WIDTH,wd);
  }

  