-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcruzamento_media.cpp
161 lines (119 loc) · 3.6 KB
/
cruzamento_media.cpp
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
#include <Trade\Trade.mqh>
CTrade trade;
MqlRates rates[];
input int in_takeProfitProportion = 2;
double tickSize;
input int in_lotsPercent=2;
input int VerySlow_MA_period=200;
input int Fast_MA_period=20;
input int Slow_MA_period=80;
int EA_Magic = 123459;
input double percentmin=0.005;
double Fast_Ma_Buff[];
double Slow_Ma_Buff[];
double VerySlow_Ma_Buff[];
int Fast_Ma_Handle;
int Slow_Ma_Handle;
int VerySlow_Ma_Handle;
double ask;
double bid;
double distance;
input bool VSlowMa=false;
input int LOTE=300;
input double Perda_Maxima = 0.2;
input double Kelly = 0.5;
input bool Kelly_ON =true;
int OnInit()
{
tickSize = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
Fast_Ma_Handle=iMA(_Symbol,PERIOD_CURRENT,Fast_MA_period,0,MODE_EMA, PRICE_CLOSE);
Slow_Ma_Handle=iMA(_Symbol,PERIOD_CURRENT,Slow_MA_period,0,MODE_EMA, PRICE_CLOSE);
if (VSlowMa==true){
VerySlow_Ma_Handle=iMA(_Symbol,PERIOD_CURRENT,VerySlow_MA_period,0,MODE_EMA, PRICE_CLOSE);
ArraySetAsSeries(VerySlow_Ma_Buff,true);
}
ArraySetAsSeries(Fast_Ma_Buff,true);
ArraySetAsSeries(rates,true);
ArraySetAsSeries(Slow_Ma_Buff,true);
return(INIT_SUCCEEDED);
}
void OnTick()
{
if (VSlowMa==true){
CopyBuffer(VerySlow_Ma_Handle,0,0,7,VerySlow_Ma_Buff);
}
CopyBuffer(Fast_Ma_Handle,0,0,7,Fast_Ma_Buff);
CopyBuffer(Slow_Ma_Handle,0,0,7,Slow_Ma_Buff);
CopyRates(_Symbol,PERIOD_CURRENT,0,7,rates);
ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
if(PositionsTotal()<=0 ){
if (
(Fast_Ma_Buff[1]>Slow_Ma_Buff[1])&&
(Fast_Ma_Buff[2]<Slow_Ma_Buff[2])
// && (Fast_Ma_Buff[0]>VerySlow_Ma_Buff[0])
)
{
trade.Buy(CalcLots(),NULL,0.0,0.0,0.0);
}
if (
(Fast_Ma_Buff[1]<Slow_Ma_Buff[1])&&
(Fast_Ma_Buff[2]>Slow_Ma_Buff[2])
// && (Fast_Ma_Buff[0]<VerySlow_Ma_Buff[0])
)
{
// trade.Sell(CalcLots(),NULL,0.0,0.0,0.0);
}
}
if(PositionsTotal()<=1 ){
Rastreia();
}
}
int CalcLots()
{
if (Kelly_ON ==false){
return LOTE;
}
double balance = AccountInfoDouble(ACCOUNT_EQUITY);
double CapitalKelly;
double QTD_asset;
CapitalKelly= (Kelly*balance)/Perda_Maxima;
QTD_asset= CapitalKelly/ask;
Print("CapitalKelly", CapitalKelly);
Print("ask ", ask);
Print("QTD_asset", QTD_asset);
int lot_arrendondado = MathRound(QTD_asset/100) * 100;
Print("lot_arrendondado ", lot_arrendondado);
return lot_arrendondado;
}
void Rastreia()
{
for (int i=PositionsTotal()-1;i>=0;i--)
{
string symbol=PositionGetSymbol(i);
if (symbol==Symbol())
{
ulong ticket= PositionGetInteger(POSITION_TICKET);
if (PositionGetInteger(POSITION_TYPE)== POSITION_TYPE_BUY)
{
if (
(Fast_Ma_Buff[1]<Slow_Ma_Buff[1])&&
(Fast_Ma_Buff[2]>Slow_Ma_Buff[2])
)
{
trade.PositionClose(ticket,0);
}
}
if (PositionGetInteger(POSITION_TYPE)== POSITION_TYPE_SELL)
{
if (
(Fast_Ma_Buff[1]>Slow_Ma_Buff[1])&&
(Fast_Ma_Buff[2]<Slow_Ma_Buff[2])
)
{
trade.PositionClose(ticket,0);
}
}
}
}
}