forked from LucasMarianoVieira/MT5-MA-Crossover-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacrossoverbot.mq5
197 lines (155 loc) · 4.09 KB
/
macrossoverbot.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
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
#property copyright "Lucas Mariano Vieira"
#property link "https://www.mql5.com"
#property version "1.00"
input int MAPeriodShort=9;
input int MAPeriodLong=21;
input int MAShift=0;
input ENUM_MA_METHOD MAMethodS= MODE_SMA;
input ENUM_MA_METHOD MAMethodL= MODE_SMA;
input ENUM_APPLIED_PRICE MAPrice= PRICE_CLOSE;
input double stopLoss=0.1;
input double takeProfit=0.3;
input int volume=100;
enum orderType{
orderBuy,
orderSell
};
datetime candleTimes[],lastCandleTime;
MqlTradeRequest request;
MqlTradeResult result;
MqlTradeCheckResult checkResult;
bool checkNewCandle(datetime &candles[],datetime &last){
bool newCandle=false;
CopyTime(_Symbol,_Period,0,3,candles);
if(last!=0){
if(candles[0]>last){
newCandle=true;
last=candles[0];
}
}else{
last=candles[0];
}
return newCandle;
}
bool closePosition(){
double vol=0;
long type=WRONG_VALUE;
long posID=0;
ZeroMemory(request);
if(PositionSelect(_Symbol)){
vol=PositionGetDouble(POSITION_VOLUME);
type=PositionGetInteger(POSITION_TYPE);
posID=PositionGetInteger(POSITION_IDENTIFIER);
request.sl=PositionGetDouble(POSITION_SL);
request.tp=PositionGetDouble(POSITION_TP);
}else{
return false;
}
request.symbol=_Symbol;
request.volume=vol;
request.action=TRADE_ACTION_DEAL;
request.type_filling=ORDER_FILLING_FOK;
request.deviation=10;
double price=0;
if(type==POSITION_TYPE_BUY){
//Buy
request.type=ORDER_TYPE_BUY;
price=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
}else if(POSITION_TYPE_SELL){
//Sell
request.type=ORDER_TYPE_SELL;
price=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
}
request.price=price;
if(OrderCheck(request,checkResult)){
Print("Checked!");
}else{
Print("Not correct! ERROR :"+IntegerToString(checkResult.retcode));
return false;
}
if(OrderSend(request,result)){
Print("Successful send!");
}else{
Print("Error order not send!");
return false;
}
if(result.retcode==TRADE_RETCODE_DONE || result.retcode==TRADE_RETCODE_PLACED){
Print("Trade Placed!");
return true;
}else{
return false;
}
}
bool makePosition(orderType type){
ZeroMemory(request);
request.symbol=_Symbol;
request.volume=volume;
request.action=TRADE_ACTION_DEAL;
request.type_filling=ORDER_FILLING_FOK;
double price=0;
if(type==orderBuy){
//Buy
request.type=ORDER_TYPE_BUY;
price=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
request.sl=NormalizeDouble(price-stopLoss,_Digits);
request.tp=NormalizeDouble(price+takeProfit,_Digits);
}else if(type==orderSell){
//Sell
request.type=ORDER_TYPE_SELL;
price=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
request.sl=NormalizeDouble(price+stopLoss,_Digits);
request.tp=NormalizeDouble(price-takeProfit,_Digits);
}
request.deviation=10;
request.price=price;
if(OrderCheck(request,checkResult)){
Print("Checked!");
}else{
Print("Not Checked! ERROR :"+IntegerToString(checkResult.retcode));
return false;
}
if(OrderSend(request,result)){
Print("Ordem enviada com sucesso!");
}else{
Print("Ordem não enviada!");
return false;
}
if(result.retcode==TRADE_RETCODE_DONE || result.retcode==TRADE_RETCODE_PLACED){
Print("Trade Placed!");
return true;
}else{
return false;
}
}
int OnInit(){
ArraySetAsSeries(candleTimes,true);
return(0);
}
void OnTick(){
if(checkNewCandle(candleTimes,lastCandleTime)){
double maS[];
double maL[];
ArraySetAsSeries(maS,true);
ArraySetAsSeries(maL,true);
double candleClose[];
ArraySetAsSeries(candleClose,true);
int maSHandle= iMA(_Symbol,_Period,MAPeriodShort,MAShift,MAMethodS,MAPrice);
int maLHandle= iMA(_Symbol,_Period,MAPeriodLong,MAShift,MAMethodL,MAPrice);
CopyBuffer(maSHandle,0,0,3,maS);
CopyBuffer(maLHandle,0,0,3,maL);
CopyClose(_Symbol,_Period,0,3,candleClose);
if((maS[1] < maL[1])&&(maS[0]>maL[0])){
//cross up
Print("Cross above!");
closePosition();
makePosition(orderBuy);
}else if((maS[1]>maL[1])&&(maS[0]<maL[0])){
//cross down
Print("Cross under!");
closePosition();
makePosition(orderSell);
}else{
//trailing
}
}
}