| | |
| | | from third_data.history_k_data_manager import HistoryKDataManager |
| | | from third_data.history_k_data_util import JueJinApi, HistoryKDatasUtils |
| | | from third_data.kpl_data_manager import KPLDataManager |
| | | from third_data.kpl_limit_up_data_manager import CodeLimitUpSequenceManager |
| | | from third_data.kpl_util import KPLDataType |
| | | from trade import trade_manager, l2_trade_util |
| | | from trade import trade_manager, l2_trade_util, trade_data_manager |
| | | import l2_data_util as l2_data_util_old |
| | | |
| | | from trade.huaxin import huaxin_trade_api, huaxin_trade_data_update, \ |
| | |
| | | except Exception as e: |
| | | self.send_response({"code": 1, "msg": str(e)}, client_id, request_id) |
| | | elif ctype == "get_delegated_buy_code_infos": |
| | | account_available_money = trade_manager.AccountAvailableMoneyManager().get_available_money_cache() |
| | | account_available_money = trade_data_manager.AccountMoneyManager().get_available_money_cache() |
| | | # 获取委托中的代码 |
| | | # current_delegates = huaxin_trade_record_manager.DelegateRecordManager().list_current_delegates() |
| | | current_delegates, update_time = huaxin_trade_record_manager.DelegateRecordManager.list_by_day( |
| | |
| | | limit_up_data = kpl_data_manager.KPLLimitUpDataRecordManager.record_code_dict.get(code) |
| | | # 获取当前板块 |
| | | try: |
| | | can_buy_result = CodePlateKeyBuyManager.can_buy(code) |
| | | if can_buy_result: |
| | | if can_buy_result[0]: |
| | | fdata['block'] = ",".join( |
| | | [f"{x[0]}-{x[1] + 1}({x[2]}&{x[3] - x[2]})" for x in can_buy_result[0]]) |
| | | else: |
| | | if can_buy_result[1]: |
| | | if limit_up_data: |
| | | fdata['block'] = f"{limit_up_data[5]}-独苗" |
| | | else: |
| | | fdata['block'] = f"独苗" |
| | | limit_up_sequence = CodeLimitUpSequenceManager.get_current_limit_up_sequence(code) |
| | | if limit_up_sequence: |
| | | fdata['block'] = f"{limit_up_sequence[0]}-{limit_up_sequence[1]}({limit_up_sequence[2]}&{limit_up_sequence[2] - limit_up_sequence[3]})" |
| | | except: |
| | | pass |
| | | # 获取涨停时间 |
| | |
| | | elif ctype == "get_buy_block_mode": |
| | | # 获取买入板块的模式 |
| | | can_buy_unique_block = TradeBlockBuyModeManager().can_buy_unique_block() |
| | | self.send_response({"code": 0, "data": {"unique_block": 1 if can_buy_unique_block else 0 }, "msg": f""}, client_id, |
| | | self.send_response({"code": 0, "data": {"unique_block": 1 if can_buy_unique_block else 0}, "msg": f""}, |
| | | client_id, |
| | | request_id) |
| | | |
| | | elif ctype == "set_buy_unique_block_mode": |
| | |
| | | time.sleep(1) |
| | | |
| | | def refresh_data(): |
| | | money = trade_manager.AccountAvailableMoneyManager().get_available_money() |
| | | money = trade_data_manager.AccountMoneyManager().get_available_money() |
| | | if money is not None: |
| | | sv_trade_money.set(money) |
| | | else: |
| | |
| | | "usefulMoney": round(pTradingAccountField.UsefulMoney, 2), |
| | | "frozenCash": round(pTradingAccountField.FrozenCash, 2), |
| | | "fetchLimit": round(pTradingAccountField.FetchLimit, 2), |
| | | "preDeposit": round(pTradingAccountField.PreDeposit, 2)}) |
| | | "preDeposit": round(pTradingAccountField.PreDeposit, 2), |
| | | "commission": round(pTradingAccountField.Commission, 2) |
| | | }) |
| | | # logger.info( |
| | | # 'OnRspQryTradingAccount[%d]: DepartmentID[%s] InvestorID[%s] AccountID[%s] CurrencyID[%s] UsefulMoney[%.2f] FetchLimit[%.2f]' |
| | | # % (nRequestID, pTradingAccountField.DepartmentID, pTradingAccountField.InvestorID, |
| | |
| | | from ths import client_manager |
| | | import constant |
| | | from trade.deal_big_money_manager import DealOrderNoManager |
| | | from trade.trade_manager import AccountAvailableMoneyManager, AutoCancelSellModeManager |
| | | from trade.trade_manager import AutoCancelSellModeManager |
| | | from utils import global_util, tool |
| | | import threading |
| | | |
| | |
| | | from trade import l2_trade_util, trade_manager |
| | | from l2.cancel_buy_strategy import L2LimitUpSellStatisticUtil |
| | | from log_module.log import logger_juejin_tick, logger_system |
| | | from trade.trade_data_manager import CodeActualPriceProcessor, PlaceOrderCountManager |
| | | from trade.trade_data_manager import CodeActualPriceProcessor, PlaceOrderCountManager, AccountMoneyManager |
| | | from trade.trade_queue_manager import JueJinBuy1VolumnManager |
| | | |
| | | redisManager = redis_manager.RedisManager(0) |
| | |
| | | # 载入量 |
| | | global_data_loader.load_volumn() |
| | | # 初始化载入 |
| | | AccountAvailableMoneyManager() |
| | | AccountMoneyManager() |
| | | # 初始化载入 |
| | | AutoCancelSellModeManager() |
| | | |
| | |
| | | from log_module.log import logger_l2_trade_buy, logger_l2_process, logger_l2_error, logger_debug, \ |
| | | logger_l2_not_buy_reasons, logger_real_place_order_position |
| | | |
| | | from trade.trade_data_manager import CodeActualPriceProcessor, PlaceOrderCountManager |
| | | from trade.trade_data_manager import CodeActualPriceProcessor, PlaceOrderCountManager, AccountMoneyManager |
| | | |
| | | from trade.trade_manager import TradeTargetCodeModeManager, AccountAvailableMoneyManager |
| | | from trade.trade_manager import TradeTargetCodeModeManager |
| | | |
| | | from settings.trade_setting import MarketSituationManager, TradeBlockBuyModeManager |
| | | |
| | |
| | | __CodesTradeStateManager = trade_manager.CodesTradeStateManager() |
| | | __PauseBuyCodesManager = gpcode_manager.PauseBuyCodesManager() |
| | | __Buy1PriceManager = code_price_manager.Buy1PriceManager() |
| | | __AccountAvailableMoneyManager = AccountAvailableMoneyManager() |
| | | __AccountMoneyManager = AccountMoneyManager() |
| | | __TradeBuyDataManager = trade_data_manager.TradeBuyDataManager() |
| | | __LimitUpTimeManager = limit_up_time_manager.LimitUpTimeManager() |
| | | __BlackListCodeManager = gpcode_manager.BlackListCodeManager() |
| | |
| | | |
| | | # 买入锁 |
| | | __buy_lock_dict = {} |
| | | |
| | | # 当前批次正在处理的数据索引 |
| | | __processing_data_indexes = {} |
| | | |
| | | # 获取代码评分 |
| | | @classmethod |
| | |
| | | def process_add_datas(cls, code, add_datas, capture_timestamp, __start_time): |
| | | now_time_str = tool.get_now_time_str() |
| | | if len(add_datas) > 0: |
| | | # 记录当前批数据的索引 |
| | | cls.__processing_data_indexes[code] = (add_datas[0]["index"], add_datas[1]["index"]) |
| | | if code not in cls.__trade_log_placr_order_info_dict: |
| | | cls.__trade_log_placr_order_info_dict[code] = trade_record_log_util.PlaceOrderInfo() |
| | | # 拼接数据 |
| | |
| | | place_order_count = trade_data_manager.PlaceOrderCountManager().get_place_order_count(code) |
| | | # 第一和第二次下单都必须要有至少一笔未成交/待成交的大单 |
| | | # 计算大单 |
| | | total_datas = local_today_datas.get(code) |
| | | if place_order_count < 2: |
| | | trade_index, is_default = transaction_progress.TradeBuyQueue().get_traded_index(code) |
| | | if trade_index is None: |
| | | trade_index = 0 |
| | | total_datas = local_today_datas.get(code) |
| | | min_money = l2_data_util.get_big_money_val(limit_up_price, tool.is_ge_code(code)) |
| | | left_count, left_num = L2DataComputeUtil.compute_left_buy_order(code, trade_index, |
| | | total_datas[-1]["index"], |
| | |
| | | if left_count < 1: |
| | | return False, False, f"第{place_order_count + 1}下单无待成交的大单" |
| | | |
| | | # 暂时注释想买单功能 |
| | | # 执行位那一戳数据要小于20条数据 |
| | | THRESHOLD_BUY_COUNT = 20 |
| | | buy_count = 0 |
| | | min_num = int(5000 / limit_up_price) |
| | | range_indexes = cls.__processing_data_indexes.get(code) |
| | | if range_indexes: |
| | | for i in range(range_indexes[0], range_indexes[1] + 1): |
| | | val = total_datas[i]["val"] |
| | | if not L2DataUtil.is_limit_up_price_buy(val): |
| | | continue |
| | | if val["num"] < min_num: |
| | | continue |
| | | buy_count += 1 |
| | | if buy_count > THRESHOLD_BUY_COUNT: |
| | | break |
| | | if buy_count > THRESHOLD_BUY_COUNT: |
| | | HuaXinSellOrderStatisticManager.clear_latest_deal_volume(code) |
| | | return False, True, f"执行位批次数据量({buy_count})大于{THRESHOLD_BUY_COUNT}" |
| | | # 暂时注释想买单功能 |
| | | if not cls.__WantBuyCodesManager.is_in_cache(code): |
| | | # if cls.__TradeTargetCodeModeManager.get_mode_cache() == TradeTargetCodeModeManager.MODE_ONLY_BUY_WANT_CODES: |
| | | # return False, True, f"只买想买单中的代码" |
| | |
| | | from urllib.parse import parse_qs |
| | | from output import code_info_output, limit_up_data_filter, output_util, kp_client_msg_manager |
| | | |
| | | from trade import bidding_money_manager, trade_manager, l2_trade_util, trade_record_log_util, trade_constant |
| | | from trade import bidding_money_manager, trade_manager, l2_trade_util, trade_record_log_util, trade_constant, \ |
| | | trade_data_manager |
| | | import concurrent.futures |
| | | |
| | | # 禁用http.server的日志输出 |
| | |
| | | num = total_datas[trade_progress]['val']['num'] |
| | | if dealing_active_info and dealing_info[0] == dealing_active_info[0]: |
| | | if tool.is_sh_code(code): |
| | | num += dealing_active_info[1]//100 |
| | | num += dealing_active_info[1] // 100 |
| | | percent = int(dealing_info[1] / num) |
| | | response_data = json.dumps( |
| | | {"code": 0, "data": {"trade_progress": trade_progress, "is_default": is_default, "percent": percent}}) |
| | |
| | | except Exception as e: |
| | | logger_debug.exception(e) |
| | | |
| | | elif url.path == "/get_account_commission_detail": |
| | | # 获取手续费详情 |
| | | try: |
| | | fdata = {"delegates": {}} |
| | | delegates = trade_data_manager.AccountMoneyManager().get_delegated_count_info() |
| | | delegates = [{"count": x[1], "price": 0.1, "money": round(0.1 * x[1], 2)} for x in delegates] |
| | | fdata["delegates"]["buy"] = delegates[0] |
| | | fdata["delegates"]["buy_cancel"] = delegates[1] |
| | | fdata["delegates"]["sell_cancel"] = delegates[2] |
| | | fdata["delegates"]["sell"] = delegates[3] |
| | | deal_count = trade_data_manager.AccountMoneyManager().get_deal_count() |
| | | fdata["deal"] = {"count": deal_count, "price": 5, "money": round(5 * deal_count, 2)} |
| | | fdata["commission"] = trade_data_manager.AccountMoneyManager().get_commission_cache() |
| | | response_data = json.dumps({"code": 0, "data": fdata}) |
| | | except Exception as e: |
| | | logger_debug.exception(e) |
| | | |
| | | async_log_util.info(logger_request_api, f"结束请求{tool.get_thread_id()}-{url}") |
| | | self.send_response(200) |
| | | # 发给请求客户端的响应数据 |
| | |
| | | self.__kplDataManager.save_data(type_, result_list_) |
| | | except Exception as e: |
| | | logger_debug.exception(e) |
| | | |
| | | # 将"概念"二字替换掉 |
| | | data = data_origin |
| | | type_ = data["type"] |
| | |
| | | while True: |
| | | try: |
| | | # 获取账户可用资金 |
| | | account_available_money = trade_manager.AccountAvailableMoneyManager().get_available_money_cache() |
| | | account_available_money = trade_data_manager.AccountMoneyManager().get_available_money_cache() |
| | | if account_available_money is not None and account_available_money > constant.BUY_MONEY_PER_CODE: |
| | | continue |
| | | can_cancel_codes = [] |
| | |
| | | client = datas["client"] |
| | | money = datas["money"] |
| | | # TODO存入缓存文件 |
| | | trade_manager.AccountAvailableMoneyManager().set_available_money(client, money) |
| | | trade_data_manager.AccountMoneyManager().set_available_money(client, money) |
| | | # l2交易队列 |
| | | elif type == 10: |
| | | # 可用金额 |
| | |
| | | # 首板身位 |
| | | __first_block_sequence_dict = {} |
| | | |
| | | def set_current_limit_up_datas(self, current_limit_up_datas): |
| | | @classmethod |
| | | def set_current_limit_up_datas(cls, current_limit_up_datas): |
| | | """ |
| | | 设置目前的涨停代码 |
| | | @param current_limit_up_datas: |
| | |
| | | if b not in block_codes: |
| | | block_codes[b] = set() |
| | | block_codes[b].add(code) |
| | | # 获取上个交易日涨停的代码 |
| | | yesterday_codes = kpl_data_manager.get_yesterday_limit_up_codes() |
| | | if yesterday_codes is None: |
| | | yesterday_codes = set() |
| | | |
| | | for code in limit_up_codes: |
| | | # 计算身位 |
| | |
| | | total_count = len(codes) |
| | | # 统计真正涨停数 |
| | | limit_up_count = 0 |
| | | limit_up_codes_list = [] |
| | | for c in codes: |
| | | if c in limit_up_codes: |
| | | limit_up_count += 1 |
| | | # TODO 获取首板代码的排位 |
| | | if c not in yesterday_codes: |
| | | limit_up_codes_list.append((c, current_code_block_dict[c][2])) |
| | | # 获取首板代码的排位 |
| | | limit_up_codes_list.sort(key=lambda x: x[1]) |
| | | index = 1 |
| | | for i in range(0, len(limit_up_codes_list)): |
| | | if limit_up_codes_list[i][0] == code: |
| | | index = i + 1 |
| | | break |
| | | cls.__first_block_sequence_dict[code] = (b, index, total_count, limit_up_count) |
| | | |
| | | |
| | | @classmethod |
| | | def get_current_limit_up_sequence(cls, code): |
| | | """ |
| | | 获取代码当前的板块身位 |
| | | @param code: |
| | | @return:(板块名称,身位,总涨停数量,目前涨停数量) |
| | | """ |
| | | return cls.__first_block_sequence_dict.get(code) |
| | | |
| | | |
| | | class LatestLimitUpBlockManager: |
| | |
| | | from huaxin_client import constant as huaxin_client_constant |
| | | from log_module import async_log_util |
| | | from log_module.log import hx_logger_trade_debug, logger_system |
| | | from trade import trade_manager |
| | | from trade import trade_manager, trade_data_manager |
| | | from trade.huaxin import huaxin_trade_api, huaxin_trade_record_manager |
| | | |
| | | from trade.huaxin.huaxin_trade_order_processor import HuaxinOrderEntity, TradeResultProcessor |
| | |
| | | huaxin_trade_record_manager.MoneyManager.save_data(data) |
| | | if data: |
| | | usefulMoney = data[0]["usefulMoney"] |
| | | commission = data[0]["commission"] |
| | | # 设置可用资金 |
| | | trade_manager.AccountAvailableMoneyManager().set_available_money(0, usefulMoney) |
| | | trade_data_manager.AccountMoneyManager().set_available_money(0, usefulMoney) |
| | | trade_data_manager.AccountMoneyManager().set_commission(commission) |
| | | # 设置可用资金 |
| | | elif type_ == "deal_list": |
| | | dataJSON = huaxin_trade_api.get_deal_list(timeout=10) |
| | |
| | | |
| | | # 交易撤销数据管理器 |
| | | import constant |
| | | from db.mysql_data_delegate import Mysqldb |
| | | from db.redis_manager_delegate import RedisUtils |
| | | from utils import global_util, tool |
| | | import l2_data_util |
| | |
| | | RedisUtils.delete(self.__get_redis(), k) |
| | | |
| | | |
| | | # 账户可用资金管理 |
| | | class AccountMoneyManager: |
| | | __db = 2 |
| | | __redis_manager = redis_manager.RedisManager(2) |
| | | __available_money_cache = None |
| | | __commission_cache = None |
| | | __instance = None |
| | | __mysqldb = Mysqldb() |
| | | |
| | | def __new__(cls, *args, **kwargs): |
| | | if not cls.__instance: |
| | | cls.__instance = super(AccountMoneyManager, cls).__new__(cls, *args, **kwargs) |
| | | __redis = cls.__get_redis() |
| | | result = RedisUtils.get(cls.__get_redis(), "trade-account-canuse-money") |
| | | if result: |
| | | cls.__available_money_cache = round(float(result), 2) |
| | | return cls.__instance |
| | | |
| | | @classmethod |
| | | def __get_redis(cls): |
| | | return cls.__redis_manager.getRedis() |
| | | |
| | | def set_available_money(self, client_id, money): |
| | | self.__available_money_cache = round(float(money), 2) |
| | | RedisUtils.set(self.__get_redis(), "trade-account-canuse-money", money) |
| | | |
| | | def set_commission(self, commission): |
| | | self.__commission_cache = commission |
| | | |
| | | # 获取交易账户的可用金额 |
| | | def get_available_money(self): |
| | | result = RedisUtils.get(self.__get_redis(), "trade-account-canuse-money") |
| | | if result is None: |
| | | return None |
| | | return round(float(result), 2) |
| | | |
| | | def get_available_money_cache(self): |
| | | return self.__available_money_cache |
| | | |
| | | def get_commission_cache(self): |
| | | return self.__commission_cache |
| | | |
| | | def get_delegated_count_info(self, from_date=None, to_date=None): |
| | | """ |
| | | 获取委托数量信息 |
| | | @return: |
| | | """ |
| | | if not from_date: |
| | | from_date = tool.get_now_date_str("%Y%m%d") |
| | | if not to_date: |
| | | to_date = tool.get_now_date_str("%Y%m%d") |
| | | |
| | | sql = f"SELECT * FROM (SELECT '挂买', COUNT(*) AS '数量' FROM `hx_trade_delegate_record` r WHERE r.`direction`=0 AND r.`insertDate`>='{from_date}' AND r.`insertDate`<='{to_date}'" |
| | | sql += " UNION ALL " |
| | | sql += f"SELECT '撤挂买',COUNT(*) AS '数量' FROM `hx_trade_delegate_record` r WHERE r.`direction`=0 AND r.`cancelTime`!='' AND r.`cancelTime`IS NOT NULL AND r.`insertDate`>='{from_date}' AND r.`insertDate`<='{to_date}'" |
| | | sql += " UNION ALL " |
| | | sql += f"SELECT '撤挂卖', COUNT(*) AS '数量' FROM `hx_trade_delegate_record` r WHERE r.`direction`=1 AND r.`cancelTime`!='' AND r.`cancelTime`IS NOT NULL AND r.`insertDate`>='{from_date}' AND r.`insertDate`<='{to_date}'" |
| | | sql += " UNION ALL " |
| | | sql += f"SELECT '挂卖' ,COUNT(*) AS '数量' FROM `hx_trade_delegate_record` r WHERE r.`direction`=1 AND r.`insertDate`>='{from_date}' AND r.`insertDate`<='{to_date}'" |
| | | sql += ") a" |
| | | return self.__mysqldb.select_all(sql) |
| | | |
| | | def get_deal_count(self, from_date=None, to_date=None): |
| | | if not from_date: |
| | | from_date = tool.get_now_date_str("%Y%m%d") |
| | | if not to_date: |
| | | to_date = tool.get_now_date_str("%Y%m%d") |
| | | sql = f"SELECT COUNT(*) FROM (SELECT * FROM `hx_trade_deal_record` r WHERE r.`tradeDate` >='{from_date}' and r.`tradeDate` <='{to_date}' GROUP BY " \ |
| | | "r.`orderSysID`) a " |
| | | return self.__mysqldb.select_one(sql)[0] |
| | | |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | processor = CodeActualPriceProcessor() |
| | | print(processor.get_top_rate_codes(30)) |
| | |
| | | |
| | | from code_attribute import gpcode_manager |
| | | from db import mysql_data_delegate as mysql_data, redis_manager_delegate as redis_manager |
| | | from db.mysql_data_delegate import Mysqldb |
| | | from db.redis_manager_delegate import RedisUtils |
| | | from log_module import async_log_util |
| | | from output import kp_client_msg_manager |
| | |
| | | |
| | | from log_module.log import * |
| | | from trade.huaxin.huaxin_trade_record_manager import TradeOrderIdManager |
| | | from trade.trade_data_manager import AccountMoneyManager |
| | | from utils import import_util, tool, huaxin_util |
| | | |
| | | trade_gui = import_util.import_lib("trade.trade_gui") |
| | |
| | | # 设置交易账户的可用金额 |
| | | |
| | | |
| | | # 账户可用资金管理 |
| | | class AccountAvailableMoneyManager: |
| | | __db = 2 |
| | | __redis_manager = redis_manager.RedisManager(2) |
| | | __available_money_cache = None |
| | | __instance = None |
| | | |
| | | def __new__(cls, *args, **kwargs): |
| | | if not cls.__instance: |
| | | cls.__instance = super(AccountAvailableMoneyManager, cls).__new__(cls, *args, **kwargs) |
| | | __redis = cls.__get_redis() |
| | | result = RedisUtils.get(cls.__get_redis(), "trade-account-canuse-money") |
| | | if result: |
| | | cls.__available_money_cache = round(float(result), 2) |
| | | return cls.__instance |
| | | |
| | | @classmethod |
| | | def __get_redis(cls): |
| | | return cls.__redis_manager.getRedis() |
| | | |
| | | def set_available_money(self, client_id, money): |
| | | self.__available_money_cache = round(float(money), 2) |
| | | RedisUtils.set(self.__get_redis(), "trade-account-canuse-money", money) |
| | | |
| | | # 获取交易账户的可用金额 |
| | | def get_available_money(self): |
| | | result = RedisUtils.get(self.__get_redis(), "trade-account-canuse-money") |
| | | if result is None: |
| | | return None |
| | | return round(float(result), 2) |
| | | |
| | | def get_available_money_cache(self): |
| | | return self.__available_money_cache |
| | | |
| | | |
| | | # 保存交易成功的数据 |
| | | def save_trade_success_data(datas, day=datetime.datetime.now().strftime("%Y%m%d")): |
| | |
| | | return None, trade_state |
| | | |
| | | def is_money_enough(code): |
| | | money = AccountAvailableMoneyManager().get_available_money_cache() |
| | | money = AccountMoneyManager().get_available_money_cache() |
| | | if money is None: |
| | | return Exception("未获取到账户可用资金"), None |
| | | price = gpcode_manager.get_limit_up_price(code) |