def __init__(self, symbol: str, tick: TickData, max_depth: int): self.symbol = symbol self.max_depth = max_depth self.buy_book = sortedcontainers.SortedDict() self.sell_book = sortedcontainers.SortedDict() for idx in range(tick.data_depth): q = OrderQueue(tick.bid_price[idx]) q.add_order(OrderData({'volume': tick.bid_volume[idx], 'is_history': True, 'traded': 0})) self.buy_book[tick.bid_price[idx]] = q q = OrderQueue(tick.ask_price[idx]) q.add_order(OrderData({'volume': tick.ask_volume[idx], 'is_history': True, 'traded': 0})) self.sell_book[tick.ask_price[idx]] = q
def place_order(self, d) -> OrderData: if 'is_history' not in d: d['is_history'] = False order = OrderData(d) order.submit_time = datetime.datetime.now() order.traded = 0 order.status = Status.SUBMITTING symbol = order.symbol if symbol in self.futures: self.futures[symbol].place_order(order) else: print(f'future {symbol} not exist!') return order
def add_order(self, order: OrderData): if order.is_history: self.queue.append([order, self.next_orders]) self.next_orders = [] else: self.next_orders.append(order) self.total_amount_var += order.remain()
def cancel_order(self, order_id: int): order = OrderData.get_order(order_id) if order.price in self.sell_book: self.sell_book[order.price].cancel_algo_order(order_id) if self.sell_book[order.price].history_amount() == 0: del self.sell_book[order.price] if order.price in self.buy_book: self.buy_book[order.price].cancel_algo_order(order_id) if self.buy_book[order.price].history_amount() == 0: del self.buy_book[order.price]
def place_order(self, d, account_name = None) -> OrderData: if 'is_history' not in d: d['is_history'] = False order = OrderData(d) order.submit_time = datetime.datetime.now() order.traded = 0 order.status = Status.SUBMITTING symbol = order.symbol if symbol in self.futures: self.futures[symbol].place_order(order) else: print(f'future {symbol} not exist!') return if account_name is not None: if account_name not in self.accounts: print(f'account {account_name} not exist!') else: self.order_account[order.order_id] = account_name self._process_trade_data() return order
def place_order(self, order: OrderData): if order.volume == 0: return if order.order_type == OrderType.LIMIT: if order.direction == Direction.LONG and order.offset == Offset.OPEN or order.direction == Direction.SHORT and order.offset == Offset.CLOSE: sell_prices = list(self.sell_book.keys()) for sp in sell_prices: if sp > order.price: break order.volume = self.sell_book[sp].match_order(order.volume) if order.volume > 0: del self.sell_book[sp] else: break if order.volume > 0: if order.price not in self.buy_book: self.buy_book[order.price] = OrderQueue() self.buy_book[order.price].add_order(order) elif order.direction == Direction.SHORT and order.offset == Offset.OPEN or order.direction == Direction.LONG and order.offset == Offset.CLOSE: buy_prices = list(reversed(self.buy_book.keys())) for bp in buy_prices: if bp < order.price: break order.volume = self.buy_book[bp].match_order(order.volume) if order.volume > 0: del self.buy_book[bp] else: break if order.volume > 0: if order.price not in self.sell_book: self.sell_book[order.price] = OrderQueue() self.sell_book[order.price].add_order(order) elif order.order_type == OrderType.MARKET: pass else: pass
def _process_trade_data(self): global order_fill_list for fill in order_fill_list: order_id = fill.order_id if order_id in self.order_account: account = self.accounts[self.order_account[order_id]] order = OrderData.get_order(order_id) if order.direction == Direction.LONG and order.offset == Offset.OPEN: # print(f'long open, balance - {fill.fill_amount * fill.price}') account.balance -= fill.fill_amount * fill.price account.position[order.symbol]['long'] += fill.fill_amount elif order.direction == Direction.LONG and order.offset == Offset.CLOSE: # print(f'long close, balance + {fill.fill_amount * fill.price}') account.balance += fill.fill_amount * fill.price account.position[order.symbol]['long'] -= fill.fill_amount elif order.direction == Direction.SHORT and order.offset == Offset.OPEN: # print(f'short open, balance + {fill.fill_amount * fill.price}') account.balance += fill.fill_amount * fill.price account.position[order.symbol]['short'] += fill.fill_amount elif order.direction == Direction.SHORT and order.offset == Offset.CLOSE: # print(f'short close, balance - {fill.fill_amount * fill.price}') account.balance -= fill.fill_amount * fill.price account.position[order.symbol]['short'] -= fill.fill_amount order_fill_list = []
def place_order(self, order: OrderData): global order_fill_list if order.volume == 0: order.callback() if hasattr(order, 'callback') else None return if order.order_type == OrderType.LIMIT: if order.direction == Direction.LONG and order.offset == Offset.OPEN or order.direction == Direction.SHORT and order.offset == Offset.CLOSE: sell_prices = list(self.sell_book.keys()) for sp in sell_prices: if sp > order.price: break if not order.is_history: order.callback() if hasattr(order, 'callback') else None order_fill_list.append(TradeData(order.order_id, sp, order.volume)) order.volume = 0 break order.volume = self.sell_book[sp].match_order(order.volume) if self.sell_book[sp].history_amount() <= 0: del self.sell_book[sp] else: break if order.volume > 0: if order.price not in self.buy_book: self.buy_book[order.price] = OrderQueue(order.price) self.buy_book[order.price].add_order(order) elif order.direction == Direction.SHORT and order.offset == Offset.OPEN or order.direction == Direction.LONG and order.offset == Offset.CLOSE: buy_prices = list(reversed(self.buy_book.keys())) for bp in buy_prices: if bp < order.price: break if not order.is_history: order.callback() if hasattr(order, 'callback') else None order_fill_list.append(TradeData(order.order_id, bp, order.volume)) order.volume = 0 break order.volume = self.buy_book[bp].match_order(order.volume) if self.buy_book[bp].history_amount() <= 0: del self.buy_book[bp] else: break if order.volume > 0: if order.price not in self.sell_book: self.sell_book[order.price] = OrderQueue(order.price) self.sell_book[order.price].add_order(order) elif order.order_type == OrderType.MARKET: if order.is_history: # not possible return if order.direction == Direction.LONG and order.offset == Offset.OPEN or order.direction == Direction.SHORT and order.offset == Offset.CLOSE: sell_prices = list(self.sell_book.keys()) order.callback() if hasattr(order, 'callback') else None order_fill_list.append(TradeData(order.order_id, sell_prices[0], order.volume)) order.volume = 0 elif order.direction == Direction.SHORT and order.offset == Offset.OPEN or order.direction == Direction.LONG and order.offset == Offset.CLOSE: buy_prices = list(reversed(self.buy_book.keys())) order.callback() if hasattr(order, 'callback') else None order_fill_list.append(TradeData(order.order_id, buy_prices[0], order.volume)) order.volume = 0 else: pass