Skip to content

Commit

Permalink
Add expired time for orderbook
Browse files Browse the repository at this point in the history
  • Loading branch information
kleninmaxim committed May 25, 2022
1 parent 2193740 commit 4bc2940
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/common_config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
const CONFIG = [
'exchange' => EXCHANGE, //название биржи
'exchanges' => [EXCHANGE], //список всех бирж для взятие всех данных из memcached
'expired_orderbook_time' => 5, //сколько по времени в секундах считаем ордербуки актуальными
'min_profit' => [
'BTC' => 0,
'ETH' => 0,
Expand Down
2 changes: 2 additions & 0 deletions kernel/receive_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ function handler_orderbooks(string $message): void
// если event как data, а node как gate
if ($data['event'] == 'data' && $data['node'] == 'gate' && $data['action'] == 'orderbook' && isset($data['data'])) {

$data['data']['core_timestamp'] = microtime(true);

// записать в memcached
$memcached->set(
$data['exchange'] . '_' . $data['action'] . '_' . $data['data']['symbol'],
Expand Down
16 changes: 15 additions & 1 deletion src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ private function getAllMemcachedKeys(): array
private function reformatAndSeparateData(array $memcached_data): array
{

$microtime = microtime(true);

foreach ($memcached_data as $key => $data) {

if (isset($data)) {
Expand All @@ -70,11 +72,23 @@ private function reformatAndSeparateData(array $memcached_data): array
$value = $parts[2] ?? null;

if ($action == 'balances') {

$balances[$exchange] = $data;

} elseif ($action == 'orderbook' && $value) {
$orderbooks[$value][$exchange] = $data;

if (
($microtime - $data['core_timestamp']) <= $this->config['expired_orderbook_time']
) {

$orderbooks[$value][$exchange] = $data;

}

} else {

$undefined[$key] = $data;

}

}
Expand Down

0 comments on commit 4bc2940

Please sign in to comment.