Close All Panel Info

Author: Copyright © 2021, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Close All Panel Info
ÿþ//+------------------------------------------------------------------+

//|                                         Close All Panel Info.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

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

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

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "1.000"

//---

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

//---

CPositionInfo  m_position;                   // object of CPositionInfo class

CTrade         m_trade;                      // object of CTrade class

//---

#include <Controls\Dialog.mqh>

#include <Controls\Button.mqh>

#include <Controls\Label.mqh>

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

//| defines                                                          |

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

//--- indents and gaps

#define INDENT_LEFT                         (11)      // indent from left (with allowance for border width)

#define INDENT_TOP                          (11)      // indent from top (with allowance for border width)

#define CONTROLS_GAP_X                      (5)       // gap by X coordinate

//--- for buttons

#define BUTTON_WIDTH                        (70)      // size by X coordinate

#define BUTTON_HEIGHT                       (20)      // size by Y coordinate

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

//| Class CControlsDialog                                            |

//| Usage: main dialog of the Controls application                   |

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

class CControlsDialog : public CAppDialog

  {

private:

   CButton           m_button_close_all;              // the button object

   CLabel            m_label_total_profit;            // CLabel object



public:

                     CControlsDialog(void);

                    ~CControlsDialog(void);

   //--- create

   virtual bool      Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2);

   //--- chart event handler

   virtual bool      OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam);

   //--- refresh data

   void              RefreshData(void);



protected:

   //--- create dependent controls

   bool              CreateLabelTotalProfit(void);

   bool              CreateButtonCloseAll(void);

   //--- handlers of the dependent controls events

   void              OnClickButtonCloseAll(void);

   //--- close positions

   void              ClosePositions(void);

  };

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

//| Event Handling                                                   |

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

EVENT_MAP_BEGIN(CControlsDialog)

ON_EVENT(ON_CLICK,m_button_close_all,OnClickButtonCloseAll)

EVENT_MAP_END(CAppDialog)

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

//| Constructor                                                      |

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

CControlsDialog::CControlsDialog(void)

  {

  }

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

//| Destructor                                                       |

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

CControlsDialog::~CControlsDialog(void)

  {

  }

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

//| Create                                                           |

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

bool CControlsDialog::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)

  {

   if(!CAppDialog::Create(chart,name,subwin,x1,y1,x2,y2))

      return(false);

//--- create dependent controls

   if(!CreateLabelTotalProfit())

      return(false);

   if(!CreateButtonCloseAll())

      return(false);

//--- succeed

   return(true);

  }

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

//| Refresh Data                                                     |

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

void CControlsDialog::RefreshData(void)

  {

   double profit=0.0;

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

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

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

   m_label_total_profit.Color(clrGray);

   m_label_total_profit.Text("Total Profit: "+DoubleToString(profit,2)+" "+AccountInfoString(ACCOUNT_CURRENCY));

   if(profit<0.0)

      m_label_total_profit.Color(clrRed);

   else

      if(profit>0.0)

         m_label_total_profit.Color(clrBlue);

  }

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

//| Create the "Total Profit" label                                  |

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

bool CControlsDialog::CreateLabelTotalProfit(void)

  {

//--- coordinates

   int x1=INDENT_LEFT;

   int y1=INDENT_TOP;

   int x2=x1+2*BUTTON_WIDTH;

   int y2=y1+BUTTON_HEIGHT;

//--- create

   if(!m_label_total_profit.Create(m_chart_id,m_name+"LabelTotalProfit",m_subwin,x1,y1,x2,y2))

      return(false);

   if(!m_label_total_profit.Text("Total Profit: "))

      return(false);

   if(!Add(m_label_total_profit))

      return(false);

//--- succeed

   return(true);

  }

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

//| Create the "Close all" button                                    |

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

bool CControlsDialog::CreateButtonCloseAll(void)

  {

//--- coordinates

   int x1=INDENT_LEFT+3*BUTTON_WIDTH+CONTROLS_GAP_X;

   int y1=INDENT_TOP;

   int x2=x1+BUTTON_WIDTH;

   int y2=y1+BUTTON_HEIGHT;

//--- create

   if(!m_button_close_all.Create(m_chart_id,m_name+"ButtonCloseAll",m_subwin,x1,y1,x2,y2))

      return(false);

   if(!m_button_close_all.Text("Close all"))

      return(false);

   if(!Add(m_button_close_all))

      return(false);

//--- succeed

   return(true);

  }

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

//| Event handler                                                    |

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

void CControlsDialog::OnClickButtonCloseAll(void)

  {

   ClosePositions();

  }

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

//| Close positions                                                  |

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

void CControlsDialog::ClosePositions(void)

  {

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions

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

         m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol

  }

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

//| Global Variables                                                 |

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

CControlsDialog ExtDialog;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   m_trade.SetMarginMode();

   m_trade.SetTypeFillingBySymbol(Symbol());

   m_trade.SetDeviationInPoints(10);

//--- create application dialog

   if(!ExtDialog.Create(0,"Close All Panel Info",0,40,40,352,111))

      return(INIT_FAILED);

//--- run application

   ExtDialog.Run();

//--- succeed

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//--- destroy dialog

   ExtDialog.Destroy(reason);

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   ExtDialog.RefreshData();

  }

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

//| Expert chart event function                                      |

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

void OnChartEvent(const int id,         // event ID

                  const long& lparam,   // event parameter of the long type

                  const double& dparam, // event parameter of the double type

                  const string& sparam) // event parameter of the string type

  {

   ExtDialog.ChartEvent(id,lparam,dparam,sparam);

  }

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

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