-
Notifications
You must be signed in to change notification settings - Fork 35
/
AFAOSMD.mq5
208 lines (180 loc) · 7.73 KB
/
AFAOSMD.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
//+------------------------------------------------------------------+
//| AFAOSMD.mq5 |
//| Copyright 2023, Geraked |
//| https://github.com/geraked |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Geraked"
#property link "https://github.com/geraked"
#property version "1.5"
#property description "A strategy using Average Force, Andean Oscillator, and MACD"
#property description "NZDCAD-30M 2019.01.01 - 2023.10.22"
#include <EAUtils.mqh>
#define PATH_AF "Indicators\\AverageForce.ex5"
#define I_AF "::" + PATH_AF
#resource "\\" + PATH_AF
#define PATH_AOS "Indicators\\AndeanOscillator.ex5"
#define I_AOS "::" + PATH_AOS
#resource "\\" + PATH_AOS
enum ENUM_AOS_BI {
AOS_BI_BULL,
AOS_BI_BEAR,
AOS_BI_SIGNAL
};
input group "Indicator Parameters"
input int AfPeriod = 20; // Average Force Period
input int AfSmooth = 9; // Average Force Smooth
input int AosPeriod = 50; // Andean Oscillator Period
input int AosSignalPeriod = 9; // Andean Oscillator Signal Period
input int MdFast = 100; // MACD Fast
input int MdSlow = 200; // MACD Slow
input group "General"
input double TPCoef = 1.0; // TP Coefficient
input ENUM_SL SLType = SL_SWING; // SL Type
input int SLLookback = 7; // SL Look Back
input int SLDev = 60; // SL Deviation (Points)
input bool Reverse = true; // Reverse Signal
input group "Risk Management"
input double Risk = 1.0; // Risk
input ENUM_RISK RiskMode = RISK_DEFAULT; // Risk Mode
input bool IgnoreSL = false; // Ignore SL
input bool IgnoreTP = true; // Ignore TP
input bool Trail = true; // Trailing Stop
input double TrailingStopLevel = 50; // Trailing Stop Level (%) (0: Disable)
input double EquityDrawdownLimit = 0; // Equity Drawdown Limit (%) (0: Disable)
input group "Strategy: Grid"
input bool Grid = true; // Grid Enable
input double GridVolMult = 1.1; // Grid Volume Multiplier
input double GridTrailingStopLevel = 0; // Grid Trailing Stop Level (%) (0: Disable)
input int GridMaxLvl = 20; // Grid Max Levels
input group "News"
input bool News = false; // News Enable
input ENUM_NEWS_IMPORTANCE NewsImportance = NEWS_IMPORTANCE_MEDIUM; // News Importance
input int NewsMinsBefore = 60; // News Minutes Before
input int NewsMinsAfter = 60; // News Minutes After
input int NewsStartYear = 0; // News Start Year to Fetch for Backtesting (0: Disable)
input group "Open Position Limit"
input bool OpenNewPos = true; // Allow Opening New Position
input bool MultipleOpenPos = true; // Allow Having Multiple Open Positions
input double MarginLimit = 300; // Margin Limit (%) (0: Disable)
input int SpreadLimit = -1; // Spread Limit (Points) (-1: Disable)
input group "Auxiliary"
input int Slippage = 30; // Slippage (Points)
input int TimerInterval = 30; // Timer Interval (Seconds)
input ulong MagicNumber = 1001; // Magic Number
input ENUM_FILLING Filling = FILLING_DEFAULT; // Order Filling
GerEA ea;
datetime lastCandle;
datetime tc;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double AF(int i = -1) {
int handle = iCustom(NULL, 0, I_AF, AfPeriod, AfSmooth);
if (i == -1) return -1;
return Ind(handle, i);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double AOS(ENUM_AOS_BI bi = 0, int i = -1) {
int handle = iCustom(NULL, 0, I_AOS, AosPeriod, AosSignalPeriod);
if (i == -1) return -1;
return Ind(handle, i, bi);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double MD(int i = -1) {
int handle = iMACD(NULL, 0, MdFast, MdSlow, 1, PRICE_CLOSE);
if (i == -1) return -1;
return Ind(handle, i);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool BuySignal() {
if (!(AOS(AOS_BI_BULL, 1) > AOS(AOS_BI_BEAR, 1))) return false;
if (!(AF(2) < 0 && AF(1) > 0)) return false;
if (!(MD(2) > 0 && MD(1) > 0)) return false;
if (!(MD(2) < MD(1))) return false;
double in = Ask();
double sl = BuySL(SLType, SLLookback, in, SLDev, 1);
double tp = in + TPCoef * MathAbs(in - sl);
ea.BuyOpen(in, sl, tp, IgnoreSL, IgnoreTP);
return true;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool SellSignal() {
if (!(AOS(AOS_BI_BULL, 1) < AOS(AOS_BI_BEAR, 1))) return false;
if (!(AF(2) > 0 && AF(1) < 0)) return false;
if (!(MD(2) < 0 && MD(1) < 0)) return false;
if (!(MD(2) > MD(1))) return false;
double in = Bid();
double sl = SellSL(SLType, SLLookback, in, SLDev, 1);
double tp = in - TPCoef * MathAbs(in - sl);
ea.SellOpen(in, sl, tp, IgnoreSL, IgnoreTP);
return true;
}
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit() {
ea.Init();
ea.SetMagic(MagicNumber);
ea.risk = Risk * 0.01;
ea.reverse = Reverse;
ea.trailingStopLevel = TrailingStopLevel * 0.01;
ea.grid = Grid;
ea.gridVolMult = GridVolMult;
ea.gridTrailingStopLevel = GridTrailingStopLevel * 0.01;
ea.gridMaxLvl = GridMaxLvl;
ea.equityDrawdownLimit = EquityDrawdownLimit * 0.01;
ea.slippage = Slippage;
ea.news = News;
ea.newsImportance = NewsImportance;
ea.newsMinsBefore = NewsMinsBefore;
ea.newsMinsAfter = NewsMinsAfter;
ea.filling = Filling;
ea.riskMode = RiskMode;
if (RiskMode == RISK_FIXED_VOL || RiskMode == RISK_MIN_AMOUNT) ea.risk = Risk;
if (News) fetchCalendarFromYear(NewsStartYear);
AOS();
AF();
MD();
EventSetTimer(TimerInterval);
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
EventKillTimer();
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer() {
datetime oldTc = tc;
tc = TimeCurrent();
if (tc == oldTc) return;
if (Trail) ea.CheckForTrail();
if (EquityDrawdownLimit) ea.CheckForEquity();
if (Grid) ea.CheckForGrid();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick() {
if (lastCandle != Time(0)) {
lastCandle = Time(0);
if (!OpenNewPos) return;
if (SpreadLimit != -1 && Spread() > SpreadLimit) return;
if (MarginLimit && PositionsTotal() > 0 && AccountInfoDouble(ACCOUNT_MARGIN_LEVEL) < MarginLimit) return;
if ((Grid || !MultipleOpenPos) && ea.OPTotal() > 0) return;
if (BuySignal()) return;
SellSignal();
}
}
//+------------------------------------------------------------------+