C
MQL4
MT4
Dialog
1
どのような問題がありますか?

投稿日

更新日

【MQL4 : MT4】GUI で 売買機能を実装した EA を作る。② 【Dialog】

はじめに

ここでは、GUIで売買機能(ワンクリックトレード)を実装します。
作成手順としては、以下の通りです。

  1. GUIの基盤
  2. ロットを表示するEdit部品
  3. ロットを増減させるButton部品
  4. 買い、売りを入れるButton部品
  5. それぞれの部品にイベントを付与
    前回は、2.ロットを表示するEdit部品を作りました。詳しくは以下の記事で。
    【MQL4 : MT4】GUI で 売買機能を実装した EA を作る。①
    今回は、3 と 4 を作ります。
    いろいろと初心者な為、至らない部分もありますが、宜しくお願いします。
    :footprints:※前回の続きです。
    +---

ロットを増減させるButton部品

前回のEdit表示とButtonの表示はほとんど同じなので、説明は省きます。

#include <Controls\Dialog.mqh>
#include <Controls\Edit.mqh>
#include <Controls\Button.mqh>
class CPanelDialog : public CAppDialog
{
public:
CPanelDialog();
~CPanelDialog();
/* 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 */ //--- 1
virtual bool OnEvent( const int id, const long &lparam, const double &dparam, const string &sparam );
CEdit m_editLots;
CButton m_btnBuy;
CButton m_btnSell;
CButton m_btnUp;
CButton m_btnDown;
protected:
bool CreateEditLots();
bool CreateBtnBuy();
bool CreateBtnSell();
bool CreateBtnUp();
bool CreateBtnDown();
void OnClickBtnUp(); // --- 2
void OnClickBtnDown();
};
EVENT_MAP_BEGIN( CPanelDialog ) // --- 3
ON_EVENT( ON_CLICK, m_btnUp, OnClickBtnUp )
ON_EVENT( ON_CLICK, m_btnDown, OnClickBtnDown )
EVENT_MAP_END( CAppDialog )
CPanelDialog::CPanelDialog(){}
CPanelDialog::~CPanelDialog(){}
bool CPanelDialog::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;
if( !CreateEditLots() ) return false;
if( !CreateBtnUp() ) return false;
if( !CreateBtnDown() ) return false;
if( !CreateBtnBuy() ) return false;
if( !CreateBtnSell() ) return false;
return true;
}
bool CPanelDialog::CreateEditLots()
{
int widths = ClientAreaWidth() / 5;
int x1 = widths;
int y1 = 0;
int x2 = widths * 4;
int y2 = 20;
if( !m_editLots.Create( m_chart_id, m_name + "Edit", m_subwin, x1, y1, x2, y2 ) ) return false;
if( !m_editLots.ReadOnly( false ) ) return false;
if( !Add( m_editLots ) ) return false;
m_editLots.TextAlign( ALIGN_CENTER );
m_editLots.Text( "0.10" );
return true;
}
bool CPanelDialog::CreateBtnUp()
{
int widths = ClientAreaWidth() / 5;
int x1 = widths * 4;
int y1 = 0;
int x2 = ClientAreaWidth();
int y2 = 20;
if( !m_btnUp.Create( m_chart_id, m_name + "btnUp", m_subwin, x1, y1, x2, y2 ) ) return false;
if( !m_btnUp.Text( "UP" ) ) return false;
if( !Add( m_btnUp ) ) return false;
return true;
}
bool CPanelDialog::CreateBtnDown()
{
int x1 = 0;
int y1 = 0;
int x2 = ClientAreaWidth() / 5;
int y2 = 20;
if( !m_btnDown.Create( m_chart_id, m_name + "btnDown", m_subwin, x1, y1, x2, y2 ) ) return false;
if( !m_btnDown.Text( "DOWN" ) ) return false;
if( !Add( m_btnDown ) ) return false;
return true;
}
bool CPanelDialog::CreateBtnBuy()
{
int x1 = ClientAreaWidth() / 2;
int y1 = 20;
int x2 = ClientAreaWidth();
int y2 = ClientAreaHeight();
if( !m_btnBuy.Create( m_chart_id, m_name + "btnBuy", m_subwin, x1, y1, x2, y2 ) ) return false;
if( !m_btnBuy.Text( "Buy" ) ) return false;
if( !Add( m_btnBuy ) ) return false;
return true;
}
bool CPanelDialog::CreateBtnSell()
{
int x1 = 0;
int y1 = 20;
int x2 = ClientAreaWidth() / 2;
int y2 = ClientAreaHeight();
if( !m_btnSell.Create( m_chart_id, m_name + "btnSell", m_subwin, x1, y1, x2, y2 ) ) return false;
if( !m_btnSell.Text( "Sell" ) ) return false;
if( !Add( m_btnSell ) ) return false;
return true;
}
void CPanelDialog::OnClickBtnUp() // --- 4
{
string str_editText = m_editLots.Text();
double d_lots = StringToDouble( str_editText );
d_lots += 0.01;
m_editLots.Text( DoubleToStr( d_lots, 2 ) );
}
void CPanelDialog::OnClickBtnDown()
{
string str_editText = m_editLots.Text();
double d_lots = StringToDouble( str_editText );
if( d_lots > 0.01 ) // --- 5
{
d_lots -= 0.01;
m_editLots.Text( DoubleToStr( d_lots, 2 ) );
}
}

ソースコード詳細

#####1. CAppDialogのOnEventメソッド宣言

virtual bool OnEvent( const int id, const long &lparam, const double &dparam, const string &sparam );
2. イベントの関数宣言
void OnClickBtnUp();
void OnClickBtnDown();
3. イベントの取り扱い

Define.mqhで定義されてるマクロを使います。

EVENT_MAP_BEGIN( CPanelDialog )
ON_EVENT( ON_CLICK, m_btnUp, OnClickBtnUp )
ON_EVENT( ON_CLICK, m_btnDown, OnClickBtnDown )
EVENT_MAP_END( CAppDialog )

マップの先頭:#define EVENT_MAP_BEGIN( class_name )
マップの終わり:#define EVENT_MAP_END( parent_class_name )
イベント処理:#define ON_EVENT( event, control, handler )
※マップの先頭と終わりでは、引数が違います。
BEGINは、子供クラス
ENDでは、親クラス
私は気づかず、20分ぐらい格闘しました:joy:

4. Buttonイベント処理

ロットの増減だけは、簡単なのでAppのイベントを使って実装します。

void CPanelDialog::OnClickBtnUp()
{
string str_editText = m_editLots.Text();
double d_lots = StringToDouble( str_editText );
d_lots += 0.01;
m_editLots.Text( DoubleToStr( d_lots, 2 ) );
}

ロット増減の手順は以下の通りです。
1.Editに表示されてるロットを取得(文字列)
string str_editText = m_editLots.Text();
2.文字列なので、小数に変換
double d_lots = StringToDouble( str_editText );
3.Upボタンなら、プラス0.01をする。
d_lots += 0.01;
4.小数からも文字列に変換して、Editにセットする。
m_editLots.Text( DoubleToStr( d_lots, 2 ) );

5. バリデーション

Editのロット数がマイナスにならないように、0.01より大きいならと条件を追加。

if( d_lots > 0.01 )

実行結果

Screenshot_8.png
Up、Downボタンを押せば、ロット数が増減します。

さいごに

今回は、Buttonの表示とロットの増減をしました。
明日は、Buttonに売買機能を付けます:thumbsup_tone1:
YoutubeでLive配信しながら作ってます。
https://www.youtube.com/channel/UCcTw_iVgpLfrep9f94KxwLg?sub_confirmation=1
チャンネル登録お願いします:relaxed:
Twitterでは毎日呟いています。
https://twitter.com/IceSeed_bz
フォローお願いします:relaxed:
明日は、Buttonを実装します。
お疲れ様。:eye::eye:

新規登録して、もっと便利にQiitaを使ってみよう

  1. ユーザーやタグをフォローできます
  2. 便利な情報をストックできます
  3. 記事の編集提案をすることができます
ログインすると使える機能について
IceSeed
何か始めようとして取りあえず作った。 youtube :【https://www.youtube.com/channel/UCcTw_iVgpLfrep9f94KxwLg?sub_confirmation=1】

コメント

この記事にコメントはありません。
あなたもコメントしてみませんか :)
新規登録
すでにアカウントを持っている方はログイン
1
どのような問題がありますか?
新規登録して、Qiitaをもっと便利に使ってみませんか

この機能を利用するにはログインする必要があります。ログインするとさらに下記の機能が使えます。

  1. ユーザーやタグのフォロー機能であなたにマッチした記事をお届け
  2. ストック機能で便利な情報を後から効率的に読み返せる
新規登録ログイン
ストックするカテゴリー