-
Notifications
You must be signed in to change notification settings - Fork 3
/
knoxville_divergence.mq4
190 lines (170 loc) · 12.8 KB
/
knoxville_divergence.mq4
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//+------------------------------------------------------------------+
//| knoxville_divergence.mq4 |
//| Paúl Herrera |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Avanti Servicios Financieros, C.A."
#property link "https://avantifs.herokuapp.com"
#property version "2.1"
#property strict
#property indicator_chart_window
//--- input parameters
input int Periods = 30;
input bool BullishDivergence = True;
input bool BearishDivergence = True;
int arrowCount = 0;
double divisor = MathPow(10, Digits);
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
//---
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0, OBJ_ARROW);
}
//+------------------------------------------------------------------+
//| 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 = rates_total - prev_calculated;
int KD, i;
if(limit > 1)
{
for(i=1; i < limit - Periods; i++)
{
KD = knoxville_divergence(i);
create_arrow(KD, i, time, high, low);
}
}
else if (limit == 1)
{
KD = knoxville_divergence(1);
create_arrow(KD, 1, time, high, low);
}
else
{
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//------------------------------------------------------------------------------ 1 --
//----------------------- FUNCTIONS -------------------------- 1 --
//------------------------------------------------------------------------------ 1 --
int knoxville_divergence(int period)
{
int MinPeriod = 4, KD = 0, i, j;
int os[210], ob[210];
double rsi = 50;
ArrayInitialize(os,999999999);
ArrayInitialize(ob,999999999);
//---- Checking if oversold/overbought.
for (j = 0; j <= Periods; j++)
{
rsi = iRSI(NULL, 0, 21, PRICE_CLOSE, period + j);
if (rsi <= 30)
{
os[j] = period + j;
}
if (rsi >= 70)
{
ob[j] = period + j;
}
}
ArraySort(os);
ArraySort(ob);
int c = 0;
//---- Checking for Knoxville Divergence.
if (period>0){
for (i = MinPeriod; i <= Periods; i++)
{
//---- Checking for Momentum Divergence.
if(BullishDivergence)
{
if (iMomentum(NULL, 0, 20, PRICE_CLOSE, period) > iMomentum(NULL, 0, 20, PRICE_CLOSE, period + i)){
if (iClose(NULL, 0, period) < iClose(NULL, 0, period + i)){
if (iLow(NULL, 0, period) <= iLow(NULL,0,iLowest(NULL, 0, MODE_LOW, i, period + 1))){
for(j=0; j < ArraySize(os); j++)
{
if (os[j] <= period + i)
{
KD = 1;
Print(" Bullish Divergence - Divergent periods: ", period + i, " -> ", period,
". Oversold at period: ", os[j], ". Name: arrow", arrowCount + 1);
return(KD);
}
}
}
}
}
}
if(BearishDivergence)
{
if (iMomentum(NULL, 0, 20, PRICE_CLOSE, period) < iMomentum(NULL, 0, 20, PRICE_CLOSE, period + i)){
if (iClose(NULL, 0, period) > iClose(NULL, 0, period + i)){
if (iHigh(NULL, 0, period) >= iHigh(NULL,0,iHighest(NULL, 0, MODE_HIGH, i, period + 1))){
for(j=0; j < ArraySize(os); j++)
{
if (ob[j] <= period + i)
{
KD = -1;
Print(" Bearish Divergence - Divergent periods: ", period + i, " -> ", period,
". Overbought at period: ", ob[j], ". Name: arrow", arrowCount + 1);
return(KD);
}
}
}
}
}
}
}}
return(0);
}
void create_arrow(int KD, int period, const datetime &time[], const double &high[],
const double &low[])
{
string name;
if (KD == 1)
{
arrowCount++;
name = get_name();
ObjectCreate(0,name,OBJ_ARROW,0,0,0,0,0); // Create an arrow
ObjectSetInteger(0,name,OBJPROP_ARROWCODE,225); // Set the arrow code
ObjectSetInteger(0,name,OBJPROP_TIME,time[period]); // Set time
ObjectSetDouble(0,name,OBJPROP_PRICE,high[period] + 2.5*iATR(NULL,0,10,period)); // Set price
ObjectSetInteger(0,name,OBJPROP_WIDTH,3);
ObjectSetInteger(0,name,OBJPROP_COLOR,clrForestGreen);
ChartRedraw(0);
}
else if (KD == -1)
{
arrowCount++;
name = get_name();
ObjectCreate(0,name,OBJ_ARROW,0,0,0,0,0); // Create an arrow
ObjectSetInteger(0,name,OBJPROP_ARROWCODE,226); // Set the arrow code
ObjectSetInteger(0,name,OBJPROP_TIME,time[period]); // Set time
ObjectSetDouble(0,name,OBJPROP_PRICE,low[period] - 0.5*iATR(NULL,0,10,period)); // Set price
ObjectSetInteger(0,name,OBJPROP_WIDTH,3);
ChartRedraw(0);
}
}
string get_name()
{
return(StringConcatenate("arrow", IntegerToString(arrowCount)));
}
//+------------------------------------------------------------------+