HLine Profit

Author: Copyright © 2019, Vladimir Karputov
Price Data Components
Series array that contains tick volumes of each bar
0 Views
0 Downloads
0 Favorites
HLine Profit
ÿþ//+------------------------------------------------------------------+

//|                                                 HLine Profit.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

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

#property copyright "Copyright © 2019, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots 0

//---

#include <Trade\PositionInfo.mqh>

CPositionInfo  m_position;                   // trade position object

//--- input parameters

input string      InpName="HLine Profit";    // HLine name

//---

string prefix="";

string label_name="";

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//---  

   prefix=InpName;

   label_name=prefix+"_"+"label";

//---

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   ObjectsDeleteAll(0,prefix,-1,-1);

  }

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const int begin,

                const double &price[])

  {

//---



//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

//| ChartEvent function                                              |

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//---

   if(id==CHARTEVENT_OBJECT_DRAG && sparam==InpName)

     {

      double hline_price=ObjectGetDouble(0,InpName,OBJPROP_PRICE);



      long x_distance;

      int x;

      int y;

      //--- set window size 

      if(!ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0,x_distance))

        {

         Print("Failed to get the chart width! Error code = ",GetLastError());

         return;

        }

      if(!ChartTimePriceToXY(0,0,TimeCurrent(),hline_price,x,y))

        {

         Print("Failed ChartTimePriceToXY! Error code = ",GetLastError());

         return;

        }

      //---

      if(ObjectFind(0,label_name)<0)

         LabelCreate(0,label_name,0,(int)x_distance/2,y,0,"Profit: "+DoubleToString(ProfitAllPositions(hline_price),2));

      else

        {

         LabelMove(0,label_name,(int)x_distance/2,y);

         LabelTextChange(0,label_name,"Profit: "+DoubleToString(ProfitAllPositions(hline_price),2));

        }

     }

  }

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

//| Profit all positions                                             |

//|   Profit:   (close_price - open_price) * Contract_Size * Lots    |

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

double ProfitAllPositions(const double price)

  {

   double profit=0.0;

   double contract_size=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_CONTRACT_SIZE);



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

      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

         if(m_position.Symbol()==Symbol()/* && m_position.Magic()==InpMagic*/)

           {

            profit+=m_position.Commission()+m_position.Swap();

            if(m_position.PositionType()==POSITION_TYPE_BUY)

               profit+=(price-m_position.PriceOpen())*contract_size*m_position.Volume();

            else

               profit+=(m_position.PriceOpen()-price)*contract_size*m_position.Volume();

           }

//---

   return(profit);

  }

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

//| Create a text label                                              | 

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

bool LabelCreate(const long              chart_ID=0,               // chart's ID 

                 const string            name="Label",             // label name 

                 const int               sub_window=0,             // subwindow index 

                 const int               x=0,                      // X coordinate 

                 const int               y=0,                      // Y coordinate 

                 const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring 

                 const string            text="Label",             // text 

                 const string            font="Tahoma",// font 

                 const int               font_size=10,             // font size 

                 const color             clr=clrDarkOrchid,        // color 

                 const double            angle=0.0,                // text slope 

                 const ENUM_ANCHOR_POINT anchor=ANCHOR_CENTER,     // anchor type 

                 const bool              back=false,               // in the background 

                 const bool              selection=false,          // highlight to move 

                 const bool              hidden=true,              // hidden in the object list 

                 const long              z_order=0)                // priority for mouse click 

  {

//--- reset the error value 

   ResetLastError();

//--- create a text label 

   if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))

     {

      Print(__FUNCTION__,

            ": failed to create text label! Error code = ",GetLastError());

      return(false);

     }

//--- set label coordinates 

   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);

   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);

//--- set the chart's corner, relative to which point coordinates are defined 

   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);

//--- set the text 

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

//--- set text font 

   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);

//--- set font size 

   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);

//--- set the slope angle of the text 

   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);

//--- set anchor type 

   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);

//--- set color 

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- display in the foreground (false) or background (true) 

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- enable (true) or disable (false) the mode of moving the label by mouse 

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- hide (true) or display (false) graphical object name in the object list 

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//--- set the priority for receiving the event of a mouse click in the chart 

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//--- successful execution 

   return(true);

  }

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

//| Move the text label                                              | 

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

bool LabelMove(const long   chart_ID=0,   // chart's ID 

               const string name="Label", // label name 

               const int    x=0,          // X coordinate 

               const int    y=0)          // Y coordinate 

  {

//--- reset the error value 

   ResetLastError();

//--- move the text label 

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x))

     {

      Print(__FUNCTION__,

            ": failed to move X coordinate of the label! Error code = ",GetLastError());

      return(false);

     }

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y))

     {

      Print(__FUNCTION__,

            ": failed to move Y coordinate of the label! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution 

   return(true);

  }

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

//| Change corner of the chart for binding the label                 | 

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

bool LabelChangeCorner(const long             chart_ID=0,               // chart's ID 

                       const string           name="Label",             // label name 

                       const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER) // chart corner for anchoring 

  {

//--- reset the error value 

   ResetLastError();

//--- change anchor corner 

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner))

     {

      Print(__FUNCTION__,

            ": failed to change the anchor corner! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution 

   return(true);

  }

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

//| Change the label text                                            | 

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

bool LabelTextChange(const long   chart_ID=0,   // chart's ID 

                     const string name="Label", // object name 

                     const string text="Text")  // text 

  {

//--- reset the error value 

   ResetLastError();

//--- change object text 

   if(!ObjectSetString(chart_ID,name,OBJPROP_TEXT,text))

     {

      Print(__FUNCTION__,

            ": failed to change the text! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution 

   return(true);

  }

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

Comments