/
TradeManager.mqh
267 lines (211 loc) · 6.68 KB
/
TradeManager.mqh
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//
// Copyright 2018-2021, Artur Zas
// GNU General Public License v3.0 -> https://github.com/9nix6/Median-and-Turbo-Renko-indicator-bundle/blob/master/LICENSE
// https://www.az-invest.eu
// https://www.mql5.com/en/users/arturz
//
#include <AZ-INVEST/SDK/TradeFunctions.mqh>
class CTradeManagerState
{
public:
void Clear() { DoneBreakEven = false; TrailStarted = false; DonePartialClose = false; };
// Break Even
bool DoneBreakEven;
// Trailing Stop
bool TrailStarted;
// Partial Close
bool DonePartialClose;
};
struct CTradeManagerParameters
{
// Break Even
int BEPoints;
// Trailing Stop
int TrailByPoints;
int TrailStartPoints;
// Partial Close
int PartialCloseAtProfitPoints;
int PartialClosePercentage;
};
class CTradeManager
{
private:
bool initialized;
CMarketOrder *orderHandler;
CTradeManagerParameters inputs;
ENUM_ORDER_TYPE __type;
double __open;
double __lots;
string __symbol;
double __tp;
double __sl;
double __bid;
double __ask;
public:
CTradeManager();
CTradeManager(CTradeManagerParameters ¶ms, CMarketOrder *orderHalder);
~CTradeManager();
bool IsInitialized() { return this.initialized; };
bool Initialize(CTradeManagerParameters ¶ms, CMarketOrder *orderHalder);
bool Manage(ulong ticket, CTradeManagerState &_state);
string ToString();
private:
bool GetTradeInfo(ulong ticket);
bool BreakEven(ulong ticket);
bool OkToTrailTheStop(ulong ticket);
bool TrailTheStop(ulong ticket);
bool PartialClose(ulong ticket, double lots);
double GetPartialCloseLotSize();
bool IsDistanceFromOpenReached(double distancePriceDiff);
ENUM_ORDER_TYPE GetType(ulong ticket);
};
CTradeManager::CTradeManager(void)
{
this.orderHandler = NULL;
this.initialized = false;
}
CTradeManager::CTradeManager(CTradeManagerParameters ¶ms,CMarketOrder *_orderHandler)
{
this.orderHandler = NULL;
this.initialized = false;
Initialize(params, _orderHandler);
}
CTradeManager::~CTradeManager(void)
{
}
bool CTradeManager::GetTradeInfo(ulong ticket)
{
__type = GetType(ticket);
__open = PositionGetDouble(POSITION_PRICE_OPEN);
if(__open == 0)
return false;
__lots = PositionGetDouble(POSITION_VOLUME);
__symbol = PositionGetString(POSITION_SYMBOL);
__tp = PositionGetDouble(POSITION_TP);
__sl = PositionGetDouble(POSITION_SL);
__bid = SymbolInfoDouble(__symbol,SYMBOL_BID);
__ask = SymbolInfoDouble(__symbol,SYMBOL_ASK);
return true;
}
bool CTradeManager::Initialize(CTradeManagerParameters ¶ms, CMarketOrder *_orderHandler)
{
// Dependency injection
this.orderHandler = _orderHandler;
if(this.orderHandler == NULL)
{
Print(__FUNCTION__," failed on orderHandler == NULL");
return false;
}
//
this.inputs = params;
// normalize inputs
this.inputs.PartialClosePercentage = MathMin(MathAbs(this.inputs.PartialClosePercentage), 100);
//
this.initialized = true;
return initialized;
}
bool CTradeManager::Manage(ulong ticket, CTradeManagerState &_state)
{
if(!GetTradeInfo(ticket))
return false; // trade info not available
if(!_state.DonePartialClose)
{
_state.DonePartialClose = PartialClose(ticket, GetPartialCloseLotSize());
}
if(!_state.DoneBreakEven)
{
if(BreakEven(ticket))
_state.DoneBreakEven = true;
}
if(!_state.TrailStarted)
{
_state.TrailStarted = OkToTrailTheStop(ticket);
}
if(_state.TrailStarted)
{
TrailTheStop(ticket);
}
return true;
}
bool CTradeManager::PartialClose(ulong ticket, double lots)
{
if(this.inputs.PartialCloseAtProfitPoints == 0 || lots == 0)
return false; // nothing to do
double _partialCloseDistance = SymbolInfoDouble(__symbol,SYMBOL_POINT) * this.inputs.PartialCloseAtProfitPoints;
if(!IsDistanceFromOpenReached(_partialCloseDistance))
return false;
return orderHandler.ClosePartial(ticket, lots);
}
bool CTradeManager::BreakEven(ulong ticket)
{
if(this.inputs.BEPoints == 0)
return false; // nothing to do
double _beDistance = SymbolInfoDouble(__symbol,SYMBOL_POINT) * this.inputs.BEPoints;
if(!IsDistanceFromOpenReached(_beDistance))
return false;
return orderHandler.Modify(ticket,__open,__tp);
}
bool CTradeManager::OkToTrailTheStop(ulong ticket)
{
if(this.inputs.TrailByPoints == 0)
return false; // nothing to do
double _startDistance = SymbolInfoDouble(__symbol,SYMBOL_POINT) * this.inputs.TrailStartPoints;
if(!IsDistanceFromOpenReached(_startDistance))
return false;
return true;
}
bool CTradeManager::TrailTheStop(ulong ticket)
{
if(this.inputs.TrailByPoints == 0)
return false; // nothing to do
double _trailDistance = SymbolInfoDouble(__symbol,SYMBOL_POINT) * this.inputs.TrailByPoints;
double _sl = __sl;
bool okToModify = false;
if(__type == ORDER_TYPE_BUY)
{
_sl = (__bid - _trailDistance);
if(_sl > __sl)
okToModify = true;
}
else if(__type == ORDER_TYPE_SELL)
{
_sl = (__ask + _trailDistance);
if(_sl < __sl)
okToModify = true;
}
if(okToModify)
return orderHandler.Modify(ticket,_sl,__tp);
return false;
}
ENUM_ORDER_TYPE CTradeManager::GetType(ulong ticket)
{
ENUM_POSITION_TYPE _pType;
orderHandler.GetPositionType(ticket,_pType);
return orderHandler.TradeBias((ENUM_ORDER_TYPE)_pType);
}
double CTradeManager::GetPartialCloseLotSize()
{
double lotsToClose = (__lots * inputs.PartialClosePercentage) / 100;
return NormalizeLots(__symbol, lotsToClose);
}
bool CTradeManager::IsDistanceFromOpenReached(double distancePriceDiff)
{
if(__type == ORDER_TYPE_BUY)
{
if((__bid - distancePriceDiff) >= __open)
return true;
}
else if(__type == ORDER_TYPE_SELL)
{
if((__ask + distancePriceDiff) <= __open)
return true;
}
return false;
}
string CTradeManager::ToString()
{
string _be = (inputs.BEPoints > 0) ? "[BE ON] " : "[BE off] ";
string _trail = (inputs.TrailByPoints > 0) ? "[Trail ON] ": "[Trail off] ";
string _partial = (inputs.PartialCloseAtProfitPoints > 0) ? "[Partial "+(string)inputs.PartialClosePercentage+"%] ": "[Partial off] ";
return _be+_trail+_partial;
}