|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace cHeeSaW\AlphaVantageBundle\Endpoints; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @link https://www.alphavantage.co/documentation/#technical-indicators |
|
11
|
|
|
*/ |
|
12
|
|
|
class TechnicalIndicator implements Endpoint |
|
13
|
|
|
{ |
|
14
|
|
|
public const TI_SMA = 'SMA'; |
|
15
|
|
|
public const TI_EMA = 'EMA'; |
|
16
|
|
|
public const TI_WMA = 'WMA'; |
|
17
|
|
|
public const TI_DEMA = 'DEMA'; |
|
18
|
|
|
public const TI_TEMA = 'TEMA'; |
|
19
|
|
|
public const TI_TRIMA = 'TRIMA'; |
|
20
|
|
|
public const TI_KAMA = 'KAMA'; |
|
21
|
|
|
public const TI_MAMA = 'MAMA'; |
|
22
|
|
|
public const TI_VWAP = 'VWAP'; |
|
23
|
|
|
public const TI_T3 = 'T3'; |
|
24
|
|
|
public const TI_MACD = 'MACD'; |
|
25
|
|
|
public const TI_MACDEXT = 'MACDEXT'; |
|
26
|
|
|
public const TI_STOCH = 'STOCH'; |
|
27
|
|
|
public const TI_STOCHF = 'STOCHF'; |
|
28
|
|
|
public const TI_RSI = 'RSI'; |
|
29
|
|
|
public const TI_STOCHRSI = 'STOCHRSI'; |
|
30
|
|
|
public const TI_WILLR = 'WILLR'; |
|
31
|
|
|
public const TI_ADX = 'ADX'; |
|
32
|
|
|
public const TI_ADXR = 'ADXR'; |
|
33
|
|
|
public const TI_APO = 'APO'; |
|
34
|
|
|
public const TI_PPO = 'PPO'; |
|
35
|
|
|
public const TI_MOM = 'MOM'; |
|
36
|
|
|
public const TI_BOP = 'BOP'; |
|
37
|
|
|
public const TI_CCI = 'CCI'; |
|
38
|
|
|
public const TI_CMO = 'CMO'; |
|
39
|
|
|
public const TI_ROC = 'ROC'; |
|
40
|
|
|
public const TI_ROCR = 'ROCR'; |
|
41
|
|
|
public const TI_AROON = 'AROON'; |
|
42
|
|
|
public const TI_AROONOSC = 'AROONOSC'; |
|
43
|
|
|
public const TI_MFI = 'MFI'; |
|
44
|
|
|
public const TI_TRIX = 'TRIX'; |
|
45
|
|
|
public const TI_ULTOSC = 'ULTOSC'; |
|
46
|
|
|
public const TI_DX = 'DX'; |
|
47
|
|
|
public const TI_MINUS_DI = 'MINUS_DI'; |
|
48
|
|
|
public const TI_PLUS_DI = 'PLUS_DI'; |
|
49
|
|
|
public const TI_MINUS_DM = 'MINUS_DM'; |
|
50
|
|
|
public const TI_PLUS_DM = 'PLUS_DM'; |
|
51
|
|
|
public const TI_PLUS_BBANDS = 'BBANDS'; |
|
52
|
|
|
public const TI_PLUS_MIDPOINT = 'MIDPOINT'; |
|
53
|
|
|
public const TI_PLUS_MIDPRICE = 'MIDPRICE'; |
|
54
|
|
|
public const TI_PLUS_SAR = 'SAR'; |
|
55
|
|
|
public const TI_PLUS_TRANGE = 'TRANGE'; |
|
56
|
|
|
public const TI_PLUS_ATR = 'ATR'; |
|
57
|
|
|
public const TI_PLUS_NATR = 'NATR'; |
|
58
|
|
|
public const TI_PLUS_AD = 'AD'; |
|
59
|
|
|
public const TI_PLUS_ADOSC = 'ADOSC'; |
|
60
|
|
|
public const TI_PLUS_OBV = 'OBV'; |
|
61
|
|
|
public const TI_PLUS_HT_TRENDLINE = 'HT_TRENDLINE'; |
|
62
|
|
|
public const TI_PLUS_HT_SINE = 'HT_SINE'; |
|
63
|
|
|
public const TI_PLUS_HT_TRENDMODE = 'HT_TRENDMODE'; |
|
64
|
|
|
public const TI_PLUS_HT_DCPERIOD = 'HT_DCPERIOD'; |
|
65
|
|
|
public const TI_PLUS_HT_DCPHASE = 'HT_DCPHASE'; |
|
66
|
|
|
public const TI_PLUS_HT_PHASOR = 'HT_PHASOR'; |
|
67
|
|
|
|
|
68
|
|
|
private static array $validFunctions = [ |
|
69
|
|
|
self::TI_SMA, |
|
70
|
|
|
self::TI_EMA, |
|
71
|
|
|
self::TI_WMA, |
|
72
|
|
|
self::TI_DEMA, |
|
73
|
|
|
self::TI_TEMA, |
|
74
|
|
|
self::TI_TRIMA, |
|
75
|
|
|
self::TI_KAMA, |
|
76
|
|
|
self::TI_MAMA, |
|
77
|
|
|
self::TI_VWAP, |
|
78
|
|
|
self::TI_T3, |
|
79
|
|
|
self::TI_MACD, |
|
80
|
|
|
self::TI_MACDEXT, |
|
81
|
|
|
self::TI_STOCH, |
|
82
|
|
|
self::TI_STOCHF, |
|
83
|
|
|
self::TI_RSI, |
|
84
|
|
|
self::TI_STOCHRSI, |
|
85
|
|
|
self::TI_WILLR, |
|
86
|
|
|
self::TI_ADX, |
|
87
|
|
|
self::TI_ADXR, |
|
88
|
|
|
self::TI_APO, |
|
89
|
|
|
self::TI_PPO, |
|
90
|
|
|
self::TI_MOM, |
|
91
|
|
|
self::TI_BOP, |
|
92
|
|
|
self::TI_CCI, |
|
93
|
|
|
self::TI_CMO, |
|
94
|
|
|
self::TI_ROC, |
|
95
|
|
|
self::TI_ROCR, |
|
96
|
|
|
self::TI_AROON, |
|
97
|
|
|
self::TI_AROONOSC, |
|
98
|
|
|
self::TI_MFI, |
|
99
|
|
|
self::TI_TRIX, |
|
100
|
|
|
self::TI_ULTOSC, |
|
101
|
|
|
self::TI_DX, |
|
102
|
|
|
self::TI_MINUS_DI, |
|
103
|
|
|
self::TI_PLUS_DI, |
|
104
|
|
|
self::TI_MINUS_DM, |
|
105
|
|
|
self::TI_PLUS_DM, |
|
106
|
|
|
self::TI_PLUS_BBANDS, |
|
107
|
|
|
self::TI_PLUS_MIDPOINT, |
|
108
|
|
|
self::TI_PLUS_MIDPRICE, |
|
109
|
|
|
self::TI_PLUS_SAR, |
|
110
|
|
|
self::TI_PLUS_TRANGE, |
|
111
|
|
|
self::TI_PLUS_ATR, |
|
112
|
|
|
self::TI_PLUS_NATR, |
|
113
|
|
|
self::TI_PLUS_AD, |
|
114
|
|
|
self::TI_PLUS_ADOSC, |
|
115
|
|
|
self::TI_PLUS_OBV, |
|
116
|
|
|
self::TI_PLUS_HT_TRENDLINE, |
|
117
|
|
|
self::TI_PLUS_HT_SINE, |
|
118
|
|
|
self::TI_PLUS_HT_TRENDMODE, |
|
119
|
|
|
self::TI_PLUS_HT_DCPERIOD, |
|
120
|
|
|
self::TI_PLUS_HT_DCPHASE, |
|
121
|
|
|
self::TI_PLUS_HT_PHASOR, |
|
122
|
|
|
]; |
|
123
|
|
|
|
|
124
|
|
|
private static array $validIntervals = [ |
|
125
|
|
|
self::INTERVAL_1_MIN, |
|
126
|
|
|
self::INTERVAL_5_MIN, |
|
127
|
|
|
self::INTERVAL_15_MIN, |
|
128
|
|
|
self::INTERVAL_30_MIN, |
|
129
|
|
|
self::INTERVAL_60_MIN |
|
130
|
|
|
]; |
|
131
|
|
|
|
|
132
|
|
|
private static array $validSeriesTypes = [ |
|
133
|
|
|
self::SERIES_TYPE_CLOSE, |
|
134
|
|
|
self::SERIES_TYPE_HIGH, |
|
135
|
|
|
self::SERIES_TYPE_LOW, |
|
136
|
|
|
self::SERIES_TYPE_OPEN |
|
137
|
|
|
]; |
|
138
|
|
|
|
|
139
|
|
|
private string $function; |
|
140
|
|
|
private string $symbol; |
|
141
|
|
|
private string $interval; |
|
142
|
|
|
private int $time_period; |
|
143
|
|
|
private string $series_type; |
|
144
|
|
|
private string $datatype; |
|
145
|
|
|
|
|
146
|
|
|
public function __construct( |
|
147
|
|
|
string $function, |
|
148
|
|
|
string $symbol, |
|
149
|
|
|
string $interval, |
|
150
|
|
|
int $time_period, |
|
151
|
|
|
string $series_type, |
|
152
|
|
|
string $dataType = self::DATATYPE_JSON |
|
153
|
|
|
) { |
|
154
|
|
|
if ($dataType !== self::DATATYPE_JSON && $dataType !== self::DATATYPE_CSV) { |
|
155
|
|
|
throw new InvalidArgumentException($dataType . ' is not valid, try: csv or json'); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
if (!in_array($function, self::$validFunctions, true)) { |
|
159
|
|
|
throw new InvalidArgumentException( |
|
160
|
|
|
$function . ' is not valid, |
|
161
|
|
|
check https://www.alphavantage.co/documentation/ for valid Technical Indicator functions' |
|
162
|
|
|
); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
if (!in_array($interval, self::$validIntervals, true)) { |
|
166
|
|
|
throw new InvalidArgumentException( |
|
167
|
|
|
$interval . ' is not valid, valid ones are: 1min, 5min, 15min, 30min, 60min' |
|
168
|
|
|
); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if (!in_array($series_type, self::$validSeriesTypes, true)) { |
|
172
|
|
|
throw new InvalidArgumentException( |
|
173
|
|
|
$series_type . ' is not valid, valid ones are: close, open, low, high' |
|
174
|
|
|
); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$this->function = $function; |
|
178
|
|
|
$this->symbol = $symbol; |
|
179
|
|
|
$this->interval = $interval; |
|
180
|
|
|
$this->time_period = $time_period; |
|
181
|
|
|
$this->series_type = $series_type; |
|
182
|
|
|
$this->datatype = $dataType; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public static function getValidFunctions(): array |
|
186
|
|
|
{ |
|
187
|
|
|
return self::$validFunctions; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
public static function getValidIntervals(): array |
|
191
|
|
|
{ |
|
192
|
|
|
return self::$validIntervals; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public static function getValidSeriesTypes(): array |
|
196
|
|
|
{ |
|
197
|
|
|
return self::$validSeriesTypes; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function getFunction(): string |
|
201
|
|
|
{ |
|
202
|
|
|
return $this->function; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public function getSymbol(): string |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->symbol; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function getInterval(): string |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->interval; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
public function getTimePeriod(): int |
|
216
|
|
|
{ |
|
217
|
|
|
return $this->time_period; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function getSeriesType(): string |
|
221
|
|
|
{ |
|
222
|
|
|
return $this->series_type; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
public function getDatatype(): string |
|
226
|
|
|
{ |
|
227
|
|
|
return $this->datatype; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
public function getQueryString(): string |
|
231
|
|
|
{ |
|
232
|
|
|
return http_build_query($this); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|