123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- /**
- * Created by PhpStorm.
- * User: 25276
- * Date: 2018/4/23
- * Time: 17:54
- */
- /**
- * @link http://www.yiiframework.com/
- * @copyright Copyright (c) 2008 Yii Software LLC
- * @license http://www.yiiframework.com/license/
- */
- namespace console\controllers;
- use yii\console\Controller;
- use yii;
- class KdataController extends Controller
- {
- public function actionSavek($symbol,$level) {
- date_default_timezone_set("UTC");
- $isnew = 0;
- $nextSt = -1;
- $redis = Yii::$app->redis;
- $len = $redis->llen("{$symbol}_{$level}_KDATA");
- $last = $len>0?json_decode($redis->lrange("{$symbol}_{$level}_KDATA",$len-1,$len-1)[0],true)['last']:0;
- $url = 'http://47.74.9.155:45656/tender/default/kline?';
- $dataArray = array('ts'=>-1,'last'=>-1,'open'=>-1,'high'=>-1,'low'=>-1,'close'=>-1,'realVol'=>-1,'tickVol'=>-1);
- $stset = array(
- 'm1'=>60,
- 'm3'=>180,
- 'm5'=>300,
- 'm15'=>900,
- 'm30'=>1800,
- 'h1'=>3600,
- 'h2'=>7200,
- 'h4'=>14400,
- 'h6'=>21600,
- 'h12'=>43200,
- 'd1'=>86400,
- 'd3'=>259200,
- 'd5'=>432000,
- 'd7'=>604800,
- 'd15'=>1296000,
- );
- $saveset = array(
- 'm1'=>86400,//分钟线数据一天一文件
- 'h1'=>604800,//小时线数据一周一文件
- 'd1'=>2592000//日线数据30天一文件
- );
- if(isset($stset[$level])){
- $timeblock = $stset[$level];
- }
- if(isset($saveset[$level])){
- $saveblock = $saveset[$level];
- }
- $arraySetted = array();
- if(isset($stset[$level])){
- while(true){
- $urlsymbol = $url."symbol={$symbol}&offset={$last}&limit=1";
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
- curl_setopt($ch, CURLOPT_URL, $urlsymbol);
- $data = curl_exec($ch);
- curl_close($ch);
- // $data = file_get_contents($urlsymbol);
- $data = json_decode($data,true);
- echo("{$last}\n");
- echo("{$dataArray['last']}\n");
- if(sizeof($data['result'])==0){//当前为最新数据 存入或更新最新数据 控制参数置1
- if($isnew == 0&&$dataArray['ts']>0){//最新数据没被存入 则存入最新数据
- $savedata = json_encode($dataArray);
- $redis->rpush("{$symbol}_{$level}_KDATA",$savedata);
- }
- if($isnew == 1&&$dataArray['ts']>0){//当前为最新数据 更新最新数据
- $redis->rpop("{$symbol}_{$level}_KDATA");
- $savedata = json_encode($dataArray);
- $redis->rpush("{$symbol}_{$level}_KDATA",$savedata);
- }
- if(!file_exists('/tmp/kdata/log')){
- mkdir('/tmp/kdata/log',0777,true);
- }
- $logfile = fopen("/tmp/kdata/log/{$symbol}_{$level}.log",'a+');
- fwrite($logfile,'['.date('Y-m-d H:i:s',time()).']:'.$last."\n");
- fclose($logfile);
- $isnew = 1;
- continue;
- }else{
- $st = floor(strtotime($data['result'][0]['time'])/$timeblock)*$timeblock;
- $price = $data['result'][0]['price']/100000000;
- $realVol = $data['result'][0]['quantity']/100000000;
- if(isset($arraySetted[$st.$level])&&$arraySetted[$st.$level]==1){//当前时间块内的存在上一笔交易
- $dataArray['last'] = $data['result'][0]['id'];
- if($price>$dataArray['high']){
- $dataArray['high'] = $price;
- }
- if($price<$dataArray['low']){
- $dataArray['low'] = $price;
- }
- $dataArray['close'] = $price;
- $dataArray['realVol'] += $realVol;
- $dataArray['tickVol']++;
- }else{//当前时间块的第一笔交易
- if($isnew==1){//如果曾存入之前时间块的最新数据 则出栈 控制参数归零
- $redis->rpop("{$symbol}_{$level}_KDATA");
- $isnew = 0;
- }
- //打包存入上一个时间块的数据 清除$arraySetted中上个时间节点的handle
- if($dataArray['ts']>0){
- $savedata = json_encode($dataArray);
- $redis->rpush("{$symbol}_{$level}_KDATA",$savedata);
- if(isset($saveset[$level])){
- $ts = $dataArray['ts'];
- $savest = floor($ts/$saveblock)*$saveblock;//文件的时间块
- $savedate = date("Y-m-d",$savest);
- $savepath = "/tmp/kdata/{$savedate}_{$symbol}_{$level}.json";
- $savefile = fopen($savepath,'a+');
- fwrite($savefile,$savedata."\n");
- fclose($savefile);
- }
- unset($arraySetted[$dataArray['ts']]);
- }
- while($nextSt>0&&$st>$nextSt){//当前交易与上一个交易中间存在若干时间块无成交的情况
- $dataArray['ts'] = $nextSt;
- $lastclose = $dataArray['close'];
- $dataArray['open'] = $lastclose;
- $dataArray['close'] = $lastclose;
- $dataArray['high'] = $lastclose;
- $dataArray['low'] = $lastclose;
- $dataArray['realVol'] = 0;
- $dataArray['tickVol'] = 0;
- //打包存储
- $savedata = json_encode($dataArray);
- $redis->rpush("{$symbol}_{$level}_KDATA",$savedata);
- if(isset($saveset[$level])){
- $ts = $dataArray['ts'];
- $savest = floor($ts/$saveblock)*$saveblock;//文件的时间块
- $savedate = date("Y-m-d",$savest);
- $savepath = "/tmp/kdata/{$savedate}_{$symbol}_{$level}.json";
- $savefile = fopen($savepath,'a+');
- fwrite($savefile,$savedata."\n");
- fclose($savefile);
- }
- $nextSt += $timeblock;
- }
- $nextSt = $st+$timeblock;
- $dataArray['ts'] = $st;
- $dataArray['last'] = $data['result'][0]['id'];
- $dataArray['open'] = $price;
- $dataArray['high'] = $price;
- $dataArray['low'] = $price;
- $dataArray['close'] = $price;
- $dataArray['realVol'] = $realVol;
- $dataArray['tickVol'] = 1;
- $arraySetted[$st.$level] = 1;
- }
- if(!file_exists('/tmp/kdata/log')){
- mkdir('/tmp/kdata/log',0777,true);
- }
- $logfile = fopen("/tmp/kdata/log/{$symbol}_{$level}.log",'a+');
- fwrite($logfile,'['.date('Y-m-d H:i:s',time()).']:'.$last."\n");
- fclose($logfile);
- $last = $data['result'][0]['id'];
- }
- }
- }else if($level == 'mo'){
- while(true){
- $urlsymbol = $url."symbol={$symbol}&offset={$last}&limit=1";
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
- curl_setopt($ch, CURLOPT_URL, $urlsymbol);
- $data = curl_exec($ch);
- $data = json_decode($data,true);
- if(sizeof($data['result'])==0){//当前为最新数据 存入或更新最新数据 控制参数置1
- if($isnew == 0&&$dataArray['ts']>0){//最新数据没被存入 则存入最新数据
- $savedata = json_encode($dataArray);
- $redis->rpush("{$symbol}_{$level}_KDATA",$savedata);
- }
- if($isnew == 1&&$dataArray['ts']>0){//当前为最新数据 更新最新数据
- $redis->rpop("{$symbol}_{$level}_KDATA");
- $savedata = json_encode($dataArray);
- $redis->rpush("{$symbol}_{$level}_KDATA",$savedata);
- }
- if(!file_exists('/tmp/kdata/log')){
- mkdir('/tmp/kdata/log',0777,true);
- }
- $logfile = fopen("/tmp/kdata/log/{$symbol}_{$level}.log",'a+');
- fwrite($logfile,'['.date('Y-m-d H:i:s',time()).']:'.$last."\n");
- fclose($logfile);
- $isnew = 1;
- continue;
- }else{
- $st = strtotime(substr($data['result'][0]['time'],0,7)."-01");
- $price = $data['result'][0]['price']/100000000;
- $realVol = $data['result'][0]['quantity']/100000000;
- if(isset($arraySetted[$st.$level])&&$arraySetted[$st.$level]==1){//当前时间块内的存在上一笔交易
- $dataArray['last'] = $last;
- if($price>$dataArray['high']){
- $dataArray['high'] = $price;
- }
- if($price<$dataArray['low']){
- $dataArray['low'] = $price;
- }
- $dataArray['close'] = $price;
- $dataArray['realVol'] += $realVol;
- $dataArray['tickVol']++;
- }else{//当前时间块的第一笔交易
- if($isnew==1){//如果曾存入之前时间块的最新数据 则出栈 控制参数归零
- $redis->rpop("{$symbol}_{$level}_KDATA");
- $isnew = 0;
- }
- //打包存入上一个时间块的数据 清除$arraySetted中上个时间节点的handle
- if($dataArray['ts']>0){
- $savedata = json_encode($dataArray);
- $redis->rpush("{$symbol}_{$level}_KDATA",$savedata);
- unset($arraySetted[$dataArray['ts']]);
- }
- while($nextSt>0&&$st>$nextSt){//当前交易与上一个交易中间存在若干时间块无成交的情况
- $dataArray['ts'] = $nextSt;
- $lastclose = $dataArray['close'];
- $dataArray['open'] = $lastclose;
- $dataArray['close'] = $lastclose;
- $dataArray['high'] = $lastclose;
- $dataArray['low'] = $lastclose;
- $dataArray['realVol'] = 0;
- $dataArray['tickVol'] = 0;
- //打包存储
- $savedata = json_encode($dataArray);
- $redis->rpush("{$symbol}_{$level}_KDATA",$savedata);
- $nextSt = strtotime('+1month',$nextSt);
- }
- $nextSt = strtotime('+1month',strtotime(substr($data['result'][0]['time'],0,7)."-01"));
- $dataArray['ts'] = $st;
- $dataArray['last'] = $data['result'][0]['id'];
- $dataArray['open'] = $price;
- $dataArray['high'] = $price;
- $dataArray['low'] = $price;
- $dataArray['close'] = $price;
- $dataArray['realVol'] = $realVol;
- $dataArray['tickVol'] = 1;
- $arraySetted[$st.$level] = 1;
- }
- if(!file_exists('/tmp/kdata/log')){
- mkdir('/tmp/kdata/log',0777,true);
- }
- $logfile = fopen("/tmp/kdata/log/{$symbol}_{$level}.log",'a+');
- fwrite($logfile,'['.date('Y-m-d H:i:s',time()).']:'.$last."\n");
- fclose($logfile);
- $last = $data['result'][0]['id'];//下一轮的offset
- }
- }
- }else{
- echo("params error!!!");
- return;
- }
- }
- }
|