【MT4のプログラミングをやってみる】MAクロスでサインを表示

 

まえがき

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

 

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

 

 

HTのMQL習得 Step3 MAクロスでサインを表示

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

 

www.youtube.com

 

 

コード

//+------------------------------------------------------------------+
//| MA_Cross_Sign_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 4
#property indicator_plots 4
//--- 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 string _00 = ""; // [MA1]
input int PERIOD0 = 5; // 期間
input int SHIFT0 = 0; // シフト
input ENUM_MA_METHOD METHOD0 = MODE_SMA; // MA種別
input ENUM_APPLIED_PRICE PRICE0 = PRICE_CLOSE; // 適用価格
input color CLR0 = clrWhite; // 色
input ENUM_LINE_STYLE STYLE0 = STYLE_SOLID; // 線種
input int WIDTH0 = 1; // 太さ

input string _10 = ""; //  
input string _11 = ""; // [MA2]
input int PERIOD1 = 20; // 期間
input int SHIFT1 = 0; // シフト
input ENUM_MA_METHOD METHOD1 = MODE_SMA; // MA種別
input ENUM_APPLIED_PRICE PRICE1 = PRICE_CLOSE; // 適用価格
input color CLR1 = clrYellow; // 色
input ENUM_LINE_STYLE STYLE1 = STYLE_SOLID; // 線種
input int WIDTH1 = 1; // 太さ

//--- indicator buffers
double MABuffer0, MABuffer1;
double UP, DN;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,MABuffer0);
SetIndexBuffer(1,MABuffer1);
SetIndexBuffer(2,UP);
SetIndexBuffer(3,DN);
SetIndexStyle(0, DRAW_LINE, STYLE0, WIDTH0, CLR0);
SetIndexStyle(1, DRAW_LINE, STYLE1, WIDTH1, CLR1);
SetIndexStyle(2, DRAW_ARROW, STYLE_SOLID, 2, clrRed);
SetIndexStyle(3, DRAW_ARROW, STYLE_SOLID, 2, clrDodgerBlue);
SetIndexLabel(0, "MA1");
SetIndexLabel(1, "MA2");
SetIndexLabel(2, "Buy");
SetIndexLabel(3, "Sell");
SetIndexArrow(2, 233);
SetIndexArrow(3, 234);
//---
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--) {
MABuffer0[i] = iMA(NULL, 0, PERIOD0, SHIFT0, METHOD0, PRICE0, i);
MABuffer1[i] = iMA(NULL, 0, PERIOD1, SHIFT1, METHOD1, PRICE1, i);

if(MABuffer0[i] > MABuffer1[i] && MABuffer0[i+1]<= MABuffer1[i+1]) {
UP[i] = Low[i];
} else {
UP[i] = EMPTY_VALUE;
}

if(MABuffer0[i] < MABuffer1[i] && MABuffer0[i+1]>= MABuffer1[i+1]) {
DN[i] = High[i];
} else {
DN[i] = EMPTY_VALUE;
}

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

 

 

あとがき

コンパイル&実行した画像は次の状態でした。

 

f:id:satsumaim0:20200627123116p:plain

 

以上、さつま芋でした。