| | |
| | | self.__big_buy_orders = [] |
| | | # 精确的买单信息,{买单号:订单信息} |
| | | self.__big_accurate_buy_order_dict = {} |
| | | self.__big_accurate_sell_order_dict = {} |
| | | self.__latest_sell_order = None |
| | | self.__big_sell_orders = [] |
| | | self.big_accurate_buy_order_queue = queue.Queue(maxsize=10240) |
| | | self.big_accurate_sell_order_queue = queue.Queue(maxsize=10240) |
| | | self.big_buy_order_queue = queue.Queue(maxsize=10240) |
| | | self.big_sell_order_queue = queue.Queue(maxsize=10240) |
| | | self.accurate_buy = accurate_buy |
| | | self.__last_accurate_buy_count = 0 |
| | | self.__last_accurate_sell_count = 0 |
| | | |
| | | def get_big_buy_orders(self): |
| | | return self.__big_buy_orders |
| | |
| | | def get_big_sell_orders(self): |
| | | return self.__big_sell_orders |
| | | |
| | | def add_transaction_data_for_accurate_buy(self, data): |
| | | def add_transaction_data_for_accurate(self, data): |
| | | """ |
| | | 获取精确的买单信息 |
| | | @param data: |
| | |
| | | if last_buy_order[2] > 299e4: |
| | | self.big_accurate_buy_order_queue.put_nowait(last_buy_order) |
| | | # 如果数据过多需要移除过长时间的小金额数据 |
| | | accurate_buy_count = len(self.__big_accurate_buy_order_dict) |
| | | accurate_buy_count = len(self.__big_accurate_buy_order_dict.keys()) |
| | | if accurate_buy_count > 10000 and accurate_buy_count - self.__last_accurate_buy_count > 2000: |
| | | # 超过1w条数据且新增2000条数据 |
| | | # 超过1w条数据就要移除30分钟之前的数据 |
| | | now_time_int = int(tool.trade_time_add_second(tool.get_now_time_str(), -1800).replace(":", "")) |
| | | now_time_int = int(tool.trade_time_add_second(tool.get_now_time_str(), -3600).replace(":", "")) |
| | | try: |
| | | remove_order_nos = [x for x in self.__big_accurate_buy_order_dict if |
| | | now_time_int - format_timestamp( |
| | |
| | | self.__big_accurate_buy_order_dict.pop(order_no) |
| | | finally: |
| | | self.__last_accurate_buy_count = len(self.__big_accurate_buy_order_dict) |
| | | |
| | | # 统计卖单 |
| | | if item[1] not in self.__big_accurate_sell_order_dict: |
| | | # (卖单号, 量, 金额, 时间, 最新成交价格) |
| | | self.__big_accurate_sell_order_dict[item[1]] = [item[1], 0, 0, order_time, price] |
| | | sell_order_info = self.__big_accurate_sell_order_dict[item[1]] |
| | | sell_order_info[1] += volume |
| | | sell_order_info[2] += money |
| | | sell_order_info[3] = order_time |
| | | sell_order_info[4] = price |
| | | if self.__latest_sell_order and self.__latest_sell_order[0] != item[1]: |
| | | # 有可能是大单成交完成, 判断上个订单是否是大单 |
| | | last_sell_order = self.__big_accurate_sell_order_dict.get(self.__latest_sell_order[0]) |
| | | if last_sell_order[2] > 299e4: |
| | | self.big_accurate_sell_order_queue.put_nowait(last_sell_order) |
| | | # 如果数据过多需要移除过长时间的小金额数据 |
| | | accurate_sell_count = len(self.__big_accurate_sell_order_dict.keys()) |
| | | if accurate_sell_count > 10000 and accurate_sell_count - self.__last_accurate_sell_count > 2000: |
| | | # 超过1w条数据且新增2000条数据 |
| | | # 超过1w条数据就要移除30分钟之前的数据 |
| | | now_time_int = int(tool.trade_time_add_second(tool.get_now_time_str(), -3600).replace(":", "")) |
| | | try: |
| | | remove_order_nos = [x for x in self.__big_accurate_sell_order_dict if |
| | | now_time_int - format_timestamp( |
| | | self.__big_accurate_sell_order_dict[x][3]) > 0] |
| | | if remove_order_nos: |
| | | for order_no in remove_order_nos: |
| | | self.__big_accurate_sell_order_dict.pop(order_no) |
| | | finally: |
| | | self.__last_accurate_sell_count = len(self.__big_accurate_sell_order_dict.keys()) |
| | | |
| | | def add_transaction_data(self, data): |
| | | item = (data["BuyNo"], data["SellNo"], data["TradePrice"], data["TradeVolume"]) |
| | |
| | | order_time = data["OrderTime"] |
| | | |
| | | if self.accurate_buy: |
| | | self.add_transaction_data_for_accurate_buy(data) |
| | | self.add_transaction_data_for_accurate(data) |
| | | |
| | | if not self.__latest_buy_order: |
| | | # (买单号, 量, 金额, 时间, 最新成交价格) |
| | |
| | | "SellNo": pTransaction['SellNo'], |
| | | "ExecType": pTransaction['ExecType'].decode()} |
| | | if item["SecurityID"] not in l2_transaction_data_dict: |
| | | l2_transaction_data_dict[item["SecurityID"]] = L2TransactionDataManager(item["SecurityID"], item["SecurityID"] in self.special_codes) |
| | | l2_transaction_data_dict[item["SecurityID"]] = L2TransactionDataManager(item["SecurityID"], item[ |
| | | "SecurityID"] in self.special_codes) |
| | | l2_transaction_data_dict[item["SecurityID"]].add_transaction_data(item) |
| | | |
| | | def OnRtnNGTSTick(self, pTick): |
| | |
| | | "SellNo": pTick['SellNo'], |
| | | "ExecType": '1'} |
| | | if item["SecurityID"] not in l2_transaction_data_dict: |
| | | l2_transaction_data_dict[item["SecurityID"]] = L2TransactionDataManager(item["SecurityID"], item["SecurityID"] in self.special_codes) |
| | | l2_transaction_data_dict[item["SecurityID"]] = L2TransactionDataManager(item["SecurityID"], item[ |
| | | "SecurityID"] in self.special_codes) |
| | | l2_transaction_data_dict[item["SecurityID"]].add_transaction_data(item) |
| | | except Exception as e: |
| | | logger_local_huaxin_l2_subscript.exception(e) |
| | |
| | | logger_system.exception(e) |
| | | while True: |
| | | try: |
| | | # 读取一遍 |
| | | for code in l2_transaction_data_dict: |
| | | l2_transaction_data_manager: L2TransactionDataManager = l2_transaction_data_dict[code] |
| | | try: |
| | | while True: |
| | | |
| | | while True: |
| | | if not l2_transaction_data_manager.big_buy_order_queue.empty(): |
| | | result = l2_transaction_data_manager.big_buy_order_queue.get(block=False) |
| | | if result: |
| | | _queue.put_nowait((code, 0, result)) |
| | | else: |
| | | break |
| | | except: |
| | | pass |
| | | else: |
| | | break |
| | | |
| | | try: |
| | | while True: |
| | | while True: |
| | | if not l2_transaction_data_manager.big_accurate_buy_order_queue.empty(): |
| | | result = l2_transaction_data_manager.big_accurate_buy_order_queue.get(block=False) |
| | | if result: |
| | | accurate_buy_order_queue.put_nowait((code, 0, result)) |
| | | else: |
| | | break |
| | | except: |
| | | pass |
| | | else: |
| | | break |
| | | |
| | | while True: |
| | | if not l2_transaction_data_manager.big_accurate_sell_order_queue.empty(): |
| | | result = l2_transaction_data_manager.big_accurate_sell_order_queue.get(block=False) |
| | | if result: |
| | | accurate_buy_order_queue.put_nowait((code, 1, result)) |
| | | else: |
| | | break |
| | | |
| | | try: |
| | | while True: |
| | | while True: |
| | | if not l2_transaction_data_manager.big_sell_order_queue.empty(): |
| | | result = l2_transaction_data_manager.big_sell_order_queue.get(block=False) |
| | | if result: |
| | | _queue.put_nowait((code, 1, result)) |
| | | else: |
| | | break |
| | | except: |
| | | pass |
| | | else: |
| | | break |
| | | except: |
| | | pass |
| | | finally: |
| | | time.sleep(1) |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | pass |