PC版MT4からスマホに次の通知を行うサンプルコードです。
- スマホ通知
- メール通知
設定方法
通知を行うためにはPC版MT4の方で設定が必要です。
メールの設定は面倒くさいので、スマホ通知だけでも良いかもしれません。
メール設定
PC版MT4の「ツール→オプション」をクリックし「E-メール」タブを表示させて、メールの情報を入力します。
入力内容は「Gmail アプリパスワード」でググってください。
入力設定後、テストボタンクリックでメールが送信されます。
スマホ通知
私はiPhoneしか持っていませんので、Androidでの設定はググってください。
iPhone版のMT4の設定「チャットとメッセージ」をクリックし、ここの文字列を控える。
PC版MT4の「ツール→オプション」をクリックし「通知機能」タブを表示させて、先ほど控えたMetaQuotes IDを入力します。
テストをクリックするとスマホに通知が行われます。通知が行われない場合は、スマホの通知設定でMT4を許可していないか、MetaQuotes IDが間違えているかのどちらかですので、確認してください。
サンプルコード
一つ前の足から変動したPIPSの幅によって通知する簡単なサンプルコードです。このままコピペしてコンパイルすると使えるようになります。
デフォルト設定値の場合、10PIPSですべての通知が行われるようになります。
アラートとサウンド部分はコメントアウトにして実行されないようにしています。アラートサウンドが必要な場合は、「//」を削除してください。
通知メッセージはインジケーターのプロパティにしていますので、お好きなように変更してお使いください。
//+------------------------------------------------------------------+
//| MT4Notification.mq4 |
//| Copyright 2022,Greeds Co., Ltd. |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022,Greeds Co., Ltd."
#property link ""
#property version "1.00"
#property strict
#property indicator_chart_window
input string _alert = "Alert"; //アラートメッセージ
input string _sound = "alert2.wav"; //アラート音
input string _mail_title = "メールタイトル"; //メールタイトル
input string _mail_body = "メール本文"; //メール本文
input string _notification = "通知"; //通知メッセージ
input double _detectionPips = 10;
datetime NotificationTime;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
NotificationTime=Time[1];
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 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[])
{
double abs = MathAbs(Close[0] - Close[1]);
if(Digits==2 || Digits==3)
{
abs *= 100;
}
else if(Digits==4 || Digits==5){
abs *= 10000;
}
if(abs > _detectionPips)
{
if(NotificationTime!=Time[0])
{
NotificationTime=Time[0];
Notification();
}
}
return(rates_total);
}
void Notification()
{
//Alert(_alert + ":"+(string)NotificationTime + " " + (string)Close[0]);
//PlaySound(_sound);
SendMail(_mail_title + ":"+(string)NotificationTime + " " + (string)Close[0],_mail_body);
SendNotification(_notification + ":"+(string)NotificationTime + " " + (string)Close[0]);
}
主要部分はNotification関数にまとまっていますので、サインインジケーターを自作している方であれば、流用可能です。