-
Notifications
You must be signed in to change notification settings - Fork 1
/
Simple.mq4
executable file
·304 lines (278 loc) · 9.78 KB
/
Simple.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
//+------------------------------------------------------------------+
//| Simple MA.mq4 |
//| Copyright 2015, Angus Software Corp. |
//| https://www.google.com |
//+------------------------------------------------------------------+
//Preprocessor section
#include <stdlib.mqh>
#property copyright "Copyright 2015, Angus Software Corp."
#property link "https://www.google.com"
#property version "1.00"
#property strict
//External, variables
extern double LotSize = 0.1;
extern double StopLoss = 50;
extern double TakeProfit = 100;
//extern int PendingPips = 10;
extern int Slippage = 5;
extern int MagicNumber = 123;
extern int FastMAPeriod = 10;
extern int SlowMAPeriod = 20;
//Global variables
int BuyTicket;
int SellTicket;
double UsePoint;
int UseSlippage;
int ErrorCode;
int Ticket;
//init function
int init()
{
UsePoint = PipPoint(Symbol());
Alert("UsePoint: ", UsePoint);
UseSlippage = GetSlippage(Symbol(),Slippage);
Alert("UseSlippage: ", UseSlippage);
return(0);
}
//Start function
int start()
{
double BuyStopLoss = 0;
double BuyTakeProfit = 0;
double CloseLots = 0;
double ClosePrice = 0;
bool Closed;
bool Deleted;
double OpenPrice;
double SellStopLoss;
double SellTakeProfit;
//Moving averages
if(FastMAPeriod>=SlowMAPeriod)
{
Alert("FastMAPeriod is longer than SlowMAPeriod, EA should no proceed.");
return(0);
}
double FastMA = iMA(NULL,0, FastMAPeriod,0,MODE_SMA,PRICE_CLOSE,0);
double SlowMA = iMA(NULL,0, SlowMAPeriod,0,MODE_SMA,PRICE_CLOSE,0);
//Buy order
if(FastMA > SlowMA && BuyTicket == 0)
{
Ticket = OrderSelect(SellTicket,SELECT_BY_TICKET);
if (Ticket == -1)
{
string ErrDesc;
string ErrAlert;
//string ErrLog;
ErrorCode = GetLastError();
ErrDesc = ErrorDescription(ErrorCode);
ErrAlert = StringConcatenate("OrderSelect error - Error ",ErrorCode,":",ErrDesc);
Alert(ErrAlert);
//ErrLog = StringConcatenate("Bid:",Bid," Ask",Ask," Price: ",PendingPrice," Lots:",LotSize);
//Print(ErrLog);
}
//Close existing sell position, if any
if(OrderCloseTime()==0 && SellTicket>0 && OrderType()==OP_SELL)
{
CloseLots = OrderLots();
ClosePrice = Ask;
Closed = OrderClose(SellTicket,CloseLots,ClosePrice,UseSlippage,Red);
if (Closed == false)
{
string ErrDesc;
string ErrAlert;
string ErrLog;
ErrorCode = GetLastError();
ErrDesc = ErrorDescription(ErrorCode);
ErrAlert = StringConcatenate("OrderSelect error - Error ",ErrorCode,":",ErrDesc);
Alert(ErrAlert);
ErrLog = StringConcatenate("Bid:",Bid," Ask",Ask," Price: ",ClosePrice," Lots:",CloseLots);
Print(ErrLog);
}
}
//Delete outstanding sell order, if any
else if(OrderCloseTime() ==0 && SellTicket>0 && OrderType() == OP_SELLSTOP)
{
Deleted = OrderDelete(SellTicket,Red);
if (Deleted == false)
{
string ErrDesc;
string ErrAlert;
//string ErrLog;
ErrorCode = GetLastError();
ErrDesc = ErrorDescription(ErrorCode);
ErrAlert = StringConcatenate("OrderSelect error - Error ",ErrorCode,":",ErrDesc);
Alert(ErrAlert);
//ErrLog = StringConcatenate("Bid:",Bid," Ask",Ask," Price: ",ClosePrice," Lots:",CloseLots);
//Print(ErrLog);
}
}
OpenPrice = Ask;
//Calculate stop loss and take profit
if(StopLoss > 0) BuyStopLoss = OpenPrice - (StopLoss * UsePoint);
if(TakeProfit> 0) BuyTakeProfit = OpenPrice + (TakeProfit * UsePoint);
//Open buy order
BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,OpenPrice,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber, 0,Green);
if (BuyTicket == -1)
{
string ErrDesc;
string ErrAlert;
string ErrLog;
ErrorCode = GetLastError();
ErrDesc = ErrorDescription(ErrorCode);
ErrAlert = StringConcatenate("OrderSelect error - Error ",ErrorCode,":",ErrDesc);
Alert(ErrAlert);
ErrLog = StringConcatenate("Symbol: ",Symbol()," OP_BUY: ",OP_BUY," LotSize: ",LotSize," OpenPrice: ",OpenPrice," UseSlippage: ",UseSlippage," BuyStopLoss: ",BuyStopLoss," BuyTakeProfit: ",BuyTakeProfit," Market Buy Order "," MagicNumber: ",MagicNumber);
Print(ErrLog);
}
SellTicket=0;
}
//Sell order
if(FastMA<SlowMA && SellTicket==0)
{
Ticket = OrderSelect(BuyTicket,SELECT_BY_TICKET);
if (Ticket == -1)
{
string ErrDesc;
string ErrAlert;
//string ErrLog;
ErrorCode = GetLastError();
ErrDesc = ErrorDescription(ErrorCode);
ErrAlert = StringConcatenate("OrderSelect error - Error ",ErrorCode,":",ErrDesc);
Alert(ErrAlert);
//ErrLog = StringConcatenate("Bid:",Bid," Ask",Ask," Price: ",PendingPrice," Lots:",LotSize);
//Print(ErrLog);
}
//Close existing buy position, if any
if(OrderCloseTime()==0 && BuyTicket>0 && OrderType()==OP_BUY)
{
CloseLots = OrderLots();
ClosePrice = Bid;
Closed = OrderClose(BuyTicket,CloseLots,ClosePrice,UseSlippage,Red);
if (Closed == false)
{
string ErrDesc;
string ErrAlert;
string ErrLog;
ErrorCode = GetLastError();
ErrDesc = ErrorDescription(ErrorCode);
ErrAlert = StringConcatenate("OrderSelect error - Error ",ErrorCode,":",ErrDesc);
Alert(ErrAlert);
ErrLog = StringConcatenate("Bid:",Bid," Ask",Ask," Price: ",ClosePrice," Lots:",CloseLots);
Print(ErrLog);
}
}
//Delete outstanding buy order, if any
else if(OrderCloseTime() ==0 && BuyTicket>0 && OrderType() == OP_BUYSTOP)
{
Deleted = OrderDelete(BuyTicket,Red);
if (Deleted == false)
{
string ErrDesc;
string ErrAlert;
//string ErrLog;
ErrorCode = GetLastError();
ErrDesc = ErrorDescription(ErrorCode);
ErrAlert = StringConcatenate("OrderSelect error - Error ",ErrorCode,":",ErrDesc);
Alert(ErrAlert);
//ErrLog = StringConcatenate("Bid:",Bid," Ask",Ask," Price: ",ClosePrice," Lots:",CloseLots);
//Print(ErrLog);
}
}
OpenPrice = Bid;
//Calculate stop loss and take profit
if(StopLoss > 0) SellStopLoss = OpenPrice + (StopLoss * UsePoint);
if(TakeProfit> 0) SellTakeProfit = OpenPrice - (TakeProfit * UsePoint);
//Open sell order
SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,OpenPrice,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Order",MagicNumber,0, Red);
if (SellTicket == -1)
{
string ErrDesc;
string ErrAlert;
string ErrLog;
ErrorCode = GetLastError();
ErrDesc = ErrorDescription(ErrorCode);
ErrAlert = StringConcatenate("OrderSelect error - Error ",ErrorCode,":",ErrDesc);
Alert(ErrAlert);
ErrLog = StringConcatenate("Symbol: ",Symbol()," OP_SELL: ",OP_SELL," LotSize: ",LotSize," OpenPrice: ",OpenPrice," UseSlippage: ",UseSlippage," SellStopLoss: ",SellTakeProfit," SellTakeProfit: ",BuyTakeProfit," Market Sell Order "," MagicNumber: ",MagicNumber);
Print(ErrLog);
}
BuyTicket=0;
}
return(0);
}
// Pip Point Function
double PipPoint(string Currency)
{
double CalcPoint;
int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
Alert("MarketInfo(Currency,MODE_DIGITS): ", CalcDigits);
if (CalcDigits == 1)
CalcPoint = 1;
if (CalcDigits == 2 ||CalcDigits == 3)
CalcPoint = 0.01;
else if(CalcDigits == 4 || CalcDigits ==5)
CalcPoint = 0.0001;
return (CalcPoint);
}
// GetSlippage Function
int GetSlippage(string Currency,int SlippagePips)
{
double CalcSlippage;
int CalcDigits = MarketInfo(Currency,MODE_DIGITS);
if (CalcDigits == 1)
CalcSlippage = SlippagePips *10;
if (CalcDigits == 2 ||CalcDigits == 4)
CalcSlippage = SlippagePips;
else if(CalcDigits == 3 || CalcDigits ==5)
CalcSlippage = SlippagePips *10;
return (CalcSlippage);
}
////+------------------------------------------------------------------+
////| Expert initialization function |
////+------------------------------------------------------------------+
//int OnInit()
// {
////---
//
////---
// return(INIT_SUCCEEDED);
// }
////+------------------------------------------------------------------+
////| Expert deinitialization function |
////+------------------------------------------------------------------+
//void OnDeinit(const int reason)
// {
////---
//
// }
////+------------------------------------------------------------------+
////| Expert tick function |
////+------------------------------------------------------------------+
//void OnTick()
// {
////---
//
// }
////+------------------------------------------------------------------+
////| Tester function |
////+------------------------------------------------------------------+
//double OnTester()
// {
////---
// double ret=0.0;
////---
//
////---
// return(ret);
// }
////+------------------------------------------------------------------+
////| ChartEvent function |
////+------------------------------------------------------------------+
//void OnChartEvent(const int id,
// const long &lparam,
// const double &dparam,
// const string &sparam)
// {
////---
//
// }