-
Notifications
You must be signed in to change notification settings - Fork 4
/
Stg_Alligator.mqh
184 lines (176 loc) · 9.77 KB
/
Stg_Alligator.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* @file
* Implements Alligator strategy based on the Alligator indicator.
*/
// User input params.
INPUT_GROUP("Alligator strategy: strategy params");
INPUT float Alligator_LotSize = 0; // Lot size
INPUT int Alligator_SignalOpenMethod = 22; // Signal open method (-127-127)
INPUT float Alligator_SignalOpenLevel = 4.0f; // Signal open level
INPUT int Alligator_SignalOpenFilterMethod = 32; // Signal open filter method
INPUT int Alligator_SignalOpenFilterTime = 3; // Signal open filter time
INPUT int Alligator_SignalOpenBoostMethod = 0; // Signal open boost method
INPUT int Alligator_SignalCloseMethod = 3; // Signal close method (-127-127)
INPUT int Alligator_SignalCloseFilter = 0; // Signal close filter (-127-127)
INPUT float Alligator_SignalCloseLevel = 1.0f; // Signal close level
INPUT int Alligator_PriceStopMethod = 20; // Price stop method (0-127)
INPUT float Alligator_PriceStopLevel = 2.2f; // Price stop level
INPUT int Alligator_TickFilterMethod = 32; // Tick filter method
INPUT float Alligator_MaxSpread = 4.0; // Max spread to trade (pips)
INPUT short Alligator_Shift = 2; // Shift
INPUT float Alligator_OrderCloseLoss = 80; // Order close loss
INPUT float Alligator_OrderCloseProfit = 80; // Order close profit
INPUT int Alligator_OrderCloseTime = -30; // Order close time in mins (>0) or bars (<0)
INPUT_GROUP("Alligator strategy: Alligator indicator params");
INPUT int Alligator_Indi_Alligator_Period_Jaw = 30; // Jaw Period
INPUT int Alligator_Indi_Alligator_Period_Teeth = 6; // Teeth Period
INPUT int Alligator_Indi_Alligator_Period_Lips = 4; // Lips Period
INPUT int Alligator_Indi_Alligator_Shift_Jaw = 6; // Jaw Shift
INPUT int Alligator_Indi_Alligator_Shift_Teeth = 4; // Teeth Shift
INPUT int Alligator_Indi_Alligator_Shift_Lips = 2; // Lips Shift
INPUT ENUM_MA_METHOD Alligator_Indi_Alligator_MA_Method = MODE_LWMA; // MA Method
INPUT ENUM_APPLIED_PRICE Alligator_Indi_Alligator_Applied_Price = PRICE_TYPICAL; // Applied Price
INPUT int Alligator_Indi_Alligator_Shift = 0; // Shift
// Structs.
// Defines struct with default user strategy values.
struct Stg_Alligator_Params_Defaults : StgParams {
Stg_Alligator_Params_Defaults()
: StgParams(::Alligator_SignalOpenMethod, ::Alligator_SignalOpenFilterMethod, ::Alligator_SignalOpenLevel,
::Alligator_SignalOpenBoostMethod, ::Alligator_SignalCloseMethod, ::Alligator_SignalCloseFilter,
::Alligator_SignalCloseLevel, ::Alligator_PriceStopMethod, ::Alligator_PriceStopLevel,
::Alligator_TickFilterMethod, ::Alligator_MaxSpread, ::Alligator_Shift) {
Set(STRAT_PARAM_LS, Alligator_LotSize);
Set(STRAT_PARAM_OCL, Alligator_OrderCloseLoss);
Set(STRAT_PARAM_OCP, Alligator_OrderCloseProfit);
Set(STRAT_PARAM_OCT, Alligator_OrderCloseTime);
Set(STRAT_PARAM_SOFT, Alligator_SignalOpenFilterTime);
}
};
#ifdef __config__
// Loads pair specific param values.
#include "config/H1.h"
#include "config/H4.h"
#include "config/H8.h"
#include "config/M1.h"
#include "config/M15.h"
#include "config/M30.h"
#include "config/M5.h"
#endif
class Stg_Alligator : public Strategy {
public:
Stg_Alligator(StgParams &_sparams, TradeParams &_tparams, ChartParams &_cparams, string _name = "")
: Strategy(_sparams, _tparams, _cparams, _name) {}
static Stg_Alligator *Init(ENUM_TIMEFRAMES _tf = NULL) {
// Initialize strategy initial values.
Stg_Alligator_Params_Defaults stg_alli_defaults;
StgParams _stg_params(stg_alli_defaults);
#ifdef __config__
SetParamsByTf<StgParams>(_stg_params, _tf, stg_alli_m1, stg_alli_m5, stg_alli_m15, stg_alli_m30, stg_alli_h1,
stg_alli_h4, stg_alli_h8);
#endif
// Initialize indicator.
// Initialize Strategy instance.
ChartParams _cparams(_tf, _Symbol);
TradeParams _tparams;
Strategy *_strat = new Stg_Alligator(_stg_params, _tparams, _cparams, "Alligator");
return _strat;
}
/**
* Event on strategy's init.
*/
void OnInit() {
IndiAlligatorParams _indi_params(::Alligator_Indi_Alligator_Period_Jaw, ::Alligator_Indi_Alligator_Shift_Jaw,
::Alligator_Indi_Alligator_Period_Teeth, ::Alligator_Indi_Alligator_Shift_Teeth,
::Alligator_Indi_Alligator_Period_Lips, ::Alligator_Indi_Alligator_Shift_Lips,
::Alligator_Indi_Alligator_MA_Method, ::Alligator_Indi_Alligator_Applied_Price,
::Alligator_Indi_Alligator_Shift);
_indi_params.SetTf(Get<ENUM_TIMEFRAMES>(STRAT_PARAM_TF));
SetIndicator(new Indi_Alligator(_indi_params));
}
/**
* Check strategy's opening signal.
*/
bool SignalOpen(ENUM_ORDER_TYPE _cmd, int _method = 0, float _level = 0.0f, int _shift = 0) {
Indi_Alligator *_indi = GetIndicator();
int _ishift = _shift + ::Alligator_Indi_Alligator_Shift;
bool _result = _indi.GetFlag(INDI_ENTRY_FLAG_IS_VALID, _ishift);
if (!_result) {
// Returns false when indicator data is not valid.
return false;
}
double _level_pips = _level * Chart().GetPipSize();
switch (_cmd) {
case ORDER_TYPE_BUY:
_result = (_indi[_ishift][(int)LINE_LIPS] >
_indi[_ishift][(int)LINE_TEETH] + _level_pips && // Check if Lips are above Teeth ...
_indi[_ishift][(int)LINE_TEETH] >
_indi[_ishift][(int)LINE_JAW] + _level_pips // ... Teeth are above Jaw ...
);
if (_method != 0) {
if (METHOD(_method, 0))
_result &=
(_indi[_ishift][(int)LINE_LIPS] > _indi[_ishift + 1][(int)LINE_LIPS] && // Check if Lips increased.
_indi[_ishift][(int)LINE_TEETH] > _indi[_ishift + 1][(int)LINE_TEETH] && // Check if Teeth increased.
_indi[_ishift][(int)LINE_JAW] > _indi[_ishift + 1][(int)LINE_JAW] // // Check if Jaw increased.
);
if (METHOD(_method, 1))
_result &=
(_indi[_ishift + 1][(int)LINE_LIPS] > _indi[_ishift + 2][(int)LINE_LIPS] && // Check if Lips increased.
_indi[_ishift + 1][(int)LINE_TEETH] >
_indi[_ishift + 2][(int)LINE_TEETH] && // Check if Teeth increased.
_indi[_ishift + 1][(int)LINE_JAW] > _indi[_ishift + 2][(int)LINE_JAW] // // Check if Jaw increased.
);
if (METHOD(_method, 2))
_result &= _indi[_ishift][(int)LINE_LIPS] > _indi[_ishift + 2][(int)LINE_LIPS]; // Check if Lips increased.
if (METHOD(_method, 3))
_result &= _indi[_ishift][(int)LINE_LIPS] - _indi[_ishift][(int)LINE_TEETH] >
_indi[_ishift][(int)LINE_TEETH] - _indi[_ishift][(int)LINE_JAW];
if (METHOD(_method, 4))
_result &= (_indi[_ishift + 2][(int)LINE_LIPS] <=
_indi[_ishift + 2][(int)LINE_TEETH] || // Check if Lips are below Teeth and ...
_indi[_ishift + 2][(int)LINE_LIPS] <=
_indi[_ishift + 2][(int)LINE_JAW] || // ... Lips are below Jaw and ...
_indi[_ishift + 2][(int)LINE_TEETH] <=
_indi[_ishift + 2][(int)LINE_JAW] // ... Teeth are below Jaw ...
);
}
break;
case ORDER_TYPE_SELL:
_result = (_indi[_ishift][(int)LINE_LIPS] + _level_pips <
_indi[_ishift][(int)LINE_TEETH] && // Check if Lips are below Teeth and ...
_indi[_ishift][(int)LINE_TEETH] + _level_pips <
_indi[_ishift][(int)LINE_JAW] // ... Teeth are below Jaw ...
);
if (_method != 0) {
if (METHOD(_method, 0))
_result &=
(_indi[_ishift][(int)LINE_LIPS] < _indi[_ishift + 1][(int)LINE_LIPS] && // Check if Lips decreased.
_indi[_ishift][(int)LINE_TEETH] < _indi[_ishift + 1][(int)LINE_TEETH] && // Check if Teeth decreased.
_indi[_ishift][(int)LINE_JAW] < _indi[_ishift + 1][(int)LINE_JAW] // // Check if Jaw decreased.
);
if (METHOD(_method, 1))
_result &=
(_indi[_ishift + 1][(int)LINE_LIPS] < _indi[_ishift + 2][(int)LINE_LIPS] && // Check if Lips decreased.
_indi[_ishift + 1][(int)LINE_TEETH] <
_indi[_ishift + 2][(int)LINE_TEETH] && // Check if Teeth decreased.
_indi[_ishift + 1][(int)LINE_JAW] < _indi[_ishift + 2][(int)LINE_JAW] // // Check if Jaw decreased.
);
if (METHOD(_method, 2))
_result &= _indi[_ishift][(int)LINE_LIPS] < _indi[_ishift + 2][(int)LINE_LIPS]; // Check if Lips decreased.
if (METHOD(_method, 3))
_result &= _indi[_ishift][(int)LINE_TEETH] - _indi[_ishift][(int)LINE_LIPS] >
_indi[_ishift][(int)LINE_JAW] - _indi[_ishift][(int)LINE_TEETH];
if (METHOD(_method, 4))
_result &= (_indi[_ishift + 2][(int)LINE_LIPS] >=
_indi[_ishift + 2][(int)LINE_TEETH] || // Check if Lips are above Teeth ...
_indi[_ishift + 2][(int)LINE_LIPS] >=
_indi[_ishift + 2][(int)LINE_JAW] || // ... Lips are above Jaw ...
_indi[_ishift + 2][(int)LINE_TEETH] >=
_indi[_ishift + 2][(int)LINE_JAW] // ... Teeth are above Jaw ...
);
}
break;
}
return _result;
}
};