-
Notifications
You must be signed in to change notification settings - Fork 49
/
Sclab-MqlRates.mq5
82 lines (72 loc) · 6.6 KB
/
Sclab-MqlRates.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
#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 smaArray[];
int smaHandle;
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;
}
if(ultimoTick.last>smaArray[0] && rates[1].close>rates[1].open && PositionsTotal()==0)
{
//if(trade.Buy(lote, _Symbol, ask, ask-stopLoss, ask+takeProfit, ""))
if(trade.Buy(lote, _Symbol, ultimoTick.ask, ultimoTick.ask-stopLoss, ultimoTick.ask+takeProfit, ""))
{
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 && PositionsTotal()==0)
{
if(trade.Sell(lote, _Symbol, ultimoTick.bid, ultimoTick.bid+stopLoss, ultimoTick.bid-takeProfit, ""))
{
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());
}
}
}