作者 karlet

feat:优化交易对转换

@@ -73,6 +73,7 @@ class CmBroker @@ -73,6 +73,7 @@ class CmBroker
73 $exBroker = new BybitBroker($apiInfo); 73 $exBroker = new BybitBroker($apiInfo);
74 } 74 }
75 $this->exBroker = $exBroker; 75 $this->exBroker = $exBroker;
  76 + $this->init();
76 } 77 }
77 public function setWsHost($host) 78 public function setWsHost($host)
78 { 79 {
@@ -224,6 +225,11 @@ class CmBroker @@ -224,6 +225,11 @@ class CmBroker
224 { 225 {
225 $symbol = str_replace('-USDT-SWAP', 'USDT', $symbol); 226 $symbol = str_replace('-USDT-SWAP', 'USDT', $symbol);
226 if (preg_match('/^[A-Z0-9]+USDT$/', $symbol)) { 227 if (preg_match('/^[A-Z0-9]+USDT$/', $symbol)) {
  228 + if ($this->plat == self::PLAT_BINANCE || $this->plat == self::PLAT_BYBIT) {
  229 + $num = SymbolInfo::extractNumber($symbol);
  230 + $symbol = str_replace($num, '', $symbol);
  231 + return $symbol;
  232 + }
227 return $symbol; 233 return $symbol;
228 } else { 234 } else {
229 throw new Exception('转换标准交易对错误' . $symbol . ' to ', $symbol); 235 throw new Exception('转换标准交易对错误' . $symbol . ' to ', $symbol);
@@ -263,19 +269,14 @@ class CmBroker @@ -263,19 +269,14 @@ class CmBroker
263 } 269 }
264 270
265 //转换为原始交易对 271 //转换为原始交易对
266 - public function getSymbolOri($symbol, $platTarget) 272 + public function getSymbolOri($symbol): string
267 { 273 {
268 - $symbolSt = $this->getSymbolSt($symbol);  
269 - if ($platTarget == self::PLAT_BINANCE) {  
270 - return $symbolSt;  
271 - }  
272 - if ($platTarget == self::PLAT_OKX) {  
273 - return str_replace('USDT', '-USDT-SWAP', $symbolSt);  
274 - }  
275 - if ($platTarget == self::PLAT_BYBIT) {  
276 - return $symbolSt; 274 + /** @var SymbolInfo $symbolInfo */
  275 + $symbolInfo = $this->symbolInfos[$symbol] ?? false;
  276 + if ($symbolInfo) {
  277 + return $symbolInfo->symbolOri;
277 } 278 }
278 - throw new Exception('平台错误' . $platTarget); 279 + return "";
279 } 280 }
280 281
281 public function placeOrder(Order $order) 282 public function placeOrder(Order $order)
@@ -284,7 +285,7 @@ class CmBroker @@ -284,7 +285,7 @@ class CmBroker
284 $symbolInfo = $this->symbolInfos[$order->symbol]; 285 $symbolInfo = $this->symbolInfos[$order->symbol];
285 if ($this->plat == self::PLAT_OKX) { 286 if ($this->plat == self::PLAT_OKX) {
286 $orderOri = $order->toOkxOrder($this->symbolInfos, function ($symbol) { 287 $orderOri = $order->toOkxOrder($this->symbolInfos, function ($symbol) {
287 - return $this->getSymbolOri($symbol, $this->plat); 288 + return $this->getSymbolOri($symbol);
288 }); 289 });
289 $this->msg("下单", $orderOri); 290 $this->msg("下单", $orderOri);
290 if ($orderOri['sz'] == 0) { 291 if ($orderOri['sz'] == 0) {
@@ -335,8 +336,11 @@ class CmBroker @@ -335,8 +336,11 @@ class CmBroker
335 $pos = $this->positions[$key]; 336 $pos = $this->positions[$key];
336 return abs($pos->qty); 337 return abs($pos->qty);
337 } 338 }
338 - private function initSymbolInfos() 339 + private function initSymbolInfos($count = 1)
339 { 340 {
  341 + if ($count > 3) {
  342 + new Exception("broker初始化交易对信息失败");
  343 + }
340 if ($this->plat == self::PLAT_OKX) { 344 if ($this->plat == self::PLAT_OKX) {
341 //获取所有USDT SWAP 交易对 345 //获取所有USDT SWAP 交易对
342 $res = $this->exBroker->getSymbolInfos(); 346 $res = $this->exBroker->getSymbolInfos();
@@ -364,6 +368,10 @@ class CmBroker @@ -364,6 +368,10 @@ class CmBroker
364 } 368 }
365 $this->symbolInfos = $infos; 369 $this->symbolInfos = $infos;
366 } 370 }
  371 + if (count($this->symbolInfos) == 0) {
  372 + $this->initSymbolInfos($count + 1);
  373 + return;
  374 + }
367 //定时10m刷新 375 //定时10m刷新
368 swoole_timer_after(1000 * 60 * 10, function () { 376 swoole_timer_after(1000 * 60 * 10, function () {
369 $this->initSymbolInfos(); 377 $this->initSymbolInfos();
@@ -416,7 +424,7 @@ class CmBroker @@ -416,7 +424,7 @@ class CmBroker
416 //设置杠杆 424 //设置杠杆
417 public function setLever($symbol, $lever) 425 public function setLever($symbol, $lever)
418 { 426 {
419 - $symbol = $this->getSymbolOri($symbol, $this->plat); 427 + $symbol = $this->getSymbolOri($symbol);
420 $res = $this->exBroker->setLever($symbol, $lever); 428 $res = $this->exBroker->setLever($symbol, $lever);
421 if ($res) { 429 if ($res) {
422 $this->levers[$symbol] = $lever; 430 $this->levers[$symbol] = $lever;
@@ -446,7 +454,7 @@ class CmBroker @@ -446,7 +454,7 @@ class CmBroker
446 //获取某个品种的某个方向仓位 454 //获取某个品种的某个方向仓位
447 public function getPos($symbol, $posSide) 455 public function getPos($symbol, $posSide)
448 { 456 {
449 - $symbolOri = $this->getSymbolOri($symbol, $this->plat); 457 + $symbolOri = $this->getSymbolOri($symbol);
450 if ($this->plat == self::PLAT_OKX) { 458 if ($this->plat == self::PLAT_OKX) {
451 $posSide = strtolower($posSide); 459 $posSide = strtolower($posSide);
452 $symbolInfo = $this->symbolInfos[$symbol]; 460 $symbolInfo = $this->symbolInfos[$symbol];
@@ -465,7 +473,7 @@ class CmBroker @@ -465,7 +473,7 @@ class CmBroker
465 */ 473 */
466 public function getKlines($symbol, $peroid, $limit = 100) 474 public function getKlines($symbol, $peroid, $limit = 100)
467 { 475 {
468 - $symbolOri = $this->getSymbolOri($symbol, $this->plat); 476 + $symbolOri = $this->getSymbolOri($symbol);
469 if ($this->plat == self::PLAT_OKX) { 477 if ($this->plat == self::PLAT_OKX) {
470 $res = $this->exBroker->getKlines($symbolOri, $peroid, $limit); 478 $res = $this->exBroker->getKlines($symbolOri, $peroid, $limit);
471 if ($res['code'] != '0') { 479 if ($res['code'] != '0') {
@@ -16,8 +16,9 @@ class SymbolInfo @@ -16,8 +16,9 @@ class SymbolInfo
16 public string $lotPrec; 16 public string $lotPrec;
17 public string $minLot; // 最小下单张数 17 public string $minLot; // 最小下单张数
18 public string $minQty; // 最小下单数量 18 public string $minQty; // 最小下单数量
  19 + public int $mul; // 合约乘数//放大倍数,比如币安 1000CATUSDT,乘数为1000
19 20
20 - public function __construct($symbolOri, $symbol, $ctVal, $pricePrec, $qtyPrec, $lotPrec, $minLot, $minQty) 21 + public function __construct($symbolOri, $symbol, $ctVal, $pricePrec, $qtyPrec, $lotPrec, $minLot, $minQty, $mul = 1)
21 { 22 {
22 $this->symbolOri = $symbolOri; 23 $this->symbolOri = $symbolOri;
23 $this->symbol = $symbol; 24 $this->symbol = $symbol;
@@ -27,6 +28,7 @@ class SymbolInfo @@ -27,6 +28,7 @@ class SymbolInfo
27 $this->lotPrec = $lotPrec; 28 $this->lotPrec = $lotPrec;
28 $this->minLot = $minLot; 29 $this->minLot = $minLot;
29 $this->minQty = $minQty; 30 $this->minQty = $minQty;
  31 + $this->mul = $mul;
30 } 32 }
31 public function toArray() 33 public function toArray()
32 { 34 {
@@ -39,6 +41,7 @@ class SymbolInfo @@ -39,6 +41,7 @@ class SymbolInfo
39 'lotPrec' => $this->lotPrec, 41 'lotPrec' => $this->lotPrec,
40 'minLot' => $this->minLot, 42 'minLot' => $this->minLot,
41 'minQty' => $this->minQty, 43 'minQty' => $this->minQty,
  44 + 'mul' => $this->mul,
42 ]; 45 ];
43 } 46 }
44 public static function transferOkx($data, callable $getSymbolSt): SymbolInfo 47 public static function transferOkx($data, callable $getSymbolSt): SymbolInfo
@@ -73,7 +76,19 @@ class SymbolInfo @@ -73,7 +76,19 @@ class SymbolInfo
73 $lotPrec = getPrecision($minLot); 76 $lotPrec = getPrecision($minLot);
74 } 77 }
75 } 78 }
76 - $info = new SymbolInfo($symbolOri, $symbolSt, $ctVal, $pricePrec, $qtyPrec, $lotPrec, $minLot, $minQty); 79 + $mul = self::extractNumber($symbolOri);
  80 + $info = new SymbolInfo($symbolOri, $symbolSt, $ctVal, $pricePrec, $qtyPrec, $lotPrec, $minLot, $minQty, $mul);
77 return $info; 81 return $info;
78 } 82 }
  83 + public static function extractNumber($str)
  84 + {
  85 + // 示例
  86 + // echo extractNumber("1000CATUSDT") . PHP_EOL; // 输出:1000
  87 + // echo extractNumber("1000PEPEUSDT") . PHP_EOL; // 输出:1000
  88 + // echo extractNumber("1000000MOGUSDT") . PHP_EOL; // 输出:1000000
  89 + if (preg_match('/^\d+/', $str, $matches)) {
  90 + return (int)$matches[0]; // 返回整数
  91 + }
  92 + return 1;
  93 + }
79 } 94 }