//+------------------------------------------------------------------+
//|                                                   ProfitLine.mq4 |
//|                               Copyright © 2010, Evgeniy Trofimov |
//|                           http://www.mql4.com/ru/users/EvgeTrofi |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Evgeniy Trofimov"
#property link      "http://www.mql4.com/ru/users/EvgeTrofi"  // modification_24.06.2011 Anry
#property indicator_chart_window
extern double Profit=0;                          // Размер предполагаемой прибыли в тиках (0,1 пункта)
extern int MagicNumber = 0;
extern double Commission=6;                      // Размер комиссии в USD - сумма за открытие и закрытие позиции в 1 лот = 6 для Альпари счета NDD
extern color ColorBuy = MediumBlue;
extern color ColorSell = FireBrick;
extern color ColorBuyAll = C'132,132,255';
extern color ColorSellAll = C'227,113,113';
string Buy = "LineBuy";
string BuyPrice = "PriceLineBuy";
string Sell = "LineSell";
string SellPrice = "PriceLineSell";
string BuyAll = "LineBuyAll";
string BuyAllPrice = "PriceLineAllBuy";
string SellAll = "LineSellAll";
string SellAllPrice = "PriceLineAllSell";
double z;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void init()
  {return(0);}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void deinit()
  {if (ObjectFind(Buy)!=-1) ObjectDelete(Buy);
   if (ObjectFind(Sell)!=-1) ObjectDelete(Sell);
   if (ObjectFind(BuyAll)!=-1) ObjectDelete(BuyAll);
   if (ObjectFind(SellAll)!=-1) ObjectDelete(SellAll);
   if (ObjectFind(BuyPrice)!=-1) ObjectDelete(BuyPrice);
   if (ObjectFind(SellPrice)!=-1) ObjectDelete(SellPrice);
   if (ObjectFind(BuyAllPrice)!=-1) ObjectDelete(BuyAllPrice);
   if (ObjectFind(SellAllPrice)!=-1) ObjectDelete(SellAllPrice);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
void start()
  {int counted_bars=IndicatorCounted();
   if(Digits==3) z=0.001;
   if(Digits==5) z=0.00001;
   double PB = Price(Symbol(), OP_BUY, MagicNumber, Profit);
   double PS = Price(Symbol(), OP_SELL, MagicNumber, Profit);
   if (ObjectFind(Buy)==-1) ObjectCreate(Buy,OBJ_TREND,0,Time[5],PB,Time[0]+Period()*60*20,PB);
   ObjectSet(Buy,OBJPROP_TIME1,Time[5]);
   ObjectSet(Buy,OBJPROP_PRICE1,PB);
   ObjectSet(Buy,OBJPROP_TIME2,Time[0]+Period()*60*20);
   ObjectSet(Buy,OBJPROP_PRICE2,PB);
   ObjectSet(Buy,OBJPROP_RAY,false);
   ObjectSet(Buy,OBJPROP_COLOR,ColorBuy);
   if (ObjectFind(Sell)==-1) ObjectCreate(Sell,OBJ_TREND,0,Time[5],PS,Time[0]+Period()*60*20,PS);
   ObjectSet(Sell,OBJPROP_TIME1,Time[5]);
   ObjectSet(Sell,OBJPROP_PRICE1,PS);
   ObjectSet(Sell,OBJPROP_TIME2,Time[0]+Period()*60*20);
   ObjectSet(Sell,OBJPROP_PRICE2,PS);
   ObjectSet(Sell,OBJPROP_RAY,false);
   ObjectSet(Sell,OBJPROP_COLOR,ColorSell);
   if (ObjectFind(BuyPrice)==-1) ObjectCreate(BuyPrice,OBJ_ARROW,0,Time[5],PB);
   ObjectSet(BuyPrice,OBJPROP_TIME1,Time[5]);
   ObjectSet(BuyPrice,OBJPROP_PRICE1,PB);
   ObjectSet(BuyPrice,OBJPROP_ARROWCODE,5); 
   ObjectSet(BuyPrice,OBJPROP_COLOR,ColorBuy);
   if (ObjectFind(SellPrice)==-1) ObjectCreate(SellPrice,OBJ_ARROW,0,Time[5],PS);
   ObjectSet(SellPrice,OBJPROP_TIME1,Time[5]);
   ObjectSet(SellPrice,OBJPROP_PRICE1,PS);
   ObjectSet(SellPrice,OBJPROP_ARROWCODE,5); 
   ObjectSet(SellPrice,OBJPROP_COLOR,ColorSell);
   double PBAll = PriceBUYAll(Symbol(), MagicNumber, Profit);
   double PSAll = PriceSELLAll(Symbol(), MagicNumber, Profit);
   if (ObjectFind(BuyAll)==-1) ObjectCreate(BuyAll,OBJ_TREND,0,Time[5],PBAll,Time[0]+Period()*60*20,PBAll);
   ObjectSet(BuyAll,OBJPROP_TIME1,Time[5]);
   ObjectSet(BuyAll,OBJPROP_PRICE1,PBAll);
   ObjectSet(BuyAll,OBJPROP_TIME2,Time[0]+Period()*60*20);
   ObjectSet(BuyAll,OBJPROP_PRICE2,PBAll);
   ObjectSet(BuyAll,OBJPROP_RAY,false);
   ObjectSet(BuyAll, OBJPROP_STYLE, STYLE_DASH);
   ObjectSet(BuyAll,OBJPROP_COLOR,ColorBuyAll);
   if (ObjectFind(SellAll)==-1) ObjectCreate(SellAll,OBJ_TREND,0,Time[5],PSAll,Time[0]+Period()*60*20,PSAll);
   ObjectSet(SellAll,OBJPROP_TIME1,Time[5]);
   ObjectSet(SellAll,OBJPROP_PRICE1,PSAll);
   ObjectSet(SellAll,OBJPROP_TIME2,Time[0]+Period()*60*20);
   ObjectSet(SellAll,OBJPROP_PRICE2,PSAll);
   ObjectSet(SellAll,OBJPROP_RAY,false);
   ObjectSet(SellAll, OBJPROP_STYLE, STYLE_DASH);
   ObjectSet(SellAll,OBJPROP_COLOR,ColorSellAll);
   if (ObjectFind(BuyAllPrice)==-1) ObjectCreate(BuyAllPrice,OBJ_ARROW,0,Time[5],PBAll);
   ObjectSet(BuyAllPrice,OBJPROP_TIME1,Time[5]);
   ObjectSet(BuyAllPrice,OBJPROP_PRICE1,PBAll);
   ObjectSet(BuyAllPrice,OBJPROP_ARROWCODE,5); 
   ObjectSet(BuyAllPrice,OBJPROP_COLOR,ColorBuyAll);
   if (ObjectFind(SellAllPrice)==-1) ObjectCreate(SellAllPrice,OBJ_ARROW,0,Time[5],PSAll);
   ObjectSet(SellAllPrice,OBJPROP_TIME1,Time[5]);
   ObjectSet(SellAllPrice,OBJPROP_PRICE1,PSAll);
   ObjectSet(SellAllPrice,OBJPROP_ARROWCODE,5); 
   ObjectSet(SellAllPrice,OBJPROP_COLOR,ColorSellAll);
  }//start()
//+------------------------------------------------------------------+
double Price(string fSymbol, int fType, int fMagic=0, double MyProfit=0)      //Функция возвращает цену, на которую необходимо установить уровень TakeProfit, чтобы получить прибыль Profit, для открытых ордеров
  {double SummPrice=0, SummLots=0, Formula=0, StoimTik, SummSwap=0, SummCom=0;
   int k;
   int total = OrdersTotal();
   for (int i=total-1; i>=0; i--)
     {OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol()==fSymbol)
        {if(OrderMagicNumber()==fMagic || fMagic==0)
           {if(OrderType()==fType)
              {k++;
               StoimTik=MarketInfo(fSymbol,MODE_TICKVALUE)/(MarketInfo(fSymbol,MODE_TICKSIZE)/MarketInfo(fSymbol,MODE_POINT));    //Стоимость 1 тика при ордере в 1 лот
               if(OrderSwap()!=0) SummSwap+=OrderSwap()/StoimTik;
               if(Commission>0)
                 {if(AccountCurrency()=="RUR") SummCom=Commission*MarketInfo("USDRUR",MODE_BID)/StoimTik;
                  if(AccountCurrency()=="USD") SummCom=Commission/StoimTik;
                 }
               SummLots+=OrderLots();
               SummPrice+=OrderOpenPrice()*OrderLots();
              }
           }
        }
     }//Next i  
   if(k>0)
     {if(fType==OP_BUY) Formula=SummPrice/SummLots+z*(SummCom+MyProfit-SummSwap/SummLots);
      if(fType==OP_SELL) Formula=SummPrice/SummLots-z*(SummCom+MyProfit-SummSwap/SummLots);
     }
   return(Formula);
  }//Price()
//+------------------------------------------------------------------+
double PriceBUYAll(string fSymbol, int fMagic=0, double MyProfit=0)      //Функция возвращает цену, на которую необходимо установить уровень TakeProfit, чтобы получить прибыль Profit, для всех ордеров BUY
  {double SummPrice=0, SummLots=0, Formula=0, StoimTik, SummSwap=0, SummCom=0;
   int k;
   int total = OrdersTotal();
   for (int i = total-1; i >= 0; i--)
     {OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol()==fSymbol)
        {if(OrderMagicNumber()==fMagic || fMagic==0)
           {if(OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT)
              {if(OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT) k++;
               if (OrderSwap()!=0 || Commission!=0)
                 {StoimTik=MarketInfo(fSymbol,MODE_TICKVALUE)/(MarketInfo(fSymbol,MODE_TICKSIZE)/MarketInfo(fSymbol,MODE_POINT));    //Стоимость 1 тика при ордере в 1 лот
                  SummSwap+=OrderSwap()/StoimTik;
                  if(AccountCurrency()=="RUR") SummCom=Commission*MarketInfo("USDRUR",MODE_BID)/StoimTik;
                  if(AccountCurrency()=="USD") SummCom=Commission/StoimTik;
                 }
               SummLots+=OrderLots();
               SummPrice+=OrderOpenPrice()*OrderLots();
              }
            if(k>0) Formula=SummPrice/SummLots+z*(SummCom+MyProfit-SummSwap/SummLots);
           }
        }
     }//Next i  
   return(Formula);
  }//PriceBUYAll()
//+------------------------------------------------------------------+
double PriceSELLAll(string fSymbol, int fMagic=0, double MyProfit=0)      //Функция возвращает цену, на которую необходимо установить уровень TakeProfit, чтобы получить прибыль Profit, для всех ордеров SELL
  {double SummPrice=0, SummLots=0, Formula=0, StoimTik, SummSwap=0, SummCom=0;
   int k;
   int total = OrdersTotal();
   for (int i = total-1; i >= 0; i--)
     {OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol()==fSymbol)
        {if(OrderMagicNumber()==fMagic || fMagic==0)
           {if(OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT)
              {if(OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT) k++;
               if (OrderSwap()!=0 || Commission!=0)
                 {StoimTik=MarketInfo(fSymbol,MODE_TICKVALUE)/(MarketInfo(fSymbol,MODE_TICKSIZE)/MarketInfo(fSymbol,MODE_POINT));    //Стоимость 1 тика при ордере в 1 лот
                  SummSwap+=OrderSwap()/StoimTik;
                  if(AccountCurrency()=="RUR") SummCom=Commission*MarketInfo("USDRUR",MODE_BID)/StoimTik;
                  if(AccountCurrency()=="USD") SummCom=Commission/StoimTik;
                 }
               SummLots+=OrderLots();
               SummPrice+=OrderOpenPrice()*OrderLots();
              }
            if(k>0) Formula=SummPrice/SummLots-z*(SummCom+MyProfit-SummSwap/SummLots);
           }
        }
     }//Next i  
   return(Formula);
  }//PriceSELLAll()
//+------------------------------------------------------------------+

