-
Notifications
You must be signed in to change notification settings - Fork 3
/
SMA_Knoxville.mq4
267 lines (243 loc) · 15.8 KB
/
SMA_Knoxville.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//+------------------------------------------------------------------+
//| SMA_Knoxville.mq4 |
//| Copyright 2017, Avanti Servicios Financieros, C.A. |
//| https://avantifs.herokuapp.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Avanti Servicios Financieros, C.A."
#property link "https://avantifs.herokuapp.com"
#property version "2.1"
#property strict
//--- input parameters
input int SMA_Period = 1000;
input int KnoxvilleDivergence_Periods = 30;
input int Exit_Periods = 20;
input int Periods_Between_Trades = 15;
input int Emergency_StopLoss = 500;
input double Lots = 0.01;
input bool Buy_Signals = true;
input bool Sell_Signals = true;
input bool Reverse = false;
//--- global variables.
double divisor = MathPow(10, Digits);
int periodsBetweenTrades = 0;
int maxSlippage = 5;
string label_name = "label";
//--- Data structures.
struct Order
{
int id;
int exitTimer;
int ticket;
};
Order nullOrder = {0, 0, 0};
Order orders[20];
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
for(int i=0; i<ArraySize(orders) ; i++)
{
orders[i] = nullOrder;
}
string text = "Avanti's SMA + Knoxville EA running.";
ObjectCreate(0,label_name,OBJ_LABEL,0,0,0);
ObjectSetString(0,label_name,OBJPROP_TEXT,text);
ObjectSetInteger(0,label_name,OBJPROP_XDISTANCE,5);
ObjectSetInteger(0,label_name,OBJPROP_YDISTANCE,20);
ObjectSetInteger(0,label_name,OBJPROP_COLOR,clrForestGreen);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
ObjectDelete(label_name);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
if(NewBar())
{
// If a trade is possible.
--periodsBetweenTrades;
if(periodsBetweenTrades <= 0)
{
int ticket;
bool aboveSMA = false;
bool belowSMA = false;
double SMA = iMA(NULL, 0, SMA_Period, 0, MODE_SMA, PRICE_CLOSE, 0);
if(SMA != 0)
{
aboveSMA = iClose(NULL, 0, 1) >= SMA ? true : false;
belowSMA = iClose(NULL, 0, 1) <= SMA ? true : false;
}
int divergence = knoxville_divergence(1);
//Orders.
if(aboveSMA==true && divergence==1)
{
int magicNumber = MathRand();
// Buying or selling depending on the Reverse status.
if(Reverse)
{
ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, maxSlippage, Bid + Emergency_StopLoss / divisor * 10, 0, NULL, magicNumber, 0, clrRed);
}
else
{
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, maxSlippage, Ask - Emergency_StopLoss / divisor * 10, 0, NULL, magicNumber, 0, clrGreen);
}
// Saving the order data in the structs array.
if(ticket > 0)
{
save_order_data(magicNumber, ticket);
periodsBetweenTrades = Periods_Between_Trades;
}
}
else if(belowSMA==true && divergence==-1)
{
int magicNumber = MathRand();
// Buying or selling depending on the Reverse status.
if(Reverse)
{
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, maxSlippage, Ask - Emergency_StopLoss / divisor * 10, 0, NULL, magicNumber, 0, clrGreen);
}
else
{
ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, maxSlippage, Bid + Emergency_StopLoss / divisor * 10, 0, NULL, magicNumber, 0, clrRed);
}
// Saving the order data in the structs array.
if(ticket > 0)
{
save_order_data(magicNumber, ticket);
periodsBetweenTrades = Periods_Between_Trades;
}
}
}
//Checks if there's an exit.
for(int i=0; i<ArraySize(orders) ; i++)
{
Order order = orders[i];
//Checks if the array's slot has a an order.
if(order.id != 0)
{
//Checks the order's exit time.
if(order.exitTimer == 0)
{
//Close trade.
bool exit = OrderClose(order.ticket, Lots, Bid, 50, clrBlue);
if(exit)
{
orders[i] = nullOrder;
continue;
}
}
--order.exitTimer;
orders[i] = order;
}
}
}
}
//+------------------------------------------------------------------+
bool NewBar()
{
static datetime lastbar;
datetime curbar = Time[0];
if(lastbar!=curbar)
{
lastbar=curbar;
return (true);
}
else
{
return(false);
}
}
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 <= KnoxvilleDivergence_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 <= KnoxvilleDivergence_Periods; i++)
{
if (Sell_Signals)
{
//---- Checking for Momentum Divergence.
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;
return(KD);
}
}
}
}
}
}
if (Buy_Signals)
{
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;
return(KD);
}
}
}
}
}
}
}
}
return(0);
}
void save_order_data(int magicNumber, int ticket)
{
for(int i=0; i<ArraySize(orders) ; i++)
{
//Looks for an empty space in the orders array.
Order order = orders[i];
if(order.id == 0)
{
order.id = magicNumber;
order.exitTimer = Exit_Periods;
order.ticket = ticket;
orders[i] = order;
break;
}
}
}