-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathDarcie_Complete.mq4
317 lines (252 loc) · 13.2 KB
/
Darcie_Complete.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//+------------------------------------------------------------------+
//| This MQL is generated by Expert Advisor Builder |
//| http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/ |
//| |
//| In no event will author be liable for any damages whatsoever. |
//| Use at your own risk. |
//| |
//| Modified by Lucas Liew |
//| |
//+------------------- DO NOT REMOVE THIS HEADER --------------------+
/*
DARCIE - RETRACEMENT BOT
DARCIE ENTRY RULES:
Long when:
Closing price touch bottom line of Donchian(24).
SMA(24) is greater than SMA(72). This indicates that we are in a up trend.
Short when:
Closing price touch top line of Donchian(24).
SMA(24) is less than SMA(72). This indicates that we are in a down trend.
DARCIE EXIT RULES:
Profit-Taking Exit 1: Exit the long trade when closing price moved up > 0.5 * Donchian(24) width. (defined as Donchian(24) top line - bottom line)
Profit-Taking Exit 2: Exit the short trade when closing price moved down > 0.5 * Donchian(24) width.
Stop Loss Exits: Exit when closing price move 2 * ATR(24) in the averse direction.
Stop Loss Exit: 2 ATR(24)
DARCIE POSITION SIZING RULE:
2% of Capital risked per trade
8 TDLs in Total. Good Luck!
*/
#define SIGNAL_NONE 0
#define SIGNAL_BUY 1
#define SIGNAL_SELL 2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4
#property copyright "Expert Advisor Builder"
#property link "http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/"
extern int MagicNumber = 00003;
extern bool SignalMail = False;
extern double Lots = 1.0;
extern int Slippage = 3;
extern bool UseStopLoss = True;
extern int StopLoss = 0;
extern bool UseTakeProfit = True;
extern int TakeProfit = 0;
extern bool UseTrailingStop = False;
extern int TrailingStop = 0;
extern bool isSizingOn = True;
extern int Risk = 2;
// TDL 8: Declare Extern Variables
extern string Donchian_variables;
extern int Periods_Entry=24;
extern int Extremes=3;
extern int Margins=-2;
extern int Advance=0;
extern int max_bars=1000;
extern string Donchian_variables_end;
extern double tpDC_k = 0.5; // Take Profit Multiple of donchian
extern double slATR_k = 2; // Stop Loss Multiple of ATR
extern int atr_period = 24; // Used for stop loss
extern int smaPeriodShort = 24;
extern int smaPeriodLong = 72;
int P = 1;
int Order = SIGNAL_NONE;
int Total, Ticket, Ticket2;
double StopLossLevel, TakeProfitLevel, StopLevel;
bool isYenPair;
// TDL 7: Declare variables
double donchianTop1, donchianTop2, donchianBottom1, donchianBottom2, donchianWidth, close1, close2;
double atr_current, atr_past;
double takeprofit1, takeprofit2;
double timeexit;
double sma_short, sma_long;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int init() {
if(Digits == 5 || Digits == 3 || Digits == 1)P = 10;else P = 1; // To account for 5 digit brokers
if(Digits == 3 || Digits == 2) isYenPair = True; // To account for Yen Pairs
return(0);
}
//+------------------------------------------------------------------+
//| Expert initialization function - END |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
int deinit() {
return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function - END |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Expert start function |
//+------------------------------------------------------------------+
int start() {
Total = OrdersTotal();
Order = SIGNAL_NONE;
//+------------------------------------------------------------------+
//| Variable Setup |
//+------------------------------------------------------------------+
// TDL 1: Initialise Donchian indicators (Hint: Create 4 variables)
donchianTop1 = iCustom(NULL, 0, "Donchian Channels", Periods_Entry, Extremes, Margins, Advance, max_bars, 1, 1);
donchianTop2 = iCustom(NULL, 0, "Donchian Channels", Periods_Entry, Extremes, Margins, Advance, max_bars, 1, 2);
donchianBottom1 = iCustom(NULL, 0, "Donchian Channels", Periods_Entry, Extremes, Margins, Advance, max_bars, 0, 1);
donchianBottom2 = iCustom(NULL, 0, "Donchian Channels", Periods_Entry, Extremes, Margins, Advance, max_bars, 0, 2);
// TDL 2: Initialise MAs
sma_short = iMA(NULL, 0, smaPeriodShort, 0, 0, 0, 1);
sma_long = iMA(NULL, 0, smaPeriodLong, 0, 0, 0, 1);
// TDL 3: Calculate donchianWidth and ATR(20)
donchianWidth = donchianTop1 - donchianBottom1;
atr_current = iATR(NULL, 0, atr_period, 1); // ATR(20)
// TDL 4: Initialise Closing Price Variables
close1 = iClose(NULL, 0, 1);
close2 = iClose(NULL, 0, 2);
// TDL 6: Declare Stop Loss and Take Profits Exits
StopLoss = slATR_k * atr_current / (P * Point); // Note that StopLoss need to be initialised before the Sizing Algo as we are using this value there
TakeProfit = tpDC_k * donchianWidth / (P * Point); // Take Profit 2 Donchian(24);
// Sizing Algo (2% risked per trade)
if (isSizingOn == true) {
Lots = Risk * 0.01 * AccountBalance() / (MarketInfo(Symbol(),MODE_LOTSIZE) * StopLoss * P * Point); // Sizing Algo based on account size
if(isYenPair == true) Lots = Lots * 100; // Adjust for Yen Pairs
Lots = NormalizeDouble(Lots, 2); // Round to 2 decimal place
}
StopLevel = (MarketInfo(Symbol(), MODE_STOPLEVEL) + MarketInfo(Symbol(), MODE_SPREAD)) / P; // Defining minimum StopLevel
if (StopLoss < StopLevel) StopLoss = StopLevel;
if (TakeProfit < StopLevel) TakeProfit = StopLevel;
//+------------------------------------------------------------------+
//| Variable Setup - END |
//+------------------------------------------------------------------+
//Check position
bool IsTrade = False;
for (int i = 0; i < Total; i ++) {
Ticket2 = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderType() <= OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
IsTrade = True;
if(OrderType() == OP_BUY) {
//Close
//+------------------------------------------------------------------+
//| Signal Begin(Exit Buy) |
//+------------------------------------------------------------------+
/*
DARCIE EXIT RULES:
Profit-Taking Exit 1: Exit the long trade when closing price moved up > 1 Donchian(24) width. (defined as Donchian(24) top line - bottom line)
Profit-Taking Exit 2: Exit the short trade when closing price moved down > 1 Donchian(24) width.
*/
// Exit rules are incorporated into the StopLoss and TakeProfit Variables
//if() Order = SIGNAL_CLOSEBUY; // Rule to EXIT a Long trade
//+------------------------------------------------------------------+
//| Signal End(Exit Buy) |
//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSEBUY) {
Ticket2 = OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
IsTrade = False;
continue;
}
//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if(Bid - OrderOpenPrice() > P * Point * TrailingStop) {
if(OrderStopLoss() < Bid - P * Point * TrailingStop) {
Ticket2 = OrderModify(OrderTicket(), OrderOpenPrice(), Bid - P * Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
continue;
}
}
}
} else {
//+------------------------------------------------------------------+
//| Signal Begin(Exit Sell) |
//+------------------------------------------------------------------+
//if () Order = SIGNAL_CLOSESELL; // Rule to EXIT a Short trade
//+------------------------------------------------------------------+
//| Signal End(Exit Sell) |
//+------------------------------------------------------------------+
if (Order == SIGNAL_CLOSESELL) {
Ticket2 = OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
IsTrade = False;
continue;
}
//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if((OrderOpenPrice() - Ask) > (P * Point * TrailingStop)) {
if((OrderStopLoss() > (Ask + P * Point * TrailingStop)) || (OrderStopLoss() == 0)) {
Ticket2 = OrderModify(OrderTicket(), OrderOpenPrice(), Ask + P * Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
continue;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| Signal Begin(Entries) |
//+------------------------------------------------------------------+
/*
DARCIE ENTRY RULES:
Long when: Closing price touch bottom line of Donchian(24)
Short when: Closing price touch top line of Donchian(24)
*/
// TDL 5: Add all entry rules
if (sma_short > sma_long) if (close2 > donchianBottom2 && close1 <= donchianBottom1 ) Order = SIGNAL_BUY; // Rule to ENTER a Long trade
if (sma_short < sma_long) if (close2 < donchianTop2 && close1 >= donchianTop1) Order = SIGNAL_SELL; // Rule to ENTER a Short trade
//+------------------------------------------------------------------+
//| Signal End |
//+------------------------------------------------------------------+
//Buy
if (Order == SIGNAL_BUY) {
if(!IsTrade) {
//Check free margin
if (AccountFreeMargin() < (1000 * Lots)) {
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point * P; else StopLossLevel = 0.0;
if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point * P; else TakeProfitLevel = 0.0;
Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
if(Ticket > 0) {
if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
Print("BUY order opened : ", OrderOpenPrice());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");
} else {
Print("Error opening BUY order : ", GetLastError());
}
}
return(0);
}
}
//Sell
if (Order == SIGNAL_SELL) {
if(!IsTrade) {
//Check free margin
if (AccountFreeMargin() < (1000 * Lots)) {
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
if (UseStopLoss) StopLossLevel = Bid + StopLoss * Point * P; else StopLossLevel = 0.0;
if (UseTakeProfit) TakeProfitLevel = Bid - TakeProfit * Point * P; else TakeProfitLevel = 0.0;
Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
if(Ticket > 0) {
if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
Print("SELL order opened : ", OrderOpenPrice());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");
} else {
Print("Error opening SELL order : ", GetLastError());
}
}
return(0);
}
}
return(0);
}
//+------------------------------------------------------------------+