//+------------------------------------------------------------------+ //| 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" #property indicator_chart_window extern double Profit=0.0; extern int MagicNumber = 0; extern string NameBuy = "LineBuy"; extern string NameSell = "LineSell"; extern color ColorBuy = DarkBlue; extern color ColorSell = FireBrick; double LotsBuy, LotsSell; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ void init() { return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ void deinit() { if (ObjectFind(NameBuy)!=-1) ObjectDelete(NameBuy); if (ObjectFind(NameSell)!=-1) ObjectDelete(NameSell); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ void start() { int counted_bars=IndicatorCounted(); int Window; double PB = ProfitPrice(Symbol(), OP_BUY, MagicNumber, Profit); double PS = ProfitPrice(Symbol(), OP_SELL, MagicNumber, Profit); //double P = (PB*LotsBuy + PS*LotsSell) / (LotsBuy+LotsSell); //Window=WindowFind(Shortname); Window=0; if (ObjectFind(NameBuy)==-1) ObjectCreate(NameBuy,OBJ_HLINE,Window,0,PB); ObjectSet(NameBuy,OBJPROP_PRICE1,PB); ObjectSet(NameBuy,OBJPROP_COLOR,ColorBuy); if (ObjectFind(NameSell)==-1) ObjectCreate(NameSell,OBJ_HLINE,Window,0,PS); ObjectSet(NameSell,OBJPROP_PRICE1,PS); ObjectSet(NameSell,OBJPROP_COLOR,ColorSell); } //start() //+------------------------------------------------------------------+ double ProfitPrice(string fSymbol, int fType, int fMagic=0, double MyProfit=0.0){ //Функция возвращает цену, на которую необходимо установить уровень TakeProfit, чтобы получить прибыль MyProfit double SummPrice=0.0, SummLots=0.0, Formula=0.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++; SummLots=SummLots+OrderLots(); SummPrice=SummPrice+OrderOpenPrice()*OrderLots(); } } } }//Next i if(k>0){ if(fType==OP_BUY){ Formula = SummPrice/SummLots + MyProfit * MarketInfo(fSymbol, MODE_POINT) / (MarketInfo(fSymbol, MODE_TICKVALUE) * SummLots) + MarketInfo(fSymbol, MODE_SPREAD) * MarketInfo(fSymbol, MODE_POINT); LotsBuy = SummLots; } else { Formula = SummPrice/SummLots - MyProfit * MarketInfo(fSymbol, MODE_POINT) / (MarketInfo(fSymbol, MODE_TICKVALUE) * SummLots) - MarketInfo(fSymbol, MODE_SPREAD) * MarketInfo(fSymbol, MODE_POINT); LotsSell = SummLots; } } return(Formula); }//ProfitPrice() //+------------------------------------------------------------------+