Kline.php 1.7 KB
<?php

namespace trader\struct;

class Kline
{
    public $time; //k线开始时间
    public $open;
    public $high;
    public $low;
    public $close;
    public $vol;
    public $volQuote;
    public $uts; //k线更新时间

    public function __construct($time, $open, $high, $low, $close, $vol, $volQuote, $uts)
    {
        $this->time = $time;
        $this->open = $open;
        $this->high = $high;
        $this->low = $low;
        $this->close = $close;
        $this->vol = $vol;
        $this->volQuote = $volQuote;
        $this->uts = $uts;
    }
    public function toArray()
    {
        return [
            'time' => $this->time,
            'open' => $this->open,
            'high' => $this->high,
            'low' => $this->low,
            'close' => $this->close,
            'vol' => $this->vol,
            'volQuote' => $this->volQuote,
        ];
    }
    public static function transferOkx($data)
    {
        $time = $data[0];
        $open = $data[1];
        $high = $data[2];
        $low = $data[3];
        $close = $data[4];
        if (count($data) > 6) {
            $vol = $data[6];
            $volQuote = $data[7];
        } else {
            $vol = 0;
            $volQuote = 0;
        }
        return new Kline($time, $open, $high, $low, $close, $vol, $volQuote, $time);
    }
    public static function transferBinance($data)
    {
        $kline = $data['k'];
        $time = $kline['t'];
        $open = $kline['o'];
        $high = $kline['h'];
        $low = $kline['l'];
        $close = $kline['c'];
        $vol = $kline['v'];
        $volQuote = $kline['q'];
        $uts = $data['E'];
        return new Kline($time, $open, $high, $low, $close, $vol, $volQuote, $uts);
    }
}