見出し画像

【MQL】移動平均線を表示させるインジケーターの作り方

移動平均線ソースコードです。
解説もつけておきますのでよかったら参考にしてください。
//+------------------------------------------------------------------+
//| simple_moving_average.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_color1 Red
#property indicator_width1 1

double MA[];

input int MA_Period = 20;
input int MA_Shift = 0;
extern ENUM_MA_METHOD MA_Method = MODE_SMA;
extern ENUM_APPLIED_PRICE MA_Prce = PRICE_CLOSE;

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping

SetIndexBuffer(0,MA);
SetIndexStyle(0,DRAW_LINE);

//---
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 limit = Bars - IndicatorCounted() - 1;

for(int i = limit; i>=0; i--)
{MA[i] = iMA(Symbol() , 0 , MA_Period , MA_Shift , MA_Method , MA_Prce , i);}

//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+

この記事が気に入ったらサポートをしてみませんか?