【MT4】MQLによる移動平均線インジケーターのプログラミング
※当ページはプロモーションが含まれています
【動画】MQLによる移動平均線インジケーターの自作プログラミング
移動平均線インジケーターのソースコード
//+------------------------------------------------------------------+
//| ma.mq4 |
//| Copyright 2020, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
//--- plot Label1
#property indicator_label1 "Label1"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrYellow
#property indicator_style1 STYLE_SOLID
#property indicator_width1 3
//--- indicator buffers
double Label1Buffer[];
extern int MA_Period=5;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,Label1Buffer);
//---
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[])
{
//---
int i;
int j;
int limit;
if(prev_calculated==0){
limit=rates_total-1;
}
else
{
limit=rates_total-prev_calculated;
}
for(i=limit; i>=0; i--)
{
if(i+MA_Period-1<rates_total)
{
Label1Buffer[i]=0;
for(j=0; j<ma_period; j++)<br="">{
Label1Buffer[i]=Label1Buffer[i]+Close[i+j];
}
Label1Buffer[i]=Label1Buffer[i]/MA_Period;
}
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
</ma_period;></rates_total)MT4で移動平均線インジケーターを自作する
まず、前回のソースコード。ローソク足の高値をラインで結ぶ、というものでした。線を1本引いただけで、インジケーターとはいいがたいものでしたので、今回は移動平均線を作ってみます。
前回のソースコードの、チャートが動くたび実行されるOnCalculate関数の中身を、高値のラインから移動平均線へと変えていきます。試しに5日移動平均線を作ってみたいのですが、移動平均線の仕組みはこのようになっています。













