-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTrendFlex.mq5
148 lines (127 loc) · 8.57 KB
/
TrendFlex.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//------------------------------------------------------------------
#property copyright "© mladen, 2020"
#property link "[email protected]"
#property version "1.00"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots 1
#property indicator_label1 "TrendFlex"
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 clrDodgerBlue,clrCoral
#property indicator_width1 2
//
//---
//
input int inpFastPeriod = 20; // Fast trend-flex period
input int inpSlowPeriod = 50; // Slow trend-flex period
double val[],valc[];
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
int OnInit()
{
SetIndexBuffer(0,val ,INDICATOR_DATA);
SetIndexBuffer(1,valc ,INDICATOR_COLOR_INDEX);
iTfFast.OnInit(inpFastPeriod);
iTfSlow.OnInit(inpSlowPeriod);
//
//
//
IndicatorSetString(INDICATOR_SHORTNAME,"TrendFlex ("+(string)inpFastPeriod+","+(string)inpSlowPeriod+")");
return(INIT_SUCCEEDED);
}
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
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 = MathMax(prev_calculated-1,0);
for(int i=limit; i<rates_total && !_StopFlag; i++)
{
double _price = (i>0 ? (close[i]+close[i-1])/2 : close[i]);
val[i] = iTfFast.OnCalculate(_price,i,rates_total)-iTfSlow.OnCalculate(_price,i,rates_total);
valc[i] = (i>0) ? val[i]>val[i-1] ? 0 : val[i]<val[i-1] ? 1 : 0 : 0;
}
return(rates_total);
}
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
class CTrendFlex
{
private :
double m_c1;
double m_c2;
double m_c3;
struct sWorkStruct
{
double value;
double ssm;
double sum;
double ms;
};
sWorkStruct m_array[];
int m_arraySize;
int m_period;
public :
CTrendFlex() : m_c1(1), m_c2(1), m_c3(1), m_arraySize(-1) { }
~CTrendFlex() { }
//
//---
//
void OnInit(int period)
{
m_period = (period>1) ? period : 1;
double a1 = MathExp(-1.414*M_PI/m_period);
double b1 = 2.0*a1*MathCos(1.414*M_PI/m_period);
m_c2 = b1;
m_c3 = -a1*a1;
m_c1 = 1.0 - m_c2 - m_c3;
}
double OnCalculate(double value, int i, int bars)
{
if (m_arraySize<bars) m_arraySize=ArrayResize(m_array,bars+500);
//
//
//
m_array[i].value = value;
if (i>1)
m_array[i].ssm = m_c1*(m_array[i].value+m_array[i-1].value)/2.0 + m_c2*m_array[i-1].ssm + m_c3*m_array[i-2].ssm;
else m_array[i].ssm = value;
if (i>m_period)
m_array[i].sum = m_array[i-1].sum + m_array[i].ssm - m_array[i-m_period].ssm;
else
{
m_array[i].sum = m_array[i].ssm;
for (int k=1; k<m_period && (i-k)>=0; k++) m_array[i].sum += m_array[i-k].ssm;
}
//
//
//
double sum = m_period*m_array[i].ssm-m_array[i].sum;
sum /= m_period;
m_array[i].ms = (i>0) ? 0.04 * sum*sum+0.96*m_array[i-1].ms : 0;
return (m_array[i].ms!=0 ? sum/MathSqrt(m_array[i].ms) : 0);
}
};
CTrendFlex iTfSlow,iTfFast;