KdataController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 25276
  5. * Date: 2018/4/23
  6. * Time: 17:54
  7. */
  8. /**
  9. * @link http://www.yiiframework.com/
  10. * @copyright Copyright (c) 2008 Yii Software LLC
  11. * @license http://www.yiiframework.com/license/
  12. */
  13. namespace console\controllers;
  14. use yii\console\Controller;
  15. use yii;
  16. class KdataController extends Controller
  17. {
  18. public function actionSavek($symbol,$level) {
  19. date_default_timezone_set("UTC");
  20. $isnew = 0;
  21. $nextSt = -1;
  22. $redis = Yii::$app->redis;
  23. $len = $redis->llen("{$symbol}_{$level}_KDATA");
  24. $last = $len>0?json_decode($redis->lrange("{$symbol}_{$level}_KDATA",$len-1,$len-1)[0],true)['last']:0;
  25. $url = 'http://47.74.9.155:45656/tender/default/kline?';
  26. $dataArray = array('ts'=>-1,'last'=>-1,'open'=>-1,'high'=>-1,'low'=>-1,'close'=>-1,'realVol'=>-1,'tickVol'=>-1);
  27. $stset = array(
  28. 'm1'=>60,
  29. 'm3'=>180,
  30. 'm5'=>300,
  31. 'm15'=>900,
  32. 'm30'=>1800,
  33. 'h1'=>3600,
  34. 'h2'=>7200,
  35. 'h4'=>14400,
  36. 'h6'=>21600,
  37. 'h12'=>43200,
  38. 'd1'=>86400,
  39. 'd3'=>259200,
  40. 'd5'=>432000,
  41. 'd7'=>604800,
  42. 'd15'=>1296000,
  43. );
  44. $saveset = array(
  45. 'm1'=>86400,//分钟线数据一天一文件
  46. 'h1'=>604800,//小时线数据一周一文件
  47. 'd1'=>2592000//日线数据30天一文件
  48. );
  49. if(isset($stset[$level])){
  50. $timeblock = $stset[$level];
  51. }
  52. if(isset($saveset[$level])){
  53. $saveblock = $saveset[$level];
  54. }
  55. $arraySetted = array();
  56. if(isset($stset[$level])){
  57. while(true){
  58. $urlsymbol = $url."symbol={$symbol}&offset={$last}&limit=1";
  59. $ch = curl_init();
  60. curl_setopt($ch, CURLOPT_HEADER, 0);
  61. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
  62. curl_setopt($ch, CURLOPT_URL, $urlsymbol);
  63. $data = curl_exec($ch);
  64. curl_close($ch);
  65. // $data = file_get_contents($urlsymbol);
  66. $data = json_decode($data,true);
  67. echo("{$last}\n");
  68. echo("{$dataArray['last']}\n");
  69. if(sizeof($data['result'])==0){//当前为最新数据 存入或更新最新数据 控制参数置1
  70. if($isnew == 0&&$dataArray['ts']>0){//最新数据没被存入 则存入最新数据
  71. $savedata = json_encode($dataArray);
  72. $redis->rpush("{$symbol}_{$level}_KDATA",$savedata);
  73. }
  74. if($isnew == 1&&$dataArray['ts']>0){//当前为最新数据 更新最新数据
  75. $redis->rpop("{$symbol}_{$level}_KDATA");
  76. $savedata = json_encode($dataArray);
  77. $redis->rpush("{$symbol}_{$level}_KDATA",$savedata);
  78. }
  79. if(!file_exists('/tmp/kdata/log')){
  80. mkdir('/tmp/kdata/log');
  81. }
  82. $logfile = fopen("/tmp/kdata/log/{$symbol}_{$level}.log",'a+');
  83. fwrite($logfile,'['.date('Y-m-d H:i:s',time()).']:'.$last."\n");
  84. fclose($logfile);
  85. $isnew = 1;
  86. continue;
  87. }else{
  88. $st = floor(strtotime($data['result'][0]['time'])/$timeblock)*$timeblock;
  89. $price = $data['result'][0]['price']/100000000;
  90. $realVol = $data['result'][0]['quantity']/100000000;
  91. if(isset($arraySetted[$st.$level])&&$arraySetted[$st.$level]==1){//当前时间块内的存在上一笔交易
  92. $dataArray['last'] = $data['result'][0]['id'];
  93. if($price>$dataArray['high']){
  94. $dataArray['high'] = $price;
  95. }
  96. if($price<$dataArray['low']){
  97. $dataArray['low'] = $price;
  98. }
  99. $dataArray['close'] = $price;
  100. $dataArray['realVol'] += $realVol;
  101. $dataArray['tickVol']++;
  102. }else{//当前时间块的第一笔交易
  103. if($isnew==1){//如果曾存入之前时间块的最新数据 则出栈 控制参数归零
  104. $redis->rpop("{$symbol}_{$level}_KDATA");
  105. $isnew = 0;
  106. }
  107. //打包存入上一个时间块的数据 清除$arraySetted中上个时间节点的handle
  108. if($dataArray['ts']>0){
  109. $savedata = json_encode($dataArray);
  110. $redis->rpush("{$symbol}_{$level}_KDATA",$savedata);
  111. if(isset($saveset[$level])){
  112. $ts = $dataArray['ts'];
  113. $savest = floor($ts/$saveblock)*$saveblock;//文件的时间块
  114. $savedate = date("Y-m-d",$savest);
  115. $savepath = "/tmp/kdata/{$savedate}_{$symbol}_{$level}.json";
  116. $savefile = fopen($savepath,'a+');
  117. fwrite($savefile,$savedata."\n");
  118. fclose($savefile);
  119. }
  120. unset($arraySetted[$dataArray['ts']]);
  121. }
  122. while($nextSt>0&&$st>$nextSt){//当前交易与上一个交易中间存在若干时间块无成交的情况
  123. $dataArray['ts'] = $nextSt;
  124. $lastclose = $dataArray['close'];
  125. $dataArray['open'] = $lastclose;
  126. $dataArray['close'] = $lastclose;
  127. $dataArray['high'] = $lastclose;
  128. $dataArray['low'] = $lastclose;
  129. $dataArray['realVol'] = 0;
  130. $dataArray['tickVol'] = 0;
  131. //打包存储
  132. $savedata = json_encode($dataArray);
  133. $redis->rpush("{$symbol}_{$level}_KDATA",$savedata);
  134. if(isset($saveset[$level])){
  135. $ts = $dataArray['ts'];
  136. $savest = floor($ts/$saveblock)*$saveblock;//文件的时间块
  137. $savedate = date("Y-m-d",$savest);
  138. $savepath = "/tmp/kdata/{$savedate}_{$symbol}_{$level}.json";
  139. $savefile = fopen($savepath,'a+');
  140. fwrite($savefile,$savedata."\n");
  141. fclose($savefile);
  142. }
  143. $nextSt += $timeblock;
  144. }
  145. $nextSt = $st+$timeblock;
  146. $dataArray['ts'] = $st;
  147. $dataArray['last'] = $data['result'][0]['id'];
  148. $dataArray['open'] = $price;
  149. $dataArray['high'] = $price;
  150. $dataArray['low'] = $price;
  151. $dataArray['close'] = $price;
  152. $dataArray['realVol'] = $realVol;
  153. $dataArray['tickVol'] = 1;
  154. $arraySetted[$st.$level] = 1;
  155. }
  156. if(!file_exists('/tmp/kdata/log')){
  157. mkdir('/tmp/kdata/log');
  158. }
  159. $logfile = fopen("/tmp/kdata/log/{$symbol}_{$level}.log",'a+');
  160. fwrite($logfile,'['.date('Y-m-d H:i:s',time()).']:'.$last."\n");
  161. fclose($logfile);
  162. $last = $data['result'][0]['id'];
  163. }
  164. }
  165. }else if($level == 'mo'){
  166. while(true){
  167. $urlsymbol = $url."symbol={$symbol}&offset={$last}&limit=1";
  168. $ch = curl_init();
  169. curl_setopt($ch, CURLOPT_HEADER, 0);
  170. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
  171. curl_setopt($ch, CURLOPT_URL, $urlsymbol);
  172. $data = curl_exec($ch);
  173. $data = json_decode($data,true);
  174. if(sizeof($data['result'])==0){//当前为最新数据 存入或更新最新数据 控制参数置1
  175. if($isnew == 0&&$dataArray['ts']>0){//最新数据没被存入 则存入最新数据
  176. $savedata = json_encode($dataArray);
  177. $redis->rpush("{$symbol}_{$level}_KDATA",$savedata);
  178. }
  179. if($isnew == 1&&$dataArray['ts']>0){//当前为最新数据 更新最新数据
  180. $redis->rpop("{$symbol}_{$level}_KDATA");
  181. $savedata = json_encode($dataArray);
  182. $redis->rpush("{$symbol}_{$level}_KDATA",$savedata);
  183. }
  184. if(!file_exists('/tmp/kdata/log')){
  185. mkdir('/tmp/kdata/log');
  186. }
  187. $logfile = fopen("/tmp/kdata/log/{$symbol}_{$level}.log",'a+');
  188. fwrite($logfile,'['.date('Y-m-d H:i:s',time()).']:'.$last."\n");
  189. fclose($logfile);
  190. $isnew = 1;
  191. continue;
  192. }else{
  193. $st = strtotime(substr($data['result'][0]['time'],0,7)."-01");
  194. $price = $data['result'][0]['price']/100000000;
  195. $realVol = $data['result'][0]['quantity']/100000000;
  196. if(isset($arraySetted[$st.$level])&&$arraySetted[$st.$level]==1){//当前时间块内的存在上一笔交易
  197. $dataArray['last'] = $last;
  198. if($price>$dataArray['high']){
  199. $dataArray['high'] = $price;
  200. }
  201. if($price<$dataArray['low']){
  202. $dataArray['low'] = $price;
  203. }
  204. $dataArray['close'] = $price;
  205. $dataArray['realVol'] += $realVol;
  206. $dataArray['tickVol']++;
  207. }else{//当前时间块的第一笔交易
  208. if($isnew==1){//如果曾存入之前时间块的最新数据 则出栈 控制参数归零
  209. $redis->rpop("{$symbol}_{$level}_KDATA");
  210. $isnew = 0;
  211. }
  212. //打包存入上一个时间块的数据 清除$arraySetted中上个时间节点的handle
  213. if($dataArray['ts']>0){
  214. $savedata = json_encode($dataArray);
  215. $redis->rpush("{$symbol}_{$level}_KDATA",$savedata);
  216. unset($arraySetted[$dataArray['ts']]);
  217. }
  218. while($nextSt>0&&$st>$nextSt){//当前交易与上一个交易中间存在若干时间块无成交的情况
  219. $dataArray['ts'] = $nextSt;
  220. $lastclose = $dataArray['close'];
  221. $dataArray['open'] = $lastclose;
  222. $dataArray['close'] = $lastclose;
  223. $dataArray['high'] = $lastclose;
  224. $dataArray['low'] = $lastclose;
  225. $dataArray['realVol'] = 0;
  226. $dataArray['tickVol'] = 0;
  227. //打包存储
  228. $savedata = json_encode($dataArray);
  229. $redis->rpush("{$symbol}_{$level}_KDATA",$savedata);
  230. $nextSt = strtotime('+1month',$nextSt);
  231. }
  232. $nextSt = strtotime('+1month',strtotime(substr($data['result'][0]['time'],0,7)."-01"));
  233. $dataArray['ts'] = $st;
  234. $dataArray['last'] = $data['result'][0]['id'];
  235. $dataArray['open'] = $price;
  236. $dataArray['high'] = $price;
  237. $dataArray['low'] = $price;
  238. $dataArray['close'] = $price;
  239. $dataArray['realVol'] = $realVol;
  240. $dataArray['tickVol'] = 1;
  241. $arraySetted[$st.$level] = 1;
  242. }
  243. if(!file_exists('/tmp/kdata/log')){
  244. mkdir('/tmp/kdata/log');
  245. }
  246. $logfile = fopen("/tmp/kdata/log/{$symbol}_{$level}.log",'a+');
  247. fwrite($logfile,'['.date('Y-m-d H:i:s',time()).']:'.$last."\n");
  248. fclose($logfile);
  249. $last = $data['result'][0]['id'];//下一轮的offset
  250. }
  251. }
  252. }else{
  253. echo("params error!!!");
  254. return;
  255. }
  256. }
  257. }