-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.analysis.php
More file actions
212 lines (188 loc) · 6.09 KB
/
functions.analysis.php
File metadata and controls
212 lines (188 loc) · 6.09 KB
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
209
210
211
212
<?
/* calculate and save price indicators */
function technical_analysis($config, $pair_id = FALSE, $indicator_list = FALSE) {
$query = "SELECT DISTINCT pair, period, source, currency, history_start, class FROM asset_pairs WHERE analyse";
if ($pair_id) $query .= " AND pair_id = " . $pair_id;
$pairs = query($query, $config);
$stop = $config['timestamp'];
$points = 100; # chart
$points_buffer = 200;
$analysis = array();
/* technical analysis indicators */
$rsi = array(8, 12, 24, 36); // 'rsi'.$n
$rsiob = 75; // overbought, 'rsi'.$n.'ob'
$rsios = 25; // oversold, 'rsi'.$n.'os'
$ema = array(6, 12, 26, 50, 100, 200); // 'ema'.$n, crossover upwards 'ema'.$n.'cu', crossover downwards 'ema'.$n.'cd'
$roc = array(1, 2, 4, 6, 12, 24); // 'roc'.$n
$corr = array(
'BTC' => array(30,50),
'ETH' => array(30,50)
); // 'corr'.$n.'btc'
/* build list */
if ($indicator_list) {
$indicators = array('close' => 'close', 'open' => 'open', 'high' => 'high', 'low' => 'low', 'volume' => 'volume');
foreach ($rsi as $n) {
$indicators['rsi'.$n] = 'RSI ' . $n;
$indicators['rsi'.$n.'ob'] = 'RSI ' . $n . ' overbought if RSI > ' . $rsiob . ' (" = 1" to trigger)';
$indicators['rsi'.$n.'os'] = 'RSI ' . $n . ' oversold if RSI < ' . $rsios . ' (" = 1" to trigger)';
}
foreach ($ema as $n) {
$indicators['ema'.$n] = 'EMA ' . $n;
$indicators['ema'.$n.'cu'] = 'EMA ' . $n . ' cross upwards if prev close < EMA & new close > EMA (" = 1" to trigger)';
$indicators['ema'.$n.'os'] = 'EMA ' . $n . ' cross downwards if prev close > EMA & new close < EMA (" = 1" to trigger)';
}
foreach ($roc as $n) {
$indicators['roc'.$n] = 'Rate of Change ' . $n . ' (i.e. "50" for 50%)';
}
foreach ($corr as $key => $arr) {
foreach ($arr as $val)
$indicators['corr'.$val.strtolower($key)] = 'Correlation to ' . $key . ' ' . $val;
}
return $indicators;
}
foreach ($pairs as $pair) {
$period_txt = $config['period'][$pair['period']];
$period = $pair['period'] * 60000;
$start = $stop - $period * ($points + $points_buffer);
$query = "
SELECT history_id, timestamp, close FROM price_history
WHERE pair = '" . $pair['pair'] . "'
AND source = '" . $pair['source'] . "'
AND period = '" . $period_txt . "'
AND timestamp >= " . $start . "
AND timestamp <= " . $stop . "
ORDER BY timestamp ASC
";
$hist = query($query, $config);
if ($config['debug']) echo $query . PHP_EOL;
if (empty($hist)) continue 1;
$last = $hist[0]['timestamp'] - $period;
$missing_data = FALSE;
$weekends = weekends($pair['class']);
$i = 0;
foreach ($hist as $h) {
$diff_obs = $h['timestamp'] - $last;
if ($period !== $diff_obs) {
$day = date('l', $h['timestamp'] / 1000);
if (
$weekends # asset classes like crypto expect data on weekends, no exception for missing data
|| (!$weekends && !in_array($day, ['Sunday','Monday'])) # classes like fiat or stocks do not expect so allow Sat/Sun missing using test today is Sun/Mon
) {
$missing_data = TRUE;
$missing_obs = $h['timestamp'] - $period;
$missing_array = $i;
}
}
$last = $h['timestamp'];
$i++;
}
if ($missing_data) {
echo '
missing data at array position ' . $missing_array . ', <br>
time ' . date('Y-m-d H:i T', $missing_obs / 1000) . ', <br>
timestamp ' . $missing_obs . ', <br>
period ' . $period . '<br>
';
var_dump($hist[$missing_array - 1]);
var_dump($hist[$missing_array]);
continue 1;
}
$count = count($hist);
$dc = array_column($hist, 'close');
foreach ($rsi as $n) {
$r = trader_rsi($dc, $n);
$j = 0;
for ($i = 0; $i < $count; $i++) {
if (isset($r[$i])) {
$hist[$i]['rsi'.$n] = $r[$i];
if (isset($r[$j])) {
if ($r[$j] <= $rsiob && $r[$i] > $rsiob)
$hist[$i]['rsi'.$n.'ob'] = 1;
else
$hist[$i]['rsi'.$n.'ob'] = 0;
if ($r[$j] >= $rsios && $r[$i] < $rsios)
$hist[$i]['rsi'.$n.'os'] = 1;
else
$hist[$i]['rsi'.$n.'os'] = 0;
}
}
$j = $i;
}
}
foreach ($ema as $n) {
$r = trader_ema($dc, $n);
$j = 0;
for ($i = 0; $i < $count; $i++) {
if (isset($r[$i])) {
$hist[$i]['ema'.$n] = $r[$i];
if (isset($r[$j])) {
if ($dc[$j] <= $r[$j] && $dc[$i] > $r[$i]) {
$hist[$i]['ema'.$n.'cu'] = 1; // crossover upwards
} else {
$hist[$i]['ema'.$n.'cu'] = 0;
}
if ($dc[$j] >= $r[$j] && $dc[$i] < $r[$i]) {
$hist[$i]['ema'.$n.'cd'] = 1; // crossover downwards
} else {
$hist[$i]['ema'.$n.'cd'] = 0;
}
}
}
$j = $i;
}
}
foreach ($roc as $n) {
$r = trader_roc($dc, $n);
for ($i = 0; $i < $count; $i++) {
if (isset($r[$i]))
$hist[$i]['roc'.$n] = $r[$i];
}
}
foreach ($corr as $key => $array) {
$query = "
SELECT DISTINCT pair_id, pair, source, currency, history_start FROM asset_pairs
WHERE analyse
AND period = " . $pair['period'] . "
AND pair LIKE '%" . $key . "/USD%'
";
$res = query($query, $config);
if (!empty($res)) {
$query = "
SELECT timestamp, close FROM price_history
WHERE pair = '" . $res[0]['pair'] . "'
AND source = '" . $res[0]['source'] . "'
AND period = '" . $period_txt . "'
AND timestamp >= " . $hist[0]['timestamp'] . "
AND timestamp <= " . $hist[$count-1]['timestamp'] . "
ORDER BY timestamp ASC
";
$res = query($query, $config);
if (!empty($res) && count($res) == $count) {
$cc = array_column($res, 'close');
foreach ($array as $n) {
$r = trader_correl($dc, $cc, $n);
for ($i = 0; $i < $count; $i++) {
if (isset($r[$i])) {
$hist[$i]['corr'.$n.strtolower($key)] = $r[$i];
}
}
}
}
}
}
$omit = array('history_id', 'timestamp', 'open', 'high', 'low', 'close', 'volume');
foreach ($hist as $h) {
$sep = '';
$query = "UPDATE price_history SET ";
foreach ($h as $key => $val) {
if (!in_array($key, $omit, TRUE)) {
$query .= $sep . $key . " = '" . $val . "'";
$sep = ',';
}
}
$query .= " WHERE history_id = " . $h['history_id'];
if ($config['debug']) echo $query . PHP_EOL;
query($query, $config);
}
}
}