-
Notifications
You must be signed in to change notification settings - Fork 3
/
TradingConditions.mqh
126 lines (101 loc) · 4.35 KB
/
TradingConditions.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
//+------------------------------------------------------------------+
/*
TradingConditions.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 version "1.00"
#property strict
#include <Trade\AccountInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include "AutumnHarvest.mq5"
bool isTradingPossible(CAccountInfo& accountInfo) {
if(TRADE_CUTOFF_TIME != "") {
datetime finishTime = StringToTime(TRADE_CUTOFF_TIME) % 86400;
datetime serverTimeOfDay = TimeCurrent() % 86400;
if(serverTimeOfDay >= finishTime) {
printHelper(LOG_INFO, StringFormat("Trading not allowed after %s", TRADE_CUTOFF_TIME));
return false;
}
}
//Check spread
int spread = SymbolInfoInteger(_Symbol,SYMBOL_SPREAD);
if(spread > MAX_SPREAD) {
return false;
}
//Check recent deals
if(recentDealExist()) {
return false;
}
if(numberOfHeavyDrawDownDeals() >= MAX_HEAVY_DD_DEALS) {
printHelper(LOG_WARN, StringFormat("Trading not possible due to heavy DD limit of %d", MAX_HEAVY_DD_DEALS));
return false;
}
//Do we have enougn funds
double totalAllowedUsedMargin = GlobalVariableGet("TOTAL_ALLOWED_USED_MARGIN");
if(totalAllowedUsedMargin == 0.00) {
totalAllowedUsedMargin = accountInfo.Balance();
}
double usedMargin = accountInfo.Margin();
if((usedMargin + MathMax(FIXED_DEAL_AMOUNT_BUY,FIXED_DEAL_AMOUNT_SELL)) > totalAllowedUsedMargin) {
printHelper(LOG_WARN, StringFormat("Used margin is already on %f, Can't place deal of %f as it puts us above [%f]", usedMargin, MathMax(FIXED_DEAL_AMOUNT_BUY,FIXED_DEAL_AMOUNT_SELL), totalAllowedUsedMargin));
return false;
}
return true;
}
int numberOfHeavyDrawDownDeals() {
int count = 0;
for (int i = PositionsTotal()-1; i >= 0; i--) {
PositionGetSymbol(i);
ulong magic = PositionGetInteger(POSITION_MAGIC);
string symbol = PositionGetString(POSITION_SYMBOL);
double profit = PositionGetDouble(POSITION_PROFIT);
double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);
int positionType = PositionGetInteger(POSITION_TYPE);
//Calc margin
double volume = PositionGetDouble(POSITION_VOLUME);
double margin = 0.00;
ENUM_ORDER_TYPE orderType = ORDER_TYPE_BUY;
if(positionType == POSITION_TYPE_SELL) {
orderType = ORDER_TYPE_SELL;
}
OrderCalcMargin(orderType, symbol,volume,openPrice,margin);
if(profit < 0.00 && baseMagic == getMagicWithoutTimeframe(magic)) {
if(MathAbs(profit) >= (margin/2)) {
count++;
}
}
}
return count;
}
bool recentDealExist() {
//Don't place trade if one exists within last number of bars (number input by TRADE_INTERVAL_BARS)
for (int i = PositionsTotal()-1; i >= 0; i--) {
PositionGetSymbol(i);
ulong magic = PositionGetInteger(POSITION_MAGIC);
string symbol = PositionGetString(POSITION_SYMBOL);
long posTicket = PositionGetInteger(POSITION_TICKET);
long posSeconds = PositionGetInteger(POSITION_TIME_MSC) / 1000;
long currSeconds = (long)TimeCurrent();
if(symbol == _Symbol && magic == getMagicWithTimeframe()) {
long secondsBetweenDeals = currSeconds - posSeconds;
long secondsToSkip = PeriodSeconds(PERIOD_CURRENT) * TRADE_INTERVAL_BARS;
if(secondsBetweenDeals < secondsToSkip) {
printHelper(LOG_INFO, StringFormat("Not placing trade as ticket %d is not older than %d seconds...possible duplication", posTicket, secondsToSkip));
return true;
}
}
}
return false;
}
//+------------------------------------------------------------------+