Author: Copyright © 2010, x5d
0 Views
0 Downloads
0 Favorites
ProfLine
ÿþ//+------------------------------------------------------------------+

//|                                                     ProfLine.mq5 |

//|                                                              x5d |

//|                                https://www.mql5.com/ru/users/x5d |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2010, x5d"

#property link      "https://www.mql5.com/ru/users/x5d"

#property indicator_chart_window

#property version   "1.00"



//+------------------------------------------------------------------+

//| Declaration of variables                                         |

//+------------------------------------------------------------------+

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 deinitialization function                       | 

//+------------------------------------------------------------------+ 

 void deinit() 

 { 

   if (ObjectFind(0,NameBuy)!=-1) ObjectDelete(0,NameBuy); 

   if (ObjectFind(0,NameSell)!=-1) ObjectDelete(0,NameSell); 

 } 



//+------------------------------------------------------------------+

//| Script program start function                                    |

//+------------------------------------------------------------------+

void OnStart()

  {

   if (ObjectFind(0,NameBuy)!=-1) ObjectDelete(0,NameBuy); 

   if (ObjectFind(0,NameSell)!=-1) ObjectDelete(0,NameSell); 

    

   double ProfitBuy = ProfitPrice(Symbol(), "ORDER_TYPE_BUY", MagicNumber, Profit); 

   double ProfitSell = ProfitPrice(Symbol(), "ORDER_TYPE_SELL", MagicNumber, Profit); 



   if (ObjectFind(0,NameBuy)==-1)

   {

    if(!HLineCreate(0,NameBuy,0,ProfitBuy,ColorBuy)) 

     { 

      return; 

     } 

   ChartRedraw(); 

   }

   if (ObjectFind(0,NameSell)==-1)

   {

    if(!HLineCreate(0,NameSell,0,ProfitSell,ColorSell)) 

     { 

      return; 

     } 

   ChartRedraw(); 

   }

   

  }

  

//+------------------------------------------------------------------+

//| Function of drawing a horizontal line                            |

//+------------------------------------------------------------------+

bool HLineCreate(const long            chart_ID=0,        // ID 3@0D8:0 

                 const string          name="HLine",      // 8<O ;8=88 

                 const int             sub_window=0,      // =><5@ ?>4>:=0 

                 double                price=0,           // F5=0 ;8=88 

                 const color           clr=clrRed,        // F25B ;8=88 

                 const ENUM_LINE_STYLE style=STYLE_SOLID, // AB8;L ;8=88 

                 const int             width=1,           // B>;I8=0 ;8=88 

                 const bool            back=false,        // =0 704=5< ?;0=5 

                 const bool            selection=true,    // 2K45;8BL 4;O ?5@5<5I5=89 

                 const bool            hidden=true,       // A:@KB 2 A?8A:5 >1J5:B>2 

                 const long            z_order=0)         // ?@8>@8B5B =0 =060B85 <KHLN 

  { 



   ResetLastError(); 

   if(!ObjectCreate(chart_ID,name,OBJ_HLINE,sub_window,0,price)) 

     { 

      Print(__FUNCTION__, 

            ": =5 C40;>AL A>740BL 3>@87>=B0;L=CN ;8=8N! >4 >H81:8 = ",GetLastError()); 

      return(false); 

     } 

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr); 

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style); 

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width); 

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back); 

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection); 

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection); 

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden); 

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order); 

   return(true); 

  } 





//+------------------------------------------------------------------+

//| Profit line calculation function                                 |

//+------------------------------------------------------------------+

double ProfitPrice(string fSymbol, string fType, int fMagic=0, double MyProfit=0.0)

{ 

   //$C=:F8O 2>72@0I05B F5=C, =0 :>B>@CN =5>1E>48<> CAB0=>28BL C@>25=L TakeProfit, GB>1K ?>;CG8BL ?@81K;L MyProfit 

   double SummPrice=0.0, SummLots=0.0, Formula=0.0; 

   int SumTiket =0;

   int total=PositionsTotal();

      

   for(int i=total-1; i>=0; i--)

   {

   PositionGetSymbol(i);

    if(PositionGetString(POSITION_SYMBOL)==fSymbol)

     {

     if(PositionGetInteger(POSITION_MAGIC)==fMagic || fMagic==0)

      {

      if(EnumToString(ENUM_ORDER_TYPE(PositionGetInteger(POSITION_TYPE)))==fType)

       {

        SummLots=SummLots+PositionGetDouble(POSITION_VOLUME);

        SummPrice=SummPrice+(PositionGetDouble(POSITION_PRICE_OPEN)*PositionGetDouble(POSITION_VOLUME));

        SumTiket = SumTiket+1;

       }

      }

     }

   }

   

   if(SumTiket>0)

   { 

    

    if(fType=="ORDER_TYPE_BUY")

    { 

     Formula = SummPrice/SummLots + 

     MyProfit * SymbolInfoDouble(fSymbol,SYMBOL_POINT) / 

     (SymbolInfoDouble(fSymbol,SYMBOL_TRADE_TICK_SIZE) * SummLots) + 

     (SymbolInfoInteger(fSymbol,SYMBOL_SPREAD) * SymbolInfoDouble(fSymbol,SYMBOL_POINT)); 

     LotsBuy = SummLots; 

    } 

    if(fType=="ORDER_TYPE_SELL") 

    { 

     Formula = SummPrice/SummLots - 

     MyProfit * SymbolInfoDouble(fSymbol,SYMBOL_POINT) / 

     (SymbolInfoDouble(fSymbol,SYMBOL_TRADE_TICK_SIZE) * SummLots) - 

     (SymbolInfoInteger(fSymbol,SYMBOL_SPREAD) * SymbolInfoDouble(fSymbol,SYMBOL_POINT));

     LotsSell = SummLots;    

    } 

   } 

   

   return(Formula); 

}//ProfitPrice() 

Comments