作者 karlet

feat:获取资金费率

@@ -16,6 +16,7 @@ require_once __DIR__ . '/struct/SymbolInfo.php'; @@ -16,6 +16,7 @@ require_once __DIR__ . '/struct/SymbolInfo.php';
16 require_once __DIR__ . '/struct/WsDataOrder.php'; 16 require_once __DIR__ . '/struct/WsDataOrder.php';
17 require_once __DIR__ . '/struct/WsDataAccount.php'; 17 require_once __DIR__ . '/struct/WsDataAccount.php';
18 require_once __DIR__ . '/struct/Pos.php'; 18 require_once __DIR__ . '/struct/Pos.php';
  19 +require_once __DIR__ . '/struct/Premium.php';
19 require_once __DIR__ . '/../jytools/func.php'; 20 require_once __DIR__ . '/../jytools/func.php';
20 require_once __DIR__ . '/../jytools/Websocket.php'; 21 require_once __DIR__ . '/../jytools/Websocket.php';
21 22
@@ -34,6 +35,7 @@ use trader\struct\SymbolInfo; @@ -34,6 +35,7 @@ use trader\struct\SymbolInfo;
34 use trader\struct\WsDataOrder; 35 use trader\struct\WsDataOrder;
35 use trader\struct\WsDataAccount; 36 use trader\struct\WsDataAccount;
36 use trader\struct\Pos; 37 use trader\struct\Pos;
  38 +use trader\struct\Premium;
37 use \Exception; 39 use \Exception;
38 use function jytools\timeFormat; 40 use function jytools\timeFormat;
39 use function jytools\getMicrotime; 41 use function jytools\getMicrotime;
@@ -679,4 +681,32 @@ class CmBroker @@ -679,4 +681,32 @@ class CmBroker
679 { 681 {
680 $this->positions = $this->getAllPos(); 682 $this->positions = $this->getAllPos();
681 } 683 }
  684 + //----------公告数据接口--------
  685 + //获取所有品种资金费率,返回数据为 Premium[]
  686 + public function getPremiumAll(): array
  687 + {
  688 + $data = [];
  689 + if ($this->plat == self::PLAT_OKX) {
  690 + foreach ($this->symbolInfos as $key => $value) {
  691 + $res = $this->exBroker->getPremium($value->symbolOri);
  692 + if (isset($res['data'])) {
  693 + $prem = Premium::transferOkx($res['data'][0], function ($symbol) {
  694 + return $this->getSymbolSt($symbol);
  695 + });
  696 + $data[$prem->symbol] = $prem;
  697 + }
  698 + }
  699 + } else if ($this->plat == self::PLAT_BINANCE) {
  700 + $res = $this->exBroker->getPremiumAll();
  701 + foreach ($res as $key => $value) {
  702 + $prem = Premium::transferBinance($value, function ($symbol) {
  703 + return $this->getSymbolSt($symbol);
  704 + });
  705 + $data[$prem->symbol] = $prem;
  706 + }
  707 + } else {
  708 + throw new Exception("获取资金费 该交易所未实现,请先实现代码");
  709 + }
  710 + return $data;
  711 + }
682 } 712 }
@@ -148,4 +148,14 @@ class ExBroker @@ -148,4 +148,14 @@ class ExBroker
148 } 148 }
149 return $newPositions; 149 return $newPositions;
150 } 150 }
  151 + //获取资金费率
  152 + public function getPremiumAll($symbol = null)
  153 + {
  154 + $params = [];
  155 + if ($symbol) {
  156 + $params['symbol'] = $symbol;
  157 + }
  158 + $res = $this->api->getPremiumIndex($params);
  159 + return $res;
  160 + }
151 } 161 }
@@ -390,4 +390,13 @@ class ExBroker @@ -390,4 +390,13 @@ class ExBroker
390 } 390 }
391 return $this->api->klines($param); 391 return $this->api->klines($param);
392 } 392 }
  393 + //获取资金费率
  394 + public function getPremium($symbol)
  395 + {
  396 + $param = [
  397 + 'instId' => $symbol,
  398 + ];
  399 + $res = $this->api->fundingRate($param);
  400 + return $res;
  401 + }
393 } 402 }
1 <?php 1 <?php
2 2
3 -namespace trader\PreOrd; 3 +namespace trader\struct;
4 4
5 class Premium 5 class Premium
6 { 6 {
7 - private $symbol;  
8 - private $rate;  
9 - private $settleTs;  
10 - private $settleTime; 7 + public $symbol; //标准交易对
  8 + public $symbolOri; //原始交易对
  9 + public $rate; //资金费率
  10 + public $settleTs; //下次结算时间
  11 + public $settlePeriod; //结算周期
  12 + public $uts; //最近更新时间
11 13
12 - public function __construct($symbol, $rate, $settleTs, $settleTime) 14 + public function __construct($symbol, $symbolOri, $rate, $settleTs, $settlePeriod, $uts)
13 { 15 {
14 $this->symbol = $symbol; 16 $this->symbol = $symbol;
  17 + $this->symbolOri = $symbolOri;
15 $this->rate = $rate; 18 $this->rate = $rate;
16 $this->settleTs = $settleTs; 19 $this->settleTs = $settleTs;
17 - $this->settleTime = $settleTime; 20 + $this->settlePeriod = $settlePeriod;
  21 + $this->uts = $uts;
18 } 22 }
19 23
20 public function toArray() 24 public function toArray()
21 { 25 {
22 return [ 26 return [
23 'symbol' => $this->symbol, 27 'symbol' => $this->symbol,
  28 + 'symbolOri' => $this->symbolOri,
24 'rate' => $this->rate, 29 'rate' => $this->rate,
25 'settleTs' => $this->settleTs, 30 'settleTs' => $this->settleTs,
26 - 'settleTime' => $this->settleTime, 31 + 'settlePeriod' => $this->settlePeriod,
  32 + 'uts' => $this->uts,
27 ]; 33 ];
28 } 34 }
  35 + public static function transferOkx($data, callable $getSymbolSt): self
  36 + {
  37 + $symbolOri = $data['instId'];
  38 + $symbolSt = $getSymbolSt($symbolOri);
  39 + $rate = $data['fundingRate'];
  40 + $settleTs = $data['fundingTime'];
  41 + $settlePeriod = $data['nextFundingTime'] - $settleTs;
  42 + $uts = $data['ts'];
  43 + return new self($symbolSt, $symbolOri, $rate, $settleTs, $settlePeriod, $uts);
  44 + }
  45 + public static function transferBinance($data, callable $getSymbolSt): self
  46 + {
  47 + $symbolOri = $data['symbol'];
  48 + $symbolSt = $getSymbolSt($symbolOri);
  49 + $rate = $data['lastFundingRate'];
  50 + $settleTs = $data['nextFundingTime'];
  51 + $settlePeriod = 0;
  52 + $uts = $data['time'];
  53 + return new self($symbolSt, $symbolOri, $rate, $settleTs, $settlePeriod, $uts);
  54 + }
29 } 55 }