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; } } }