123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- /**
- * Created by PhpStorm.
- * User: 25276
- * Date: 2018/4/26
- * Time: 9:59
- */
- namespace backend\forms;
- use components\Curl;
- use components\Exception;
- use Yii;
- /**
- * Class QueueForm
- * @package backend\forms
- */
- class KdataForm extends BaseForm
- {
- public function getData($symbol,$period,$from,$to,$count){
- date_default_timezone_set("UTC");
- $key = "{$symbol}_{$period}_KDATA";
- if($symbol == 'BTCUSDT1')
- $key = "BTCUSDT_{$period}_KDATA";
- $startIndex = 0;
- $endIndex = 0;
- $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,
- );
- $redis = Yii::$app->redis;
- $data = array();
- $t = array();
- $h = array();
- $o = array();
- $l = array();
- $c = array();
- $v = array();
- $head = json_decode($redis->lrange($key,0,0)[0],true)['ts'];
- if($period!='mo'){//除了月级别 其他正常处理
- if($from!=-1&&$to!=-1){//起点终点都设置了
- if($from<$head){
- $startIndex = 0;
- }else{
- $startIndex = floor(($from-$head)/$stset[$period]);
- }
- if($to<$head){
- $endIndex = 0;
- }else{
- $endIndex = floor(($to-$head)/$stset[$period]);
- }
- }else if($from==-1){//只设置了终点
- if($to<$head){
- $endIndex = 0;
- }else{
- $endIndex = floor(($to-$head)/$stset[$period]);
- }
- $startIndex = ($endIndex-$count+1)>0?($endIndex-$count+1):0;
- }else if($to==-1){//只设置了起点
- if($from<$head){
- $startIndex = 0;
- }else{
- $startIndex = floor(($from-$head)/$stset[$period]);
- }
- $endIndex = ($startIndex+$count-1)>0?:0;
- }else{
- $data['t'] = null;
- $data['h'] = null;
- $data['o'] = null;
- $data['l'] = null;
- $data['c'] = null;
- $data['v'] = null;
- $data['s'] = 'error';
- return $data;
- }
- }else{//月级别单独处理
- $headDate = date('Y-m',$head)."-01";
- if($from!=-1&&$to!=-1){//起点终点都设置了
- $fromDate = date('Y-m',$from)."-01";
- $toDate = date('Y-m',$to)."-01";
- if($from<$head){
- $startIndex = 0;
- }else{
- $startIndex = $this->getMonthNum($headDate,$fromDate,'-');
- }
- if($to<$head){
- $endIndex = 0;
- }else{
- $endIndex = $this->getMonthNum($headDate,$toDate,'-');
- }
- }else if($from==-1){//只设置了终点
- $toDate = date('Y-m',$to)."-01";
- if($to<$head){
- $endIndex = 0;
- }else{
- $endIndex = $this->getMonthNum($headDate,$toDate,'-');
- }
- $startIndex = ($endIndex-$count+1)>0?($endIndex-$count+1):0;
- }else if($to==-1){//只设置了起点
- $fromDate = date('Y-m',$from)."-01";
- if($from<$head){
- $startIndex = 0;
- }else{
- $startIndex = $this->getMonthNum($headDate,$fromDate,'-');
- }
- $endIndex = ($startIndex+$count-1)>0?($startIndex+$count-1):0;
- }else{
- $data['t'] = null;
- $data['h'] = null;
- $data['o'] = null;
- $data['l'] = null;
- $data['c'] = null;
- $data['v'] = null;
- $data['s'] = 'error';
- return $data;
- }
- }
- $result = $redis->lrange($key,$startIndex,$endIndex);
- foreach ($result as $key=>$val){
- $val = json_decode($val,true);
- $t[] = $val['ts'];
- $h[] = $val['high'];
- $o[] = $val['open'];
- $l[] = $val['low'];
- $c[] = $val['close'];
- $v[] = $val['realVol'];
- }
- $data['t'] = $t;
- $data['h'] = $h;
- $data['o'] = $o;
- $data['l'] = $l;
- $data['c'] = $c;
- $data['v'] = $v;
- $data['s'] = 'ok';
- return $data;
- }
- function getMonthNum( $date1, $date2, $tags='-' ){//获取月份差 $date2>$date1
- $date1 = explode($tags,$date1);
- $date2 = explode($tags,$date2);
- return abs($date1[0] - $date2[0]) * 12 + ($date2[1] - $date1[1]);
- }
- }
|