Close All Panel

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

//|                                              Close All Panel.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"

//---

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

CPositionInfo  m_position;                   // trade position object

CTrade         m_trade;                      // trading object

#include <Controls\Dialog.mqh>

#include <Controls\Button.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)

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



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



protected:

   //--- create dependent controls

   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(!CreateButtonCloseAll())

      return(false);

//--- succeed

   return(true);

  }

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

//| Create the "Close all" button                                    |

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

bool CControlsDialog::CreateButtonCloseAll(void)

  {

//--- coordinates

   int x1=INDENT_LEFT;

   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();

//--- create application dialog

   if(!ExtDialog.Create(0,"Close All",0,40,40,140,111))

      return(INIT_FAILED);

//--- run application

   ExtDialog.Run();

//--- succeed

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   Comment("");

//--- destroy dialog

   ExtDialog.Destroy(reason);

  }

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

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