forked from guilhermetabordaribas/MQL5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSclab-TrailingStop.mq5
231 lines (213 loc) · 19.9 KB
/
Sclab-TrailingStop.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
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
#include <Trade\Trade.mqh>
CTrade trade;
input int ma_periodo = 20;//Período da Média
input int ma_desloc = 0;//Deslocamento da Média
input ENUM_MA_METHOD ma_metodo = MODE_SMA;//Método Média Móvel
input ENUM_APPLIED_PRICE ma_preco = PRICE_CLOSE;//Preço para Média
input ulong magicNum = 123456;//Magic Number
input ulong desvPts = 50;//Desvio em Pontos
input ENUM_ORDER_TYPE_FILLING preenchimento = ORDER_FILLING_RETURN;//Preenchimento da Ordem
input double lote = 5.0;//Volume
input double stopLoss = 5;//Stop Loss
input double takeProfit = 20;//Take Profit
input double gatilhoBE = 2;//Gatilho BreakEven
input double gatilhoTS = 6;//Gatilho TrailingStop
input double stepTS = 2;//Step TrailingStop
double PRC;//Preço normalizado
double STL;//StopLoss normalizado
double TKP;//TakeProfit normalizado
double smaArray[];
int smaHandle;
bool posAberta;
bool ordPendente;
bool beAtivo;
MqlTick ultimoTick;
MqlRates rates[];
int OnInit()
{
smaHandle = iMA(_Symbol, _Period, ma_periodo, ma_desloc, ma_metodo, ma_preco);
if(smaHandle==INVALID_HANDLE)
{
Print("Erro ao criar média móvel - erro", GetLastError());
return(INIT_FAILED);
}
ArraySetAsSeries(smaArray, true);
ArraySetAsSeries(rates, true);
trade.SetTypeFilling(preenchimento);
trade.SetDeviationInPoints(desvPts);
trade.SetExpertMagicNumber(magicNum);
return(INIT_SUCCEEDED);
}
void OnTick()
{
if(!SymbolInfoTick(Symbol(),ultimoTick))
{
Alert("Erro ao obter informações de Preços: ", GetLastError());
return;
}
if(CopyRates(_Symbol, _Period, 0, 3, rates)<0)
{
Alert("Erro ao obter as informações de MqlRates: ", GetLastError());
return;
}
if(CopyBuffer(smaHandle, 0, 0, 3, smaArray)<0)
{
Alert("Erro ao copiar dados da média móvel: ", GetLastError());
return;
}
posAberta = false;
for(int i = PositionsTotal()-1; i>=0; i--)
{
string symbol = PositionGetSymbol(i);
ulong magic = PositionGetInteger(POSITION_MAGIC);
if(symbol == _Symbol && magic == magicNum)
{
posAberta = true;
break;
}
}
ordPendente = false;
for(int i = OrdersTotal()-1; i>=0; i--)
{
ulong ticket = OrderGetTicket(i);
string symbol = OrderGetString(ORDER_SYMBOL);
ulong magic = OrderGetInteger(ORDER_MAGIC);
if(symbol == _Symbol && magic == magicNum)
{
ordPendente = true;
break;
}
}
if(!posAberta)
{
beAtivo = false;
}
if(posAberta && !beAtivo)
{
BreakEven(ultimoTick.last);
}
if(posAberta && beAtivo)
{
TrailingStop(ultimoTick.last);
}
if(ultimoTick.last>smaArray[0] && rates[1].close>rates[1].open && !posAberta && !ordPendente)
{
PRC = NormalizeDouble(ultimoTick.ask, _Digits);
STL = NormalizeDouble(PRC - stopLoss, _Digits);
TKP = NormalizeDouble(PRC + takeProfit, _Digits);
if(trade.Buy(lote, _Symbol, PRC, STL, TKP, ""))
{
Print("Ordem de Compra - sem falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
}
else
{
Print("Ordem de Compra - com falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
}
}
else if(ultimoTick.last<smaArray[0] && rates[1].close<rates[1].open && !posAberta && !ordPendente)
{
PRC = NormalizeDouble(ultimoTick.bid, _Digits);
STL = NormalizeDouble(PRC + stopLoss, _Digits);
TKP = NormalizeDouble(PRC - takeProfit, _Digits);
if(trade.Sell(lote, _Symbol, PRC, STL, TKP, ""))
{
Print("Ordem de Venda - sem falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
}
else
{
Print("Ordem de Venda - com falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
}
}
}
//---
//---
void TrailingStop(double preco)
{
for(int i = PositionsTotal()-1; i>=0; i--)
{
string symbol = PositionGetSymbol(i);
ulong magic = PositionGetInteger(POSITION_MAGIC);
if(symbol == _Symbol && magic==magicNum)
{
ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
double StopLossCorrente = PositionGetDouble(POSITION_SL);
double TakeProfitCorrente = PositionGetDouble(POSITION_TP);
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
if(preco >= (StopLossCorrente + gatilhoTS) )
{
double novoSL = NormalizeDouble(StopLossCorrente + stepTS, _Digits);
if(trade.PositionModify(PositionTicket, novoSL, TakeProfitCorrente))
{
Print("TrailingStop - sem falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
}
else
{
Print("TrailingStop - com falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
}
}
}
else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
if(preco <= (StopLossCorrente - gatilhoTS) )
{
double novoSL = NormalizeDouble(StopLossCorrente - stepTS, _Digits);
if(trade.PositionModify(PositionTicket, novoSL, TakeProfitCorrente))
{
Print("TrailingStop - sem falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
}
else
{
Print("TrailingStop - com falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
}
}
}
}
}
}
//---
//---
void BreakEven(double preco)
{
for(int i = PositionsTotal()-1; i>=0; i--)
{
string symbol = PositionGetSymbol(i);
ulong magic = PositionGetInteger(POSITION_MAGIC);
if(symbol == _Symbol && magic == magicNum)
{
ulong PositionTicket = PositionGetInteger(POSITION_TICKET);
double PrecoEntrada = PositionGetDouble(POSITION_PRICE_OPEN);
double TakeProfitCorrente = PositionGetDouble(POSITION_TP);
if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
{
if( preco >= (PrecoEntrada + gatilhoBE) )
{
if(trade.PositionModify(PositionTicket, PrecoEntrada, TakeProfitCorrente))
{
Print("BreakEven - sem falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
beAtivo = true;
}
else
{
Print("BreakEven - com falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
}
}
}
else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
{
if( preco <= (PrecoEntrada - gatilhoBE) )
{
if(trade.PositionModify(PositionTicket, PrecoEntrada, TakeProfitCorrente))
{
Print("BreakEven - sem falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
beAtivo = true;
}
else
{
Print("BreakEven - com falha. ResultRetcode: ", trade.ResultRetcode(), ", RetcodeDescription: ", trade.ResultRetcodeDescription());
}
}
}
}
}
}