-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalerts.php
More file actions
246 lines (227 loc) · 7.33 KB
/
alerts.php
File metadata and controls
246 lines (227 loc) · 7.33 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
include('includes.php');
echo '<pre>';
// delete alert
if (isset($_GET['delete']) && $_GET['delete'] > 0) {
$query = "DELETE FROM tactics WHERE tactic_id = {$_GET['delete']}";
query($query, $config);
header("Location: " . strtok($_SERVER["REQUEST_URI"], "?"));
exit;
}
// Load data
if (isset($_GET['pair_id_load'])) {
$pair_id_load = $_GET['pair_id_load'];
} else {
$pair_id_load = 1;
}
// Fetch data from database for input
$query = "SELECT * FROM asset_pairs WHERE pair_id = {$pair_id_load}";
$pair_load = query($query, $config);
$pair_load = $pair_load[0];
$query = "
SELECT * FROM price_history
WHERE pair = '{$pair_load['pair']}'
AND source = '{$pair_load['source']}'
AND period = '{$config['period'][$pair_load['period']]}'
ORDER BY timestamp DESC
LIMIT 20
";
$data = query($query, $config);
// Form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$pair_id = $_POST['pair_id'];
$pair_indicator = $_POST['pair_indicator'];
$pair_value_operand = $_POST['pair_value_operand'];
$pair_value = $_POST['pair_value'];
$condition_tactic = $_POST['condition_tactic'];
if (is_numeric($condition_tactic) && $condition_tactic > 0) $condition_tactic_test = 0;
else $condition_tactic_test = 1;
$note = $_POST['note'];
// Fetch data from database for input
$query = "SELECT * FROM asset_pairs WHERE pair_id = {$pair_id}";
$pair = query($query, $config);
$pair = $pair[0];
// Insert data into database
$query = "INSERT INTO tactics (
status
, condition_tactic_test
, condition_tactic
, condition_pair_test
, condition_pair_id
, condition_pair_currency_min
, condition_pair_indicator
, condition_pair_value_operand
, condition_pair_value
, action
, note
) VALUES (
'conditional'
, '{$condition_tactic_test}'
, '{$condition_tactic}'
, '0'
, '{$pair_id}'
, '60'
, '{$pair_indicator}'
, '{$pair_value_operand}'
, '{$pair_value}'
, 'alert'
, '{$note}'
)";
query($query, $config);
}
// Fetch data from database for form
$query = "SELECT * FROM asset_pairs WHERE collect = '1' AND analyse = '1'";
$pairs = query($query, $config);
// var_dump($pairs);die;
$indicators = technical_analysis($config, FALSE, TRUE);
// print_r($indicators);die;
// Fetch data from database for summary table
$query = "
SELECT t.*, ap.pair, ap.source, ap.reference, ap.period
FROM tactics t
LEFT JOIN asset_pairs ap
ON t.condition_pair_id = ap.pair_id
WHERE t.action = 'alert'
ORDER BY status DESC
";
$alerts = query($query, $config);
foreach ($alerts as $key => $r) {
$query = "
SELECT timestamp, {$r['condition_pair_indicator']}
FROM price_history
WHERE pair = '{$r['pair']}'
AND period = '{$config['period'][$r['period']]}'
AND source = '{$r['source']}'
ORDER BY timestamp DESC
LIMIT 1
";
$res = query($query, $config);
$res = $res[0];
$alerts[$key]['condition_pair_value'] = rtrim(rtrim($r['condition_pair_value'], "0"), ".");
$alerts[$key]['as_at'] = date('Y-m-d', $res['timestamp']/1000);
$alerts[$key]['value'] = rtrim(rtrim($res[$r['condition_pair_indicator']], "0"), ".");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Alerts</title>
</head>
<script>
function reloadPage() {
var selectedValue = document.getElementById("pairSelect").value;
window.location.href = window.location.pathname + '?pair_id_load=' + selectedValue;
}
</script>
<body>
<h2>Submit</h2>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Pair: <select name = "pair_id" id = "pairSelect" onchange="reloadPage()">
<?php
if ($pairs != 0) {
echo "<option value=''>--</option>";
foreach ($pairs as $row) {
echo "<option value='".$row['pair_id']."' ".($pair_id_load == $row['pair_id'] ? 'selected' : '').">"
.$row['pair']." "
.$config['period'][$row['period']]
."</option>";
}
} else {
echo "<option value=''>No asset pairs available</option>";
}
?>
</select><br>
Indicator: <select name = "pair_indicator">
<?php
if ($indicators != 0) {
foreach ($indicators as $key => $val) {
echo "<option value='".$key."'>".$val."</option>";
}
} else {
echo "<option value=''>No indicators available</option>";
}
?>
</select><br>
Operand: <select name = "pair_value_operand">
<option value=">=">>=</option>
<option value="<="><=</option>
<option value="=">==</option>
</select><br>
Value:<input type="text" name="pair_value"><br>
Note:<input type="text" name="note"> (optional)<br>
Conditional id:<input type="text" name="condition_tactic"> (optional)<br>
<input type="submit" value="Submit">
</form>
<h2>Alerts</h2>
<table border="1">
<tr>
<th>id</th>
<th>status</th>
<th>refresh</th>
<th>pair</th>
<th>period</th>
<th>con_id</th>
<th>condition</th>
<th>satisfied</th>
<th>value</th>
<th>as at</th>
<th>note</th>
<th>delete</th>
</tr>
<?php
if ($alerts != 0) {
foreach ($alerts as $row) {
echo "<tr>
<td>".$row['tactic_id']."</td>
<td>".$row['status']."</td>
<td>".$config['period'][$row['refresh']]."</td>
<td>".$row['pair']."</td>
<td>".$config['period'][$row['period']]."</td>
<td>".$row['condition_tactic']."</td>
<td>".$row['condition_pair_indicator']." "
.$row['condition_pair_value_operand']." "
.$row['condition_pair_value']."</td>
<td>".($row['condition_pair_test'] ? 'true' : 'false')."</td>
<td>".$row['value']."</td>
<td>".$row['as_at']."</td>
<td>".$row['note']."</td>
<td><a href='?delete=".$row['tactic_id']."'>X</a></td>
</tr>";
}
} else {
echo "<tr><td colspan='3'>No data available</td></tr>";
}
?>
</table>
<?php
echo "<h2>Data {$pair_load['pair']} {$config['period'][$pair_load['period']]}</h2>Source: {$pair_load['reference']}";
?>
<table border="1">
<?php
if ($pair_id_load) {
echo '<tr><th>time</th>';
foreach ($indicators as $key => $val) {
echo '<th>' . $key . '</th>';
}
echo '</tr>';
foreach ($data as $row) {
$time = date('Y-m-d', $row['timestamp']/1000);
echo "<tr><td>{$time}</td>";
foreach ($indicators as $key => $val) {
$d = rtrim($row[$key], "0");
$d = rtrim($d, ".");
echo "<td>".$d."</td>";
}
echo "</tr>";
}
} else {
echo "Select pair to load data";
}
?>
</table>
</body>
</html>
<?php
// Close database connection
// $conn->close();
?>