作者 karlet

feat:监听订单

... ... @@ -210,19 +210,26 @@ class CmBroker
private function bybitAccDataHandle($data, $onData)
{
if (isset($data['topic']) && $data['topic'] == 'order') {
$wsDataTrade = WsDataTrade::TransferBybitTrade($data, $this->symbolInfos, function ($symbol) {
return $this->getSymbolSt($symbol);
});
if ($wsDataTrade != null) {
$wsData = new WsData($this->plat, 'trade', $trade = $wsDataTrade);
$onData($wsData);
foreach ($data['data'] as $key => $value) {
$wsDataOrd = WsDataOrder::TransferBybitOrder($value, $this->symbolInfos, function ($symbol) {
return $this->getSymbolSt($symbol);
});
if ($wsDataOrd != null) {
$wsData = new WsData($this->plat, 'order', $trade = null, $pos = null, $order = $wsDataOrd);
$onData($wsData);
}
}
$wsDataOrd = WsDataOrder::TransferBybitOrder($data, $this->symbolInfos, function ($symbol) {
return $this->getSymbolSt($symbol);
});
if ($wsDataOrd != null) {
$wsData = new WsData($this->plat, 'order', $trade = null, $pos = null, $order = $wsDataOrd);
$onData($wsData);
return;
}
if (isset($data['topic']) && $data['topic'] == 'execution') {
foreach ($data['data'] as $key => $value) {
$wsDataTrade = WsDataTrade::TransferBybitTrade($value, $this->symbolInfos, function ($symbol) {
return $this->getSymbolSt($symbol);
});
if ($wsDataTrade != null) {
$wsData = new WsData($this->plat, 'trade', $trade = $wsDataTrade);
$onData($wsData);
}
}
return;
}
... ...
... ... @@ -160,8 +160,8 @@ class ExBroker
'req_id' => (string)time(),
'op' => 'subscribe',
'args' => [
// 'order',
// 'execution',
'order',
'execution',
'position',
// 'wallet'
]
... ...
... ... @@ -134,27 +134,36 @@ class WsDataOrder
}
public static function TransferBybitOrder($data, $symbolInfos, callable $toSymbolSt): WsDataOrder|null
{
$order = $data['data'];
$symbol = call_user_func($toSymbolSt, $order['symbol']);
$symbol = call_user_func($toSymbolSt, $data['symbol']);
/** @var SymbolInfo $symbolInfo */
$symbolInfo = $symbolInfos[$symbol] ?? null;
if ($symbolInfo == null) {
return null;
}
$platform = 'bybit';
$posSide = strtoupper($order['side']);
$side = strtoupper($order['side']);
$price = (float)$order['price'];
$avgPx = (float)$order['avg_price'];
$lot = (float)$order['qty'];
$posSide = "BOTH";
if ($data['positionIdx'] == 1) {
$posSide = "LONG";
}
if ($data['positionIdx'] == 2) {
$posSide = "SHORT";
}
$side = strtoupper($data['side']);
$price = (float)$data['price'];
$avgPx = (float)$data['avgPrice'];
$lot = (float)$data['qty'];
$qty = $lot;
$pnl = (float)$order['realised_pnl'];
$ts = (int)$order['transact_time'];
$uts = (int)$order['transact_time'];
$ordType = strtoupper($order['order_type']);
$cliOrdId = $order['order_link_id'];
$ordId = $order['order_id'];
$status = self::getBnStatus($order['order_status']);
$pnl = (float)$data['closedPnl']; //去除手续费资金费的盈亏
$ts = (int)$data['updatedTime'];
$uts = (int)$data['updatedTime'];
$ordType = strtoupper($data['orderType']);
$timeInForce = $data['timeInForce'];
if ($timeInForce == 'IOC' && $ordType == "LIMIT") {
$ordType = "IOC";
}
$cliOrdId = $data['orderLinkId'];
$ordId = $data['orderId'];
$status = self::getBybitStatus($data['orderStatus']);
return new WsDataOrder($platform, $posSide, $symbol, $side, $price, $avgPx, $qty, $lot, $pnl, $ts, $uts, $ordType, $cliOrdId, $ordId, $status);
}
private static function getBnStatus(string $state)
... ... @@ -176,4 +185,20 @@ class WsDataOrder
}
return 'UNKNOWN';
}
private static function getBybitStatus(string $state)
{
if ($state == 'New') {
return 'LIVE';
}
if ($state == 'PartiallyFilled') {
return 'PARTIALLY_FILLED';
}
if ($state == 'Filled') {
return 'FILLED';
}
if ($state == 'Cancelled' || $state == "Rejected") {
return 'CANCELED';
}
return 'UNKNOWN';
}
}
... ...