|
...
|
...
|
@@ -5,6 +5,7 @@ namespace trader; |
|
|
|
require_once __DIR__ . '/struct/ApiInfo.php';
|
|
|
|
require_once __DIR__ . '/exchange/okx/ExBroker.php';
|
|
|
|
require_once __DIR__ . '/exchange/binance/ExBroker.php';
|
|
|
|
require_once __DIR__ . '/exchange/bybit/ExBroker.php';
|
|
|
|
require_once __DIR__ . '/struct/Kline.php';
|
|
|
|
require_once __DIR__ . '/struct/Order.php';
|
|
|
|
require_once __DIR__ . '/struct/WsData.php';
|
|
...
|
...
|
@@ -21,6 +22,7 @@ require_once __DIR__ . '/../jytools/Websocket.php'; |
|
|
|
use trader\struct\ApiInfo;
|
|
|
|
use trader\exchange\okx\ExBroker as OkxBroker;
|
|
|
|
use trader\exchange\binance\ExBroker as BinanceBroker;
|
|
|
|
use trader\exchange\bybit\ExBroker as BybitBroker;
|
|
|
|
use trader\struct\Kline;
|
|
|
|
use trader\struct\Order;
|
|
|
|
use trader\struct\WsData;
|
|
...
|
...
|
@@ -47,7 +49,7 @@ class CmBroker |
|
|
|
* @see PLAT_BITGET
|
|
|
|
*/
|
|
|
|
public $plat;
|
|
|
|
private OkxBroker|BinanceBroker $exBroker;
|
|
|
|
private OkxBroker|BinanceBroker|BybitBroker $exBroker;
|
|
|
|
/** @var SymbolInfo[] $symbolInfos */
|
|
|
|
public $symbolInfos = [];
|
|
|
|
/** @var Pos[] $positions */
|
|
...
|
...
|
@@ -67,6 +69,9 @@ class CmBroker |
|
|
|
if ($plat == self::PLAT_BINANCE) {
|
|
|
|
$exBroker = new BinanceBroker($apiInfo);
|
|
|
|
}
|
|
|
|
if ($plat == self::PLAT_BYBIT) {
|
|
|
|
$exBroker = new BybitBroker($apiInfo);
|
|
|
|
}
|
|
|
|
$this->exBroker = $exBroker;
|
|
|
|
}
|
|
|
|
public function setWsHost($host)
|
|
...
|
...
|
@@ -200,13 +205,17 @@ class CmBroker |
|
|
|
return;
|
|
|
|
}
|
|
|
|
$symbol = $this->getSymbolOri($symbol, $this->plat);
|
|
|
|
$this->exBroker->klineListen($symbol, $peroid, function ($data) use ($onData) {
|
|
|
|
$periodOri = $this->getPeriodOri($this->plat, $peroid);
|
|
|
|
$this->exBroker->klineListen($symbol, $periodOri, function ($data) use ($onData) {
|
|
|
|
if ($this->plat == self::PLAT_BINANCE) {
|
|
|
|
$kline = Kline::transferBinance($data);
|
|
|
|
}
|
|
|
|
if ($this->plat == self::PLAT_OKX) {
|
|
|
|
$kline = Kline::transferOkx($data);
|
|
|
|
}
|
|
|
|
if ($this->plat == self::PLAT_BYBIT) {
|
|
|
|
$kline = Kline::transferBybit($data);
|
|
|
|
}
|
|
|
|
$onData($kline);
|
|
|
|
});
|
|
|
|
}
|
|
...
|
...
|
@@ -220,6 +229,38 @@ class CmBroker |
|
|
|
throw new Exception('转换标准交易对错误' . $symbol . ' to ', $symbol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//转换为原始周期
|
|
|
|
public function getPeriodOri($platTarget, $period)
|
|
|
|
{
|
|
|
|
if ($platTarget == self::PLAT_BINANCE) {
|
|
|
|
return $period;
|
|
|
|
}
|
|
|
|
if ($platTarget == self::PLAT_OKX) {
|
|
|
|
return str_replace('s', 'S', $period);
|
|
|
|
}
|
|
|
|
if ($platTarget == self::PLAT_BYBIT) {
|
|
|
|
$arr = [
|
|
|
|
'1m' => '1',
|
|
|
|
'3m' => '3',
|
|
|
|
'5m' => '5',
|
|
|
|
'15m' => '15',
|
|
|
|
'30m' => '30',
|
|
|
|
'1h' => '60',
|
|
|
|
'2h' => '120',
|
|
|
|
'4h' => '240',
|
|
|
|
'6h' => '360',
|
|
|
|
'12h' => '720',
|
|
|
|
'1d' => 'D',
|
|
|
|
'1w' => 'W',
|
|
|
|
'1M' => 'M',
|
|
|
|
];
|
|
|
|
if (!isset($arr[$period])) {
|
|
|
|
throw new Exception('周期错误,' . $this->plat . '不支持的周期' . $period);
|
|
|
|
}
|
|
|
|
return $arr[$period];
|
|
|
|
}
|
|
|
|
throw new Exception('平台错误' + $platTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
//转换为原始交易对
|
|
|
|
public function getSymbolOri($symbol, $platTarget)
|
|
...
|
...
|
@@ -231,7 +272,10 @@ class CmBroker |
|
|
|
if ($platTarget == self::PLAT_OKX) {
|
|
|
|
return str_replace('USDT', '-USDT-SWAP', $symbolSt);
|
|
|
|
}
|
|
|
|
throw new Exception('平台错误' + $platTarget);
|
|
|
|
if ($platTarget == self::PLAT_BYBIT) {
|
|
|
|
return $symbolSt;
|
|
|
|
}
|
|
|
|
throw new Exception('平台错误' . $platTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function placeOrder(Order $order)
|
...
|
...
|
|