作者 karlet

feat:监听订单

@@ -210,20 +210,27 @@ class CmBroker @@ -210,20 +210,27 @@ class CmBroker
210 private function bybitAccDataHandle($data, $onData) 210 private function bybitAccDataHandle($data, $onData)
211 { 211 {
212 if (isset($data['topic']) && $data['topic'] == 'order') { 212 if (isset($data['topic']) && $data['topic'] == 'order') {
213 - $wsDataTrade = WsDataTrade::TransferBybitTrade($data, $this->symbolInfos, function ($symbol) { 213 + foreach ($data['data'] as $key => $value) {
  214 + $wsDataOrd = WsDataOrder::TransferBybitOrder($value, $this->symbolInfos, function ($symbol) {
214 return $this->getSymbolSt($symbol); 215 return $this->getSymbolSt($symbol);
215 }); 216 });
216 - if ($wsDataTrade != null) {  
217 - $wsData = new WsData($this->plat, 'trade', $trade = $wsDataTrade); 217 + if ($wsDataOrd != null) {
  218 + $wsData = new WsData($this->plat, 'order', $trade = null, $pos = null, $order = $wsDataOrd);
218 $onData($wsData); 219 $onData($wsData);
219 } 220 }
220 - $wsDataOrd = WsDataOrder::TransferBybitOrder($data, $this->symbolInfos, function ($symbol) { 221 + }
  222 + return;
  223 + }
  224 + if (isset($data['topic']) && $data['topic'] == 'execution') {
  225 + foreach ($data['data'] as $key => $value) {
  226 + $wsDataTrade = WsDataTrade::TransferBybitTrade($value, $this->symbolInfos, function ($symbol) {
221 return $this->getSymbolSt($symbol); 227 return $this->getSymbolSt($symbol);
222 }); 228 });
223 - if ($wsDataOrd != null) {  
224 - $wsData = new WsData($this->plat, 'order', $trade = null, $pos = null, $order = $wsDataOrd); 229 + if ($wsDataTrade != null) {
  230 + $wsData = new WsData($this->plat, 'trade', $trade = $wsDataTrade);
225 $onData($wsData); 231 $onData($wsData);
226 } 232 }
  233 + }
227 return; 234 return;
228 } 235 }
229 if (isset($data['topic']) && $data['topic'] == 'position') { 236 if (isset($data['topic']) && $data['topic'] == 'position') {
@@ -160,8 +160,8 @@ class ExBroker @@ -160,8 +160,8 @@ class ExBroker
160 'req_id' => (string)time(), 160 'req_id' => (string)time(),
161 'op' => 'subscribe', 161 'op' => 'subscribe',
162 'args' => [ 162 'args' => [
163 - // 'order',  
164 - // 'execution', 163 + 'order',
  164 + 'execution',
165 'position', 165 'position',
166 // 'wallet' 166 // 'wallet'
167 ] 167 ]
@@ -134,27 +134,36 @@ class WsDataOrder @@ -134,27 +134,36 @@ class WsDataOrder
134 } 134 }
135 public static function TransferBybitOrder($data, $symbolInfos, callable $toSymbolSt): WsDataOrder|null 135 public static function TransferBybitOrder($data, $symbolInfos, callable $toSymbolSt): WsDataOrder|null
136 { 136 {
137 - $order = $data['data'];  
138 - $symbol = call_user_func($toSymbolSt, $order['symbol']); 137 + $symbol = call_user_func($toSymbolSt, $data['symbol']);
139 /** @var SymbolInfo $symbolInfo */ 138 /** @var SymbolInfo $symbolInfo */
140 $symbolInfo = $symbolInfos[$symbol] ?? null; 139 $symbolInfo = $symbolInfos[$symbol] ?? null;
141 if ($symbolInfo == null) { 140 if ($symbolInfo == null) {
142 return null; 141 return null;
143 } 142 }
144 $platform = 'bybit'; 143 $platform = 'bybit';
145 - $posSide = strtoupper($order['side']);  
146 - $side = strtoupper($order['side']);  
147 - $price = (float)$order['price'];  
148 - $avgPx = (float)$order['avg_price'];  
149 - $lot = (float)$order['qty']; 144 + $posSide = "BOTH";
  145 + if ($data['positionIdx'] == 1) {
  146 + $posSide = "LONG";
  147 + }
  148 + if ($data['positionIdx'] == 2) {
  149 + $posSide = "SHORT";
  150 + }
  151 + $side = strtoupper($data['side']);
  152 + $price = (float)$data['price'];
  153 + $avgPx = (float)$data['avgPrice'];
  154 + $lot = (float)$data['qty'];
150 $qty = $lot; 155 $qty = $lot;
151 - $pnl = (float)$order['realised_pnl'];  
152 - $ts = (int)$order['transact_time'];  
153 - $uts = (int)$order['transact_time'];  
154 - $ordType = strtoupper($order['order_type']);  
155 - $cliOrdId = $order['order_link_id'];  
156 - $ordId = $order['order_id'];  
157 - $status = self::getBnStatus($order['order_status']); 156 + $pnl = (float)$data['closedPnl']; //去除手续费资金费的盈亏
  157 + $ts = (int)$data['updatedTime'];
  158 + $uts = (int)$data['updatedTime'];
  159 + $ordType = strtoupper($data['orderType']);
  160 + $timeInForce = $data['timeInForce'];
  161 + if ($timeInForce == 'IOC' && $ordType == "LIMIT") {
  162 + $ordType = "IOC";
  163 + }
  164 + $cliOrdId = $data['orderLinkId'];
  165 + $ordId = $data['orderId'];
  166 + $status = self::getBybitStatus($data['orderStatus']);
158 return new WsDataOrder($platform, $posSide, $symbol, $side, $price, $avgPx, $qty, $lot, $pnl, $ts, $uts, $ordType, $cliOrdId, $ordId, $status); 167 return new WsDataOrder($platform, $posSide, $symbol, $side, $price, $avgPx, $qty, $lot, $pnl, $ts, $uts, $ordType, $cliOrdId, $ordId, $status);
159 } 168 }
160 private static function getBnStatus(string $state) 169 private static function getBnStatus(string $state)
@@ -176,4 +185,20 @@ class WsDataOrder @@ -176,4 +185,20 @@ class WsDataOrder
176 } 185 }
177 return 'UNKNOWN'; 186 return 'UNKNOWN';
178 } 187 }
  188 + private static function getBybitStatus(string $state)
  189 + {
  190 + if ($state == 'New') {
  191 + return 'LIVE';
  192 + }
  193 + if ($state == 'PartiallyFilled') {
  194 + return 'PARTIALLY_FILLED';
  195 + }
  196 + if ($state == 'Filled') {
  197 + return 'FILLED';
  198 + }
  199 + if ($state == 'Cancelled' || $state == "Rejected") {
  200 + return 'CANCELED';
  201 + }
  202 + return 'UNKNOWN';
  203 + }
179 } 204 }