【解説】 【MQL5 community】 Moving Average (移動平均): チャートの時間足が移動した値動きの平均を取って、それをラインで表示したもの。 トレンドを知るために利用。期間は利用者の目的に合わせて設定する。 例)日足チャートの場合:21日、90日、200日の移動平均線を使用。
SMA (単純移動平均、Simple Moving Average)・・・単純に期間分の平均。
EMA (指数移動平均、Exponential Moving Average)・・・(1)「SmoothFactor=2/(1+期間)」を求める。(2)現在の値・価格に、SmoothFactorをかけたものと、(i-1)期のこの移動平均と(1-SmoothFactor)をかけたものの和を指数移動平均とする。
【特徴】単純移動平均よりも価格の変動に対する反応が早い。
LWMA (線形加重移動平均、Linear Weight Moving Average)・・・単純移動平均と同じく、その期間分の平均を求めますが、直近の価格により大きな重みがかかっている。
【特徴】単純移動平均よりも価格の変動に対する反応が早い。
SMMA (平滑移動平均、Smoothed Moving Average)・・・計算方法はEMAと同じですが、 「SmoothFactor=1/期間」とする。
【シグナル】
(i) 1つの移動平均を使用: 移動平均のラインを上抜けしたら、「買い」、下抜けしたら「売り」。 上昇基調になりそうな(移動平均が右肩上がり)場合のトレンドを把握することができる。
(ii) 2つの期間の異なる移動平均を使用: 一般に中期の移動平均線が上昇し、長期の移動平均線を 下から上に突き抜ける形で交差すると、 低迷していた価格が上昇しはじめたことを示すと言われている。 これが「ゴールデンクロス(Golden Cross)」。 その逆の中期の移動平均線が下降し、長期の移動平均線を 上から下に突き抜ける形で交差すると、 価格が下降しはじめたことを示すと言われている。 これが「デットクロス(Dead Cross)」。
//+------------------------------------------------------------------+
//| Custom Moving Average.mq5 |
//| Copyright 2009, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_type1 DRAW_LINE
#property indicator_color1 Red
//--- input parameters
input int InpMAPeriod=13; // Period
input int InpMAShift=0; // Shift
input ENUM_MA_METHOD InpMAMethod=MODE_SMMA; // Method
//--- indicator buffers
double ExtLineBuffer[];
//+------------------------------------------------------------------+
//| simple moving average |
//+------------------------------------------------------------------+
void CalculateSimpleMA(int rates_total,int prev_calculated,int begin,const double &price[])
{
int i,limit;
//--- first calculation or number of bars was changed
if(prev_calculated==0)// first calculation
{
limit=InpMAPeriod+begin;
//--- set empty value for first limit bars
for(i=0;i<limit-1;i++) ExtLineBuffer[i]=0.0;
//--- calculate first visible value
double firstValue=0;
for(i=begin;i<limit;i++)
firstValue+=price[i];
firstValue/=InpMAPeriod;
ExtLineBuffer[limit-1]=firstValue;
}
else limit=prev_calculated-1;
//--- main loop
for(i=limit;i<rates_total;i++)
ExtLineBuffer[i]=ExtLineBuffer[i-1]+(price[i]-price[i-InpMAPeriod])/InpMAPeriod;
//---
}
//+------------------------------------------------------------------+
//| exponential moving average |
//+------------------------------------------------------------------+
void CalculateEMA(int rates_total,int prev_calculated,int begin,const double &price[])
{
int i,limit;
double SmoothFactor=2.0/(1.0+InpMAPeriod);
//--- first calculation or number of bars was changed
if(prev_calculated==0)
{
limit=InpMAPeriod+begin;
ExtLineBuffer[begin]=price[begin];
for(i=begin+1;i<limit;i++)
ExtLineBuffer[i]=price[i]*SmoothFactor+ExtLineBuffer[i-1]*(1.0-SmoothFactor);
}
else limit=prev_calculated-1;
//--- main loop
for(i=limit;i<rates_total;i++)
ExtLineBuffer[i]=price[i]*SmoothFactor+ExtLineBuffer[i-1]*(1.0-SmoothFactor);
//---
}
//+------------------------------------------------------------------+
//| linear weighted moving average |
//+------------------------------------------------------------------+
void CalculateLWMA(int rates_total,int prev_calculated,int begin,const double &price[])
{
int i,limit;
static int weightsum;
double sum;
//--- first calculation or number of bars was changed
if(prev_calculated==0)
{
weightsum=0;
limit=InpMAPeriod+begin;
//--- set empty value for first limit bars
for(i=0;i<limit;i++) ExtLineBuffer[i]=0.0;
//--- calculate first visible value
double firstValue=0;
for(i=begin;i<limit;i++)
{
int k=i-begin+1;
weightsum+=k;
firstValue+=k*price[i];
}
firstValue/=(double)weightsum;
ExtLineBuffer[limit-1]=firstValue;
}
else limit=prev_calculated-1;
//--- main loop
for(i=limit;i<rates_total;i++)
{
sum=0;
for(int j=0;j<InpMAPeriod;j++) sum+=(InpMAPeriod-j)*price[i-j];
ExtLineBuffer[i]=sum/weightsum;
}
//---
}
//+------------------------------------------------------------------+
//| smoothed moving average |
//+------------------------------------------------------------------+
void CalculateSmoothedMA(int rates_total,int prev_calculated,int begin,const double &price[])
{
int i,limit;
//--- first calculation or number of bars was changed
if(prev_calculated==0)
{
limit=InpMAPeriod+begin;
//--- set empty value for first limit bars
for(i=0;i<limit-1;i++) ExtLineBuffer[i]=0.0;
//--- calculate first visible value
double firstValue=0;
for(i=begin;i<limit;i++)
firstValue+=price[i];
firstValue/=InpMAPeriod;
ExtLineBuffer[limit-1]=firstValue;
}
else limit=prev_calculated-1;
//--- main loop
for(i=limit;i<rates_total;i++)
ExtLineBuffer[i]=(ExtLineBuffer[i-1]*(InpMAPeriod-1)+price[i])/InpMAPeriod;
//---
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,ExtLineBuffer,INDICATOR_DATA);
//--- set accuracy
IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
//--- sets first bar from what index will be drawn
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod);
//---- line shifts when drawing
PlotIndexSetInteger(0,PLOT_SHIFT,InpMAShift);
//--- name for DataWindow
string short_name="unknown ma";
switch(InpMAMethod)
{
case MODE_EMA : short_name="EMA"; break;
case MODE_LWMA : short_name="LWMA"; break;
case MODE_SMA : short_name="SMA"; break;
case MODE_SMMA : short_name="SMMA"; break;
}
IndicatorSetString(INDICATOR_SHORTNAME,short_name+"("+string(InpMAPeriod)+")");
//---- sets drawing line empty value--
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
//---- initialization done
}
//+------------------------------------------------------------------+
//| Moving Average |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &price[])
{
//--- check for bars count
if(rates_total<InpMAPeriod-1+begin)
return(0);// not enough bars for calculation
//--- first calculation or number of bars was changed
if(prev_calculated==0)
ArrayInitialize(ExtLineBuffer,0);
//--- sets first bar from what index will be draw
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod-1+begin);
//--- calculation
switch(InpMAMethod)
{
case MODE_EMA: CalculateEMA(rates_total,prev_calculated,begin,price); break;
case MODE_LWMA: CalculateLWMA(rates_total,prev_calculated,begin,price); break;
case MODE_SMMA: CalculateSmoothedMA(rates_total,prev_calculated,begin,price); break;
case MODE_SMA: CalculateSimpleMA(rates_total,prev_calculated,begin,price); break;
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
【表示結果】
青:14期間移動平均線。これを見ても、分かるように、所々ロウソク足と移動平均とクロスしている。そのため精度は低いがトレンドを掴むには便利な指標である。
Back to Meta Trader







