forked from KonzACDC/MQL5_MT5_EA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMartin for small deposits.mq5
403 lines (390 loc) · 15.8 KB
/
Martin for small deposits.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//+------------------------------------------------------------------+
//| Martin for small deposits(barabashkakvn's edition).mq5 |
//+------------------------------------------------------------------+
#property version "1.001"
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
CPositionInfo m_position; // trade position object
CTrade m_trade; // trading object
CSymbolInfo m_symbol; // symbol info object
//--- input parameters
input double InpLots = 0.01; // Volume
input ushort InpTakeProfit = 65; // Take Profit (in pips)
input ushort InpStopLoss = 50; // Stop Loss (in pips)
input ushort InpStep = 15; // Step between positions
input int stoploss = 1000;
input uchar InpBarsSkipped=45; // Number of bars to be skipped
input double InpIncreaseFactor = 1.7; // Volume increase factor
input double InpMaxLot = 6.0; // Max volume
input double InpMinProfit = 10.0; // Min profit for close all
//---
ulong m_ticket;
ulong m_magic=585631523; // magic number
ulong m_slippage=30; // slippage
double ExtTakeProfit=0.0;
double ExtStopLoss=0.0;
double ExtStep=0.0;
double m_adjusted_point; // point value adjusted for 3 or 5 points
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
if(!m_symbol.Name(Symbol())) // sets symbol name
return(INIT_FAILED);
RefreshRates();
string err_text="";
if(!CheckVolumeValue(InpLots,err_text))
{
Print(err_text);
return(INIT_PARAMETERS_INCORRECT);
}
//---
m_trade.SetExpertMagicNumber(m_magic);
//---
if(IsFillingTypeAllowed(SYMBOL_FILLING_FOK))
m_trade.SetTypeFilling(ORDER_FILLING_FOK);
else if(IsFillingTypeAllowed(SYMBOL_FILLING_IOC))
m_trade.SetTypeFilling(ORDER_FILLING_IOC);
else
m_trade.SetTypeFilling(ORDER_FILLING_RETURN);
//---
m_trade.SetDeviationInPoints(m_slippage);
//--- tuning for 3 or 5 digits
int digits_adjust=1;
if(m_symbol.Digits()==3 || m_symbol.Digits()==5)
digits_adjust=10;
m_adjusted_point=m_symbol.Point()*digits_adjust;
ExtTakeProfit=InpTakeProfit*m_adjusted_point;
ExtStopLoss = InpStopLoss * m_adjusted_point;
ExtStep=InpStep*m_adjusted_point;
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- we work only at the time of the birth of new bar
static datetime PrevBars=0;
datetime time_0=iTime(m_symbol.Name(),Period(),0);
if(time_0==PrevBars)
return;
PrevBars=time_0;
if(!RefreshRates())
{
PrevBars=iTime(m_symbol.Name(),Period(),1);
return;
}
static int counter_skipped=0;
//---
int count_buys =0; double price_lowest_buy =DBL_MAX;
int count_sells =0; double price_highest_sell =DBL_MIN;
datetime last_deal_in=D'1970.12.21 00:00'; double total_profit =0.0;
CalculateAllPositions(count_buys,price_lowest_buy,
count_sells,price_highest_sell,
last_deal_in,total_profit);
if(last_deal_in!=D'1970.12.21 00:00')
{
double array_open[];
int copied=CopyOpen(m_symbol.Name(),Period(),TimeCurrent(),last_deal_in,array_open);
if(copied>1)
counter_skipped=copied-1;
int d=0;
}
else
counter_skipped=0;
//---
bool need_to_open_a_buy=false;
bool need_to_open_a_sell=false;
if(count_buys!=0 && count_sells!=0) // error!
{
CloseAllPositions();
PrevBars=iTime(m_symbol.Name(),Period(),1);
return;
}
else if(count_buys==0 && count_sells==0)
{
double arr_close[];
ArraySetAsSeries(arr_close,true);
int copied=CopyClose(m_symbol.Name(),Period(),1,15,arr_close);
if(copied!=15)
return;
if(arr_close[0]<arr_close[14]) // open buy
need_to_open_a_buy=true;
else if(arr_close[0]>arr_close[14]) // open sell
need_to_open_a_sell=true;
}
if(counter_skipped<=InpBarsSkipped && (count_buys!=0 || count_sells!=0))
{
//--- check profit and return
if(total_profit>InpMinProfit)
{
CloseAllPositions();
return;
}
return;
}
if(count_buys==0 || count_sells==0) // check the opening of the position "sell"
{
if(count_buys==0 && count_sells>0)
{
if(m_symbol.Bid()-price_highest_sell>ExtStep)
need_to_open_a_sell=true;
}
else if(count_sells==0 && count_buys>0)
{
if(price_lowest_buy-m_symbol.Ask()>ExtStep)
need_to_open_a_buy=true;
}
}
//--- buy
if(need_to_open_a_buy)
{
double sl=(InpStopLoss==0)?0.0:m_symbol.Bid()+ExtStopLoss;
double tp=(InpTakeProfit==0)?0.0:m_symbol.Ask()+ExtTakeProfit;
double coef=MathPow(InpIncreaseFactor,(double)count_buys);
double lot=LotCheck(InpLots*coef);
if(lot!=0.0 && lot<InpMaxLot)
OpenBuy(sl,tp,lot);
}
//--- sell
if(need_to_open_a_sell)
{
double sl=(InpStopLoss==0)?0.0:m_symbol.Bid()+ExtStopLoss;
double tp=(InpTakeProfit==0)?0.0:m_symbol.Bid()-ExtTakeProfit;
double coef=MathPow(InpIncreaseFactor,(double)count_sells);
double lot=LotCheck(InpLots*coef);
if(lot!=0.0 && lot<InpMaxLot)
OpenSell(sl,tp,lot);
}
//---
}
//+------------------------------------------------------------------+
//| TradeTransaction function |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
const MqlTradeRequest &request,
const MqlTradeResult &result)
{
//---
}
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data |
//+------------------------------------------------------------------+
bool RefreshRates(void)
{
//--- refresh rates
if(!m_symbol.RefreshRates())
{
Print("RefreshRates error");
return(false);
}
//--- protection against the return value of "zero"
if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
return(false);
//---
return(true);
}
//+------------------------------------------------------------------+
//| Check the correctness of the order volume |
//+------------------------------------------------------------------+
bool CheckVolumeValue(double volume,string &error_description)
{
//--- minimal allowed volume for trade operations
double min_volume=m_symbol.LotsMin();
if(volume<min_volume)
{
error_description=StringFormat("Volume is less than the minimal allowed SYMBOL_VOLUME_MIN=%.2f",min_volume);
return(false);
}
//--- maximal allowed volume of trade operations
double max_volume=m_symbol.LotsMax();
if(volume>max_volume)
{
error_description=StringFormat("Volume is greater than the maximal allowed SYMBOL_VOLUME_MAX=%.2f",max_volume);
return(false);
}
//--- get minimal step of volume changing
double volume_step=m_symbol.LotsStep();
int ratio=(int)MathRound(volume/volume_step);
if(MathAbs(ratio*volume_step-volume)>0.0000001)
{
error_description=StringFormat("Volume is not a multiple of the minimal step SYMBOL_VOLUME_STEP=%.2f, the closest correct volume is %.2f",
volume_step,ratio*volume_step);
return(false);
}
error_description="Correct volume value";
return(true);
}
//+------------------------------------------------------------------+
//| Checks if the specified filling mode is allowed |
//+------------------------------------------------------------------+
bool IsFillingTypeAllowed(int fill_type)
{
//--- Obtain the value of the property that describes allowed filling modes
int filling=m_symbol.TradeFillFlags();
//--- Return true, if mode fill_type is allowed
return((filling & fill_type)==fill_type);
}
//+------------------------------------------------------------------+
//| Lot Check |
//+------------------------------------------------------------------+
double LotCheck(double lots)
{
//--- calculate maximum volume
double volume=NormalizeDouble(lots,2);
double stepvol=m_symbol.LotsStep();
if(stepvol>0.0)
volume=stepvol*MathFloor(volume/stepvol);
//---
double minvol=m_symbol.LotsMin();
if(volume<minvol)
volume=0.0;
//---
double maxvol=m_symbol.LotsMax();
if(volume>maxvol)
volume=maxvol;
return(volume);
}
//+------------------------------------------------------------------+
//| Calculate all positions |
//+------------------------------------------------------------------+
void CalculateAllPositions(int &count_buys,double &price_lowest_buy,
int &count_sells,double &price_highest_sell,
datetime &last_deal_in,double &total_profit)
{
count_buys =0; price_lowest_buy =DBL_MAX;
count_sells =0; price_highest_sell=DBL_MIN;
last_deal_in=D'1970.12.21 00:00'; total_profit =0.0;
for(int i=PositionsTotal()-1;i>=0;i--)
if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)
{
if(m_position.Time()>last_deal_in)
last_deal_in=m_position.Time();
total_profit+=m_position.Swap()+m_position.Commission()+m_position.Profit();
if(m_position.PositionType()==POSITION_TYPE_BUY)
{
count_buys++;
if(m_position.PriceOpen()<price_lowest_buy) // the lowest position of "BUY" is found
price_lowest_buy=m_position.PriceOpen();
continue;
}
else if(m_position.PositionType()==POSITION_TYPE_SELL)
{
count_sells++;
if(m_position.PriceOpen()>price_highest_sell) // the highest position of "SELL" is found
price_highest_sell=m_position.PriceOpen();
continue;
}
}
}
//+------------------------------------------------------------------+
//| Close all positions |
//+------------------------------------------------------------------+
void CloseAllPositions()
{
for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of current positions
if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)
m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol
}
//+------------------------------------------------------------------+
//| Open Buy position |
//+------------------------------------------------------------------+
void OpenBuy(double sl,double tp,double lot)
{
sl=m_symbol.NormalizePrice(sl);
tp=m_symbol.NormalizePrice(tp);
//--- check volume before OrderSend to avoid "not enough money" error (CTrade)
double check_volume_lot=m_trade.CheckVolume(m_symbol.Name(),lot,m_symbol.Ask(),ORDER_TYPE_BUY);
if(check_volume_lot!=0.0)
if(check_volume_lot>=lot)
{
if(m_trade.Buy(lot,NULL,m_symbol.Ask(),sl,tp))
{
if(m_trade.ResultDeal()==0)
{
Print("#1 Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
PrintResult(m_trade,m_symbol);
}
else
{
Print("#2 Buy -> true. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
PrintResult(m_trade,m_symbol);
}
}
else
{
Print("#3 Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
PrintResult(m_trade,m_symbol);
}
}
//---
}
//+------------------------------------------------------------------+
//| Open Sell position |
//+------------------------------------------------------------------+
void OpenSell(double sl,double tp,double lot)
{
sl=m_symbol.NormalizePrice(sl);
tp=m_symbol.NormalizePrice(tp);
//--- check volume before OrderSend to avoid "not enough money" error (CTrade)
double check_volume_lot=m_trade.CheckVolume(m_symbol.Name(),lot,m_symbol.Bid(),ORDER_TYPE_SELL);
if(check_volume_lot!=0.0)
if(check_volume_lot>=lot)
{
if(m_trade.Sell(lot,NULL,m_symbol.Bid(),sl,tp))
{
if(m_trade.ResultDeal()==0)
{
Print("#1 Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
PrintResult(m_trade,m_symbol);
}
else
{
Print("#2 Sell -> true. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
PrintResult(m_trade,m_symbol);
}
}
else
{
Print("#3 Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
PrintResult(m_trade,m_symbol);
}
}
//---
}
//+------------------------------------------------------------------+
//| Print CTrade result |
//+------------------------------------------------------------------+
void PrintResult(CTrade &trade,CSymbolInfo &symbol)
{
Print("Code of request result: "+IntegerToString(trade.ResultRetcode()));
Print("code of request result: "+trade.ResultRetcodeDescription());
Print("deal ticket: "+IntegerToString(trade.ResultDeal()));
Print("order ticket: "+IntegerToString(trade.ResultOrder()));
Print("volume of deal or order: "+DoubleToString(trade.ResultVolume(),2));
Print("price, confirmed by broker: "+DoubleToString(trade.ResultPrice(),symbol.Digits()));
Print("current bid price: "+DoubleToString(trade.ResultBid(),symbol.Digits()));
Print("current ask price: "+DoubleToString(trade.ResultAsk(),symbol.Digits()));
Print("broker comment: "+trade.ResultComment());
DebugBreak();
}
//+------------------------------------------------------------------+