作者 karlet

feat:平台定义常量

... ... @@ -35,6 +35,17 @@ use function jytools\timeFormat;
class CmBroker
{
const PLAT_OKX = 'okx';
const PLAT_BINANCE = 'binance';
const PLAT_BYBIT = 'bybit';
const PLAT_BITGET = 'bitget';
/**
* @var string 当前使用的交易平台
* @see PLAT_OKX
* @see PLAT_BINANCE
* @see PLAT_BYBIT
* @see PLAT_BITGET
*/
public $plat;
private ?OkxBroker $exBroker;
/** @var SymbolInfo[] $symbolInfos */
... ... @@ -49,10 +60,10 @@ class CmBroker
{
$this->plat = $plat;
$exBroker = null;
if ($plat == 'okx') {
if ($plat == self::PLAT_OKX) {
$exBroker = new OkxBroker($apiInfo);
}
if ($plat == 'binance') {
if ($plat == self::PLAT_BINANCE) {
$exBroker = new BinanceBroker($apiInfo);
}
$this->exBroker = $exBroker;
... ... @@ -77,10 +88,10 @@ class CmBroker
{
$this->exBroker->accListen(function ($data) use ($onData) {
// output("ws 有效数据", $data);
if ($this->plat == 'binance') {
if ($this->plat == self::PLAT_BINANCE) {
$this->msg("binance 无处理ws数据实现");
}
if ($this->plat == 'okx') {
if ($this->plat == self::PLAT_OKX) {
if (isset($data['arg']) && $data['arg']['channel'] == 'orders') {
foreach ($data['data'] as $key => $value) {
$wsDataTrade = WsDataTrade::TransferOkxOrder($value, $this->symbolInfos, function ($symbol) {
... ... @@ -155,10 +166,10 @@ class CmBroker
public function getSymbolOri($symbol, $platTarget)
{
$symbolSt = $this->getSymbolSt($symbol);
if ($platTarget == 'binance') {
if ($platTarget == self::PLAT_BINANCE) {
return $symbolSt;
}
if ($platTarget == 'okx') {
if ($platTarget == self::PLAT_OKX) {
return str_replace('USDT', '-USDT-SWAP', $symbolSt);
}
throw new Exception('平台错误' + $platTarget);
... ... @@ -166,7 +177,7 @@ class CmBroker
public function placeOrder(Order $order)
{
if ($this->plat == 'okx') {
if ($this->plat == self::PLAT_OKX) {
$orderOri = $order->toOkxOrder($this->symbolInfos, function ($symbol) {
return $this->getSymbolOri($symbol, $this->plat);
});
... ... @@ -209,7 +220,7 @@ class CmBroker
}
private function initSymbolInfos()
{
if ($this->plat == 'okx') {
if ($this->plat == self::PLAT_OKX) {
//获取所有USDT SWAP 交易对
$res = $this->exBroker->getSymbolInfos();
$infos = [];
... ... @@ -250,7 +261,7 @@ class CmBroker
public function getLevers()
{
$levers = $this->exBroker->getAllLevers();
if ($this->plat == 'okx') {
if ($this->plat == self::PLAT_OKX) {
$resNew = [];
foreach ($levers as $key => $value) {
$symbol = $this->getSymbolSt($key); //转换为标准交易对
... ... @@ -287,7 +298,7 @@ class CmBroker
public function getPos($symbol, $posSide)
{
$symbolOri = $this->getSymbolOri($symbol, $this->plat);
if ($this->plat == 'okx') {
if ($this->plat == self::PLAT_OKX) {
$posSide = strtolower($posSide);
$symbolInfo = $this->symbolInfos[$symbol];
$lot = $this->exBroker->getPos($symbolOri, $posSide);
... ... @@ -306,7 +317,7 @@ class CmBroker
public function getKlines($symbol, $peroid, $limit = 100)
{
$symbolOri = $this->getSymbolOri($symbol, $this->plat);
if ($this->plat == 'okx') {
if ($this->plat == self::PLAT_OKX) {
$res = $this->exBroker->getKlines($symbolOri, $peroid, $limit);
if ($res['code'] != '0') {
return [];
... ...