作者 karlet

feat:完善交易对精度

@@ -253,6 +253,18 @@ class CmBroker @@ -253,6 +253,18 @@ class CmBroker
253 } 253 }
254 $this->symbolInfos = $infos; 254 $this->symbolInfos = $infos;
255 } 255 }
  256 + if ($this->plat == self::PLAT_BINANCE) {
  257 + $res = $this->exBroker->getSymbolInfos();
  258 + $infos = [];
  259 + foreach ($res as $key => $value) {
  260 + if ($value["quoteAsset"] == "USDT") {
  261 + $info = SymbolInfo::transferBinance($value, function ($symbol) {
  262 + return $this->getSymbolSt($symbol);
  263 + });
  264 + $infos[$info->symbol] = $info;
  265 + }
  266 + }
  267 + }
256 //定时10m刷新 268 //定时10m刷新
257 swoole_timer_after(1000 * 60 * 10, function () { 269 swoole_timer_after(1000 * 60 * 10, function () {
258 $this->initSymbolInfos(); 270 $this->initSymbolInfos();
@@ -30,6 +30,13 @@ class Api @@ -30,6 +30,13 @@ class Api
30 $method = "GET"; 30 $method = "GET";
31 return $this->request($method, $url, $params); 31 return $this->request($method, $url, $params);
32 } 32 }
  33 + //获取交易对信息
  34 + public function getExchangeInfo($params)
  35 + {
  36 + $url = "/fapi/v1/exchangeInfo";
  37 + $method = "GET";
  38 + return $this->request($method, $url, $params);
  39 + }
33 40
34 //-------private interface ------------ 41 //-------private interface ------------
35 //获取用户监听key 42 //获取用户监听key
@@ -116,4 +116,13 @@ class ExBroker @@ -116,4 +116,13 @@ class ExBroker
116 $this->wsKline->close(); 116 $this->wsKline->close();
117 } 117 }
118 } 118 }
  119 + public function getSymbolInfos()
  120 + {
  121 + $res = $this->api->getExchangeInfo([]);
  122 + if (!isset($res['symbols'])) {
  123 + output('okx获取所有交易对信息失败');
  124 + return [];
  125 + }
  126 + return $res['symbols'];
  127 + }
119 } 128 }
@@ -53,4 +53,27 @@ class SymbolInfo @@ -53,4 +53,27 @@ class SymbolInfo
53 $info = new SymbolInfo($data["instId"], $symbolSt, $ctVal, $pricePrec, $qtyPrec, $lotPrec, $minLot, $minQty); 53 $info = new SymbolInfo($data["instId"], $symbolSt, $ctVal, $pricePrec, $qtyPrec, $lotPrec, $minLot, $minQty);
54 return $info; 54 return $info;
55 } 55 }
  56 + public static function transferBinance($data, callable $getSymbolSt): SymbolInfo
  57 + {
  58 + $symbolOri = $data['symbol'];
  59 + $symbolSt = $getSymbolSt($symbolOri); //转换为标准交易对
  60 + $ctVal = 1;
  61 + $pricePrec = 0;
  62 + $qtyPrec = 0;
  63 + $lotPrec = 0;
  64 + $minLot = 0;
  65 + foreach ($data['filters'] as $v) {
  66 + if ($v['filterType'] == 'PRICE_FILTER') {
  67 + $pricePrec = getPrecision($v['tickSize']);
  68 + }
  69 + if ($v['filterType'] == 'LOT_SIZE') {
  70 + $minLot = $v['minQty'];
  71 + $minQty = $minLot * $ctVal;
  72 + $qtyPrec = getPrecision($minLot);
  73 + $lotPrec = getPrecision($minLot);
  74 + }
  75 + }
  76 + $info = new SymbolInfo($symbolOri, $symbolSt, $ctVal, $pricePrec, $qtyPrec, $lotPrec, $minLot, $minQty);
  77 + return $info;
  78 + }
56 } 79 }