| | |
| | | import inited_data |
| | | import outside_api_command_manager |
| | | from code_attribute import gpcode_manager, code_volumn_manager, global_data_loader |
| | | from code_attribute.gpcode_manager import CodePrePriceManager |
| | | from db import mysql_data_delegate as mysql_data, redis_manager_delegate as redis_manager |
| | | from db.redis_manager_delegate import RedisUtils |
| | | from huaxin_client import l1_subscript_codes_manager, l2_data_transform_protocol |
| | |
| | | |
| | | from trade.huaxin import huaxin_trade_api as trade_api, huaxin_trade_api, huaxin_trade_data_update, \ |
| | | huaxin_trade_record_manager |
| | | from trade.huaxin.huaxin_trade_record_manager import PositionManager |
| | | from trade.l2_trade_factor import L2PlaceOrderParamsManager |
| | | from trade.sell_rule_manager import SellRuleManager, SellRule |
| | | from trade.trade_manager import TradeTargetCodeModeManager |
| | |
| | | __KPLCodeJXBlockManager = KPLCodeJXBlockManager() |
| | | __GCancelBigNumComputer = GCancelBigNumComputer() |
| | | __sell_thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=10) |
| | | __process_l1_data_thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=10) |
| | | __current_price_dict = {} |
| | | |
| | | @classmethod |
| | | def __sell(cls, datas): |
| | |
| | | huaxin_trade_api.order(2, code, r.sell_volume, tool.get_buy_min_price(buy1_price)) |
| | | SellRuleManager().excute_sell(r.id_) |
| | | |
| | | # 保存现价 |
| | | @classmethod |
| | | def __save_l1_current_price(cls, datas): |
| | | for d in datas: |
| | | code = d[0] |
| | | # 格式 (代码,现价,涨幅,量,更新时间,买1价格,买1量) |
| | | price = d[1] |
| | | cls.__current_price_dict[code] = price |
| | | |
| | | # 获取L1现价 |
| | | @classmethod |
| | | def get_l1_current_price(cls, code): |
| | | return cls.__current_price_dict.get(code) |
| | | |
| | | # 设置目标代码 |
| | | @classmethod |
| | | def set_target_codes(cls, data_json): |
| | | data = data_json["data"] |
| | | request_id = data_json["request_id"] |
| | | datas = data["data"] |
| | | cls.__save_l1_current_price(datas) |
| | | cls.__sell_thread_pool.submit(lambda: cls.__sell(datas)) |
| | | HuaXinL1TargetCodesManager.set_level_1_codes_datas(datas, request_id=request_id) |
| | | cls.__process_l1_data_thread_pool.submit( |
| | | lambda: HuaXinL1TargetCodesManager.set_level_1_codes_datas(datas, request_id=request_id)) |
| | | |
| | | @classmethod |
| | | def l2_order(cls, code, _datas, timestamp): |
| | |
| | | |
| | | async_log_util.info(hx_logger_l2_market_data, f"{code}#{data}") |
| | | L2MarketSellManager().set_current_total_sell_data(code, time_str, |
| | | data["totalAskVolume"] * data["avgAskPrice"], data["totalAskVolume"]) |
| | | data["totalAskVolume"] * data["avgAskPrice"], |
| | | data["totalAskVolume"]) |
| | | |
| | | @classmethod |
| | | def trading_order_canceled(cls, code, order_no): |
| | |
| | | price_type = data["price_type"] |
| | | price = data["price"] |
| | | sinfo = data["sinfo"] |
| | | result = huaxin_trade_api.order(direction, code, volume, price, price_type=price_type, sinfo=sinfo, |
| | | blocking=True, request_id=request_id) |
| | | self.send_response({"code": 0, "data": result}, client_id, request_id) |
| | | if direction == huaxin_util.TORA_TSTP_D_Sell: |
| | | pass |
| | | # price_type: 0-价格笼子 1-跌停价 2-涨停价 3-现价 |
| | | # 获取现价 |
| | | if price_type == 0: |
| | | current_price = TradeServerProcessor.get_l1_current_price(code) |
| | | if not current_price: |
| | | raise Exception("没有获取到L1现价") |
| | | price = tool.get_buy_min_price(current_price) |
| | | elif price_type == 1: |
| | | price_ = gpcode_manager.get_limit_down_price(code) |
| | | if not price_: |
| | | raise Exception("没有获取到跌停价") |
| | | price = round(float(price_), 2) |
| | | elif price_type == 2: |
| | | price_ = gpcode_manager.get_limit_up_price(code) |
| | | if not price_: |
| | | raise Exception("没有获取到涨停价") |
| | | price = round(float(price_), 2) |
| | | elif price_type == 3: |
| | | current_price = TradeServerProcessor.get_l1_current_price(code) |
| | | if not current_price: |
| | | raise Exception("没有获取到L1现价") |
| | | price = current_price |
| | | result = huaxin_trade_api.order(direction, code, volume, price, sinfo=sinfo, |
| | | blocking=True, request_id=request_id) |
| | | self.send_response({"code": 0, "data": result}, client_id, request_id) |
| | | else: |
| | | result = huaxin_trade_api.order(direction, code, volume, price, price_type=price_type, sinfo=sinfo, |
| | | blocking=True, request_id=request_id) |
| | | self.send_response({"code": 0, "data": result}, client_id, request_id) |
| | | elif trade_type == outside_api_command_manager.TRADE_TYPE_CANCEL_ORDER: |
| | | print("手动撤单:", data) |
| | | code = data["code"] |
| | |
| | | logging.exception(e) |
| | | self.send_response(json.dumps({"code": 1, "msg": f"数据处理出错:{e}"}), client_id, request_id) |
| | | |
| | | def OnGetCodePositionInfo(self, client_id, request_id, data): |
| | | try: |
| | | code = data["code"] |
| | | # 获取持仓 |
| | | positions = PositionManager.latest_positions |
| | | sell_rules_count = len(SellRuleManager().list_can_excut_rules_cache()) |
| | | fdata = {"code": code, "total": 0, "available": 0, "sell_rules_count": sell_rules_count} |
| | | if positions: |
| | | for d in positions: |
| | | if d["prePosition"] <= 0: |
| | | continue |
| | | if d["securityID"] != code: |
| | | continue |
| | | fdata["total"] = d["prePosition"] |
| | | fdata["available"] = d["availablePosition"] |
| | | break |
| | | result = {"code": 0, "data": fdata} |
| | | self.send_response(result, client_id, request_id) |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | self.send_response(json.dumps({"code": 1, "msg": f"数据处理出错:{e}"}), client_id, request_id) |
| | | |
| | | |
| | | class MyL2DataCallback(l2_data_transform_protocol.L2DataCallBack): |
| | | def OnL2Order(self, code, datas, timestamp): |