Double opening panel

Author: Copyright © 2018, Vladimir Karputov
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Double opening panel
ÿþ//+------------------------------------------------------------------+

//|                                         Double opening panel.mq5 |

//|                              Copyright © 2018, Vladimir Karputov |

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

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

#property copyright "Copyright © 2018, Vladimir Karputov"

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

#property version   "1.100"

//---

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh> 

CPositionInfo  m_position;                   // trade position object

CTrade         m_trade_0;                    // trading object

CTrade         m_trade_1;                    // trading object

CSymbolInfo    m_symbol_0;                   // symbol info object

CSymbolInfo    m_symbol_1;                   // symbol info object

#include <Controls\Dialog.mqh>

#include <Controls\Button.mqh>

#include <Controls\Label.mqh>

#include <Controls\SpinEditKVN.mqh>

//--- input parameters

input string   InpSymblos_0      = "USDJPY"; // Symbol 0

input string   InpSymblos_1      = "EURUSD"; // Symbol 1

input double   InpStartLots_0    = 1;        // Start Lots for Symbol 0

input double   InpStartLots_1    = 1;        // Start Lots for Symbol 1

input ulong    m_magic           = 243935536;// magic number

//---

ulong          m_slippage=10;                // slippage

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

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

#define CONTROLS_GAP_Y                      (5)       // gap by Y coordinate

//--- for buttons

#define BUTTON_WIDTH                        (60)      // 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:

   CLabel            m_label_0;                       // the label object

   CLabel            m_label_1;                       // the label object

   CSpinEditKVN      m_spin_edit_0;                   // the spin edit object

   CSpinEditKVN      m_spin_edit_1;                   // the spin edit object

   CButton           m_button_buy_0;                  // the button object

   CButton           m_button_sell_0;                 // the button object

   CButton           m_button_buy_1;                  // the button object

   CButton           m_button_sell_1;                 // the button object

   CButton           m_button_open;                   // the button object

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

   bool              CreateLabel_1(void);

   bool              CreateSpinEdit_0(void);

   bool              CreateSpinEdit_1(void);

   bool              CreateButtonBuy_0(void);

   bool              CreateButtonSell_0(void);

   bool              CreateButtonBuy_1(void);

   bool              CreateButtonSell_1(void);

   bool              CreateButtonOpen(void);

   bool              CreateButtonCloseAll(void);

   //--- handlers of the dependent controls events

   void              OnClickButtonOpen(void);

   void              OnClickButtonCloseAll(void);

   //--- close positions

   void              CloseAllPositions(void);

  };

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

//| Event Handling                                                   |

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

EVENT_MAP_BEGIN(CControlsDialog)

ON_EVENT(ON_CLICK,m_button_close_all,OnClickButtonCloseAll)

ON_EVENT(ON_CLICK,m_button_open,OnClickButtonOpen)

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

      return(false);

   if(!CreateLabel_1())

      return(false);

   if(!CreateSpinEdit_0())

      return(false);

   if(!CreateSpinEdit_1())

      return(false);

   if(!CreateButtonBuy_0())

      return(false);

   if(!CreateButtonSell_0())

      return(false);

   if(!CreateButtonBuy_1())

      return(false);

   if(!CreateButtonSell_1())

      return(false);

   if(!CreateButtonOpen())

      return(false);





   if(!CreateButtonCloseAll())

      return(false);

//--- succeed

   return(true);

  }

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

//| Create the "Symbol 0" label                                      |

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

bool CControlsDialog::CreateLabel_0(void)

  {

//--- coordinates

   int x1=INDENT_LEFT;

   int y1=INDENT_TOP;

   int x2=BUTTON_WIDTH;

   int y2=y1+BUTTON_HEIGHT;

//--- create

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

      return(false);

   if(!m_label_0.Text(m_symbol_0.Name()))

      return(false);

   if(!Add(m_label_0))

      return(false);

//--- succeed

   return(true);

  }

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

//| Create the "Symbol 1" label                                      |

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

bool CControlsDialog::CreateLabel_1(void)

  {

//--- coordinates

   int x1=INDENT_LEFT;

   int y1=INDENT_TOP+CONTROLS_GAP_Y+BUTTON_HEIGHT;

   int x2=BUTTON_WIDTH;

   int y2=y1+BUTTON_HEIGHT;

//--- create

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

      return(false);

   if(!m_label_1.Text(m_symbol_1.Name()))

      return(false);

   if(!Add(m_label_1))

      return(false);

//--- succeed

   return(true);

  }

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

//| Create the "SpinEdit 0" element                                  |

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

bool CControlsDialog::CreateSpinEdit_0(void)

  {

//--- coordinates

   int x1=INDENT_LEFT+BUTTON_WIDTH+CONTROLS_GAP_X;

   int y1=INDENT_TOP;

   int x2=x1+BUTTON_WIDTH;

   int y2=y1+BUTTON_HEIGHT;

//--- create

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

      return(false);

   if(!Add(m_spin_edit_0))

      return(false);

   m_spin_edit_0.MinValue(m_symbol_0.LotsMin());

   m_spin_edit_0.MaxValue(m_symbol_0.LotsMax());

   m_spin_edit_0.Value(InpStartLots_0);

   m_spin_edit_0.StepValue(m_symbol_0.LotsStep());

//--- succeed

   return(true);

  }

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

//| Create the "SpinEdit 1" element                                  |

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

bool CControlsDialog::CreateSpinEdit_1(void)

  {

//--- coordinates

   int x1=INDENT_LEFT+BUTTON_WIDTH+CONTROLS_GAP_X;

   int y1=INDENT_TOP+CONTROLS_GAP_Y+BUTTON_HEIGHT;;

   int x2=x1+BUTTON_WIDTH;

   int y2=y1+BUTTON_HEIGHT;

//--- create

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

      return(false);

   if(!Add(m_spin_edit_1))

      return(false);

   m_spin_edit_1.MinValue(m_symbol_1.LotsMin());

   m_spin_edit_1.MaxValue(m_symbol_1.LotsMax());

   m_spin_edit_1.Value(InpStartLots_1);

   m_spin_edit_1.StepValue(m_symbol_1.LotsStep());

//--- succeed

   return(true);

  }

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

//| Create the "Buy Symbol 0" button                                 |

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

bool CControlsDialog::CreateButtonBuy_0(void)

  {

//--- coordinates

   int x1=INDENT_LEFT+2*(BUTTON_WIDTH+CONTROLS_GAP_X);

   int y1=INDENT_TOP;

   int x2=x1+BUTTON_WIDTH;

   int y2=y1+BUTTON_HEIGHT;

//--- create

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

      return(false);

   if(!m_button_buy_0.Text("Buy"))

      return(false);

   if(!Add(m_button_buy_0))

      return(false);

   m_button_buy_0.Locking(true);

//--- succeed

   return(true);

  }

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

//| Create the "Sell Symbol 0" button                                |

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

bool CControlsDialog::CreateButtonSell_0(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_sell_0.Create(m_chart_id,m_name+"ButtonSell_0",m_subwin,x1,y1,x2,y2))

      return(false);

   if(!m_button_sell_0.Text("Sell"))

      return(false);

   if(!Add(m_button_sell_0))

      return(false);

   m_button_sell_0.Locking(true);

//--- succeed

   return(true);

  }

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

//| Create the "Buy Symbol 1" button                                 |

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

bool CControlsDialog::CreateButtonBuy_1(void)

  {

//--- coordinates

   int x1=INDENT_LEFT+2*(BUTTON_WIDTH+CONTROLS_GAP_X);

   int y1=INDENT_TOP+BUTTON_HEIGHT+CONTROLS_GAP_Y;

   int x2=x1+BUTTON_WIDTH;

   int y2=y1+BUTTON_HEIGHT;

//--- create

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

      return(false);

   if(!m_button_buy_1.Text("Buy"))

      return(false);

   if(!Add(m_button_buy_1))

      return(false);

   m_button_buy_1.Locking(true);

//--- succeed

   return(true);

  }

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

//| Create the "Sell Symbol 1" button                                |

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

bool CControlsDialog::CreateButtonSell_1(void)

  {

//--- coordinates

   int x1=INDENT_LEFT+3*(BUTTON_WIDTH+CONTROLS_GAP_X);

   int y1=INDENT_TOP+BUTTON_HEIGHT+CONTROLS_GAP_Y;

   int x2=x1+BUTTON_WIDTH;

   int y2=y1+BUTTON_HEIGHT;

//--- create

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

      return(false);

   if(!m_button_sell_1.Text("Sell"))

      return(false);

   if(!Add(m_button_sell_1))

      return(false);

   m_button_sell_1.Locking(true);

//--- succeed

   return(true);

  }

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

//| Create the "Open" button                                         |

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

bool CControlsDialog::CreateButtonOpen(void)

  {

//--- coordinates

   int x1=INDENT_LEFT+4*(BUTTON_WIDTH+CONTROLS_GAP_X);

   int y1=INDENT_TOP;

   int x2=x1+BUTTON_WIDTH;

   int y2=y1+BUTTON_HEIGHT+CONTROLS_GAP_Y+BUTTON_HEIGHT;

//--- create

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

      return(false);

   if(!m_button_open.Text("Open!"))

      return(false);

   if(!Add(m_button_open))

      return(false);

//--- succeed

   return(true);

  }

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

//| Create the "Close all" button                                    |

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

bool CControlsDialog::CreateButtonCloseAll(void)

  {

//--- coordinates

   int x1=INDENT_LEFT;

   int y1=INDENT_TOP+2*(BUTTON_HEIGHT+CONTROLS_GAP_Y);

   int x2=x1+5*BUTTON_WIDTH+4*CONTROLS_GAP_X;

   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::OnClickButtonOpen(void)

  {

   if(m_button_buy_0.Pressed())

      m_trade_0.Buy(m_spin_edit_0.Value(),m_symbol_0.Name());

   if(m_button_sell_0.Pressed())

      m_trade_0.Sell(m_spin_edit_0.Value(),m_symbol_0.Name());



   if(m_button_buy_1.Pressed())

      m_trade_1.Buy(m_spin_edit_1.Value(),m_symbol_1.Name());

   if(m_button_sell_1.Pressed())

      m_trade_1.Sell(m_spin_edit_1.Value(),m_symbol_1.Name());

  }

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

//| Event handler                                                    |

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

void CControlsDialog::OnClickButtonCloseAll(void)

  {

   CloseAllPositions();

  }

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

//| Close all positions                                              |

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

void CControlsDialog::CloseAllPositions(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

        {

         if(m_position.Symbol()==m_symbol_0.Name() && m_position.Magic()==m_magic)

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

         if(m_position.Symbol()==m_symbol_1.Name() && m_position.Magic()==m_magic)

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

        }

  }

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

//| Global Variables                                                 |

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

CControlsDialog ExtDialog;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   if(!m_symbol_0.Name(InpSymblos_0)) // sets symbol name

      return(INIT_FAILED);

   m_trade_0.SetExpertMagicNumber(m_magic);

   m_trade_0.SetMarginMode();

   m_trade_0.SetTypeFillingBySymbol(m_symbol_0.Name());

   m_trade_0.SetDeviationInPoints(m_slippage);



   if(!m_symbol_1.Name(InpSymblos_1)) // sets symbol name

      return(INIT_FAILED);

   m_trade_1.SetExpertMagicNumber(m_magic);

   m_trade_1.SetMarginMode();

   m_trade_1.SetTypeFillingBySymbol(m_symbol_1.Name());

   m_trade_1.SetDeviationInPoints(m_slippage);

//--- check the input parameter "Lots"

   string err_text="";

   if(!CheckVolumeValue(m_symbol_0,InpStartLots_0,err_text))

     {

      //--- when testing, we will only output to the log about incorrect input parameters

      if(MQLInfoInteger(MQL_TESTER))

        {

         Print(__FUNCTION__,", ERROR: ",err_text);

         return(INIT_FAILED);

        }

      else // if the Expert Advisor is run on the chart, tell the user about the error

        {

         Alert(__FUNCTION__,", ERROR: ",err_text);

         return(INIT_PARAMETERS_INCORRECT);

        }

     }



   err_text="";

   if(!CheckVolumeValue(m_symbol_1,InpStartLots_1,err_text))

     {

      //--- when testing, we will only output to the log about incorrect input parameters

      if(MQLInfoInteger(MQL_TESTER))

        {

         Print(__FUNCTION__,", ERROR: ",err_text);

         return(INIT_FAILED);

        }

      else // if the Expert Advisor is run on the chart, tell the user about the error

        {

         Alert(__FUNCTION__,", ERROR: ",err_text);

         return(INIT_PARAMETERS_INCORRECT);

        }

     }

//--- create application dialog

   if(!ExtDialog.Create(0,"Double opening panel",0,40,90,390,210))

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

  }

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

//| Check the correctness of the position volume                     |

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

bool CheckVolumeValue(CSymbolInfo &m_symbol,double volume,string &error_description)

  {

//--- minimal allowed volume for trade operations

   double min_volume=m_symbol.LotsMin();

   if(volume<min_volume)

     {

      if(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")

         error_description=StringFormat("1J5< <5=LH5 <8=8<0;L=> 4>?CAB8<>3> SYMBOL_VOLUME_MIN=%.2f",min_volume);

      else

         error_description=StringFormat("Volume is less than the minimal allowed SYMBOL_VOLUME_MIN=%.2f",min_volume);

      return(false);

     }

//--- maximal allowed volume of trade operations

   double max_volume=m_symbol.LotsMax();

   if(volume>max_volume)

     {

      if(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")

         error_description=StringFormat("1J5< 1>;LH5 <0:A8<0;L=> 4>?CAB8<>3> SYMBOL_VOLUME_MAX=%.2f",max_volume);

      else

         error_description=StringFormat("Volume is greater than the maximal allowed SYMBOL_VOLUME_MAX=%.2f",max_volume);

      return(false);

     }

//--- get minimal step of volume changing

   double volume_step=m_symbol.LotsStep();

   int ratio=(int)MathRound(volume/volume_step);

   if(MathAbs(ratio*volume_step-volume)>0.0000001)

     {

      if(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")

         error_description=StringFormat("1J5< =5 :@0B5= <8=8<0;L=><C H03C SYMBOL_VOLUME_STEP=%.2f, 1;8609H89 ?@028;L=K9 >1J5< %.2f",

                                        volume_step,ratio*volume_step);

      else

         error_description=StringFormat("Volume is not a multiple of the minimal step SYMBOL_VOLUME_STEP=%.2f, the closest correct volume is %.2f",

                                        volume_step,ratio*volume_step);

      return(false);

     }

   error_description="Correct volume value";

   return(true);

  }

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

Comments