【MT4のプログラミングをやってみる】簡単MAを改修

 

まえがき

こんにちは、こんばんは、脱初心者トレーダーさつま芋です。

 

ちょっと調べたり考えたりすれば分かるような盲点情報などをシェアしたいと思います。

 

 

HTのMQL習得 Step2 簡単MAを改修

今回は次の動画をタイプしてみました。

 

www.youtube.com

 

 

コード

//+------------------------------------------------------------------+
//| MA_demo.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 MA
/*#property indicator_label1 "MA"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1*/
//--- parameters
input int PERIOD = 20; // 期間
input int SHIFT = 0; // シフト
input ENUM_MA_METHOD METHOD = MODE_SMA; // MA種別
input ENUM_APPLIED_PRICE PRICE = PRICE_CLOSE; // 適用価格
input color CLR = clrWhite; // 色
input ENUM_LINE_STYLE STYLE = STYLE_SOLID; // 線種
input int WIDTH = 1; // 太さ

//--- indicator buffers
double MABuffer;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,MABuffer);
SetIndexStyle(0, DRAW_LINE, STYLE, WIDTH, CLR);
SetIndexLabel(0, "MA");
//---
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;
if(limit < 1) limit = 1;
//for (int i = 0; i < Bars; i++) {
for (int i = limit; i >= 0; i--) {
MABuffer[i] = iMA(NULL, 0, PERIOD, SHIFT, METHOD, PRICE, i);
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+

 

 

あとがき

コンパイル&実行したら、なんとか動作しました。

 

見た目は前回と変化ありませんが、パラメーター(期間や線種など)の編集が細かく変更できる状態になりました。

 

f:id:satsumaim0:20200626130034p:plain



 

以上、さつま芋でした。