-
Notifications
You must be signed in to change notification settings - Fork 49
/
Sclab-NormalizeDouble.mq5
119 lines (105 loc) · 9.1 KB
/
Sclab-NormalizeDouble.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
#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 = 5;//Take Profit
double PRC;//Preço normalizado
double STL;//StopLoss normalizado
double TKP;//TakeProfit normalizado
double smaArray[];
int smaHandle;
bool posAberta;
bool ordPendente;
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(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());
}
}
}