async def active_orders_df(self) -> pd.DataFrame: """ Return the active orders in a DataFrame. """ size_q_col = f"Amt({self._token})" if self.is_token_a_quote_token( ) else "Amt(Quote)" columns = [ "Market", "Side", "Price", "Spread", "Amount", size_q_col, "Age" ] data = [] for order in self.active_orders: mid_price = self._market_infos[order.trading_pair].get_mid_price() spread = 0 if mid_price == 0 else abs(order.price - mid_price) / mid_price size_q = order.quantity * mid_price age = order_age(order, self.current_timestamp) # // indicates order is a paper order so 'n/a'. For real orders, calculate age. age_txt = "n/a" if age <= 0. else pd.Timestamp( age, unit='s').strftime('%H:%M:%S') data.append([ order.trading_pair, "buy" if order.is_buy else "sell", float(order.price), f"{spread:.2%}", float(order.quantity), float(size_q), age_txt ]) df = pd.DataFrame(data=data, columns=columns) df.sort_values(by=["Market", "Side"], inplace=True) return df
def active_orders_df(self) -> pd.DataFrame: price = self.get_price() active_orders = self.active_orders no_sells = len([o for o in active_orders if not o.is_buy]) active_orders.sort(key=lambda x: x.price, reverse=True) columns = [ "Level", "Type", "Price", "Spread", "Amount (Orig)", "Amount (Adj)", "Age" ] data = [] lvl_buy, lvl_sell = 0, 0 for idx in range(0, len(active_orders)): order = active_orders[idx] level = None if order.is_buy: level = lvl_buy + 1 lvl_buy += 1 else: level = no_sells - lvl_sell lvl_sell += 1 spread = 0 if price == 0 else abs(order.price - price) / price age = pd.Timestamp(order_age(order, self.current_timestamp), unit='s').strftime('%H:%M:%S') amount_orig = "" if level is None else self._order_amount + ( (level - 1) * self._order_level_amount) data.append([ level, "buy" if order.is_buy else "sell", float(order.price), f"{spread:.2%}", amount_orig, float(order.quantity), age ]) return pd.DataFrame(data=data, columns=columns)
def test_order_age(self, time_mock): time_mock.return_value = 1640001112.223 order = LimitOrder(client_order_id="OID1", trading_pair="COINALPHA-HBOT", is_buy=True, base_currency="COINALPHA", quote_currency="HBOT", price=Decimal(1000), quantity=Decimal(1), creation_timestamp=1640001110000000) age = order_age(order) self.assertEqual(int(time_mock.return_value - 1640001110), age)
def cancel_active_orders(self, proposals: List[Proposal]): """ Cancel any orders that have an order age greater than self._max_order_age or if orders are not within tolerance """ for proposal in proposals: to_cancel = False cur_orders = [o for o in self.active_orders if o.trading_pair == proposal.market] if cur_orders and any(order_age(o, self.current_timestamp) > self._max_order_age for o in cur_orders): to_cancel = True elif self._refresh_times[proposal.market] <= self.current_timestamp and \ cur_orders and not self.is_within_tolerance(cur_orders, proposal): to_cancel = True if to_cancel: for order in cur_orders: self.cancel_order(self._market_infos[proposal.market], order.client_order_id) # To place new order on the next tick self._refresh_times[order.trading_pair] = self.current_timestamp + 0.1
def _limit_order_age(self, order: LimitOrder): calculated_age = order_age(order) return calculated_age if calculated_age >= 0 else 0