ColorX2MAインジケーターは、アラート機能を搭載しており、メールやスマホへのプッシュ通知も行います。
このインジケーターにアラート機能、メール送信、プッシュ通知を実装するために、以下の変更が行われました:
- 新しい入力パラメータを追加しました。
//---- アラート用の入力変数 input uint NumberofBar=1;// シグナルのためのバー数 input bool SoundON=true;// アラートを有効にする input uint NumberofAlerts=2;// アラートの数 input bool EMailON=false;// シグナルをメール送信するか input bool PushON=false;// モバイルデバイスにシグナルを送信するか
- インジケーターコードの最後に、BuySignal()、SellSignal()、GetStringTimeframe()の3つの新しい関数を追加しました。
//+------------------------------------------------------------------+ //| 買いシグナル関数 //+------------------------------------------------------------------+ void BuySignal(string SignalSirname,// メールとプッシュメッセージのためのインジケーター名 double &ColorArray[],// カラーインデックスバッファ int ColorIndex,// シグナルを生成するためのカラーインデックス const int Rates_total,// 現在のバー数 const int Prev_calculated,// 前回のティックでのバー数 const double &Close[],// 終値 const int &Spread[])// スプレッド { //--- static uint counter=0; if(Rates_total!=Prev_calculated) counter=0; bool BuySignal=false; bool SeriesTest=ArrayGetAsSeries(ColorArray); int index,index1; if(SeriesTest) { index=int(NumberofBar); index1=index+1; } else { index=Rates_total-int(NumberofBar)-1; index1=index-1; } if(ColorArray[index1]!=ColorIndex && ColorArray[index]==ColorIndex) BuySignal=true; if(BuySignal && counter<=NumberofAlerts) { counter++; MqlDateTime tm; TimeToStruct(TimeCurrent(),tm); string text=TimeToString(TimeCurrent(),TIME_DATE)+" "+string(tm.hour)+":"+string(tm.min); SeriesTest=ArrayGetAsSeries(Close); if(SeriesTest) index=int(NumberofBar); else index=Rates_total-int(NumberofBar)-1; double Ask=Close[index]; double Bid=Close[index]; SeriesTest=ArrayGetAsSeries(Spread); if(SeriesTest) index=int(NumberofBar); else index=Rates_total-int(NumberofBar)-1; Bid+=Spread[index]; string sAsk=DoubleToString(Ask,_Digits); string sBid=DoubleToString(Bid,_Digits); string sPeriod=GetStringTimeframe(ChartPeriod()); if(SoundON) Alert("BUY signal \n Ask=",Ask,"\n Bid=",Bid,"\n currtime=",text,"\n Symbol=",Symbol()," Period=",sPeriod); if(EMailON) SendMail(SignalSirname+": BUY signal alert","BUY signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod); if(PushON) SendNotification(SignalSirname+": BUY signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod); } //--- } //+------------------------------------------------------------------+ //| 売りシグナル関数 //+------------------------------------------------------------------+ void SellSignal(string SignalSirname,// メールとプッシュメッセージのためのインジケーター名 double &ColorArray[],// カラーインデックスバッファ int ColorIndex,// シグナルを生成するためのカラーインデックス const int Rates_total,// 現在のバー数 const int Prev_calculated,// 前回のティックでのバー数 const double &Close[],// 終値 const int &Spread[])// スプレッド { //--- static uint counter=0; if(Rates_total!=Prev_calculated) counter=0; bool SellSignal=false; bool SeriesTest=ArrayGetAsSeries(ColorArray); int index,index1; if(SeriesTest) { index=int(NumberofBar); index1=index+1; } else { index=Rates_total-int(NumberofBar)-1; index1=index-1; } if(ColorArray[index1]!=ColorIndex && ColorArray[index]==ColorIndex) SellSignal=true; if(SellSignal && counter<=NumberofAlerts) { counter++; MqlDateTime tm; TimeToStruct(TimeCurrent(),tm); string text=TimeToString(TimeCurrent(),TIME_DATE)+" "+string(tm.hour)+":"+string(tm.min); SeriesTest=ArrayGetAsSeries(Close); if(SeriesTest) index=int(NumberofBar); else index=Rates_total-int(NumberofBar)-1; double Ask=Close[index]; double Bid=Close[index]; SeriesTest=ArrayGetAsSeries(Spread); if(SeriesTest) index=int(NumberofBar); else index=Rates_total-int(NumberofBar)-1; Bid+=Spread[index]; string sAsk=DoubleToString(Ask,_Digits); string sBid=DoubleToString(Bid,_Digits); string sPeriod=GetStringTimeframe(ChartPeriod()); if(SoundON) Alert("SELL signal \n Ask=",Ask,"\n Bid=",Bid,"\n currtime=",text,"\n Symbol=",Symbol()," Period=",sPeriod); if(EMailON) SendMail(SignalSirname+": SELL signal alert","SELL signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod); if(PushON) SendNotification(SignalSirname+": SELL signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod); } //--- } //+------------------------------------------------------------------+ //| タイムフレームを文字列として取得する //+------------------------------------------------------------------+ string GetStringTimeframe(ENUM_TIMEFRAMES timeframe) { //---- return(StringSubstr(EnumToString(timeframe),7,-1)); //---- }
- インジケーター計算サイクルの後にBuySignal()とSellSignal()関数の呼び出しを追加しました。
//--- BuySignal("X2MA_Alert",ColorX2MA,1,rates_total,prev_calculated,close,spread); SellSignal("X2MA_Alert",ColorX2MA,2,rates_total,prev_calculated,close,spread); //---
ここで、ColorX2MAはラインカラーをインデックス形式で格納するためのカラーインデックスバッファの名前です。
OnCalculate()ブロック内でBuySignal()とSellSignal()関数を1回だけ呼び出すことが想定されています。
このインジケーターは、SmoothAlgorithms.mqhライブラリのクラスを使用します(これを<terminal_data_folder>\MQL5\Includeにコピーしてください)。クラスの使用については、「追加バッファを使用せずに中間計算のための価格系列の平均化」の記事で詳しく説明されています。

Fig1. ColorX2MA_Alertインジケーターのチャート上の表示
Fig.2. ColorX2MA_Alertインジケーター。アラートを生成中。

コメント 0