MT5入门到精通之十八(图表或面板上添加按钮及事件)

一.简单按钮开仓实现
1.效果图

MT5入门到精通之十八(图表或面板上添加按钮及事件)_第1张图片
image.png

2.代码实现

//+------------------------------------------------------------------+
//|                                             testChartEventEA.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include 
#include 
#include 

input int magic=170507;

Trade td;
Kline kl;
Draw dr;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   dr.label("lots_label","请输入下单量:",20,25,0,White);
   dr.edit("lots_edit","0.1",120,20,80,20,0,Yellow);

   dr.label("slPoint_label","请输入止损点:",20,50,0,White);
   dr.edit("slPoint_edit","200",120,45,80,20,0,Yellow);

   dr.label("tpPoint_label","请输入止盈点:",20,75,0,White);
   dr.edit("tpPoint_edit","200",120,70,80,20,0,Yellow);

   dr.button("openBuy_button","开多单",20,100,80,20);
   dr.button("openSell_button","开空单",120,100,80,20);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ObjectsDeleteAll(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   if(id==CHARTEVENT_OBJECT_CLICK)
     {
      double lots=StringToDouble(ObjectGetString(0,"lots_edit",OBJPROP_TEXT));
      int slPoint=StringToInteger(ObjectGetString(0,"slPoint_edit",OBJPROP_TEXT));
      int tpPoint=StringToInteger(ObjectGetString(0,"tpPoint_edit",OBJPROP_TEXT));
      if(sparam=="openBuy_button")
        {
         //Alert("按下了开多单按钮");
         td.buy(Symbol(),td.formatLots(Symbol(),lots),slPoint,tpPoint,Symbol()+"buy",magic);
         ObjectSetInteger(0,sparam,OBJPROP_STATE,false);//设置按钮回复原状
        }
      if(sparam=="openSell_button")
        {
         //Alert("按下了开空单按钮");
         td.sell(Symbol(),td.formatLots(Symbol(),lots),slPoint,tpPoint,Symbol()+"sell",magic);
         ObjectSetInteger(0,sparam,OBJPROP_STATE,false);//设置按钮回复原状
        }
     }
  }
//+------------------------------------------------------------------+

二.用系统库面板控件实现
1.效果

MT5入门到精通之十八(图表或面板上添加按钮及事件)_第2张图片
image.png

2.实现原理 完全类推contorls测试代码

MT5入门到精通之十八(图表或面板上添加按钮及事件)_第3张图片
image.png

3.代码实现
3.1EA代码

//+------------------------------------------------------------------+
//|                                            testControllersEA.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include "ControlsDialog.mqh"
//+------------------------------------------------------------------+
//| Global Variables                                                 |
//+------------------------------------------------------------------+
CControlsDialog ExtDialog;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create application dialog
   if(!ExtDialog.Create(0,"Controls",0,20,20,300,250))
      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 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);
  }
//+------------------------------------------------------------------+

3.2类推控件库代码

//+------------------------------------------------------------------+
//|                                               ControlsDialog.mqh |
//|                   Copyright 2009-2017, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 

input int magic=170506;

Trade td;
Kline kl;
Draw dr;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CControlsDialog : public CAppDialog
  {
private:
   CLabel            m_label1;
   CLabel            m_label2;
   CLabel            m_label3;
   CEdit             m_edit;                          // the display field object
   CButton           m_button1;                       // the button object
   CButton           m_button2;                       // the button object
   CSpinEdit         m_spin_edit1;                     // the up-down object
   CSpinEdit         m_spin_edit2;

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              CreateLabel1(void);
   bool              CreateEdit(void);
   bool              CreateLabel2(void);
   bool              CreateSpinEdit1(void);
   bool              CreateLabel3(void);
   bool              CreateSpinEdit2(void);
   bool              CreateButton1(void);
   bool              CreateButton2(void);

   //--- create dependent controls
   void              OnClickButton1(void);
   void              OnClickButton2(void);
   void              OnChangeSpin_edit1(void);

  };
//+------------------------------------------------------------------+
//| Event Handling                                                   |
//+------------------------------------------------------------------+
EVENT_MAP_BEGIN(CControlsDialog)
ON_EVENT(ON_CLICK,m_button1,OnClickButton1)
ON_EVENT(ON_CLICK,m_button2,OnClickButton2)
ON_EVENT(ON_CHANGE,m_spin_edit1,OnChangeSpin_edit1)
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(!CreateLabel1())
      return(false);
   if(!CreateEdit())
      return(false);
   if(!CreateLabel2())
      return(false);
   if(!CreateSpinEdit1())
      return(false);
   if(!CreateLabel3())
      return(false);
   if(!CreateSpinEdit2())
      return(false);
   if(!CreateButton1())
      return(false);
   if(!CreateButton2())
      return(false);

   return(true);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CControlsDialog::CreateLabel1(void)
  {
   bool result=true;
   if(!m_label1.Create(0,"lable1",0,20,20,20+100,20+20))result=false;
   m_label1.Text("输入下单量:");
   if(!Add(m_label1))result=false;
   return result;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CControlsDialog::CreateEdit(void)
  {
   bool result=true;
   if(!m_edit.Create(0,"lots_edit",0,20+100,20,20+100+100,20+20))result=false;
   m_edit.Text("0.1");
   m_edit.ReadOnly(false);
   if(!Add(m_edit))result=false;
   return result;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CControlsDialog::CreateLabel2(void)
  {
   bool result=true;
   if(!m_label2.Create(0,"label2",0,20,20+20,20+100,20+20+20))result=false;
   m_label2.Text("输入止损点:");
   if(!Add(m_label2))result=false;
   return result;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CControlsDialog::CreateSpinEdit1(void)
  {
   bool result=true;
   if(!m_spin_edit1.Create(0,"slPoint_spinEdit",0,20+100,20+20,20+100+100,20+20+20))result=false;
   m_spin_edit1.MaxValue(10000);
   m_spin_edit1.MinValue(100);
   m_spin_edit1.Value(200);
   if(!Add(m_spin_edit1))result=false;
   return result;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CControlsDialog::CreateLabel3(void)
  {
   bool result=true;
   if(!m_label3.Create(0,"label3",0,20,20+20+20,20+100,20+20+20+20))result=false;
   m_label3.Text("输入止盈点:");
   if(!Add(m_label3))result=false;
   return result;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CControlsDialog::CreateSpinEdit2(void)
  {
   bool result=true;
   if(!m_spin_edit2.Create(0,"tpPoint_spinEdit",0,20+100,20+20+20,20+100+100,20+20+20+20))result=false;
   m_spin_edit2.MaxValue(10000);
   m_spin_edit2.MinValue(100);
   m_spin_edit2.Value(200);
   if(!Add(m_spin_edit2))result=false;
   return result;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CControlsDialog::CreateButton1(void)
  {
   bool result=true;
   if(!m_button1.Create(0,"openBuy_button",0,20,20+20+20+20+20,20+50,20+20+20+20+20+20))result=false;
   m_button1.Text("开多单");
   m_button1.Color(Red);
   if(!Add(m_button1))result=false;
   return result;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool CControlsDialog::CreateButton2(void)
  {
   bool result=true;
   if(!m_button2.Create(0,"openSell_button",0,20+50+50,20+20+20+20+20,20+50+50+50,20+20+20+20+20+20))result=false;
   m_button2.Text("开空单");
   m_button2.Color(Yellow);
   m_button2.ColorBackground(clrBlack);
   if(!Add(m_button2))result=false;
   return result;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CControlsDialog::OnClickButton1(void)
  {
   double lots=StringToDouble(m_edit.Text());
   int sl=StringToInteger(m_spin_edit1.Value());
   int tp=StringToInteger(m_spin_edit2.Value());
   td.buy(Symbol(),td.formatLots(Symbol(),lots),sl,tp,Symbol()+"buy",magic);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CControlsDialog::OnClickButton2(void)
  {
   double lots=StringToDouble(m_edit.Text());
   int sl=StringToInteger(m_spin_edit1.Value());
   int tp=StringToInteger(m_spin_edit2.Value());
   td.sell(Symbol(),td.formatLots(Symbol(),lots),sl,tp,Symbol()+"sell",magic);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CControlsDialog::OnChangeSpin_edit1(void)
  {
   string sp=m_spin_edit1.Value();
   m_spin_edit1.Value(sp);
  }
//+------------------------------------------------------------------+

如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。

你可能感兴趣的:(MT5入门到精通之十八(图表或面板上添加按钮及事件))