現在の足と一つ前の移動平均線の大小関係を比較して、トレンド状態を視覚化したサンプルインジケーターです。
コードをすぐに無くしちゃうのでメモ。
//+------------------------------------------------------------------+
//| TredMA.mq4 |
//| Copyright 2021, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
input int ma_preiod=200;
input int ma_shift=0;
input ENUM_MA_METHOD MA_Method = MODE_SMA;//移動平均種別
input ENUM_APPLIED_PRICE MA_Price = PRICE_CLOSE;//適用価格
double UP[];
double DOWN[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer(0,UP);
SetIndexStyle(0, DRAW_ARROW, STYLE_SOLID, 2, RoyalBlue);
SetIndexBuffer(1,DOWN);
SetIndexStyle(1, DRAW_ARROW, STYLE_SOLID, 2, Crimson);
SetIndexArrow(0 ,159);
SetIndexArrow(1 ,159);
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 - prev_calculated;
for(int i=0; i<limit; i++)
{
double ma = iMA(NULL,0,ma_preiod,ma_shift,MA_Method,MA_Price,i);
double ma1 = iMA(NULL,0,ma_preiod,ma_shift,MA_Method,MA_Price,i+1);
if(ma>ma1)
{
UP[i]=ma;
}
else
{
DOWN[i]=ma;
}
}
return(rates_total);
}