KdataForm.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 25276
  5. * Date: 2018/4/26
  6. * Time: 9:59
  7. */
  8. namespace backend\forms;
  9. use components\Curl;
  10. use components\Exception;
  11. use Yii;
  12. /**
  13. * Class QueueForm
  14. * @package backend\forms
  15. */
  16. class KdataForm extends BaseForm
  17. {
  18. public function getData($symbol,$period,$from,$to,$count){
  19. date_default_timezone_set("UTC");
  20. $key = "{$symbol}_{$period}_KDATA";
  21. if($symbol == 'BTCUSDT1')
  22. $key = "BTCUSDT_{$period}_KDATA";
  23. $startIndex = 0;
  24. $endIndex = 0;
  25. $stset = array(
  26. 'm1'=>60,
  27. 'm3'=>180,
  28. 'm5'=>300,
  29. 'm15'=>900,
  30. 'm30'=>1800,
  31. 'h1'=>3600,
  32. 'h2'=>7200,
  33. 'h4'=>14400,
  34. 'h6'=>21600,
  35. 'h12'=>43200,
  36. 'd1'=>86400,
  37. 'd3'=>259200,
  38. 'd5'=>432000,
  39. 'd7'=>604800,
  40. 'd15'=>1296000,
  41. );
  42. $redis = Yii::$app->redis;
  43. $data = array();
  44. $t = array();
  45. $h = array();
  46. $o = array();
  47. $l = array();
  48. $c = array();
  49. $v = array();
  50. $head = json_decode($redis->lrange($key,0,0)[0],true)['ts'];
  51. if($period!='mo'){//除了月级别 其他正常处理
  52. if($from!=-1&&$to!=-1){//起点终点都设置了
  53. if($from<$head){
  54. $startIndex = 0;
  55. }else{
  56. $startIndex = floor(($from-$head)/$stset[$period]);
  57. }
  58. if($to<$head){
  59. $endIndex = 0;
  60. }else{
  61. $endIndex = floor(($to-$head)/$stset[$period]);
  62. }
  63. }else if($from==-1){//只设置了终点
  64. if($to<$head){
  65. $endIndex = 0;
  66. }else{
  67. $endIndex = floor(($to-$head)/$stset[$period]);
  68. }
  69. $startIndex = ($endIndex-$count+1)>0?($endIndex-$count+1):0;
  70. }else if($to==-1){//只设置了起点
  71. if($from<$head){
  72. $startIndex = 0;
  73. }else{
  74. $startIndex = floor(($from-$head)/$stset[$period]);
  75. }
  76. $endIndex = ($startIndex+$count-1)>0?:0;
  77. }else{
  78. $data['t'] = null;
  79. $data['h'] = null;
  80. $data['o'] = null;
  81. $data['l'] = null;
  82. $data['c'] = null;
  83. $data['v'] = null;
  84. $data['s'] = 'error';
  85. return $data;
  86. }
  87. }else{//月级别单独处理
  88. $headDate = date('Y-m',$head)."-01";
  89. if($from!=-1&&$to!=-1){//起点终点都设置了
  90. $fromDate = date('Y-m',$from)."-01";
  91. $toDate = date('Y-m',$to)."-01";
  92. if($from<$head){
  93. $startIndex = 0;
  94. }else{
  95. $startIndex = $this->getMonthNum($headDate,$fromDate,'-');
  96. }
  97. if($to<$head){
  98. $endIndex = 0;
  99. }else{
  100. $endIndex = $this->getMonthNum($headDate,$toDate,'-');
  101. }
  102. }else if($from==-1){//只设置了终点
  103. $toDate = date('Y-m',$to)."-01";
  104. if($to<$head){
  105. $endIndex = 0;
  106. }else{
  107. $endIndex = $this->getMonthNum($headDate,$toDate,'-');
  108. }
  109. $startIndex = ($endIndex-$count+1)>0?($endIndex-$count+1):0;
  110. }else if($to==-1){//只设置了起点
  111. $fromDate = date('Y-m',$from)."-01";
  112. if($from<$head){
  113. $startIndex = 0;
  114. }else{
  115. $startIndex = $this->getMonthNum($headDate,$fromDate,'-');
  116. }
  117. $endIndex = ($startIndex+$count-1)>0?($startIndex+$count-1):0;
  118. }else{
  119. $data['t'] = null;
  120. $data['h'] = null;
  121. $data['o'] = null;
  122. $data['l'] = null;
  123. $data['c'] = null;
  124. $data['v'] = null;
  125. $data['s'] = 'error';
  126. return $data;
  127. }
  128. }
  129. $result = $redis->lrange($key,$startIndex,$endIndex);
  130. foreach ($result as $key=>$val){
  131. $val = json_decode($val,true);
  132. $t[] = $val['ts'];
  133. $h[] = $val['high'];
  134. $o[] = $val['open'];
  135. $l[] = $val['low'];
  136. $c[] = $val['close'];
  137. $v[] = $val['realVol'];
  138. }
  139. $data['t'] = $t;
  140. $data['h'] = $h;
  141. $data['o'] = $o;
  142. $data['l'] = $l;
  143. $data['c'] = $c;
  144. $data['v'] = $v;
  145. $data['s'] = 'ok';
  146. return $data;
  147. }
  148. function getMonthNum( $date1, $date2, $tags='-' ){//获取月份差 $date2>$date1
  149. $date1 = explode($tags,$date1);
  150. $date2 = explode($tags,$date2);
  151. return abs($date1[0] - $date2[0]) * 12 + ($date2[1] - $date1[1]);
  152. }
  153. }