-
Notifications
You must be signed in to change notification settings - Fork 3
/
TradeHelper.mqh
279 lines (232 loc) · 11.2 KB
/
TradeHelper.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
268
269
270
271
272
273
274
275
276
277
278
279
//+------------------------------------------------------------------+
/*
TradeHelper.mqh
Copyright (C) 2021 Bheki Gabela
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//+------------------------------------------------------------------+
#property strict
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\AccountInfo.mqh>
CAccountInfo m_account;
CTrade m_trade;
ulong baseMagic = 11223300000;//First six digits represent ea base, last three represent minutes in timeframe
ulong m_slippage=5;
const int LOG_NONE = 0;
const int LOG_ERROR = 1;
const int LOG_WARN = 2;
const int LOG_INFO = 3;
const int LOG_DEBUG = 4;
//+------------------------------------------------------------------+
//| Returns magic number which includes timeframe info |
//+------------------------------------------------------------------+
ulong getMagicWithTimeframe()
{
long tfMinutes = PeriodSeconds(PERIOD_CURRENT) / 60;
long withTimeframe = baseMagic + tfMinutes;
printHelper(LOG_DEBUG, StringFormat("Base magic %I64u becomes %I64u with timeframe",baseMagic,withTimeframe));
return withTimeframe;
}
//+------------------------------------------------------------------+
//| Returns given magic number with timeframe info removed |
//+------------------------------------------------------------------+
ulong getMagicWithoutTimeframe(ulong magic)
{
long withoutTimeframe = MathFloor(magic / 10000) * 10000;
printHelper(LOG_DEBUG, StringFormat("Magic %I64u becomes %I64u without timeframe",magic,withoutTimeframe));
return withoutTimeframe;
}
//+------------------------------------------------------------------+
//| Adjusts the point size according to symbol digits |
//+------------------------------------------------------------------+
double getAdjustedPoint()
{
//--- tuning for 3 or 5 digits
int digits_adjust=1;
if(SymbolInfoInteger(_Symbol,SYMBOL_DIGITS) == 3 || SymbolInfoInteger(_Symbol,SYMBOL_DIGITS) == 5)
digits_adjust=10;
double m_adjusted_point = SymbolInfoDouble(_Symbol,SYMBOL_POINT) * digits_adjust;
printHelper(LOG_DEBUG, StringFormat("_Point value is %f",m_adjusted_point));
printHelper(LOG_DEBUG, StringFormat("_Period value is %d",_Period));
return m_adjusted_point;
}
//+------------------------------------------------------------------+
//| Normalizes the volume so that it aligns to lot step size |
//+------------------------------------------------------------------+
double getNormalizedVolume(ENUM_ORDER_TYPE orderType, double price, double fixedAmount)
{
//--- Get margin for one stepvol
double stepvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
double lot = stepvol;
double stepMargin = m_account.MarginCheck( _Symbol,orderType,stepvol,price);
if(stepMargin > 0.00) {
lot = stepvol*(fixedAmount/stepMargin);
}
lot=stepvol*NormalizeDouble(lot/stepvol,0);
double minvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);
if(lot<minvol)
lot=minvol;
double maxvol=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX);
if(lot>maxvol)
lot=maxvol;
return lot;
}
//+------------------------------------------------------------------+
//| Places a buy order |
//+------------------------------------------------------------------+
void placeBuyOrder(CTrade &m_trade, double sl, double tp, double fixedAmount, string commentPrefix)
{
double price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
sl = NormalizeDouble(sl,SymbolInfoInteger(_Symbol,SYMBOL_DIGITS));
int stops_level = (int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL);
if(stops_level != 0)
{
double bidPrice = SymbolInfoDouble(_Symbol,SYMBOL_BID);
bool slOk = (sl == 0) || (bidPrice-sl > stops_level*_Point);
bool tpOk = (tp == 0) || (tp-bidPrice > stops_level*_Point);
if(!(slOk && tpOk))
{
printHelper(LOG_ERROR, StringFormat("SYMBOL_TRADE_STOPS_LEVEL=%d: StopLoss and TakeProfit must not be nearer than %d points from the bid price when buying",stops_level,stops_level));
return;
}
}
double volume = getNormalizedVolume(ORDER_TYPE_BUY, price, fixedAmount);
//---BEGIN Calc margin
double margin = 0.00;
ENUM_ORDER_TYPE orderType = ORDER_TYPE_BUY;
OrderCalcMargin(orderType, _Symbol,volume,price,margin);
//---END Calc margin
printHelper(LOG_INFO, StringFormat("About to place buy order of volume %f and amount %f ", volume, margin));
string comment = commentPrefix + AccountInfoString(ACCOUNT_CURRENCY) + DoubleToString(margin, 2) + " on timeframe " + _Period;
m_trade.Buy(volume,_Symbol,price,sl,tp,comment);
}
//+------------------------------------------------------------------+
//| Places a buy limit |
//+------------------------------------------------------------------+
void placeBuyLimit(CTrade &m_trade, double price, double sl, double tp, double fixedAmount, string commentPrefix)
{
sl = NormalizeDouble(sl,SymbolInfoInteger(_Symbol,SYMBOL_DIGITS));
double volume = getNormalizedVolume(ORDER_TYPE_BUY, price, fixedAmount);
//---BEGIN Calc margin
double margin = 0.00;
ENUM_ORDER_TYPE orderType = ORDER_TYPE_BUY;
OrderCalcMargin(orderType, _Symbol,volume,price,margin);
//---END Calc margin
printHelper(LOG_INFO, StringFormat("About to place buy limit of volume %f and amount %f ", volume, margin));
string comment = commentPrefix + AccountInfoString(ACCOUNT_CURRENCY) + DoubleToString(margin, 2) + " L on timeframe " + _Period;
m_trade.BuyLimit(volume,price,_Symbol,sl,tp,0,0,comment);
}
//+------------------------------------------------------------------+
//| Places a sell order |
//+------------------------------------------------------------------+
void placeSellOrder(CTrade &m_trade, double sl, double tp, double fixedAmount, string commentPrefix)
{
double price = SymbolInfoDouble(_Symbol,SYMBOL_BID);
sl = NormalizeDouble(sl,SymbolInfoInteger(_Symbol,SYMBOL_DIGITS));
int stops_level = (int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL);
if(stops_level != 0)
{
double askPrice = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
bool slOk = (sl == 0) || (sl-askPrice > stops_level*_Point);
bool tpOk = (tp == 0) || (askPrice-tp > stops_level*_Point);
if(!(slOk && tpOk))
{
printHelper(LOG_ERROR, StringFormat("SYMBOL_TRADE_STOPS_LEVEL=%d: StopLoss and TakeProfit must not be nearer than %d points from the ask price when selling",stops_level,stops_level));
return;
}
}
double volume = getNormalizedVolume(ORDER_TYPE_SELL, price, fixedAmount);
//---BEGIN Calc margin
double margin = 0.00;
ENUM_ORDER_TYPE orderType = ORDER_TYPE_SELL;
OrderCalcMargin(orderType, _Symbol,volume,price,margin);
//---END Calc margin
printHelper(LOG_INFO, StringFormat("About to place sell order of volume %f and amount %f", volume, margin));
string comment = commentPrefix + AccountInfoString(ACCOUNT_CURRENCY) + DoubleToString(margin, 2) + " on timeframe " + _Period;
m_trade.Sell(volume,_Symbol,price,sl,tp,comment);
}
//+------------------------------------------------------------------+
//| Places a sell limit |
//+------------------------------------------------------------------+
void placeSellLimit(CTrade &m_trade, double price, double sl, double tp, double fixedAmount, string commentPrefix)
{
sl = NormalizeDouble(sl,SymbolInfoInteger(_Symbol,SYMBOL_DIGITS));
double volume = getNormalizedVolume(ORDER_TYPE_SELL, price, fixedAmount);
//---BEGIN Calc margin
double margin = 0.00;
ENUM_ORDER_TYPE orderType = ORDER_TYPE_SELL;
OrderCalcMargin(orderType, _Symbol,volume,price,margin);
//---END Calc margin
printHelper(LOG_INFO, StringFormat("About to place sell limit of volume %f and amount %f", volume, margin));
string comment = commentPrefix + AccountInfoString(ACCOUNT_CURRENCY) + DoubleToString(margin, 2) + " L on timeframe " + _Period;
m_trade.SellLimit(volume,price,_Symbol,sl,tp,0,0,comment);
}
//+------------------------------------------------------------------+
//| Checks whether the index represents a green/bull bar |
//+------------------------------------------------------------------+
bool isGreen(int index)
{
double open = iOpen(NULL,0,index);
double close = iClose(NULL,0,index);
return open < close;
}
//+------------------------------------------------------------------+
//| Checks whether the index represents a red/bear bar |
//+------------------------------------------------------------------+
bool isRed(int index)
{
double open = iOpen(NULL,0,index);
double close = iClose(NULL,0,index);
return open > close;
}
//+------------------------------------------------------------------+
//| Locates the most recent green bar in the series |
//+------------------------------------------------------------------+
int findLastGreenBar(int maxBars)
{
for(int i = 1; i <= maxBars; i++) //surely we will find our bar within maxBars?
{
if(isGreen(i))
{
return i;
}
}
return -1;
}
//+------------------------------------------------------------------+
//| Locates the most recent red bar in the series |
//+------------------------------------------------------------------+
int findLastRedBar(int maxBars)
{
for(int i = 1; i <= maxBars; i++) //surely we will find our bar within MaxBars?
{
if(isRed(i))
{
return i;
}
}
return -1;
}
//+------------------------------------------------------------------+
//| Prints a formatted string according to log level set |
//+------------------------------------------------------------------+
void printHelper(int level, string formattedText)
{
int logLevel = GlobalVariableGet("LOG_LEVEL");
if(level <= logLevel)
{
Print(formattedText);
}
}
//+------------------------------------------------------------------+