def speed_test(): orderbook = OrderBook("") for i in range(1000): j = randrange(0, 2) if j == 0: order = Order(float(random.uniform(1.0, 5.0)), float(randrange(0, 3)), 0.0, BID) orderbook.add_modify_delete(order, BID) else: order = Order(float(random.uniform(5.0, 9.0)), float(randrange(0, 3)), 0.0, ASK) orderbook.add_modify_delete(order, ASK) print(orderbook)
def __process_initial_book_state(self): self.market_state = MarketState(self.market) book = self.client.get_order_book(symbol=self.market) # print(book, type(book)) self.order_book = self.market_state.order_book for bid in book['bids']: self.order_book.add_modify_delete(Order(float(bid[0]), float(bid[1])), side='b') for ask in book['asks']: self.order_book.add_modify_delete(Order(float(ask[0]), float(ask[1])), side='a') print(self.order_book)
def submit_depth_price_order(self, market_state: MarketState, side, depth, price_offset, quantity): """price_offset - distance from mid price which the order has to be, or the depth will increase""" # check if price offsets are crossed with mid price # depth starts at 0 (top of book) d = depth mid = market_state.order_book.getMidPrice() target_price = mid + price_offset if side == BID: if target_price > mid or len( market_state.order_book.bids.sortedOrderList) < d + 1: return None price = market_state.order_book.bids.sortedOrderList[ d].price + market_state.MIN_TICK # new bid must be less than target price, or at least target price while price > target_price: d += 1 if len(market_state.order_book.bids.sortedOrderList) < d + 1: return None price = market_state.order_book.bids.sortedOrderList[ d].price + market_state.MIN_TICK new_order = Order(price=price, quantity=quantity, pos_quantity=quantity, side=side) market_state.new_limit_buy(new_order) else: if target_price < mid or len( market_state.order_book.asks.sortedOrderList) < d + 1: return None price = market_state.order_book.asks.sortedOrderList[ d].price - market_state.MIN_TICK # new ask must be greater than target price, or at least target price while price < target_price: d += 1 if len(market_state.order_book.asks.sortedOrderList) < d + 1: return None price = market_state.order_book.asks.sortedOrderList[ d].price - market_state.MIN_TICK new_order = Order(price=price, quantity=quantity, pos_quantity=quantity, side=side) market_state.new_limit_sell(new_order) return new_order
def cancel_limit_buy(self, order: Order): assert (order.side == BID) logger.debug("cancel_limit_buy\t: {}".format(order)) # print("order cancel coming in :", order) self.order_book.add_modify_delete(Order(order.price, quantity=0.0, pos_quantity=0.0), side='b', my_order=True) self.open_orders.add_modify_delete(Order(order.price, quantity=0.0, pos_quantity=0.0), side='b', my_order=True) self.power['USDT'] += order.price * order.position_quantity * ( 1 + self.MAKER_FEE) self.num_buy_cancels += 1 self.__round_portfolio()
def cancel_limit_sell(self, order: Order): assert (order.side == ASK) logger.debug("cancel_limit_sell\t: {}".format(order)) # print("order cancel coming in :", order) self.order_book.add_modify_delete(Order(order.price, quantity=0.0, pos_quantity=0.0, side='a'), side='a', my_order=True) self.open_orders.add_modify_delete(Order(order.price, quantity=0.0, pos_quantity=0.0, side='a'), side='a', my_order=True) self.power['BTC'] += order.position_quantity self.num_sell_cancels += 1 self.__round_portfolio()
def submit_depth_order(self, market_state: MarketState, side, depth, quantity): # depth starts at 0 (top of book) if side == BID: price = market_state.order_book.bids.sortedOrderList[ depth].price + market_state.MIN_TICK new_order = Order(price=price, quantity=quantity, pos_quantity=quantity, side=side) market_state.new_limit_buy(new_order) else: price = market_state.order_book.asks.sortedOrderList[ depth].price - market_state.MIN_TICK new_order = Order(price=price, quantity=quantity, pos_quantity=quantity, side=side) market_state.new_limit_sell(new_order) return new_order
def __init__(self, market='BTC', target="BTC", base="USDT"): self.market = market self.order_book = OrderBook(market, depth=60) self.portfolio_dict = {target: 0.0, base: 10000.0} self.starting_portfolio = copy.deepcopy(self.portfolio_dict) self.power = copy.deepcopy(self.portfolio_dict) self.target = target self.base = base self.last_trade = Order(0.0, 0.0) self.last_trade_orders = [] self.last_trade_msg = "{}" self.starting_portfolio_value = -1 self.num_trades = 0 self.MAKER_FEE = 0.0002 self.TAKER_FEE = 0.0002 # self.MAKER_FEE = 0.0 # self.TAKER_FEE = 0.0 self.MIN_TICK = 0.00001 # An order book with only my orders self.open_orders = OrderBook(market) self.fills = [] self.num_buy_orders = 0 self.num_sell_orders = 0 self.sum_of_buy_values = 0.0 self.sum_of_sell_values = 0.0 self.num_buy_cancels = 0 self.num_sell_cancels = 0 self.total_buy_quantity = 0.0 self.total_sell_quantity = 0.0 self.VOLUME_ALERT_THRESH = 2 self.historical_volume = [{"vol": 0.0, "alert": 0.0}] self.history_len = 20 # minutes self._clock_thread = threading.Thread(target=self.__update_clock) self._clock_lock = threading.Lock() self._clock_thread.start() self.num_alerts = 0 self.moving_averages = [{ "ma_total": 0.0, "vwma_total": 0.0, "total_trades": 0, "total_volume": 0.0 }] self.moving_averages_len = 7 # minutes self.moving_average = {'ma': 0.0, 'vwma': 0.0} self._moving_average_last = {'ma': 0.0, 'vwma': 0.0} self.moving_average_slope = NEUTRAL self.longer_moving_averages = [{ "ma_total": 0.0, "vwma_total": 0.0, "total_trades": 0, "total_volume": 0.0 }] self.longer_moving_averages_len = 25 # minutes self.longer_moving_average = {'ma': 0.0, 'vwma': 0.0} self._longer_moving_average_last = {'ma': 0.0, 'vwma': 0.0} self.longer_moving_average_slope = NEUTRAL self.trend_switched = True self.micro_trend = NEUTRAL self.macro_trend = NEUTRAL self.elapse_min = False # set false until moving average length has elapsed self.trade_cache = None