-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.tactics.php
403 lines (317 loc) · 12.3 KB
/
functions.tactics.php
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
/* process bot actions for trading tactics */
function actionable_tactics($config, $tactic_id = FALSE) {
/* query actionable tactics */
$values['filterquery'] = " WHERE status = 'actionable'";
if ($tactic_id) $values['filterquery'] .= " AND tactic_id IN (" . $tactic_id . ")";
$tactics = query('get_tactics', $config, $values);
if ($config['debug']) { print_r($tactics); }
# process each action
if (!empty($tactics))
foreach ($tactics as $t) {
# check if time limit for executing action has been exceeded, for instance when order attempts have failed
if ($t['currency'] + $t['action_time_limit'] * 60000 < $config['timestamp']) {
$query = "
UPDATE tactics SET
status = 'failed',
currency = " . $config['timestamp'] . ",
WHERE tactic_id = " . $t['tactic_id']
;
query($query, $config);
$config['chatText'] = 'actionable tactic unable to execute for tactic_id ' . $t['tactic_id'];
telegram($config);
continue 1;
}
$config['exchange'] = $t['exchange'];
$config = config_exchange($config);
# if action is to place an order
if (in_array($t['action'], array('limit', 'market'), TRUE)) {
# construct order
$order_query = array(
'exchange' => $t['exchange'],
'symbol' => $t['pair_asset'],
'orderType' => $t['action']
);
if (strpos($t['pair_asset'], $t['from_asset'] . '/') !== FALSE) {
$order_query['side'] = 'sell';
} else {
$order_query['side'] = 'buy';
}
if (!empty($t['from_amount'])) {
$order_query['orderQty'] = $t['from_amount'];
} elseif (!empty($t['from_percent'])) {
# to do:
# if percent is defined instead of amount,
# query available funds on exchange and calculate trade amount from percent
}
if ($order_query['orderType'] == 'limit') $order_query['orderPrice'] = $t['trade_price'];
switch($t['exchange']) {
case 'bitmax':
# define response requested from exchange
$order_query['respInst'] = 'ACCEPT';
break;
case 'okex':
# 0: limit order (max price and quantity)
# 1: market order (quantity only)
# 2: fill or kill (ignore)
# 3: immediate or cancel (ignore)
if ($t['action'] == 'limit') $order_query['orderTypeCode'] = 0;
if ($t['action'] == 'market') $order_query['orderTypeCode'] = 1;
break;
}
# place order
$result = place_order($config, $order_query, $orderId = '');
#var_dump($result);
# process response of order placed success
if ($result['res']['placed']) {
# slight risk order is placed but response not received
# could check number of open orders matches number in tactics and/or transactions
# ignore risk for now
# record order in transactions table
$query = "
INSERT INTO transactions
(exchange, exchange_transaction_id)
VALUES
('" . $t['exchange'] . "', '" . $result['res']['exchange_transaction_id'] . "')
";
query($query, $config);
# record order in tactics table
$query = "
SELECT transaction_id FROM transactions
WHERE exchange = '" . $t['exchange'] . "'
AND exchange_transaction_id = '" . $result['res']['exchange_transaction_id'] . "'
";
$id = query($query, $config);
$query = "
UPDATE tactics SET
status = 'ordered',
currency = " . $config['timestamp'] . ",
transaction_id = " . $id[0]['transaction_id'] . "
WHERE tactic_id = " . $t['tactic_id']
;
query($query, $config);
# note that we can leave order history to update itself
} else {
# process response of order placed failure
# if an order fails send a message to telegram
# next time actionable_tactics function is called, bot will try to place order again
$config['chatText'] = 'order attempt failed for tactic_id ' . $t['tactic_id'];
telegram($config);
}
}
/* if action is to delete order */
if ($t['action'] == 'delete') {
/* get tactic's order */
$values['filterquery'] = " WHERE transaction_id = '" . $t['transaction_id'] . "'";
$transaction = query('get_transactions', $config, $values);
/* delete order */
$order_query = array();
#$order['id'] = $t['transaction_id']; # optional, for echo back
$order_query['orderId'] = $transaction[0]['exchange_transaction_id'];
$order_query['symbol'] = $transaction[0]['pair_asset'];
$result = cancel_order($config, $order_query);
# process response of order deleted success
if ($result['res']['placed']) {
$transaction = check_transaction($config, $t['transaction_id']);
if ($transaction !== FALSE && $transaction['exchange_transaction_status'] == 'cancelled') {
$query = "
UPDATE tactics SET
status = 'inactive',
currency = " . $config['timestamp'] . ",
action = 'none'
WHERE tactic_id = " . $t['tactic_id']
;
query($query, $config);
}
} else {
# process response of order deleted failure
$config['chatText'] = 'delete order attempt failed for tactic_id ' . $t['tactic_id'];
telegram($config);
}
}
}
}
/* test conditions for tactics and update status if conditions met */
function conditional_tactics($config, $tactic_id = FALSE) {
$subquery = "";
if ($tactic_id) $subquery = " AND tactic_id IN (" . $tactic_id . ")";
/* query current tactics */
$values['filterquery'] = "
WHERE status = 'conditional'
AND currency < (" . $config['timestamp'] . " - refresh * 60000)
" . $subquery . "
ORDER BY condition_tactic DESC
";
$tactics = query('get_tactics', $config, $values);
if ($config['debug']) print_r($tactics);
# process each tactic
if (!empty($tactics))
foreach ($tactics as $t) {
$update = FALSE;
/*
notes:
there are 3 possible conditions, recorded in tactics table in database:
time delay condition (condition_time)
dependent tactic condition (condition_tactic)
pair price indicator condition (condition_pair_*)
condition_time requires that a fixed amount of time has passed
condition_tactic requires that another tactic status has changed to 'executed'
condition_pair_* requires that a pair price indicator value passes a threshold value
all 3 conditions need to be met for tactic status to change from 'conditional' to 'actionable'
when a condition is met, its test variable is set to 1
if a condition is not yet met, its test variable is set to 0
the test variables are:
condition_time_test
condition_tactic_test
condition_pair_test
if any conditions are not used for a tactic, the test variable is set to 1 by default
*/
# test time delay condition (condition_time), if not yet satisfied (condition_time_test=0)
if (!$t['condition_time_test']) {
if ($t['condition_time'] < $config['timestamp']) {
$t['condition_time_test'] = 1;
$update = TRUE;
if ($config['debug']) echo 'condition_time_test positive' . PHP_EOL;
}
}
# test dependent tactic condition (condition_tactic), if not yet satisfied (condition_tactic_test=0)
# and time delay condition already satisfied
if (!$t['condition_tactic_test'] && $t['condition_time_test']) {
$values['filterquery'] = " WHERE tactic_id = " . $t['condition_tactic'];
$res = query('get_tactics', $config, $values);
if ($res[0]['status'] == 'executed') {
$t['condition_tactic_test'] = 1;
$update = TRUE;
if ($config['debug']) echo 'condition_tactic_test positive' . PHP_EOL;
} elseif ($res[0]['status'] == 'inactive' || $res[0]['status'] == 'failed') {
# if dependent tactic is inactive or failed, change status
/* change tactic status */
$values['tactic_id'] = $res[0]['tactic_id'];
$values['field'] = 'status';
$values['value'] = 'inactive';
query('update_tactic', $config, $values);
continue 1;
}
}
# test pair price indicator condition (condition_pair_*), if not yet satisfied (condition_pair_test=0)
# and time delay and dependent tactic conditions already satisfied
if (!$t['condition_pair_test'] && $t['condition_time_test'] && $t['condition_tactic_test']) {
# query the most recent price and calculate indicators first
price_recent($config, $t['condition_pair_id']);
technical_analysis($config, $t['condition_pair_id']);
# assess threshold value
$query = "SELECT * FROM asset_pairs WHERE pair_id = " . $t['condition_pair_id'];
$res = query($query, $config);
$a = $res[0];
$currency_min = $config['timestamp'] - $t['condition_pair_currency_min'] * 60000;
if ($config['debug']) echo 'condition_pair_test if ' . $a['currency'] . ' >= ' . $currency_min . PHP_EOL;
# check if the asset_pair is current enough, also depends on asset_pair refresh rate
if ($a['currency'] >= $currency_min) {
$query = "
SELECT history_id FROM price_history
WHERE pair = '" . $a['pair'] . "'
AND source = '" . $a['source'] . "'
AND period = '" . $config['period'][$a['period']] . "'
AND timestamp = " . $a['currency'] . "
AND " . $t['condition_pair_indicator'] . $t['condition_pair_value_operand'] . $t['condition_pair_value']
;
$res = query($query, $config);
if ($config['debug']) echo 'condition_pair_test: ' . $query . PHP_EOL;
# if any result was returned from the sql query this proves the condition has been met
if (!empty($res)) {
$t['condition_pair_test'] = 1;
$update = TRUE;
if ($config['debug']) echo 'condition_pair_test positive' . PHP_EOL;
}
}
}
# test if all 3 condition tests have been met, and if the status has changed ($update=TRUE)
if (
$t['condition_pair_test'] &&
$t['condition_time_test'] &&
$t['condition_tactic_test'] &&
$update
) {
if ($config['debug']) echo 'status change' . PHP_EOL;
if ($t['action'] == 'none') {
$t['status'] = 'executed';
} else {
$t['status'] = 'actionable';
$config['chatText'] =
'trade ready, attempting to ' . $t['action'] .
' order for ' . $t['from_asset'] . ' ' . $t['to_asset'] .
' on ' . $t['exchange'];
telegram($config);
}
}
# update the tactics status if necessary
if ($update) {
$query = "
UPDATE tactics SET
status = '" . $t['status'] . "'
,condition_tactic_test = " . $t['condition_tactic_test'] . "
,condition_time_test = " . $t['condition_time_test'] . "
,condition_pair_test = " . $t['condition_pair_test'] . "
,currency = " . $config['timestamp'] . "
WHERE tactic_id = " . $t['tactic_id']
;
query($query, $config);
if ($config['debug']) echo 'update record' . PHP_EOL;
}
}
}
/* check order status for tactics with placed orders and update tactic status if order is complete */
function ordered_tactics($config, $tactic_id = FALSE) {
/* query tactics */
$subquery = "";
if ($tactic_id) $subquery = " AND tactic_id IN (" . $_GET['tactic_id'] . ")";
$values['filterquery'] = "
WHERE status = 'ordered'
AND currency < (" . $config['timestamp'] . " - refresh * 60000)" .
$subquery
;
if ($tactic_id) $values['filterquery'] .= " AND tactic_id IN (" . $tactic_id . ")";
$tactics = query('get_tactics', $config, $values);
if ($config['debug']) { print_r($tactics); }
if (!empty($tactics))
foreach ($tactics as $t) {
/* get tactic's order */
if (!empty($t['transaction_id'])) {
$order = check_transaction($config, $t['transaction_id']);
} else {
# process response of transaction_id not recorded
$config['chatText'] = 'query tactic transaction_id attempt failed for tactic_id ' . $t['tactic_id'];
telegram($config);
continue 1;
}
if ($config['debug']) { echo $config['url'] . PHP_EOL; print_r($order); }
$update = FALSE;
/* check if order completed */
if ($order['exchange_transaction_status'] == 'complete') {
$values['value'] = 'executed';
$update = TRUE;
}
/* check if order cancelled */
if ($order['exchange_transaction_status'] == 'cancelled') {
$values['value'] = 'inactive';
$update = TRUE;
}
/* process update */
if ($update) {
/* change tactic status */
$values['tactic_id'] = $t['tactic_id'];
$values['field'] = 'status';
query('update_tactic', $config, $values);
/* query any conditional tactics */
$values['filterquery'] = "
WHERE status = 'conditional'
AND condition_tactic = '" . $t['tactic_id'] . "'"
;
$triggers = query('get_tactics', $config, $values);
if ($config['debug']) print_r($triggers);
if (!empty($triggers))
foreach ($triggers as $trigger)
conditional_tactics($config, $trigger['tactic_id']);
}
}
}