作者 karlet

feat:增加订单触发事件

@@ -105,6 +105,27 @@ class CmBroker @@ -105,6 +105,27 @@ class CmBroker
105 private function binanceAccDataHandle($data, $onData) 105 private function binanceAccDataHandle($data, $onData)
106 { 106 {
107 $this->msg("binance 推送", $data); 107 $this->msg("binance 推送", $data);
  108 + if (isset($data['e']) && $data['e'] == 'ORDER_TRADE_UPDATE') {
  109 + if ($data['x'] == 'TRADE') {
  110 + $wsDataTrade = WsDataTrade::TransferBinanceTrade($data, $this->symbolInfos, function ($symbol) {
  111 + return $this->getSymbolSt($symbol);
  112 + });
  113 + if ($wsDataTrade != null) {
  114 + $wsData = new WsData($this->plat, 'trade', $trade = $wsDataTrade);
  115 + $onData($wsData);
  116 + }
  117 + }
  118 + $wsDataOrder = WsDataOrder::TransferBinanceOrder($data, $this->symbolInfos, function ($symbol) {
  119 + return $this->getSymbolSt($symbol);
  120 + });
  121 + if ($wsDataOrder != null) {
  122 + $wsData = new WsData($this->plat, 'order', $trade = null, $pos = null, $order = $wsDataOrder);
  123 + $onData($wsData);
  124 + }
  125 + }
  126 + if (isset($data['e']) && $data['e'] == 'ACCOUNT_UPDATE') {
  127 +
  128 + }
108 } 129 }
109 //处理欧意账户相关数据监听 130 //处理欧意账户相关数据监听
110 private function okxAccDataHandle($data, $onData) 131 private function okxAccDataHandle($data, $onData)
@@ -85,13 +85,13 @@ class WsDataOrder @@ -85,13 +85,13 @@ class WsDataOrder
85 $ordType = strtoupper($data['ordType']); 85 $ordType = strtoupper($data['ordType']);
86 $cliOrdId = $data['clOrdId']; 86 $cliOrdId = $data['clOrdId'];
87 $ordId = $data['ordId']; 87 $ordId = $data['ordId'];
88 - $status = self::getStatus($data['state']); 88 + $status = self::getOkStatus($data['state']);
89 if ($data['cancelSource'] != '') { 89 if ($data['cancelSource'] != '') {
90 $status = 'CANCELED'; 90 $status = 'CANCELED';
91 } 91 }
92 return new WsDataOrder($platform, $posSide, $symbol, $side, $price, $avgPx, $qty, $lot, $pnl, $ts, $uts, $ordType, $cliOrdId, $ordId, $status); 92 return new WsDataOrder($platform, $posSide, $symbol, $side, $price, $avgPx, $qty, $lot, $pnl, $ts, $uts, $ordType, $cliOrdId, $ordId, $status);
93 } 93 }
94 - private static function getStatus(string $state) 94 + private static function getOkStatus(string $state)
95 { 95 {
96 if ($state == 'live') { 96 if ($state == 'live') {
97 return 'LIVE'; 97 return 'LIVE';
@@ -107,4 +107,48 @@ class WsDataOrder @@ -107,4 +107,48 @@ class WsDataOrder
107 } 107 }
108 return 'UNKNOWN'; 108 return 'UNKNOWN';
109 } 109 }
  110 + public static function TransferBinanceOrder($data, $symbolInfos, callable $toSymbolSt): WsDataOrder|null
  111 + {
  112 + $order = $data['o'];
  113 + $symbol = call_user_func($toSymbolSt, $order['s']);
  114 + /** @var SymbolInfo $symbolInfo */
  115 + $symbolInfo = $symbolInfos[$symbol] ?? null;
  116 + if ($symbolInfo == null) {
  117 + return null;
  118 + }
  119 + $platform = 'binance';
  120 + $posSide = strtoupper($order['S']);
  121 + $side = strtoupper($order['ps']);
  122 + $price = (float)$order['p'];
  123 + $avgPx = (float)$order['ap'];
  124 + $lot = (float)$order['q'];
  125 + $qty = $lot;
  126 + $pnl = 0;
  127 + $ts = (int)$order['T'];
  128 + $uts = (int)$order['T'];
  129 + $ordType = strtoupper($order['o']);
  130 + $cliOrdId = $order['c'];
  131 + $ordId = $order['i'];
  132 + $status = self::getBnStatus($order['X']);
  133 + return new WsDataOrder($platform, $posSide, $symbol, $side, $price, $avgPx, $qty, $lot, $pnl, $ts, $uts, $ordType, $cliOrdId, $ordId, $status);
  134 + }
  135 + private static function getBnStatus(string $state)
  136 + {
  137 + if ($state == 'NEW') {
  138 + return 'LIVE';
  139 + }
  140 + if ($state == 'PARTIALLY_FILLED') {
  141 + return 'PARTIALLY_FILLED';
  142 + }
  143 + if ($state == 'FILLED') {
  144 + return 'FILLED';
  145 + }
  146 + if ($state == 'CANCELED') {
  147 + return 'CANCELED';
  148 + }
  149 + if ($state == 'EXPIRED' || $state == "EXPIRED_IN_MATCH") {
  150 + return 'CANCELED';
  151 + }
  152 + return 'UNKNOWN';
  153 + }
110 } 154 }
@@ -83,4 +83,25 @@ class WsDataTrade @@ -83,4 +83,25 @@ class WsDataTrade
83 } 83 }
84 return null; 84 return null;
85 } 85 }
  86 + public static function TransferBinanceTrade($data, $symbolInfos, callable $toSymbolSt): WsDataTrade|null
  87 + {
  88 + $platform = 'binance';
  89 + $order = $data['o'];
  90 + $symbol = call_user_func($toSymbolSt, $order['s']);
  91 + $posSide = strtoupper($order['ps']);
  92 + $side = strtoupper($order['S']);
  93 + $price = (float)$order['L'];
  94 + $qty = (float)$order['l'];
  95 + $lot = $qty;
  96 + $pnl = (float)$order['rp'];
  97 + $fee = (float)$order['n'];
  98 + if ($order['N'] == "BNB") {
  99 + $fee = $fee * 600;
  100 + }
  101 + $quoteVol = $price * $qty;
  102 + $ts = $order['T'];
  103 + $tradeId = $order['t'];
  104 + $lever = 0;
  105 + return new WsDataTrade($platform, $posSide, $symbol, $side, $price, $qty, $lot, $pnl, $fee, $quoteVol, $ts, $tradeId, $lever);
  106 + }
86 } 107 }