async def update_position(self) -> None: if self.ts_locked['update_position'] > self.ts_released[ 'update_position']: return self.ts_locked['update_position'] = time() try: position = await self.fetch_position() position['wallet_balance'] = self.adjust_wallet_balance( position['wallet_balance']) # isolated equity, not cross equity position['equity'] = position['wallet_balance'] + \ calc_upnl(position['long']['size'], position['long']['price'], position['shrt']['size'], position['shrt']['price'], self.price, self.inverse, self.c_mult) position = self.add_pbrs_to_pos(position) if self.position != position: if self.position and 'spot' in self.market_type and \ (self.position['long']['size'] != position['long']['size'] or self.position['shrt']['size'] != position['shrt']['size']): # update fills if position size changed await self.update_fills() self.dump_log({'log_type': 'position', 'data': position}) self.position = position except Exception as e: print('error with update position', e) traceback.print_exc() finally: self.ts_released['update_position'] = time()
async def on_user_stream_event(self, event: dict) -> None: try: pos_change = False if 'wallet_balance' in event: self.position['wallet_balance'] = self.adjust_wallet_balance(event['wallet_balance']) pos_change = True if 'long_psize' in event: self.position['long']['size'] = event['long_psize'] self.position['long']['price'] = event['long_pprice'] self.position = self.add_pbrs_to_pos(self.position) pos_change = True if 'shrt_psize' in event: self.position['shrt']['size'] = event['shrt_psize'] self.position['shrt']['price'] = event['shrt_pprice'] self.position = self.add_pbrs_to_pos(self.position) pos_change = True if 'new_open_order' in event: if event['new_open_order']['order_id'] not in {x['order_id'] for x in self.open_orders}: self.open_orders.append(event['new_open_order']) if 'deleted_order_id' in event: self.open_orders = [oo for oo in self.open_orders if oo['order_id'] != event['deleted_order_id']] if 'partially_filled' in event: await self.update_open_orders() if pos_change: self.position['equity'] = self.position['wallet_balance'] + \ calc_upnl(self.position['long']['size'], self.position['long']['price'], self.position['shrt']['size'], self.position['shrt']['price'], self.price, self.inverse, self.c_mult) await asyncio.sleep(0.01) # sleep 10 ms to catch both pos update and open orders update await self.cancel_and_create() except Exception as e: print(['error handling user stream event', e]) traceback.print_exc()
async def on_user_stream_event(self, event: dict) -> None: try: pos_change = False if "wallet_balance" in event: self.position["wallet_balance"] = self.adjust_wallet_balance( event["wallet_balance"]) pos_change = True if "psize_long" in event: self.position["long"]["size"] = event["psize_long"] self.position["long"]["price"] = event["pprice_long"] self.position = self.add_wallet_exposures_to_pos(self.position) pos_change = True if "psize_short" in event: self.position["short"]["size"] = event["psize_short"] self.position["short"]["price"] = event["pprice_short"] self.position = self.add_wallet_exposures_to_pos(self.position) pos_change = True if "new_open_order" in event: if event["new_open_order"]["order_id"] not in { x["order_id"] for x in self.open_orders }: self.open_orders.append(event["new_open_order"]) if "deleted_order_id" in event: self.open_orders = [ oo for oo in self.open_orders if oo["order_id"] != event["deleted_order_id"] ] if "partially_filled" in event: await self.update_open_orders() if pos_change: self.position[ "equity"] = self.position["wallet_balance"] + calc_upnl( self.position["long"]["size"], self.position["long"]["price"], self.position["short"]["size"], self.position["short"]["price"], self.price, self.inverse, self.c_mult, ) await asyncio.sleep( 0.01 ) # sleep 10 ms to catch both pos update and open orders update await self.cancel_and_create() except Exception as e: print(["error handling user stream event", e]) traceback.print_exc()
async def update_position(self) -> None: if self.ts_locked["update_position"] > self.ts_released[ "update_position"]: return self.ts_locked["update_position"] = time() try: position = await self.fetch_position() position["wallet_balance"] = self.adjust_wallet_balance( position["wallet_balance"]) # isolated equity, not cross equity position["equity"] = position["wallet_balance"] + calc_upnl( position["long"]["size"], position["long"]["price"], position["short"]["size"], position["short"]["price"], self.price, self.inverse, self.c_mult, ) position = self.add_wallet_exposures_to_pos(position) if self.position != position: if (self.position and "spot" in self.market_type and (self.position["long"]["size"] != position["long"]["size"] or self.position["short"]["size"] != position["short"]["size"])): # update fills if position size changed await self.update_fills() self.dump_log({"log_type": "position", "data": position}) self.position = position self.error_halt["update_position"] = False except Exception as e: self.error_halt["update_position"] = True print("error with update position", e) traceback.print_exc() finally: self.ts_released["update_position"] = time()
async def on_user_stream_event(self, event: dict) -> None: try: pos_change = False if 'balance' in event: onhand_change = False for token in event['balance']: self.balance[token]['free'] = event['balance'][token][ 'free'] self.balance[token]['locked'] = event['balance'][token][ 'locked'] onhand = event['balance'][token]['free'] + event[ 'balance'][token]['locked'] if token in [ self.quot, self.coin ] and ('onhand' not in self.balance[token] or self.balance[token]['onhand'] != onhand): onhand_change = True if token == 'BNB': onhand = max(0.0, onhand - 0.01) self.balance[token]['onhand'] = onhand if onhand_change: self.position = self.calc_simulated_position( self.balance, self.fills) self.position[ 'wallet_balance'] = self.adjust_wallet_balance( self.position['wallet_balance']) self.position = self.add_pbrs_to_pos(self.position) pos_change = True if 'filled' in event: if event['filled']['order_id'] not in { fill['order_id'] for fill in self.fills }: self.fills = sorted(self.fills + [event['filled']], key=lambda x: x['order_id']) self.position = self.calc_simulated_position( self.balance, self.fills) self.position['wallet_balance'] = self.adjust_wallet_balance( self.position['wallet_balance']) self.position = self.add_pbrs_to_pos(self.position) pos_change = True elif 'partially_filled' in event: await asyncio.sleep(0.01) await asyncio.gather(self.update_position(), self.update_open_orders()) pos_change = True if 'new_open_order' in event: if event['new_open_order']['order_id'] not in { x['order_id'] for x in self.open_orders }: self.open_orders.append(event['new_open_order']) elif 'deleted_order_id' in event: for i, o in enumerate(self.open_orders): if o['order_id'] == event['deleted_order_id']: self.open_orders = self.open_orders[: i] + self.open_orders[ i + 1:] break if pos_change: self.position['equity'] = self.position['wallet_balance'] + \ calc_upnl(self.position['long']['size'], self.position['long']['price'], self.position['shrt']['size'], self.position['shrt']['price'], self.price, self.inverse, self.c_mult) await asyncio.sleep( 0.01 ) # sleep 10 ms to catch both pos update and open orders update await self.cancel_and_create() except Exception as e: print(['error handling user stream event', e]) traceback.print_exc()
async def on_user_stream_event(self, event: dict) -> None: try: pos_change = False if "balance" in event: onhand_change = False for token in event["balance"]: self.balance[token]["free"] = event["balance"][token][ "free"] self.balance[token]["locked"] = event["balance"][token][ "locked"] onhand = event["balance"][token]["free"] + event[ "balance"][token]["locked"] if token in [ self.quot, self.coin ] and ("onhand" not in self.balance[token] or self.balance[token]["onhand"] != onhand): onhand_change = True if token == "BNB": onhand = max(0.0, onhand - 0.01) self.balance[token]["onhand"] = onhand if onhand_change: self.position = self.calc_simulated_position( self.balance, self.fills) self.position[ "wallet_balance"] = self.adjust_wallet_balance( self.position["wallet_balance"]) self.position = self.add_wallet_exposures_to_pos( self.position) pos_change = True if "filled" in event: if event["filled"]["order_id"] not in { fill["order_id"] for fill in self.fills }: self.fills = sorted(self.fills + [event["filled"]], key=lambda x: x["order_id"]) self.position = self.calc_simulated_position( self.balance, self.fills) self.position["wallet_balance"] = self.adjust_wallet_balance( self.position["wallet_balance"]) self.position = self.add_wallet_exposures_to_pos(self.position) pos_change = True elif "partially_filled" in event: await asyncio.sleep(0.01) await asyncio.gather(self.update_position(), self.update_open_orders()) pos_change = True if "new_open_order" in event: if event["new_open_order"]["order_id"] not in { x["order_id"] for x in self.open_orders }: self.open_orders.append(event["new_open_order"]) elif "deleted_order_id" in event: for i, o in enumerate(self.open_orders): if o["order_id"] == event["deleted_order_id"]: self.open_orders = self.open_orders[: i] + self.open_orders[ i + 1:] break if pos_change: self.position[ "equity"] = self.position["wallet_balance"] + calc_upnl( self.position["long"]["size"], self.position["long"]["price"], self.position["short"]["size"], self.position["short"]["price"], self.price, self.inverse, self.c_mult, ) await asyncio.sleep( 0.01 ) # sleep 10 ms to catch both pos update and open orders update await self.cancel_and_create() except Exception as e: print(["error handling user stream event", e]) traceback.print_exc()