-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.mq5
143 lines (133 loc) · 5.81 KB
/
bot.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
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
Print("Strategy initialized");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Cleanup if needed
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check for MACD confluence
bool macdConfirmation = CheckMACD();
// Detect significant bearish zones (after bullish push)
bool isBearishZone = DetectBearishZone();
// Identify Support and Resistance Zones
double supportLevel = IdentifySupportZone();
double resistanceLevel = IdentifyResistanceZone();
// If the conditions are met (bearish zone and MACD confirmation)
if (isBearishZone && macdConfirmation)
{
// Check for Fair Value Gap (FVG) and nearby resistance
if (IsFVGAndResistanceAligned(supportLevel, resistanceLevel))
{
// Set Sell Limit at supply zone
SetSellLimit(supportLevel, resistanceLevel);
}
}
}
//+------------------------------------------------------------------+
//| Function to check MACD confirmation |
//+------------------------------------------------------------------+
bool CheckMACD()
{
// MACD parameters
int macdFastPeriod = 12;
int macdSlowPeriod = 26;
int signalPeriod = 9;
// Get the latest MACD values
double macdMain = iCustom(Symbol(), PERIOD_M1, "MACD", macdFastPeriod, macdSlowPeriod, signalPeriod, MODE_MAIN, 0);
double macdSignal = iCustom(Symbol(), PERIOD_M1, "MACD", macdFastPeriod, macdSlowPeriod, signalPeriod, MODE_SIGNAL, 0);
if (macdMain == 0 || macdSignal == 0)
{
Print("Error retrieving MACD values");
return false;
}
// MACD Confluence: Blue line (fast) crossing below Orange line (slow) indicates sell momentum
return macdMain < macdSignal;
}
//+------------------------------------------------------------------+
//| Function to detect significant bearish zone after bullish push |
//+------------------------------------------------------------------+
bool DetectBearishZone()
{
double openPrev = iOpen(Symbol(), PERIOD_M1, 1);
double closePrev = iClose(Symbol(), PERIOD_M1, 1);
double openCurr = iOpen(Symbol(), PERIOD_M1, 0);
double closeCurr = iClose(Symbol(), PERIOD_M1, 0);
// Check if the previous candle is bullish and the current is bearish
return openPrev < closePrev && openCurr > closeCurr;
}
//+------------------------------------------------------------------+
//| Function to identify the support zone |
//+------------------------------------------------------------------+
double IdentifySupportZone()
{
double lowestLow = iLow(Symbol(), PERIOD_M1, 1);
for (int i = 2; i <= 5; i++) // Check the last 5 candles for the lowest low
{
double low = iLow(Symbol(), PERIOD_M1, i);
if (low < lowestLow)
lowestLow = low;
}
return lowestLow;
}
//+------------------------------------------------------------------+
//| Function to identify the resistance zone |
//+------------------------------------------------------------------+
double IdentifyResistanceZone()
{
double highestHigh = iHigh(Symbol(), PERIOD_M1, 1);
for (int i = 2; i <= 5; i++) // Check the last 5 candles for the highest high
{
double high = iHigh(Symbol(), PERIOD_M1, i);
if (high > highestHigh)
highestHigh = high;
}
return highestHigh;
}
//+------------------------------------------------------------------+
//| 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 riskAmount = 50.0; // Risk $50
double stopLoss = resistance + (resistance - support);
double takeProfit = resistance - 3 * (resistance - support);
// Calculate lot size
double lotSize = CalculateLotSize(riskAmount, stopLoss, resistance);
// Place a sell limit order
int ticket = OrderSend(Symbol(), OP_SELLLIMIT, lotSize, resistance, 3, stopLoss, takeProfit, "Sell Limit", 0, 0, clrRed);
if (ticket < 0)
Print("Error placing sell limit: ", GetLastError());
}
//+------------------------------------------------------------------+
//| Function to calculate lot size based on risk |
//+------------------------------------------------------------------+
double CalculateLotSize(double riskAmount, double stopLoss, double entryPrice)
{
double accountBalance = AccountBalance();
double pipValue = MarketInfo(Symbol(), MODE_TICKVALUE);
double riskPerPip = riskAmount / (MathAbs(stopLoss - entryPrice));
return NormalizeDouble(riskPerPip / pipValue, 2);
}