はじめに
ここでは、GUIで売買機能(ワンクリックトレード)を実装します。
作成手順としては、以下の通りです。
- GUIの基盤
- ロットを表示するEdit部品
- ロットを増減させるButton部品
- 買い、売りを入れるButton部品
- それぞれの部品にイベントを付与
前回は、3.4.Buttonの部品を作りました。詳しくは以下の記事で。
【MQL4 : MT4】GUI で 売買機能を実装した EA を作る。③
今回は、5. それぞれの部品にイベントを付与 します。
いろいろと初心者な為、至らない部分もありますが、宜しくお願いします。
- Experts/
- Sample/
- guiwindow.mq4 ←前々回の記事で...
- AppWindow.mqh ←前回の記事で...
- Event.mqh ←今回新しく作ります。
- Sample/
それぞれの部品にイベントを付与
今回は、guiwindow.mqhと新しいEvent.mqhを編集します。
BuyボタンとSellボタンに、OrderSend関数を使って売買機能を付けます。
#include "Event.mqh"
CEvent Event;
int i_reason;
int OnInit()
{
if( i_reason != REASON_CHARTCHANGE && i_reason != REASON_RECOMPILE )
{
if( !AppWindow.Create( 0, "AppWindow", 0, 0, 0, 250, 100 ) )
return( INIT_FAILED );
if( !AppWindow.Run() )
return ( INIT_FAILED );
}
return( INIT_SUCCEEDED );
}
void OnDeinit( const int reason )
{
if( reason != REASON_CHARTCHANGE && reason != REASON_RECOMPILE )
{
AppWindow.Destroy( reason );
}
i_reason = reason;
}
void OnChartEvent( const int id,
const long &lparam,
const double &dparam,
const string &sparam )
{
AppWindow.ChartEvent( id, lparam, dparam, sparam );
Event.OnEvent( id, lparam, dparam, sparam ); // --- コレだけ追加
}
#include "AppWindow.mqh" // --- 1
CPanelDialog AppWindow;
class CEvent
{
public:
bool OnEvent( const int id, const long lparam, const double dparam, const string sparam ); // --- 2
private:
bool btnOrder( const int id, const long lparam, const double dparam, const string sparam );
bool Order( int i_type, double d_lots );
};
bool CEvent::OnEvent( const int id, const long lparam, const double dparam, const string sparam )
{
if( !btnOrder( id, lparam, dparam, sparam ) ) // --- 3
Alert( "Error : " + (string)GetLastError() );
return true;
}
bool CEvent::btnOrder( const int id, const long lparam, const double dparam, const string sparam )
{
if( ( StringFind( sparam, "btnBuy", 4 ) != -1 ) && id == CHARTEVENT_OBJECT_CLICK ) // --- 4
{
if( !Order( 0, (double)AppWindow.m_editLots.Text() ) )
Alert( "Error : " + (string)GetLastError() );
}else if( ( StringFind( sparam, "btnSell", 4 ) != -1 ) && id == CHARTEVENT_OBJECT_CLICK )
{
if( !Order( 1, (double)AppWindow.m_editLots.Text() ) )
Alert( "Error : " + (string)GetLastError() );
}
return true;
}
bool CEvent::Order( int i_type, double d_lots )
{
if( OrderSend( Symbol(), i_type, d_lots, Close[0], 0, 0, 0, "GUISample", 999, 0, clrRed ) == -1 ) // --- 5
return false;
else
Alert( "OrderSend Success!!\n Lots = " + (string)d_lots + "\n type = " + (string)i_type );
return true;
}
ソースコード詳細
1. includeとインスタンス
Editのロット数を取得する際に使うので、guiwindow.mqhからこっちに移動しました。
(もっといい方法があると思います。今回はコードが少ないので。。。)
#include "AppWindow.mqh"
CPanelDialog AppWindow;
2. OnEvent関数の宣言
guiwindow.mqhからOnChartEventのパラメータを貰う関数です。
guiwindow.mqhには、これしか追加しません。
Event.OnEvent( id, lparam, dparam, sparam );
bool OnEvent( const int id, const long lparam, const double dparam, const string sparam );
3. OnEvent関数
保守性を上げるため、OnEventからそれぞれのイベント関数に渡します。
bool CEvent::OnEvent( const int id, const long lparam, const double dparam, const string sparam )
{
if( !btnOrder( id, lparam, dparam, sparam ) ) // --- 3
Alert( "Error : " + (string)GetLastError() );
return true;
}
4. btnOrder関数
今回のメインの場所ですね。
ボタンが押下されたか判断と、Order関数で注文を出しています。
bool CEvent::btnOrder( const int id, const long lparam, const double dparam, const string sparam )
{
if( ( StringFind( sparam, "btnBuy", 4 ) != -1 ) && id == CHARTEVENT_OBJECT_CLICK )
{
if( !Order( 0, (double)AppWindow.m_editLots.Text() ) )
Alert( "Error : " + (string)GetLastError() );
}else if( ( StringFind( sparam, "btnSell", 4 ) != -1 ) && id == CHARTEVENT_OBJECT_CLICK )
{
if( !Order( 1, (double)AppWindow.m_editLots.Text() ) )
Alert( "Error : " + (string)GetLastError() );
}
return true;
}
OnChartEventのパラメータは、以下のオブジェクトクリックを使います。
画像参照元 MT4でEA自作しちゃお~
sparamには、オブジェクト名が入るので、AppWindow.mqhで作成する際に付けた名前がとデジタルプレフィックスが入ります。
デジタルプレフィックス + Create時の名前
なので、StringFindでbtnBuyが含まれているかと、オブジェクトがクリックされたかを判断します。
if( ( StringFind( sparam, "btnBuy", 4 ) != -1 ) && id == CHARTEVENT_OBJECT_CLICK )
if文を抜けたら、Order関数に飛ばして注文を出します。
引数は、買い( 0 )か売り( 1 )と、Editに表示されてるロットです。
Order( 0, (double)AppWindow.m_editLots.Text() );
5. Order関数
btnOrder関数からパラメータを受け取り、実際に注文する関数です。
bool CEvent::Order( int i_type, double d_lots )
{
if( OrderSend( Symbol(), i_type, d_lots, Close[0], 0, 0, 0, "GUISample", 999, 0, clrRed ) == -1 )
return false;
else
Alert( "OrderSend Success!!\n Lots = " + (string)d_lots + "\n type = " + (string)i_type );
return true;
}
OrderSend関数で注文をだしています。
if( OrderSend( Symbol(), i_type, d_lots, Close[0], 0, 0, 0, "GUISample", 999, 0, clrRed ) == -1 )
OrderSendは、戻り値を判定しないと警告をだすので、「-1」なら return false;で返してます。
確認のために、Alertで注文タイプとロットを表示させてます。
エラーの際も、Alertを表示させてるので、ボタンを押下したら、何かしらAlertが出るはずです。
出ないなら、自動売買の設定がOFFとか。。。
実行結果
Buyボタンを押下したら、Buy注文が入りました。
確認のため、Alertを出していますが、邪魔な人もいるのかな?
さいごに
今回は、それぞれの部品にイベントを付与しました。
次回は、全決済、個別全決済をやります(多分...)
YoutubeでLive配信しながら作ってます。
https://www.youtube.com/channel/UCcTw_iVgpLfrep9f94KxwLg?sub_confirmation=1
チャンネル登録お願いします
Twitterでは毎日呟いています。
https://twitter.com/IceSeed_bz
フォローお願いします
お疲れ様。
コメント