| | |
| | | elif type_ == "get_code_position_info": |
| | | # 查询此仓 |
| | | code = data.get("code") |
| | | results = huaxin_trade_record_manager.PositionManager().list_by_day(tool.get_now_date_str("%Y%m%d"), code) |
| | | if code: |
| | | results = huaxin_trade_record_manager.PositionManager().list_by_day(tool.get_now_date_str("%Y%m%d"), code) |
| | | else: |
| | | results = huaxin_trade_record_manager.PositionManager().get_from_cache() |
| | | for r in results: |
| | | cb_code = r["securityID"] |
| | | underlying_code = target_codes_manager.get_underlying_code(cb_code) |
| | |
| | | r["marketInfo"] = {"code": cb_market.code, "name": r["securityName"], |
| | | "rate": f"{round(cb_market.rate * 100, 2)}%", |
| | | "price": cb_market.price, "lastVolume": cb_market.total_bid_volume // 100, |
| | | "buy1Money": output_util.money_desc(cb_market.buy1_price * cb_market.buy1_volume),"preClosePrice":cb_market.pre_close_price} |
| | | "buy1Money": output_util.money_desc(cb_market.buy1_price * cb_market.buy1_volume), |
| | | "preClosePrice": cb_market.pre_close_price} |
| | | if underlying_market: |
| | | if not gpcode_manager.CodesNameManager().get_code_name(underlying_market.code): |
| | | # 异步请求名称 |
| | |
| | | "lastVolume": underlying_market.total_bid_volume // 100, |
| | | "buy1Money": output_util.money_desc( |
| | | underlying_market.buy1_price * underlying_market.buy1_volume), |
| | | "preClosePrice":underlying_market.pre_close_price} |
| | | |
| | | "preClosePrice": underlying_market.pre_close_price} |
| | | # 获取买点与卖点 |
| | | buys = huaxin_trade_record_manager.DealRecordManager().list_buy_by_code_cache(cb_code) |
| | | sells = huaxin_trade_record_manager.DealRecordManager().list_sell_by_code_cache(cb_code) |
| | | r["buy_list"] = [{"price": x["price"], "tradeTime": x["tradeTime"], "volume": x["volume"]} for x in buys] |
| | | r["sell_list"] = [{"price": x["price"], "tradeTime": x["tradeTime"], "volume": x["volume"]} for x in sells] |
| | | send_response({"code": 0, "data": results}, client_id, request_id) |
| | | elif type_ == "refresh_trade_data": |
| | | # 刷新交易数据 |
| | |
| | | d["orderSysID"], d["insertTime"], d["insertDate"], d["acceptTime"], d["cancelTime"], |
| | | d["limitPrice"], d["turnover"], d["volume"], d["volumeTraded"], d["orderStatus"], |
| | | d["orderSubmitStatus"], d["statusMsg"], tool.get_now_datetime_str(), |
| | | tool.get_now_datetime_str(), d["accountID"],d["orderRef"])) |
| | | tool.get_now_datetime_str(), d["accountID"], d["orderRef"])) |
| | | else: |
| | | # 修改数据 |
| | | updateDict = {} |
| | |
| | | # 转dict |
| | | key_list = ["id", "orderLocalID", "securityID", "securityName", "direction", "orderSysID", "insertTime", |
| | | "insertDate", "acceptTime", "cancelTime", "limitPrice", "turnover", "volume", "volumeTraded", |
| | | "orderStatus", "orderSubmitStatus", "statusMsg", "createTime", "updateTime", "accountID", "orderRef"] |
| | | "orderStatus", "orderSubmitStatus", "statusMsg", "createTime", "updateTime", "accountID", |
| | | "orderRef"] |
| | | fresults = [] |
| | | max_update_time = None |
| | | if results: |
| | |
| | | def __get_redis(cls): |
| | | return cls.__redisManager.getRedis() |
| | | |
| | | # 保存代码的量 |
| | | @classmethod |
| | | def __save_code_volume(cls, code, volume): |
| | | RedisUtils.setex(cls.__get_redis(), f"available_position_{code}", tool.get_expire(), f"{volume}") |
| | | |
| | | @classmethod |
| | | def get_code_volume(cls, code): |
| | | val = RedisUtils.get(cls.__get_redis(), f"available_position_{code}") |
| | | if not val: |
| | | return 0 |
| | | return int(val) |
| | | |
| | | @classmethod |
| | | def cache(cls, datas): |
| | | cls.latest_positions = copy.deepcopy(datas) |
| | | for d in cls.latest_positions: |
| | | d["id"] = f"{d['investorID']}-{d['tradingDay']}-{d['securityID']}" |
| | | |
| | | @classmethod |
| | | def get_from_cache(cls): |
| | | if cls.latest_positions: |
| | | return copy.deepcopy(cls.latest_positions) |
| | | return [] |
| | | |
| | | @classmethod |
| | | def add(cls, datas): |
| | |
| | | try: |
| | | if datas: |
| | | # 统计可用量 |
| | | volume_dict = {} |
| | | for d in datas: |
| | | if d["securityID"] not in volume_dict: |
| | | volume_dict[d["securityID"]] = 0 |
| | | volume_dict[d["securityID"]] = volume_dict[d["securityID"]] + d["availablePosition"] |
| | | for k in volume_dict: |
| | | cls.__save_code_volume(k, volume_dict[k]) |
| | | d["id"] = f"{d['investorID']}-{d['tradingDay']}-{d['securityID']}" |
| | | cls.latest_positions = datas |
| | | |
| | | for d in datas: |
| | | _id = f"{d['investorID']}-{d['tradingDay']}-{d['securityID']}" |
| | | _id = d["id"] |
| | | # 查询是否有数据 |
| | | result = mysqldb.select_one( |
| | | f"select * from hx_trade_position where id='{_id}'") |
| | |
| | | mysqldb = mysql_data.Mysqldb() |
| | | try: |
| | | results = mysqldb.select_all( |
| | | f"select * from hx_trade_position r where r.tradingDay='{day}' { f'and securityID={code}' if code else ''} order by createTime") |
| | | f"select * from hx_trade_position r where r.tradingDay='{day}' {f'and securityID={code}' if code else ''} order by createTime") |
| | | # 转dict |
| | | key_list = ["id", "investorID","tradingDay", "securityName", "securityID", "historyPos", "historyPosFrozen", |
| | | key_list = ["id", "investorID", "tradingDay", "securityName", "securityID", "historyPos", |
| | | "historyPosFrozen", |
| | | "todayBSPos", "todayBSPosFrozen", "historyPosPrice", "totalPosCost", "prePosition", |
| | | "availablePosition", "currentPosition", |
| | | "openPosCost", "todayCommission", "todayTotalBuyAmount", "todayTotalSellAmount", "createTime", |
| | |
| | | fresults.append(d) |
| | | return fresults |
| | | |
| | | def list_buy_by_code_cache(self, code): |
| | | fresults = [] |
| | | for k in self.__latest_deal_trade_id_dict: |
| | | d = self.__latest_deal_trade_id_dict[k] |
| | | if d["securityID"] != code: |
| | | continue |
| | | if int(d["direction"]) != 0: |
| | | continue |
| | | fresults.append(d) |
| | | return fresults |
| | | |
| | | @classmethod |
| | | def add(cls, datas): |
| | | mysqldb = mysql_data.Mysqldb() |
| | |
| | | if datas: |
| | | for d in datas: |
| | | cls.__latest_deal_trade_id_dict[d['tradeID']] = d |
| | | |
| | | for d in datas: |
| | | # 查询是否有数据 |
| | | result = mysqldb.select_one( |
| | | f"select * from hx_trade_deal_record where tradeID='{d['tradeID']}'") |
| | |
| | | mysqldb.execute( |
| | | "insert into hx_trade_deal_record values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" % ( |
| | | d["tradeID"], d["securityID"], d["orderLocalID"], d["direction"], |
| | | d["orderSysID"], round(d["price"], 2), d["tradeTime"], d["volume"], |
| | | d["orderSysID"], round(d["price"], 3), d["tradeTime"], d["volume"], |
| | | d["tradeDate"], |
| | | d["tradingDay"], d["pbuID"], d["accountID"], |
| | | tool.get_now_datetime_str(), |
| | |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | print(len(PositionManager().list_by_day("20240520","123089"))) |
| | | print(len(PositionManager().list_by_day("20240520", "123089"))) |