-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbot.mq5
More file actions
471 lines (412 loc) · 16.1 KB
/
bot.mq5
File metadata and controls
471 lines (412 loc) · 16.1 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//+------------------------------------------------------------------+
//| Input Parameters |
//+------------------------------------------------------------------+
input double RiskAmount = 50.0; // Risk amount per trade in account currency
input double MaxDailyLoss = 200.0; // Maximum daily loss limit
input int LookbackPeriod = 20; // Candles to analyze for S/R zones
input double MinRiskReward = 2.0; // Minimum risk:reward ratio
input int MagicNumber = 234567; // Unique EA identifier
input bool EnableLogging = true; // Enable detailed logging
//+------------------------------------------------------------------+
//| Global Variables |
//+------------------------------------------------------------------+
double dailyStartBalance;
bool tradingEnabled = true;
int macdHandle;
datetime lastTradeTime = 0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Initialize MACD indicator handle
macdHandle = iMACD(Symbol(), PERIOD_M1, 12, 26, 9, PRICE_CLOSE);
if(macdHandle == INVALID_HANDLE)
{
Print("Failed to create MACD indicator handle");
return(INIT_FAILED);
}
// Store daily start balance
dailyStartBalance = AccountInfoDouble(ACCOUNT_BALANCE);
// Validate inputs
if(RiskAmount <= 0 || MaxDailyLoss <= 0 || MinRiskReward <= 1.0)
{
Print("Invalid input parameters");
return(INIT_FAILED);
}
Print("EA Trader initialized successfully");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(macdHandle != INVALID_HANDLE)
IndicatorRelease(macdHandle);
Print("EA Trader deinitialized. Reason: ", reason);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check daily loss limit
if(!CheckDailyLossLimit())
{
tradingEnabled = false;
return;
}
// Check if trading is enabled
if(!tradingEnabled)
return;
// Prevent multiple trades in same minute
if(TimeCurrent() - lastTradeTime < 60)
return;
// Check for existing positions to avoid duplicates
if(PositionsTotal() > 0)
return;
// Check spread
if(!IsSpreadAcceptable())
return;
// Check for MACD confluence
bool macdConfirmation = CheckMACD();
if(!macdConfirmation)
return;
// Detect significant bearish zones (after bullish push)
bool isBearishZone = DetectBearishZone();
if(!isBearishZone)
return;
// Identify Support and Resistance Zones
double supportLevel = IdentifySupportZone();
double resistanceLevel = IdentifyResistanceZone();
// Validate support and resistance levels
if(supportLevel <= 0 || resistanceLevel <= 0 || supportLevel >= resistanceLevel)
{
if(EnableLogging)
Print("Invalid support/resistance levels: Support=", supportLevel, ", Resistance=", resistanceLevel);
return;
}
// Check for Fair Value Gap (FVG) and nearby resistance
if (IsFVGAndResistanceAligned(supportLevel, resistanceLevel))
{
// Set Sell Limit at supply zone
SetSellLimit(supportLevel, resistanceLevel);
}
// Manage existing positions
ManagePositions();
}
//+------------------------------------------------------------------+
//| Function to check MACD confirmation |
//+------------------------------------------------------------------+
bool CheckMACD()
{
double macdMain[], macdSignal[];
// Copy MACD values
if(CopyBuffer(macdHandle, 0, 0, 3, macdMain) < 0 ||
CopyBuffer(macdHandle, 1, 0, 3, macdSignal) < 0)
{
if(EnableLogging)
Print("Error retrieving MACD values: ", GetLastError());
return false;
}
// Check for valid values
if(ArraySize(macdMain) < 3 || ArraySize(macdSignal) < 3)
{
if(EnableLogging)
Print("Insufficient MACD data");
return false;
}
// MACD Confluence: Main line crossing below Signal line indicates sell momentum
// Also check for momentum confirmation (current < previous)
bool crossBelow = macdMain[0] < macdSignal[0] && macdMain[1] >= macdSignal[1];
bool bearishMomentum = macdMain[0] < macdMain[1];
return crossBelow && bearishMomentum;
}
//+------------------------------------------------------------------+
//| Function to detect significant bearish zone after bullish push |
//+------------------------------------------------------------------+
bool DetectBearishZone()
{
double open[], close[], high[], low[];
// Get more candles for better analysis
if(CopyOpen(Symbol(), PERIOD_M1, 0, 10, open) < 0 ||
CopyClose(Symbol(), PERIOD_M1, 0, 10, close) < 0 ||
CopyHigh(Symbol(), PERIOD_M1, 0, 10, high) < 0 ||
CopyLow(Symbol(), PERIOD_M1, 0, 10, low) < 0)
{
if(EnableLogging)
Print("Error getting price data for bearish zone detection");
return false;
}
// Check for bullish momentum followed by bearish reversal
int bullishCount = 0;
for(int i = 5; i < 9; i++) // Check last 4 candles for bullish push
{
if(close[i] > open[i])
bullishCount++;
}
// Require at least 3 bullish candles in the push
bool hadBullishPush = bullishCount >= 3;
// Check current candle is bearish with significant body
bool currentBearish = close[0] < open[0];
double currentBodySize = MathAbs(close[0] - open[0]);
double avgBodySize = 0;
for(int i = 1; i <= 5; i++)
avgBodySize += MathAbs(close[i] - open[i]);
avgBodySize /= 5;
bool significantBearishCandle = currentBodySize > avgBodySize * 1.5;
return hadBullishPush && currentBearish && significantBearishCandle;
}
//+------------------------------------------------------------------+
//| Function to identify the support zone using fractal analysis |
//+------------------------------------------------------------------+
double IdentifySupportZone()
{
double low[];
if(CopyLow(Symbol(), PERIOD_M1, 0, LookbackPeriod + 5, low) < 0)
{
if(EnableLogging)
Print("Error getting low prices for support zone");
return 0;
}
double supportLevel = 0;
int touchCount = 0;
// Find fractal lows (swing lows)
for(int i = 2; i < ArraySize(low) - 2; i++)
{
if(low[i] <= low[i-1] && low[i] <= low[i-2] &&
low[i] <= low[i+1] && low[i] <= low[i+2])
{
// Check how many times price tested this level
int currentTouches = 0;
for(int j = 0; j < ArraySize(low); j++)
{
if(MathAbs(low[j] - low[i]) <= Point() * 5) // Within 5 pips
currentTouches++;
}
if(currentTouches > touchCount)
{
touchCount = currentTouches;
supportLevel = low[i];
}
}
}
return supportLevel;
}
//+------------------------------------------------------------------+
//| Function to identify the resistance zone using fractal analysis |
//+------------------------------------------------------------------+
double IdentifyResistanceZone()
{
double high[];
if(CopyHigh(Symbol(), PERIOD_M1, 0, LookbackPeriod + 5, high) < 0)
{
if(EnableLogging)
Print("Error getting high prices for resistance zone");
return 0;
}
double resistanceLevel = 0;
int touchCount = 0;
// Find fractal highs (swing highs)
for(int i = 2; i < ArraySize(high) - 2; i++)
{
if(high[i] >= high[i-1] && high[i] >= high[i-2] &&
high[i] >= high[i+1] && high[i] >= high[i+2])
{
// Check how many times price tested this level
int currentTouches = 0;
for(int j = 0; j < ArraySize(high); j++)
{
if(MathAbs(high[j] - high[i]) <= Point() * 5) // Within 5 pips
currentTouches++;
}
if(currentTouches > touchCount)
{
touchCount = currentTouches;
resistanceLevel = high[i];
}
}
}
return resistanceLevel;
}
//+------------------------------------------------------------------+
//| Function to check if FVG and resistance are aligned |
//+------------------------------------------------------------------+
bool IsFVGAndResistanceAligned(double support, double resistance)
{
double fvgStart = iLow(Symbol(), PERIOD_M1, 2);
double fvgEnd = iHigh(Symbol(), PERIOD_M1, 0);
// Check FVG is within the resistance range
return (fvgStart < resistance && fvgEnd > resistance);
}
//+------------------------------------------------------------------+
//| Function to set a sell limit order at the supply zone |
//+------------------------------------------------------------------+
void SetSellLimit(double support, double resistance)
{
double entryPrice = resistance;
double stopLoss = resistance + (resistance - support) * 0.5; // More conservative SL
double riskRewardDistance = entryPrice - support;
double takeProfit = entryPrice - (riskRewardDistance * MinRiskReward);
// Validate levels
if(stopLoss <= entryPrice || takeProfit >= entryPrice)
{
if(EnableLogging)
Print("Invalid SL/TP levels. Entry: ", entryPrice, ", SL: ", stopLoss, ", TP: ", takeProfit);
return;
}
// Calculate lot size with proper validation
double lotSize = CalculateLotSize(RiskAmount, stopLoss, entryPrice);
if(lotSize <= 0)
{
if(EnableLogging)
Print("Invalid lot size calculated: ", lotSize);
return;
}
// Create trade request
MqlTradeRequest request = {};
MqlTradeResult result = {};
request.action = TRADE_ACTION_PENDING;
request.symbol = Symbol();
request.volume = lotSize;
request.type = ORDER_TYPE_SELL_LIMIT;
request.price = entryPrice;
request.sl = stopLoss;
request.tp = takeProfit;
request.magic = MagicNumber;
request.comment = "EA Sell Limit";
request.type_filling = ORDER_FILLING_IOC;
// Send order
if(!OrderSend(request, result))
{
if(EnableLogging)
Print("Order failed. Error: ", GetLastError(), ", Retcode: ", result.retcode);
}
else
{
lastTradeTime = TimeCurrent();
if(EnableLogging)
Print("Sell limit order placed. Ticket: ", result.order, ", Entry: ", entryPrice, ", SL: ", stopLoss, ", TP: ", takeProfit);
}
}
//+------------------------------------------------------------------+
//| Function to calculate lot size based on risk with validation |
//+------------------------------------------------------------------+
double CalculateLotSize(double riskAmount, double stopLoss, double entryPrice)
{
if(riskAmount <= 0 || stopLoss <= 0 || entryPrice <= 0)
{
if(EnableLogging)
Print("Invalid parameters for lot size calculation");
return 0;
}
double riskDistance = MathAbs(stopLoss - entryPrice);
if(riskDistance <= 0)
{
if(EnableLogging)
Print("Invalid risk distance: ", riskDistance);
return 0;
}
// Get symbol information
double tickValue = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_VALUE);
double tickSize = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_SIZE);
double minLot = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MIN);
double maxLot = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MAX);
double lotStep = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_STEP);
if(tickValue <= 0 || tickSize <= 0)
{
if(EnableLogging)
Print("Invalid symbol tick information");
return 0;
}
// Calculate lot size
double riskInTicks = riskDistance / tickSize;
double lotSize = riskAmount / (riskInTicks * tickValue);
// Normalize to lot step
lotSize = MathFloor(lotSize / lotStep) * lotStep;
// Apply limits
if(lotSize < minLot)
lotSize = minLot;
if(lotSize > maxLot)
lotSize = maxLot;
// Final validation
double accountEquity = AccountInfoDouble(ACCOUNT_EQUITY);
double maxRiskPercent = 0.05; // 5% max risk per trade
double maxAllowedLot = (accountEquity * maxRiskPercent) / (riskInTicks * tickValue);
if(lotSize > maxAllowedLot)
{
if(EnableLogging)
Print("Lot size exceeds 5% account risk. Adjusted from ", lotSize, " to ", maxAllowedLot);
lotSize = maxAllowedLot;
}
return NormalizeDouble(lotSize, 2);
}
//+------------------------------------------------------------------+
//| Function to check daily loss limit |
//+------------------------------------------------------------------+
bool CheckDailyLossLimit()
{
double currentBalance = AccountInfoDouble(ACCOUNT_BALANCE);
double dailyLoss = dailyStartBalance - currentBalance;
if(dailyLoss >= MaxDailyLoss)
{
if(EnableLogging)
Print("Daily loss limit reached: ", dailyLoss, " >= ", MaxDailyLoss);
return false;
}
return true;
}
//+------------------------------------------------------------------+
//| Function to check if spread is acceptable |
//+------------------------------------------------------------------+
bool IsSpreadAcceptable()
{
double spread = SymbolInfoInteger(Symbol(), SYMBOL_SPREAD) * Point();
double maxSpread = 0.0003; // 3 pips max for EURUSD/GBPUSD
if(spread > maxSpread)
{
if(EnableLogging)
Print("Spread too high: ", spread, " > ", maxSpread);
return false;
}
return true;
}
//+------------------------------------------------------------------+
//| Function to manage existing positions (move SL to breakeven) |
//+------------------------------------------------------------------+
void ManagePositions()
{
for(int i = 0; i < PositionsTotal(); i++)
{
if(PositionSelectByTicket(PositionGetTicket(i)))
{
if(PositionGetString(POSITION_SYMBOL) == Symbol() &&
PositionGetInteger(POSITION_MAGIC) == MagicNumber)
{
double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
double currentPrice = PositionGetDouble(POSITION_PRICE_CURRENT);
double stopLoss = PositionGetDouble(POSITION_SL);
double takeProfit = PositionGetDouble(POSITION_TP);
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
double riskDistance = openPrice - stopLoss;
double currentProfit = openPrice - currentPrice;
// Move SL to breakeven when 1:1 RR is reached
if(currentProfit >= riskDistance && stopLoss != openPrice)
{
MqlTradeRequest request = {};
MqlTradeResult result = {};
request.action = TRADE_ACTION_SLTP;
request.symbol = Symbol();
request.sl = openPrice;
request.tp = takeProfit;
request.position = PositionGetTicket(i);
if(OrderSend(request, result) && EnableLogging)
Print("Stop loss moved to breakeven for position: ", PositionGetTicket(i));
}
}
}
}
}
}