ProfitOfCurrentSymbol

Orders Execution
Checks for the total of open orders
0 Views
0 Downloads
0 Favorites
ProfitOfCurrentSymbol
ÿþ//+------------------------------------------------------------------+

//|                               Profit of the current symbol 2.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

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

#property version   "3.00"

#property strict

#property description "Profit of the current symbol"

#property description "Additionally: 'Profit for all symbols' can be on the right, at the bottom or off"

#property indicator_chart_window

#property indicator_buffers   0

#property indicator_plots     0

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

//| Enum Profit All                                                  |

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

enum ENUM_PROFIT_ALL

  {

   right=0,    // right

   bottom=1,   // bottom

   off=2,      // off

  };



//--- input parameters

input string             i_sString01 = "Common parameters";                         // ======================

input string               InpFont              = "Consolas";                       // Font

input string             i_sString02 = "Profit of the current symbol";              // ======================

input string               InpCurrentName       = "Profit of the current symbol";   // Label name (Profit of the current symbol)

input int                  InpCurrentFontSize   = 30;                               // Font size (Profit of the current symbol)

input string             i_sString03 = "Profit for all symbols";                    // ======================

input ENUM_PROFIT_ALL      InpProfitAll         = bottom;                           // Profit All ...

input string               InpAllName           = "Profit for all symbols";         // Label name (Profit for all symbols)

input int                  InpAllFontSize       = 20;                               // Font size (Profit for all symbols)



int                     g_nLastXCoord = 0,

                        g_nLastYCoord = 0;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   long x_distance;

   long y_distance;

   //--- set window size

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

     {

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

      return INIT_FAILED;

     }

   if(!ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0,y_distance))

     {

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

      return INIT_FAILED;

     };

  

  

//--- create a timer with a 9 second period

   EventSetTimer(9);

//---

   ENUM_ANCHOR_POINT  anchor_current=ANCHOR_CENTER, anchor_all=ANCHOR_CENTER;

   switch(InpProfitAll)

     {

      case  right:

         anchor_current=ANCHOR_RIGHT;

         anchor_all=ANCHOR_LEFT;

         break;

      case  bottom:

         anchor_current=ANCHOR_LOWER;

         anchor_all=ANCHOR_UPPER;

         break;

     }

   g_nLastXCoord = int(x_distance / 2);

   g_nLastYCoord = int(y_distance / 2);

   LabelCreate(ChartID(),InpCurrentName,0, g_nLastXCoord, g_nLastYCoord, CORNER_LEFT_UPPER,"---",InpFont,InpCurrentFontSize,clrGray,0.0,anchor_current, false, true, false, true);

   if(InpProfitAll!=off)

      LabelCreate(ChartID(),InpAllName,0, g_nLastXCoord, g_nLastYCoord, CORNER_LEFT_UPPER,"---",InpFont,InpAllFontSize,clrGray,0.0,anchor_all, false, false, false, true);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//--- destroy the timer after completing the work

   EventKillTimer();

//--- reset the error value

   ResetLastError();

//--- delete the label

   ObjectDelete(ChartID(),InpCurrentName);

   if(InpProfitAll!=off)

      ObjectDelete(ChartID(),InpAllName);

  }

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//---

   double profit_current=0.0,profit_all=0.0;

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

      if(OrderSelect(i, SELECT_BY_POS)) // selects the position by index for further access to its properties

        {

         double profit = OrderSwap() + OrderProfit();

         profit_all+=profit;

         if(OrderSymbol() == Symbol())

            profit_current+=profit;

        }

   LabelTextChange(ChartID(),InpCurrentName,profit_current);

   if(InpProfitAll!=off)

      LabelTextChange(ChartID(),InpAllName,profit_all);

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

   return(rates_total);

  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

//---

   ENUM_ANCHOR_POINT  anchor_current=ANCHOR_CENTER, anchor_all=ANCHOR_CENTER;

   switch(InpProfitAll)

     {

      case  right:

         anchor_current=ANCHOR_RIGHT;

         anchor_all=ANCHOR_LEFT;

         break;

      case  bottom:

         anchor_current=ANCHOR_LOWER;

         anchor_all=ANCHOR_UPPER;

         break;

     }

   if(ObjectFind(ChartID(),InpCurrentName)<0)

      LabelCreate(ChartID(),InpCurrentName,0, g_nLastXCoord, g_nLastYCoord, CORNER_LEFT_UPPER,"---",InpFont,InpCurrentFontSize,clrGray,0.0,anchor_current);

   if(InpProfitAll!=off)

      if(ObjectFind(ChartID(),InpAllName)<0)

         LabelCreate(ChartID(),InpAllName,0, g_nLastXCoord, g_nLastYCoord, CORNER_LEFT_UPPER,"---",InpFont,InpAllFontSize,clrGray,0.0,anchor_all);

  }

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

//| ChartEvent function                                              |

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

{

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

   {

      g_nLastXCoord = int(ObjectGetInteger(0, InpCurrentName, OBJPROP_XDISTANCE));

      g_nLastYCoord = int(ObjectGetInteger(0, InpCurrentName, OBJPROP_YDISTANCE));

      if (InpProfitAll != off)

      {

         LabelMove(0, InpAllName, g_nLastXCoord, g_nLastYCoord);

         ChartRedraw();

      }

   }

   

   if (id == CHARTEVENT_OBJECT_DELETE && (sparam == InpCurrentName || sparam == InpAllName))

      OnTimer();

}

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

//| 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="Arial",              // font

                 const int                font_size=10,              // font size

                 const color              clr=clrRed,                // 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               selectable=false,          // allow to select

                 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,selectable);

   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 the label text                                            |

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

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

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

                     const double profit=0.0)      // profit

  {

//--- reset the error value

   ResetLastError();

//--- change object text

   color clr=clrGray;

   if(profit<0.0)

      clr=clrRed;

   if(profit>0.0)

      clr=clrBlue;

//--- change the text

   string text=DoubleToString(profit,2);

   if(name==InpAllName)

      text=" (all "+text+")";

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

//--- set color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- successful execution

   return(true);

  }

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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---