-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLimitations.mqh
160 lines (136 loc) · 13.9 KB
/
Limitations.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
//+------------------------------------------------------------------+
//| Limitations.mqh |
//| Copyright 2021, Homma.tech |
//| https://www.homma.tech |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, Homma.tech"
#property link "https://www.homma.tech"
#include <Trade/Trade.mqh>
#include "Utils.mqh"
#include "NewTypes.mqh"
#include "Storage.mqh"
#include "Positions.mqh"
#include "Operations.mqh"
#include "TimeManipulation.mqh"
CPositions positionsService;
COperations operationsService;
CTimeManipulation timeManipulationService;
class CLimitations {
private:
CLocalStorage *localState;
public:
bool CLimitations :: VolumeReached(double lim_volumeTarget,
ENUM_TIMEFRAMES lim_timeframe = PERIOD_CURRENT);
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CLimitations :: CLimitations () {
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CLimitations :: ~CLimitations () { }
//+------------------------------------------------------------------+
//| Time interval check function |
//+------------------------------------------------------------------+
bool CLimitations :: InTimeInterval(string begin_,
string finish_,
bool showMessage = false) {
if(begin_ <= TimeToString(TimeLocal(), TIME_MINUTES) &&
TimeToString(TimeLocal(), TIME_MINUTES) < finish_)
return true;
if(showMessage)
MessageBox("Out of operational time interval setted. \n\n" +
begin_ + " - " + finish_
+ "\n\nPlease, change the configurations!");
return false;
}
//+------------------------------------------------------------------+
//| Time limit check and close positions function |
//+------------------------------------------------------------------+
void CLimitations :: TimeLimit(string finish_,
int deviation) {
if((TimeToString(TimeLocal(), TIME_MINUTES) >= finish_)) {
positionsService.CloseAllPositions(_Symbol, deviation);
}
}
//+------------------------------------------------------------------+
//| Days of the week operations are enable function |
//+------------------------------------------------------------------+
bool CLimitations :: IsOperationDay(int &daysOn[]) {
MqlDateTime strTimeLocal;
TimeToStruct(TimeLocal(), strTimeLocal);
for(int i = 0; i < ArraySize(daysOn); i++)
if(strTimeLocal.day_of_week == daysOn[i])
return true;
return false;
}
//+------------------------------------------------------------------+
//| Profit limit reached function |
//+------------------------------------------------------------------+
bool CLimitations :: ProfitReached(double profitMax_PR,
datetime initDate_PR,
datetime finishDate_PR,
string symbol_PR = NULL) {
double acum = operationsService.AccumulatedProfit(initDate_PR, finishDate_PR, symbol_PR);
if(operationsService.AccumulatedProfit(initDate_PR, finishDate_PR, symbol_PR) >= profitMax_PR)
return true;
return false;
}
//+------------------------------------------------------------------+
//| Loss limit reached function |
//+------------------------------------------------------------------+
bool CLimitations :: LossReached(double lossMax_LR,
datetime initDate,
datetime finishDate,
string symbol_LR = NULL) {
if(operationsService.AccumulatedProfit(initDate, finishDate, symbol_LR) <= (lossMax_LR * -1))
return true;
return false;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CLimitations :: PercentualGainReached(double percentage,
datetime initDate,
datetime finishDate) {
double gainLimit;
gainLimit = AccountInfoDouble(ACCOUNT_BALANCE) * percentage;
return ProfitReached(gainLimit, initDate, finishDate);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CLimitations :: PercentualLossReached(double percentage,
datetime initDate,
datetime finishDate) {
double lossLimit;
lossLimit = AccountInfoDouble(ACCOUNT_BALANCE) * percentage;
return LossReached(lossLimit, initDate, finishDate);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CLimitations :: NumberOfTradesReached (int expectedNumber,
datetime initDate,
datetime finishDate) {
if(operationsService.NumberOfTrades(initDate, finishDate) >= expectedNumber)
return true;
return false;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CLimitations :: OperationalTimeAccumReached (string limitTime,
CLocalStorage* &external_state) {
int timeOnOperationLimitTarget,
sumTimeOnOperation;
sumTimeOnOperation = (int)StringToInteger(external_state.GetState("timeOperationAcumm"));
timeOnOperationLimitTarget = timeManipulationService.StringTimeToInt(limitTime, HHMM);
if(sumTimeOnOperation >= timeOnOperationLimitTarget) {
return true;
}
return false;
}
};
//+------------------------------------------------------------------+