2012年6月7日木曜日

FXのチャート分析ソフトMT4でメール通知するには

FX(外国為替証拠金取引)のチャート分析ソフトMT4(Meta Trader 4)では、インディケーターを起動中にある数値に達した時にメール通知することができます。

メール通知をするには、MT4でのメール設定と、インディケーターでのメール設定が必要になります。

▼MT4でのメール設定方法

MT4でのメール設定では、送信元のSMTPサーバーの情報や送信先のメールアドレスなどを入力します。

まず、MT4のメニューから「ツール」、「オプション」を選びます。



オプションの画面の「E-メール」タブを表示します。

「有効にする」のチェックボックスにチェックを入れます。次に、SMTPサーバーなどの情報を利用しているプロバイダーに合わせて入力します。



入力が完了したら、「Test」のボタンをクリックします。送信先で指定したメールアドレスにメールが届いていたら設定完了です。



なお、メールが届かない場合には、SMTPサーバーに、「:587」や「:465」などを追加するとうまくいく場合があります。

(例)smtp.lolipop.jp:587

▼インディケーターでのメール設定方法

インディケーターでのメール設定では、上記で設定したメールアドレスに送信する内容を指定します。

以下のソースリストは、RSIが25以下、あるいは、75以上の時に、通貨ペアと通貨ペアの価格、売り(sell)か買い(buy)かのメール通知をするインディケーターです。

なお、時間足を1分に設定すると、1分ごとにメール通知される場合がありますので注意してください。

「RSI_mail.mq4」
//+------------------------------------------------------------------+
//| RSI_mail.mq4 |
//| sample |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "sample"
#property link "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 3
#property indicator_color1 DodgerBlue
//---- input parameters
extern int RSIPeriod=14;
extern int RSIUpper=75;
extern int RSILower=25;
//---- buffers
double RSIBuffer[];
double PosBuffer[];
double NegBuffer[];
double ExtUpperBuffer[];
double ExtLowerBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- 2 additional buffers are used for counting.
IndicatorBuffers(5);
SetIndexBuffer(1,PosBuffer);
SetIndexBuffer(2,NegBuffer);
//---- indicator line
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,RSIBuffer);
//---- name for DataWindow and indicator subwindow label
short_name="RSI("+RSIPeriod+")";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
SetIndexDrawBegin(0,RSIPeriod);
//arrow
SetIndexBuffer(3,ExtUpperBuffer);
SetIndexBuffer(4,ExtLowerBuffer);
SetIndexStyle(3,DRAW_ARROW,STYLE_SOLID,1,Yellow);
SetIndexStyle(4,DRAW_ARROW,STYLE_SOLID,1,Red);
SetIndexArrow(3,241);
SetIndexArrow(4,242);
return(0);
}
//+------------------------------------------------------------------+
//| Relative Strength Index |
//+------------------------------------------------------------------+
int start()
{
int i,counted_bars=IndicatorCounted();
double rel,negative,positive;

//----
if(Bars<=RSIPeriod) return(0); //---- initial zero if(counted_bars<1) for(i=1;i<=RSIPeriod;i++) RSIBuffer[Bars-i]=0.0; //---- i=Bars-RSIPeriod-1; if(counted_bars>=RSIPeriod) i=Bars-counted_bars-1;
while(i>=0)
{
double sumn=0.0,sump=0.0;
if(i==Bars-RSIPeriod-1)
{
int k=Bars-2;
//---- initial accumulation
while(k>=i)
{
rel=Close[k]-Close[k+1];
if(rel>0) sump+=rel;
else sumn-=rel;
k--;
}
positive=sump/RSIPeriod;
negative=sumn/RSIPeriod;
}
else
{
//---- smoothed moving average
rel=Close[i]-Close[i+1];
if(rel>0) sump=rel;
else sumn=-rel;
positive=(PosBuffer[i+1]*(RSIPeriod-1)+sump)/RSIPeriod;
negative=(NegBuffer[i+1]*(RSIPeriod-1)+sumn)/RSIPeriod;
}
PosBuffer[i]=positive;
NegBuffer[i]=negative;
if(negative==0.0) RSIBuffer[i]=0.0;
else RSIBuffer[i]=100.0-100.0/(1+positive/negative);

//arrow
ExtUpperBuffer[i]=EMPTY_VALUE;
if(RSIBuffer[i]<=RSILower) { ExtUpperBuffer[i]=5; if(i==1){ SendMail("Mail Alert", Symbol()+" buy "+DoubleToStr(Close[i],Digits)); } } ExtLowerBuffer[i]=EMPTY_VALUE; if(RSIBuffer[i]>=RSIUpper)
{
ExtLowerBuffer[i]=95;
if(i==1){
SendMail("Mail Alert", Symbol()+" sell "+DoubleToStr(Close[i],Digits));
}
}

//
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+

RSIが25以下になると次のようなメールが送られてきます。



なお、パラメータで数値を変更した場合には、その数値に達した時にメール通知があります。