-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.mq5
More file actions
218 lines (184 loc) · 6.95 KB
/
example.mq5
File metadata and controls
218 lines (184 loc) · 6.95 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
//+------------------------------------------------------------------+
//| SampleEA.mq5 |
//| Copyright 2024, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
// Input parameters
input int InpMagicNumber = 12345; // Magic Number
input double InpLotSize = 0.1; // Lot Size
input int InpStopLoss = 50; // Stop Loss (points)
input int InpTakeProfit = 100; // Take Profit (points)
input ENUM_TIMEFRAMES InpTimeframe = PERIOD_H1; // Timeframe
input bool InpUseAlert = true; // Use Alert
// Global variables
int g_handle_ma_fast; // Fast MA handle
int g_handle_ma_slow; // Slow MA handle
double g_ma_fast_buffer[]; // Fast MA buffer
double g_ma_slow_buffer[]; // Slow MA buffer
bool g_initialized = false; // Initialization flag
MqlTick g_last_tick; // Last tick info
datetime g_bar_time; // Time of current bar
// Constants
#define FAST_MA_PERIOD 20
#define SLOW_MA_PERIOD 50
const color COLOR_BUY = clrGreen;
const color COLOR_SELL = clrRed;
const color COLOR_NEUTRAL = clrGray;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Initialize MA indicators
g_handle_ma_fast = iMA(_Symbol, InpTimeframe, FAST_MA_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
g_handle_ma_slow = iMA(_Symbol, InpTimeframe, SLOW_MA_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
if(g_handle_ma_fast == INVALID_HANDLE || g_handle_ma_slow == INVALID_HANDLE)
{
Print("Error creating MA indicators: ", GetLastError());
return INIT_FAILED;
}
// Set buffers as series
ArraySetAsSeries(g_ma_fast_buffer, true);
ArraySetAsSeries(g_ma_slow_buffer, true);
// Initialize g_bar_time
g_bar_time = 0;
// Set initialization flag
g_initialized = true;
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Release indicator handles
if(g_handle_ma_fast != INVALID_HANDLE)
IndicatorRelease(g_handle_ma_fast);
if(g_handle_ma_slow != INVALID_HANDLE)
IndicatorRelease(g_handle_ma_slow);
// Reset initialization flag
g_initialized = false;
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check initialization
if(!g_initialized)
return;
// Get current symbol information
if(!SymbolInfoTick(_Symbol, g_last_tick))
{
Print("Error getting last tick: ", GetLastError());
return;
}
// Check if it's a new bar
datetime bar_time = iTime(_Symbol, InpTimeframe, 0);
bool is_new_bar = (bar_time != g_bar_time);
if(is_new_bar)
{
// Update bar time
g_bar_time = bar_time;
// Process new bar logic
if(!ProcessNewBar())
return;
}
}
//+------------------------------------------------------------------+
//| Process new bar logic |
//+------------------------------------------------------------------+
bool ProcessNewBar()
{
// Copy indicator data
if(CopyBuffer(g_handle_ma_fast, 0, 0, 3, g_ma_fast_buffer) < 3)
{
Print("Error copying fast MA buffer: ", GetLastError());
return false;
}
if(CopyBuffer(g_handle_ma_slow, 0, 0, 3, g_ma_slow_buffer) < 3)
{
Print("Error copying slow MA buffer: ", GetLastError());
return false;
}
// Check for crossover/crossunder
bool signal_buy = g_ma_fast_buffer[1] > g_ma_slow_buffer[1] &&
g_ma_fast_buffer[2] <= g_ma_slow_buffer[2];
bool signal_sell = g_ma_fast_buffer[1] < g_ma_slow_buffer[1] &&
g_ma_fast_buffer[2] >= g_ma_slow_buffer[2];
// Check for open positions
if(PositionsTotal() > 0)
{
// We already have an open position
return true;
}
// Execute trades
if(signal_buy)
{
OpenBuyPosition();
if(InpUseAlert) Alert("BUY Signal: Fast MA crossed above Slow MA");
}
else if(signal_sell)
{
OpenSellPosition();
if(InpUseAlert) Alert("SELL Signal: Fast MA crossed below Slow MA");
}
return true;
}
//+------------------------------------------------------------------+
//| Open buy position |
//+------------------------------------------------------------------+
void OpenBuyPosition()
{
MqlTradeRequest request = {};
MqlTradeResult result = {};
// Prepare the request
request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = InpLotSize;
request.type = ORDER_TYPE_BUY;
request.price = g_last_tick.ask;
request.deviation = 10;
request.magic = InpMagicNumber;
// Set stop loss and take profit
if(InpStopLoss > 0)
request.sl = g_last_tick.ask - InpStopLoss * _Point;
if(InpTakeProfit > 0)
request.tp = g_last_tick.ask + InpTakeProfit * _Point;
// Send the order
if(!OrderSend(request, result))
Print("Error opening BUY position: ", GetLastError());
else
Print("BUY position opened successfully. Ticket: ", result.order);
}
//+------------------------------------------------------------------+
//| Open sell position |
//+------------------------------------------------------------------+
void OpenSellPosition()
{
MqlTradeRequest request = {};
MqlTradeResult result = {};
// Prepare the request
request.action = TRADE_ACTION_DEAL;
request.symbol = _Symbol;
request.volume = InpLotSize;
request.type = ORDER_TYPE_SELL;
request.price = g_last_tick.bid;
request.deviation = 10;
request.magic = InpMagicNumber;
// Set stop loss and take profit
if(InpStopLoss > 0)
request.sl = g_last_tick.bid + InpStopLoss * _Point;
if(InpTakeProfit > 0)
request.tp = g_last_tick.bid - InpTakeProfit * _Point;
// Send the order
if(!OrderSend(request, result))
Print("Error opening SELL position: ", GetLastError());
else
Print("SELL position opened successfully. Ticket: ", result.order);
}
//+------------------------------------------------------------------+