|
@@ -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') {
|