MT4不具合? CButtonにおけるPressedメソッドが、定型チャートからの呼び出し時に正しく動作しない。

 

MT4のインジケータにて,CButtonクラスを利用します。

その際、定型チャートから、インジケータが呼び出された場合、Pressedメソッドが正しく動作しません。

ボタンをクリックしてもPressedメソッドからの応答が0に固定されてしまいます。 

 

検証コード

//+------------------------------------------------------------------+
//|                                                   ButtonTest.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

#include <Controls/Button.mqh>

// テストボタン
CButton btnTest;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   btnTest.FontSize(8);
   btnTest.Text("Test");
   btnTest.Create(ChartID(), "CTL_BTN_TEST", 0, 10, 20, 50, 50);
   
   if( ChartGetInteger(0, CHART_SHIFT) == false )
   {
      ChartSetInteger(0, CHART_SHIFT, true);
      ChartSetDouble(0, CHART_SHIFT_SIZE, 12);
   }
   return(INIT_SUCCEEDED);
  }
  
//------------------------------------------------------------------
//終了処理
void OnDeinit( const int reason )
{
   btnTest.Destroy();
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   if(id == CHARTEVENT_OBJECT_CLICK)
   {
      if(sparam == btnTest.Name())
      {
         bool pressed = btnTest.Pressed();
         
         PrintFormat("btnTest pressed:%d", pressed);
      }
    }   
  }
//+------------------------------------------------------------------+

 

 チャートにインジケータを挿入して、Testボタンを押した際のエキスパートログ

2016.08.01 16:39:36.895 ButtonTest EURJPY,H1: btnTest pressed:0
2016.08.01 16:39:36.315 ButtonTest EURJPY,H1: btnTest pressed:1


定型チャートからインジケータを呼び出した場合に、エキスパートログ 

2016.08.01 16:41:33.451 ButtonTest EURJPY,H1: btnTest pressed:0
2016.08.01 16:41:32.904 ButtonTest EURJPY,H1: btnTest pressed:0
自動トレーディングとストラテジーテスティング
自動トレーディングとストラテジーテスティング
  • www.mql5.com
MQL5: MetaTrader 5トレーディングプラットフォームにビルトインされたトレードストラテジーの言語があれば、あなた自身のトレーディングロボット、テクニカルインディケーター、スクリプトと機能ライブラリを書くことができます。
 
daisuke_gewinn:

MT4のインジケータにて,CButtonクラスを利用します。

その際、定型チャートから、インジケータが呼び出された場合、Pressedメソッドが正しく動作しません。

ボタンをクリックしてもPressedメソッドからの応答が0に固定されてしまいます。 

 

検証コード

 

 チャートにインジケータを挿入して、Testボタンを押した際のエキスパートログ


定型チャートからインジケータを呼び出した場合に、エキスパートログ 

//+------------------------------------------------------------------+
//|                                                   ButtonTest.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

#include <Controls/Button.mqh>

// テストボタン
CButton btnTest;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   ObjectDelete(0, "CTL_BTN_TEST");
   btnTest.FontSize(8);
   btnTest.Text("Test");
   btnTest.Create(ChartID(), "CTL_BTN_TEST", 0, 10, 20, 50, 50);
   
   if( ChartGetInteger(0, CHART_SHIFT) == false )
   {
      ChartSetInteger(0, CHART_SHIFT, true);
      ChartSetDouble(0, CHART_SHIFT_SIZE, 12);
   }
   return(INIT_SUCCEEDED);
  }
  
//------------------------------------------------------------------
//終了処理
void OnDeinit( const int reason )
{
   btnTest.Destroy();
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   if(id == CHARTEVENT_OBJECT_CLICK)
   {
      if(sparam == btnTest.Name())
      {
         bool pressed = btnTest.Pressed();
         
         PrintFormat("btnTest pressed:%d", pressed);
      }
    }   
  }
//+------------------------------------------------------------------+

自己解決しました。

 初期化処理の中で、インジケータとして作成される可能性のある、オブジェクトをあらかじめ削除する必要があるようです。 

 

ObjectDelete(0, "CTL_BTN_TEST");

 今回は、初期化処理の中に上記一文を追加したら、正しく動作するようになりました。

多分、オブジェクトも再現してしまうため、一度綺麗に削除しないといけないということですね。 

理由: