作者 karlet

feat:增加订单触发事件

... ... @@ -105,6 +105,27 @@ class CmBroker
private function binanceAccDataHandle($data, $onData)
{
$this->msg("binance 推送", $data);
if (isset($data['e']) && $data['e'] == 'ORDER_TRADE_UPDATE') {
if ($data['x'] == 'TRADE') {
$wsDataTrade = WsDataTrade::TransferBinanceTrade($data, $this->symbolInfos, function ($symbol) {
return $this->getSymbolSt($symbol);
});
if ($wsDataTrade != null) {
$wsData = new WsData($this->plat, 'trade', $trade = $wsDataTrade);
$onData($wsData);
}
}
$wsDataOrder = WsDataOrder::TransferBinanceOrder($data, $this->symbolInfos, function ($symbol) {
return $this->getSymbolSt($symbol);
});
if ($wsDataOrder != null) {
$wsData = new WsData($this->plat, 'order', $trade = null, $pos = null, $order = $wsDataOrder);
$onData($wsData);
}
}
if (isset($data['e']) && $data['e'] == 'ACCOUNT_UPDATE') {
}
}
//处理欧意账户相关数据监听
private function okxAccDataHandle($data, $onData)
... ...
... ... @@ -85,13 +85,13 @@ class WsDataOrder
$ordType = strtoupper($data['ordType']);
$cliOrdId = $data['clOrdId'];
$ordId = $data['ordId'];
$status = self::getStatus($data['state']);
$status = self::getOkStatus($data['state']);
if ($data['cancelSource'] != '') {
$status = 'CANCELED';
}
return new WsDataOrder($platform, $posSide, $symbol, $side, $price, $avgPx, $qty, $lot, $pnl, $ts, $uts, $ordType, $cliOrdId, $ordId, $status);
}
private static function getStatus(string $state)
private static function getOkStatus(string $state)
{
if ($state == 'live') {
return 'LIVE';
... ... @@ -107,4 +107,48 @@ class WsDataOrder
}
return 'UNKNOWN';
}
public static function TransferBinanceOrder($data, $symbolInfos, callable $toSymbolSt): WsDataOrder|null
{
$order = $data['o'];
$symbol = call_user_func($toSymbolSt, $order['s']);
/** @var SymbolInfo $symbolInfo */
$symbolInfo = $symbolInfos[$symbol] ?? null;
if ($symbolInfo == null) {
return null;
}
$platform = 'binance';
$posSide = strtoupper($order['S']);
$side = strtoupper($order['ps']);
$price = (float)$order['p'];
$avgPx = (float)$order['ap'];
$lot = (float)$order['q'];
$qty = $lot;
$pnl = 0;
$ts = (int)$order['T'];
$uts = (int)$order['T'];
$ordType = strtoupper($order['o']);
$cliOrdId = $order['c'];
$ordId = $order['i'];
$status = self::getBnStatus($order['X']);
return new WsDataOrder($platform, $posSide, $symbol, $side, $price, $avgPx, $qty, $lot, $pnl, $ts, $uts, $ordType, $cliOrdId, $ordId, $status);
}
private static function getBnStatus(string $state)
{
if ($state == 'NEW') {
return 'LIVE';
}
if ($state == 'PARTIALLY_FILLED') {
return 'PARTIALLY_FILLED';
}
if ($state == 'FILLED') {
return 'FILLED';
}
if ($state == 'CANCELED') {
return 'CANCELED';
}
if ($state == 'EXPIRED' || $state == "EXPIRED_IN_MATCH") {
return 'CANCELED';
}
return 'UNKNOWN';
}
}
... ...
... ... @@ -83,4 +83,25 @@ class WsDataTrade
}
return null;
}
public static function TransferBinanceTrade($data, $symbolInfos, callable $toSymbolSt): WsDataTrade|null
{
$platform = 'binance';
$order = $data['o'];
$symbol = call_user_func($toSymbolSt, $order['s']);
$posSide = strtoupper($order['ps']);
$side = strtoupper($order['S']);
$price = (float)$order['L'];
$qty = (float)$order['l'];
$lot = $qty;
$pnl = (float)$order['rp'];
$fee = (float)$order['n'];
if ($order['N'] == "BNB") {
$fee = $fee * 600;
}
$quoteVol = $price * $qty;
$ts = $order['T'];
$tradeId = $order['t'];
$lever = 0;
return new WsDataTrade($platform, $posSide, $symbol, $side, $price, $qty, $lot, $pnl, $fee, $quoteVol, $ts, $tradeId, $lever);
}
}
... ...