-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOne_Shot_Charaba.mq5
400 lines (387 loc) · 15.6 KB
/
One_Shot_Charaba.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
//+------------------------------------------------------------------+
//| Buggy_Player_Charaba.mq5 |
//| Copyright 2020, Rodrigo Charaba. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Rodrigo Charaba."
#property link "https://www.mql5.com"
#property version "1.00"
#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
//--- enums
enum eAllowedTrades
{
BUY, // BUY Only
SELL, // SELL Only
};
//--- input parameters
input int Lote = 5;
input ushort InpStopLoss = 300; // Stop Loss (in pips)
input ushort InpTakeProfit = 100; // Take Profit (in pips)
input double InpProfit =1000; // Close all if Profit >=
input int Hour_Aperture = 10;
input int Minute_Aperture = 2;
input int Consecutive_Loss = 5;
input eAllowedTrades Buy_Sell =SELL;
input int Consecutive_Loss_Revert = 4;
input double Revert_Rate = 3;
//---
ulong m_magic=15489; // magic number
ulong m_slippage=5; // slippage
double ExtLot=0.0;
double ExtStopLoss=0.0;
double ExtTakeProfit=0.0;
double m_adjusted_point; // point value adjusted for 3 or 5 points
int StopCount=0;
bool m_close_all=false;
MqlRates candle[];
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
m_symbol.Name(Symbol()); // sets symbol name
RefreshRates();
m_symbol.Refresh();
ArraySetAsSeries(candle,true);
//---
m_trade.SetExpertMagicNumber(m_magic);
//---
if(IsFillingTypeAllowed(Symbol(),SYMBOL_FILLING_FOK))
m_trade.SetTypeFilling(ORDER_FILLING_FOK);
else if(IsFillingTypeAllowed(Symbol(),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;
ExtLot = Lote;
ExtStopLoss = InpStopLoss * m_adjusted_point;
ExtTakeProfit = InpTakeProfit * m_adjusted_point;
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
CopyRates(_Symbol,_Period,0,5,candle);
bool newBar = isNewBar();
//---
if(ProfitAllPositions()>=InpProfit)
m_close_all=true;
//---
if(m_close_all)
{
if(IsPositionExists())
CloseAllPositions();
else
m_close_all=false;
}
//---
if(CalculateAllPositions()==0)
{
if(!RefreshRates())
return;
int total, Hour_Int, Minute_Int, Sec_Int;
MqlDateTime str1;
TimeToStruct(TimeCurrent(),str1);
total = OrdersTotal();
Hour_Int = str1.hour;
Minute_Int = str1.min;
Sec_Int = str1.sec;
if(Hour_Int == Hour_Aperture && Minute_Int == Minute_Aperture && Sec_Int == 00 && newBar && StopCount<=Consecutive_Loss)
{Print("Time Condition 01 is satisfied");
if(total<1)
{Print("There are no orders open");
if(Buy_Sell==BUY)
{
ClosePositions(POSITION_TYPE_SELL);
Print("Open Buy order");
double sl=(InpStopLoss==0)?0.0:m_symbol.Ask()-ExtStopLoss;
double tp=(InpTakeProfit==0)?0.0:m_symbol.Ask()+ExtTakeProfit;
OpenBuy(ExtLot,sl,tp);
}
else if(Buy_Sell==SELL)
{
ClosePositions(POSITION_TYPE_BUY);
Print("Open Sell order");
double sl=(InpStopLoss==0)?0.0:m_symbol.Bid()+ExtStopLoss;
double tp=(InpTakeProfit==0)?0.0:m_symbol.Bid()-ExtTakeProfit;
OpenSell(ExtLot,sl,tp);
}
}
}
}
//---
}
//+------------------------------------------------------------------+
//| Returns true if a new bar has appeared for a symbol/period pair |
//+------------------------------------------------------------------+
bool isNewBar()
{
//--- memorize the time of opening of the last bar in the static variable
static datetime last_time=0;
//--- current time
datetime lastbar_time=(datetime)SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);
//--- if it is the first call of the function
if(last_time==0)
{
//--- set the time and exit
last_time=lastbar_time;
return(false);
}
//--- if the time differs
if(last_time!=lastbar_time)
{
//--- memorize the time and return true
last_time=lastbar_time;
return(true);
}
//--- if we passed to this line, then the bar is not new; return false
return(false);
}
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data |
//+------------------------------------------------------------------+
bool RefreshRates()
{
//--- refresh rates
if(!m_symbol.RefreshRates())
return(false);
//--- protection against the return value of "zero"
if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
return(false);
//---
return(true);
}
//+------------------------------------------------------------------+
//| Checks if the specified filling mode is allowed |
//+------------------------------------------------------------------+
bool IsFillingTypeAllowed(string symbol,int fill_type)
{
//--- Obtain the value of the property that describes allowed filling modes
int filling=(int)SymbolInfoInteger(symbol,SYMBOL_FILLING_MODE);
//--- Return true, if mode fill_type is allowed
return((filling & fill_type)==fill_type);
}
//+------------------------------------------------------------------+
//| Calculate all positions Buy and Sell |
//+------------------------------------------------------------------+
int CalculateAllPositions()
{
int totlal=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)
totlal++;
//---
return(totlal);
}
//+------------------------------------------------------------------+
//| Close positions |
//+------------------------------------------------------------------+
void ClosePositions(const ENUM_POSITION_TYPE pos_type)
{
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()==Symbol() && m_position.Magic()==m_magic)
if(m_position.PositionType()==pos_type) // gets the position type
m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol
}
//+------------------------------------------------------------------+
//| Open Buy position |
//+------------------------------------------------------------------+
void OpenBuy(double lot,double sl,double tp)
{
sl=m_symbol.NormalizePrice(sl);
tp=m_symbol.NormalizePrice(tp);
double volume=LotCheck(lot);
if(volume==0.0)
return;
//--- check volume before OrderSend to avoid "not enough money" error (CTrade)
double check_volume_lot=m_trade.CheckVolume(m_symbol.Name(),volume,m_symbol.Ask(),ORDER_TYPE_BUY);
if(check_volume_lot!=0.0)
if(check_volume_lot>=volume)
{
if(m_trade.Buy(volume,NULL,m_symbol.Ask(),sl,tp))
{
if(m_trade.ResultDeal()==0)
{
Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
PrintResult(m_trade,m_symbol);
}
else
{
Print("Buy -> true. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
PrintResult(m_trade,m_symbol);
}
}
else
{
Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
PrintResult(m_trade,m_symbol);
}
}
//---
}
//+------------------------------------------------------------------+
//| Open Sell position |
//+------------------------------------------------------------------+
void OpenSell(double lot,double sl,double tp)
{
sl=m_symbol.NormalizePrice(sl);
tp=m_symbol.NormalizePrice(tp);
double volume=LotCheck(lot);
if(volume==0.0)
return;
//--- check volume before OrderSend to avoid "not enough money" error (CTrade)
double check_volume_lot=m_trade.CheckVolume(m_symbol.Name(),volume,m_symbol.Bid(),ORDER_TYPE_SELL);
if(check_volume_lot!=0.0)
if(check_volume_lot>=volume)
{
if(m_trade.Sell(volume,NULL,m_symbol.Bid(),sl,tp))
{
if(m_trade.ResultDeal()==0)
{
Print("Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
PrintResult(m_trade,m_symbol);
}
else
{
Print("Sell -> true. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
PrintResult(m_trade,m_symbol);
}
}
else
{
Print("Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
}
}
//---
}
//+------------------------------------------------------------------+
//| 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);
}
//+------------------------------------------------------------------+
//| TradeTransaction function |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
const MqlTradeRequest &request,
const MqlTradeResult &result)
{
//--- get transaction type as enumeration value
ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
if(type==TRADE_TRANSACTION_DEAL_ADD)
{
long deal_entry =0;
double deal_profit =0.0;
double deal_volume =0.0;
string deal_symbol ="";
long deal_magic =0;
long deal_reason =-1;
if(HistoryDealSelect(trans.deal))
{
deal_entry=HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
deal_profit=HistoryDealGetDouble(trans.deal,DEAL_PROFIT);
deal_volume=HistoryDealGetDouble(trans.deal,DEAL_VOLUME);
deal_symbol=HistoryDealGetString(trans.deal,DEAL_SYMBOL);
deal_magic=HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
deal_reason=HistoryDealGetInteger(trans.deal,DEAL_REASON);
}
else
return;
if(deal_symbol==m_symbol.Name() && deal_magic==m_magic)
if(deal_entry==DEAL_ENTRY_OUT)
{
if(deal_reason==DEAL_REASON_SL){
StopCount++;
Print("Stop Loss was Triggered!! ",StopCount);
if(StopCount>=Consecutive_Loss_Revert){
ExtLot*=Revert_Rate;
}
else
ExtLot=Lote;
}
else if(deal_reason==DEAL_REASON_TP){
StopCount=0;
ExtLot=Lote;
}
}
}
}
//+------------------------------------------------------------------+
//| 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
m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol
}
//+------------------------------------------------------------------+
//| Is position exists |
//+------------------------------------------------------------------+
bool IsPositionExists(void)
{
for(int i=PositionsTotal()-1;i>=0;i--)
if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
return(true);
//---
return(false);
}
//+------------------------------------------------------------------+
//| Profit all positions |
//+------------------------------------------------------------------+
double ProfitAllPositions()
{
double 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
profit+=m_position.Commission()+m_position.Swap()+m_position.Profit();
//---
return(profit);
}
//+------------------------------------------------------------------+