Commit dd1cd4d3 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/ticker' into 'master'

Feature/ticker See merge request !155
parents c902ae05 e0755eed
...@@ -21,6 +21,8 @@ class TickerController extends BaseController ...@@ -21,6 +21,8 @@ class TickerController extends BaseController
$page = Yii::$app->request->get('page', 1); $page = Yii::$app->request->get('page', 1);
$device_code = Yii::$app->request->get('device_code', ''); $device_code = Yii::$app->request->get('device_code', '');
$exchange = Yii::$app->request->get('exchange', 'zhaobi'); $exchange = Yii::$app->request->get('exchange', 'zhaobi');
$data_value = Yii::$app->request->get('data-value','');
$sort = Yii::$app->request->get('sort','');
$exchange_arr = ['huobi', 'binance', 'okex', 'zhaobi']; $exchange_arr = ['huobi', 'binance', 'okex', 'zhaobi'];
if (!in_array($exchange, $exchange_arr)) { if (!in_array($exchange, $exchange_arr)) {
......
...@@ -98,7 +98,7 @@ class ExchangeBusiness ...@@ -98,7 +98,7 @@ class ExchangeBusiness
goto doEnd; goto doEnd;
} }
if (strtoupper($tag) == 'GM' || strtoupper($tag) == 'BSTC' || strtoupper($tag) == 'RYH' || strtoupper($tag) == 'CNDT' || strtoupper($tag) == 'WL' || strtoupper($tag) == 'ETS' || strtoupper($tag) == 'LIMS' || strtoupper($tag) == 'AT' || strtoupper($tag) == 'BTJ') { if (strtoupper($tag) == 'STO' || strtoupper($tag) == 'GM' || strtoupper($tag) == 'BSTC' || strtoupper($tag) == 'RYH' || strtoupper($tag) == 'CNDT' || strtoupper($tag) == 'WL' || strtoupper($tag) == 'ETS' || strtoupper($tag) == 'LIMS' || strtoupper($tag) == 'AT' || strtoupper($tag) == 'BTJ') {
$quotation = [ $quotation = [
'low' => 0, 'low' => 0,
'high' => 0, 'high' => 0,
......
...@@ -15,6 +15,8 @@ class BinanceBuilder extends FactoryService ...@@ -15,6 +15,8 @@ class BinanceBuilder extends FactoryService
protected $base_url = 'https://api.binance.com'; protected $base_url = 'https://api.binance.com';
protected $supported_symbol = 'supported_symbol_binance'; protected $supported_symbol = 'supported_symbol_binance';
protected $supported_symbol_list = 'supported_symbol_binance_list'; protected $supported_symbol_list = 'supported_symbol_binance_list';
protected $supported_symbol_close_sort_list = 'supported_symbol_close_sort_binance_list';
protected $supported_symbol_change_sort_list = 'supported_symbol_change_sort_binance_list';
protected $quotation_prefix = 'quotation_binance_'; protected $quotation_prefix = 'quotation_binance_';
public function getTicker() public function getTicker()
...@@ -57,7 +59,7 @@ class BinanceBuilder extends FactoryService ...@@ -57,7 +59,7 @@ class BinanceBuilder extends FactoryService
public function getTickerFromCache($page = 1) public function getTickerFromCache($page = 1)
{ {
$size = 0; $size = 0;
for ($i = 0; $i < $page; $i ++) { for ($i = 0; $i < $page; $i++) {
$size += 50; $size += 50;
$this->end = $size; $this->end = $size;
} }
...@@ -111,4 +113,37 @@ class BinanceBuilder extends FactoryService ...@@ -111,4 +113,37 @@ class BinanceBuilder extends FactoryService
]; ];
return ['code' => $this->code, 'data' => $data]; return ['code' => $this->code, 'data' => $data];
} }
public function TickerSort()
{
$len = $this->redis->llen($this->supported_symbol_list);
$ticker = [];
for ($i = 0; $i < $len; $i++) {
$symbol = $this->redis->lindex($this->supported_symbol_list, $i);
list($close, $open) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol), 'last', 'open');
$temp = [];
$temp['symbol'] = strtoupper($symbol);
$temp['close'] = number_format($close, 6, '.', '');
$temp['change'] = (0 == $open) ? 0 : (float)sprintf("%0.4f", ($close - $open) / $open * 100);
array_push($ticker, $temp);
}
$ticker_sort_close= $this->arraySort($ticker,'close');
foreach ($ticker_sort_close as $val) {
$this->redis->lpush($this->supported_symbol_close_sort_list, $val['symbol']);
}
$ticker_change_close= $this->arraySort($ticker,'change');
foreach ($ticker_change_close as $val) {
$this->redis->lpush($this->supported_symbol_change_sort_list, $val['symbol']);
}
}
protected function arraySort($array, $keys, $sort = SORT_DESC)
{
$keysValue = [];
foreach ($array as $k => $v) {
$keysValue[$k] = $v[$keys];
}
array_multisort($keysValue, $sort, $array);
return $array;
}
} }
\ No newline at end of file
...@@ -15,6 +15,8 @@ class HuobiBuilder extends FactoryService ...@@ -15,6 +15,8 @@ class HuobiBuilder extends FactoryService
protected $base_url = 'https://api.huobi.pro'; protected $base_url = 'https://api.huobi.pro';
protected $supported_symbol = 'supported_symbol_huobi'; protected $supported_symbol = 'supported_symbol_huobi';
protected $supported_symbol_list = 'supported_symbol_huobi_list'; protected $supported_symbol_list = 'supported_symbol_huobi_list';
protected $supported_symbol_close_sort_list = 'supported_symbol_close_sort_huobi_list';
protected $supported_symbol_change_sort_list = 'supported_symbol_change_sort_huobi_list';
protected $quotation_prefix = 'quotation_huobi_'; protected $quotation_prefix = 'quotation_huobi_';
public function getTicker() public function getTicker()
...@@ -111,6 +113,38 @@ class HuobiBuilder extends FactoryService ...@@ -111,6 +113,38 @@ class HuobiBuilder extends FactoryService
]; ];
return ['code' => $this->code, 'data' => $data]; return ['code' => $this->code, 'data' => $data];
} }
public function TickerSort()
{
$len = $this->redis->llen($this->supported_symbol_list);
$ticker = [];
for ($i = 0; $i < $len; $i++) {
$symbol = $this->redis->lindex($this->supported_symbol_list, $i);
list($close, $open) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol), 'last', 'open');
$temp = [];
$temp['symbol'] = strtoupper($symbol);
$temp['close'] = number_format($close, 6, '.', '');
$temp['change'] = (0 == $open) ? 0 : (float)sprintf("%0.4f", ($close - $open) / $open * 100);
array_push($ticker, $temp);
}
$ticker_sort_close= $this->arraySort($ticker,'close');
foreach ($ticker_sort_close as $val) {
$this->redis->lpush($this->supported_symbol_close_sort_list, $val['symbol']);
}
$ticker_change_close= $this->arraySort($ticker,'change');
foreach ($ticker_change_close as $val) {
$this->redis->lpush($this->supported_symbol_change_sort_list, $val['symbol']);
}
}
protected function arraySort($array, $keys, $sort = SORT_DESC)
{
$keysValue = [];
foreach ($array as $k => $v) {
$keysValue[$k] = $v[$keys];
}
array_multisort($keysValue, $sort, $array);
return $array;
}
public function getHotTicker() public function getHotTicker()
{ {
......
...@@ -13,6 +13,11 @@ use linslin\yii2\curl\Curl; ...@@ -13,6 +13,11 @@ use linslin\yii2\curl\Curl;
class ZhaobiBuilder extends FactoryService class ZhaobiBuilder extends FactoryService
{ {
protected $base_url = 'https://api.biqianbao.top'; protected $base_url = 'https://api.biqianbao.top';
protected $supported_symbol = 'supported_symbol_zhaobi';
protected $supported_symbol_list = 'supported_symbol_zhaobi_list';
protected $supported_symbol_close_sort_list = 'supported_symbol_close_sort_zhaobi_list';
protected $supported_symbol_change_sort_list = 'supported_symbol_change_sort_zhaobi_list';
protected $quotation_prefix = 'quotation_zhaobi_';
public function getTickerFromCache() public function getTickerFromCache()
{ {
...@@ -61,6 +66,35 @@ class ZhaobiBuilder extends FactoryService ...@@ -61,6 +66,35 @@ class ZhaobiBuilder extends FactoryService
return ['code' => $this->code, 'data' => $data]; return ['code' => $this->code, 'data' => $data];
} }
public function TickerSort()
{
$keys = $this->redis->smembers($this->supported_symbol);
if (false == $this->redis->exists($this->supported_symbol_list)) {
foreach ($keys as $val) {
$this->redis->lpush($this->supported_symbol_list, $val);
}
}
$len = $this->redis->llen($this->supported_symbol_list);
$ticker = [];
for ($i = 0; $i < $len; $i++) {
$symbol = $this->redis->lindex($this->supported_symbol_list, $i);
list($close, $open) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol), 'last', 'open');
$temp = [];
$temp['symbol'] = strtoupper($symbol);
$temp['close'] = number_format($close, 6, '.', '');
$temp['change'] = (0 == $open) ? 0 : (float)sprintf("%0.4f", ($close - $open) / $open * 100);
array_push($ticker, $temp);
}
$ticker_sort_close= $this->arraySort($ticker,'close');
foreach ($ticker_sort_close as $val) {
$this->redis->lpush($this->supported_symbol_close_sort_list, $val['symbol']);
}
$ticker_change_close= $this->arraySort($ticker,'change');
foreach ($ticker_change_close as $val) {
$this->redis->lpush($this->supported_symbol_change_sort_list, $val['symbol']);
}
}
public function getNotice($params = []) public function getNotice($params = [])
{ {
$curl = new Curl(); $curl = new Curl();
...@@ -90,6 +124,15 @@ class ZhaobiBuilder extends FactoryService ...@@ -90,6 +124,15 @@ class ZhaobiBuilder extends FactoryService
]; ];
} }
return ['code' => $this->code, 'notice' => $data]; return ['code' => $this->code, 'notice' => $data];
}
protected function arraySort($array, $keys, $sort = SORT_DESC)
{
$keysValue = [];
foreach ($array as $k => $v) {
$keysValue[$k] = $v[$keys];
}
array_multisort($keysValue, $sort, $array);
return $array;
} }
} }
\ No newline at end of file
<?php
namespace console\controllers;
use common\business\Chain33Business;
use common\models\psources\CoinGameBet;
use common\service\chain33\Chain33Service;
use common\service\exchange\ExchangeBuilderFactory;
use yii\console\Controller;
use Yii;
class TickerSortController extends Controller
{
/**
* 行情排序
*/
public function actionIndex()
{
$class = ['Binance', 'Huobi', 'Zhaobi'];
foreach ($class as $val) {
go(function () use ($val) {
\Co::sleep(0.5);
$ticker_builder = ExchangeBuilderFactory::create($val);
$ticker_builder->TickerSort();
});
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment