MQL4 チャート上のボタンでアラートオンオフを制御したい。

 

インジケーターをチャートに入れるときに、AlertONOFFが0だと、Alertボタンを押して、AlertONOFFを1に変更しても、矢印が描画されず、アラートも鳴りません。

なぜか教えてください。






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

//|                                                        MA_BT.mq4 |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property strict
#property indicator_chart_window

#property indicator_buffers 6

#property indicator_color1        clrMagenta
#property indicator_width1        1
#property indicator_type1         DRAW_LINE
#property indicator_color2        clrAqua
#property indicator_width2        1
#property indicator_type2         DRAW_LINE
#property indicator_color3        clrYellow
#property indicator_width3        1
#property indicator_type3         DRAW_LINE
#property indicator_color4        clrWhite
#property indicator_width4        1
#property indicator_type4         DRAW_LINE
#property indicator_color5        clrLimeGreen
#property indicator_width5        3
#property indicator_type5         DRAW_ARROW
#property indicator_color6        clrLimeGreen
#property indicator_width6        3
#property indicator_type6         DRAW_ARROW
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
long    chart_id = ChartID();                                            // チャートID設定
string objname = "MA button";  
string AlertBT = "Alert button";  
input int period1 = 6;
input int period2 = 13;
input int period3 = 26;
input int period4 = 53;
extern int AlertONOFF = 0;
input int AlertCalculateLimit = 5;
input int cal = 5000;
                                       // オブジェクト名設定
double BufMA[];
double BufMA2[];
double BufMA3[];
double BufMA4[];
double BufMA5[];
double BufMA6[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
   SetIndexBuffer(0, BufMA);
   SetIndexBuffer(1, BufMA2);
   SetIndexBuffer(2, BufMA3);
   SetIndexBuffer(3, BufMA4);
   SetIndexBuffer(4, BufMA5);
   SetIndexBuffer(5, BufMA6);
//ボタン作成
   ObjectCreate(chart_id,                                                  // チャートID
                objname,                                                   // オブジェクト名
                OBJ_BUTTON,                                                // オブジェクトタイプにボタンを選択
                0,                                                         // サブウインドウ番号
                0,                                                         // 1番目の時間のアンカーポイント
                0                                                          // 1番目の価格のアンカーポイント
               );
//ボタンの位置とサイズ設定
   ObjectSetInteger(chart_id, objname, OBJPROP_CORNER, CORNER_RIGHT_LOWER); // コーナー位置を左上に
   ObjectSetInteger(chart_id, objname, OBJPROP_ANCHOR, ANCHOR_RIGHT_LOWER); // アンカー位置を左上に
   ObjectSetInteger(chart_id, objname, OBJPROP_XDISTANCE, 62);             // X座標の位置設定
   ObjectSetInteger(chart_id, objname, OBJPROP_YDISTANCE, 75);             // Y座標の位置設定
   ObjectSetInteger(chart_id, objname, OBJPROP_XSIZE, 60);                // ボタンサイズの幅設定
   ObjectSetInteger(chart_id, objname, OBJPROP_YSIZE, 25);    
//ボタンの色設定
   ObjectSetInteger(chart_id, objname, OBJPROP_BGCOLOR, clrSkyBlue);       // ボタンの色設定
   ObjectSetInteger(chart_id, objname, OBJPROP_BORDER_COLOR, clrWhite);    // ボタンの枠の色設定
//ボタンのフォント設定
   ObjectSetString(chart_id, objname, OBJPROP_FONT, "Arial");              // フォントの種別設定
   ObjectSetInteger(chart_id, objname, OBJPROP_COLOR, clrBlack);           // ボタンのフォント色設定
   ObjectSetString(chart_id, objname, OBJPROP_TEXT, "MA");       // 表示するテキストの設定
   ObjectSetInteger(chart_id, objname, OBJPROP_FONTSIZE, 13);              // フォントサイズ設定
//オブジェクトを選択できないようにする
   ObjectSetInteger(chart_id, objname, OBJPROP_SELECTABLE, false);         // ダブルクリックでオブジェクトの選択可否設定
   ObjectSetInteger(chart_id, objname, OBJPROP_SELECTED, false);           // オブジェクトの選択状態
//その他設定
   ObjectSetInteger(chart_id, objname, OBJPROP_BACK, false);               // オブジェクトをローソク足などの前側に設置する
   ObjectSetInteger(chart_id, objname, OBJPROP_STATE, false);              // ボタン押下状態
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

//ボタン作成
   ObjectCreate(chart_id,                                                  // チャートID
                AlertBT,                                                   // オブジェクト名
                OBJ_BUTTON,                                                // オブジェクトタイプにボタンを選択
                0,                                                         // サブウインドウ番号
                0,                                                         // 1番目の時間のアンカーポイント
                0                                                          // 1番目の価格のアンカーポイント
               );
//ボタンの位置とサイズ設定
   ObjectSetInteger(chart_id, AlertBT, OBJPROP_CORNER, CORNER_RIGHT_LOWER); // コーナー位置を左上に
   ObjectSetInteger(chart_id, AlertBT, OBJPROP_ANCHOR, ANCHOR_RIGHT_LOWER); // アンカー位置を左上に
   ObjectSetInteger(chart_id, AlertBT, OBJPROP_XDISTANCE, 124);             // X座標の位置設定
   ObjectSetInteger(chart_id, AlertBT, OBJPROP_YDISTANCE, 75);             // Y座標の位置設定
   ObjectSetInteger(chart_id, AlertBT, OBJPROP_XSIZE, 60);                // ボタンサイズの幅設定
   ObjectSetInteger(chart_id, AlertBT, OBJPROP_YSIZE, 25);    
//ボタンの色設定
   ObjectSetInteger(chart_id, AlertBT, OBJPROP_BGCOLOR, clrSkyBlue);       // ボタンの色設定
   ObjectSetInteger(chart_id, AlertBT, OBJPROP_BORDER_COLOR, clrWhite);    // ボタンの枠の色設定
//ボタンのフォント設定
   ObjectSetString(chart_id, AlertBT, OBJPROP_FONT, "Arial");              // フォントの種別設定
   ObjectSetInteger(chart_id, AlertBT, OBJPROP_COLOR, clrBlack);           // ボタンのフォント色設定
   ObjectSetString(chart_id, AlertBT, OBJPROP_TEXT, "Alert");       // 表示するテキストの設定
   ObjectSetInteger(chart_id, AlertBT, OBJPROP_FONTSIZE, 13);              // フォントサイズ設定
//オブジェクトを選択できないようにする
   ObjectSetInteger(chart_id, AlertBT, OBJPROP_SELECTABLE, false);         // ダブルクリックでオブジェクトの選択可否設定
   ObjectSetInteger(chart_id, AlertBT, OBJPROP_SELECTED, false);           // オブジェクトの選択状態
//その他設定
   ObjectSetInteger(chart_id, AlertBT, OBJPROP_BACK, false);               // オブジェクトをローソク足などの前側に設置する
   ObjectSetInteger(chart_id, AlertBT, OBJPROP_STATE, false);              // ボタン押下状態
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
   ObjectsDeleteAll(chart_id, objname);
}

//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam) {
   if(id == CHARTEVENT_OBJECT_CLICK) {
      if(sparam == objname ) {//sparamという変数の中に、どのオブジェクトがクリックされたかが入っている
         if(ObjectGetInteger(chart_id, objname, OBJPROP_STATE)) {
            ObjectSetString(chart_id, objname, OBJPROP_TEXT, "MA");                // 表示するテキスト
            ObjectSetInteger(chart_id, objname, OBJPROP_COLOR, clrLightGray);                 // フォント色
            ObjectSetInteger(chart_id, objname, OBJPROP_BGCOLOR, clrGray);           // ボタン色
            SetIndexStyle(0, DRAW_NONE);
            SetIndexStyle(1, DRAW_NONE);
            SetIndexStyle(2, DRAW_NONE);
            SetIndexStyle(3, DRAW_NONE);

         } else {
            ObjectSetString(chart_id, objname, OBJPROP_TEXT, "MA");                 // 表示するテキスト
            ObjectSetInteger(chart_id, objname, OBJPROP_COLOR, clrBlack);                     // フォント色
            ObjectSetInteger(chart_id, objname, OBJPROP_BGCOLOR, clrMagenta);                 // ボタン色
            SetIndexStyle(0, DRAW_LINE, EMPTY, EMPTY, clrMagenta);
            SetIndexStyle(1, DRAW_LINE, EMPTY, EMPTY, clrAqua);
            SetIndexStyle(2, DRAW_LINE, EMPTY, EMPTY, clrYellow);
            SetIndexStyle(3, DRAW_LINE, EMPTY, EMPTY, clrWhite);
         }
      }
      if(sparam == AlertBT ) {//sparamという変数の中に、どのオブジェクトがクリックされたかが入っている
         if(ObjectGetInteger(chart_id, AlertBT, OBJPROP_STATE)) {
            ObjectSetString(chart_id, AlertBT, OBJPROP_TEXT, "Alert");                // 表示するテキスト
            ObjectSetInteger(chart_id, AlertBT, OBJPROP_COLOR, clrLightGray);                 // フォント色
            ObjectSetInteger(chart_id, AlertBT, OBJPROP_BGCOLOR, clrGray);           // ボタン色
  
            AlertONOFF = 0;
            SetIndexStyle(4, DRAW_NONE);
            SetIndexStyle(5, DRAW_NONE); 
            printf(AlertONOFF);

            

         } else {
            ObjectSetString(chart_id, AlertBT, OBJPROP_TEXT, "Alert");                 // 表示するテキスト
            ObjectSetInteger(chart_id, AlertBT, OBJPROP_COLOR, clrBlack);                     // フォント色
            ObjectSetInteger(chart_id, AlertBT, OBJPROP_BGCOLOR, clrMagenta);                 // ボタン色

            AlertONOFF = 1;
             SetIndexStyle(4, DRAW_ARROW, EMPTY, EMPTY, clrLimeGreen);
             SetIndexStyle(5, DRAW_ARROW, EMPTY, EMPTY, clrLimeGreen);
             printf(AlertONOFF);
             

            
         }
      }
   }
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| 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[]) {

   for(int i = 0; i <= cal; i++) {
      double MA = iMA( Symbol(), PERIOD_CURRENT, period1, 0, MODE_EMA, PRICE_CLOSE, i );
      BufMA[i] = MA;

      double MA2 = iMA( Symbol(), PERIOD_CURRENT, period2, 0, MODE_EMA, PRICE_CLOSE, i );
      BufMA2[i] = MA2;

      double MA3 = iMA( Symbol(), PERIOD_CURRENT, period3, 0, MODE_EMA, PRICE_CLOSE, i );
      BufMA3[i] = MA3;
      
      double MA4 = iMA( Symbol(), PERIOD_CURRENT, period4, 0, MODE_EMA, PRICE_CLOSE, i );
      BufMA4[i] = MA4;
   }
   
   
     // 処理するローソク足の本数
    int limit = AlertCalculateLimit - prev_calculated;
    
    // 初回の配列添え字対策
    if( prev_calculated == 0 ) limit--;

    //過去から更新
    for (int i = limit; i >= 0; i-- ) {

      double preMA = iMA(Symbol(),0,period1,0,MODE_EMA,PRICE_CLOSE,i);
      double prePrice = iClose(Symbol(),0,i);
      double nowMA = iMA(Symbol(),0,period1,0,MODE_EMA,PRICE_CLOSE,i+1);
      double nowPrice = iClose(Symbol(),0,i+1);

      if(preMA >= prePrice && nowMA <= nowPrice && AlertONOFF == 1){
       BufMA5[i] = High[i]+10*Point;
       Alert(Symbol()+" MA Sell");
      }
      
      if(preMA <= prePrice && nowMA >= nowPrice && AlertONOFF == 1){
       BufMA6[i] = Low[i]-10*Point;
       Alert(Symbol()+" MA Buy");  
      }
        }
   
   
   
   return(rates_total);
}
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2023.07.09
  • www.mql5.com
MQL5: MetaTrader 5取引プラットフォームにビルトインされた取引ストラテジーの言語があれば、ご自分の自動売買ロボット、テクニカル指標、スクリプトと機能ライブラリを書くことができます。
 

この質問は次のとおり修正して再投稿してください。

この質問の投稿場所はここではなく、最下段の「MQL4 and MetaTrader 4」です。

ソースコードを投稿するときはツールバーの </> (またはAlt+S)を使ってください。

Tool

理由: