-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMTF_MA.mq5
86 lines (75 loc) · 6.09 KB
/
MTF_MA.mq5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//+------------------------------------------------------------------+
//| MTF_MA.mq4 |
//| Copyright © 2019, EarnForex.com |
//| https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link "https://www.earnforex.com/"
#property version "1.00"
#property strict
#property description "Displays a higher timeframe moving average on a lower timeframe chart."
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
#property indicator_width1 1
#property indicator_color1 clrRed
#property indicator_type1 DRAW_LINE
#property indicator_style1 STYLE_SOLID
input ENUM_TIMEFRAMES MA_TF = PERIOD_H1; // MA Timeframe
input int MA_Period = 14; // MA Period
input int MA_Shift = 0; // MA Shift
input ENUM_MA_METHOD MA_Method = MODE_EMA; // MA Method
input ENUM_APPLIED_PRICE MA_Applied_Price = PRICE_CLOSE; // MA Applied Price
double MABuf[];
int MA_Handle;
void OnInit()
{
if (MA_TF < Period()) Alert("MA Timeframe should be greater or equal to the chart timeframe.");
SetIndexBuffer(0, MABuf, INDICATOR_DATA);
ArraySetAsSeries(MABuf, true);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, 0);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, MA_Period);
IndicatorSetString(INDICATOR_SHORTNAME, "MTF MA (" + IntegerToString(MA_Period) + ") @ " + EnumToString(MA_TF));
MA_Handle = iMA(NULL, MA_TF, MA_Period, MA_Shift, MA_Method, MA_Applied_Price);
}
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[]
)
{
ArraySetAsSeries(Time, true);
int counted_bars = prev_calculated;
if (counted_bars < 0) return(0);
if (counted_bars > 0) counted_bars--;
int limit = rates_total - counted_bars;
// Always recalculate for the current bar (on higher timeframe).
limit += MA_TF / Period();
if (limit > rates_total - 1) limit = rates_total - 1;
double MA_HTF[];
// Won't need more HTF bars than we have on LTF.
int n = Bars(NULL, MA_TF, Time[0], Time[rates_total - 1]);
if (CopyBuffer(MA_Handle, 0, 0, n, MA_HTF) != n)
{
Print("Higher timeframe data is not ready yet.");
return(0);
}
for (int i = limit; i >= 0; i--)
{
int shift = iBarShift(NULL, MA_TF, Time[i], true);
// If bar not found or if current timeframe has more bars than HTF (a rare case).
if ((shift == -1) || (shift >= n))
{
MABuf[i] = 0;
continue;
}
MABuf[i] = MA_HTF[n - shift - 1]; // MA_HTF has everything backwards.
}
return(rates_total);
}