| | |
| | | if not cache_result[0] or cache_result[1] is None: |
| | | global_util.limit_up_time[code] = time |
| | | tool.CodeDataCacheUtil.set_cache(self.__limit_up_time_cache, code, time) |
| | | # TODO 测试 |
| | | # RedisUtils.setex_async( |
| | | # self.__db, "limit_up_time-{}".format(code), tool.get_expire(), time) |
| | | RedisUtils.setex_async( |
| | | self.__db, "limit_up_time-{}".format(code), tool.get_expire(), time) |
| | | |
| | | def get_limit_up_time(self, code): |
| | | time = global_util.limit_up_time.get(code) |
| | |
| | | try: |
| | | return lamada_method() |
| | | finally: |
| | | logger_redis_debug.info("{}({}):{}", method_name, round((time.time() - __start_time) * 1000, 3), key) |
| | | # logger_redis_debug.info("{}({}):{}", method_name, round((time.time() - __start_time) * 1000, 3), key) |
| | | pass |
| | | |
| | | @classmethod |
| | | def __request(cls, db, cmd, key, args=None): |
| | |
| | | "cmd": cmd, |
| | | "key": key, |
| | | } |
| | | if args: |
| | | if args is not None: |
| | | data["args"] = args |
| | | fdata = middle_api_protocol.load_redis_cmd(data) |
| | | result = middle_api_protocol.request(fdata) |
| | |
| | | @classmethod |
| | | def delete_async(cls, db, key, auto_free=True): |
| | | cls.add_async_task(db, "delete", (key)) |
| | | logger_redis_debug.info("delete_async({}):{}", 0, key) |
| | | # logger_redis_debug.info("delete_async({}):{}", 0, key) |
| | | |
| | | @classmethod |
| | | def keys(cls, redis_, key, auto_free=True): |
| | |
| | | @classmethod |
| | | def setex_async(cls, db, key, expire, val, auto_free=True): |
| | | cls.add_async_task(db, "setex", (key, expire, val)) |
| | | logger_redis_debug.info("setex_async({}):{}", 0, key) |
| | | # logger_redis_debug.info("setex_async({}):{}", 0, key) |
| | | |
| | | @classmethod |
| | | def setnx(cls, redis_, key, val, auto_free=True): |
| | |
| | | @classmethod |
| | | def expire_async(cls, db, key, expire, auto_free=True): |
| | | cls.add_async_task(db, "expire", (key, expire)) |
| | | logger_redis_debug.info("expire_async({}):{}", 0, key) |
| | | # logger_redis_debug.info("expire_async({}):{}", 0, key) |
| | | |
| | | @classmethod |
| | | def sadd(cls, redis_, key, val, auto_free=True): |
| | |
| | | @classmethod |
| | | def sadd_async(cls, db, key, val, auto_free=True): |
| | | cls.add_async_task(db, "sadd", (key, val)) |
| | | logger_redis_debug.info("sadd_async({}):{}", 0, key) |
| | | # logger_redis_debug.info("sadd_async({}):{}", 0, key) |
| | | |
| | | @classmethod |
| | | def sismember(cls, redis_, key, val, auto_free=True): |
| | |
| | | @classmethod |
| | | def srem_async(cls, db, key, val, auto_free=True): |
| | | cls.add_async_task(db, "srem", (key, val)) |
| | | logger_redis_debug.info("srem_async({}):{}", 0, key) |
| | | # logger_redis_debug.info("srem_async({}):{}", 0, key) |
| | | |
| | | @classmethod |
| | | def incrby(cls, redis_, key, num, auto_free=True, _async=False): |
| | |
| | | @classmethod |
| | | def incrby_async(cls, db, key, num, auto_free=True): |
| | | cls.add_async_task(db, "incrby", (key, num)) |
| | | logger_redis_debug.info("incrby_async({}):{}", 0, key) |
| | | # logger_redis_debug.info("incrby_async({}):{}", 0, key) |
| | | |
| | | @classmethod |
| | | def lpush(cls, redis_, key, val, auto_free=True): |
| | |
| | | process_end_index = cancel_index |
| | | # 保存最新累计金额 |
| | | # cls.__set_l2_latest_money_record(code, process_end_index, total_num) |
| | | l2_data_log.l2_time(code, round(time.time() * 1000) - start_time, |
| | | "l2数据封单额计算时间", |
| | | False) |
| | | # l2_data_log.l2_time(code, round(time.time() * 1000) - start_time, |
| | | # "l2数据封单额计算时间", |
| | | # False) |
| | | if cancel_index: |
| | | l2_log.cancel_debug(code, "数据处理位置:{}-{},{},最终买1为:{}", start_index, end_index, record_msg, |
| | | total_num) |
| | |
| | | code) |
| | | data_ = (_buy_single_index, buy_exec_index, compute_index, nums, count, list(max_num_sets), |
| | | volume_rate) |
| | | logger_debug.info(f"保存买入执行位置信息{code}:{data_}") |
| | | CodeDataCacheUtil.set_cache(self.__buy_compute_index_info_cache, code, data_) |
| | | RedisUtils.setex_async(self.__db, _key, expire, json.dumps(data_)) |
| | | |
| | |
| | | # 删除买撤点数据 |
| | | def delete_buy_cancel_point(self, code): |
| | | tool.CodeDataCacheUtil.clear_cache(self.__buy_cancel_single_pos_cache, code) |
| | | # TODO 测试 |
| | | # RedisUtils.delete_async(self.__db, "buy_cancel_single_pos-{}".format(code)) |
| | | RedisUtils.delete_async(self.__db, "buy_cancel_single_pos-{}".format(code)) |
| | | |
| | | |
| | | # 清除l2数据 |
| | |
| | | # 保存数据 |
| | | __start_time = round(t.time() * 1000) |
| | | l2.l2_data_util.save_l2_data(code, datas, add_datas) |
| | | __start_time = l2_data_log.l2_time(code, |
| | | round(t.time() * 1000) - __start_time, |
| | | "保存数据时间({})".format(len(add_datas))) |
| | | # __start_time = l2_data_log.l2_time(code, |
| | | # round(t.time() * 1000) - __start_time, |
| | | # "保存数据时间({})".format(len(add_datas))) |
| | | finally: |
| | | if code in cls.unreal_buy_dict: |
| | | cls.unreal_buy_dict.pop(code) |
| | |
| | | logger_l2_error.exception(f"code:{code}") |
| | | logger_l2_error.exception(e) |
| | | finally: |
| | | l2_data_log.l2_time(code, round(t.time() * 1000) - origin_start_time, |
| | | "l2数据处理总耗时", |
| | | True) |
| | | # l2_data_log.l2_time(code, round(t.time() * 1000) - origin_start_time, |
| | | # "l2数据处理总耗时", |
| | | # True) |
| | | pass |
| | | if datas: |
| | | l2.l2_data_util.save_l2_data(code, None, datas) |
| | | |
| | |
| | | limit_up_time_manager.LimitUpTimeManager().save_limit_up_time(code, "09:30:00") |
| | | |
| | | total_datas = local_today_datas[code] |
| | | __start_time = l2_data_log.l2_time(code, round(t.time() * 1000) - __start_time, |
| | | "l2数据预处理时间") |
| | | # __start_time = l2_data_log.l2_time(code, round(t.time() * 1000) - __start_time, |
| | | # "l2数据预处理时间") |
| | | |
| | | if len(add_datas) > 0: |
| | | # 是否为首板代码 |
| | |
| | | |
| | | latest_time = add_datas[len(add_datas) - 1]["val"]["time"] |
| | | |
| | | __start_time = l2_data_log.l2_time(code, round(t.time() * 1000) - __start_time, |
| | | "l2数据准备时间") |
| | | # __start_time = l2_data_log.l2_time(code, round(t.time() * 1000) - __start_time, |
| | | # "l2数据准备时间") |
| | | # 时间差不能太大才能处理 |
| | | if not l2_trade_util.is_in_forbidden_trade_codes(code): |
| | | # 判断是否已经挂单 |
| | |
| | | logger_l2_process.info("code:{} 处理数据范围: {}-{} 处理时间:{} 截图时间戳:{}", code, add_datas[0]["index"], |
| | | add_datas[-1]["index"], round(t.time() * 1000) - __start_time, |
| | | capture_timestamp) |
| | | __start_time = l2_data_log.l2_time(code, round(t.time() * 1000) - __start_time, |
| | | "l2数据处理时间") |
| | | # __start_time = l2_data_log.l2_time(code, round(t.time() * 1000) - __start_time, |
| | | # "l2数据处理时间") |
| | | |
| | | # 处理未挂单 |
| | | @classmethod |
| | |
| | | __start_time = round(t.time() * 1000) |
| | | # 获取阈值 |
| | | threshold_money, msg = cls.__get_threshmoney(code) |
| | | if round(t.time() * 1000) - __start_time > 10: |
| | | __start_time = l2_data_log.l2_time(code, round(t.time() * 1000) - __start_time, |
| | | "获取m值数据耗时") |
| | | # if round(t.time() * 1000) - __start_time > 10: |
| | | # __start_time = l2_data_log.l2_time(code, round(t.time() * 1000) - __start_time, |
| | | # "获取m值数据耗时") |
| | | if True: # end_index - start_index < 10: |
| | | cls.__start_compute_buy(code, start_index, end_index, threshold_money, capture_time, is_first_code) |
| | | else: |
| | |
| | | cls.__buyL2SafeCountManager.compute_left_rate(code, start_index, end_index, total_data, |
| | | local_today_num_operate_map.get(code)) |
| | | |
| | | l2_data_log.l2_time(code, round(t.time() * 1000) - _start_time, |
| | | "已下单-获取买入信息耗时") |
| | | # l2_data_log.l2_time(code, round(t.time() * 1000) - _start_time, |
| | | # "已下单-获取买入信息耗时") |
| | | return None, "" |
| | | |
| | | @dask.delayed |
| | |
| | | # 计算m值大单 |
| | | cls.l2BigNumForMProcessor.process(code, max(buy_single_index, start_index), end_index, |
| | | gpcode_manager.get_limit_up_price(code)) |
| | | l2_data_log.l2_time(code, round(t.time() * 1000) - _start_time, |
| | | "已下单-m值大单计算") |
| | | # l2_data_log.l2_time(code, round(t.time() * 1000) - _start_time, |
| | | # "已下单-m值大单计算") |
| | | return None, "" |
| | | |
| | | # 买1撤计算 |
| | |
| | | end_index, |
| | | buy_single_index, buy_exec_index) |
| | | |
| | | l2_data_log.l2_time(code, round(t.time() * 1000) - _start_time, |
| | | "已下单-买1统计耗时") |
| | | # l2_data_log.l2_time(code, round(t.time() * 1000) - _start_time, |
| | | # "已下单-买1统计耗时") |
| | | # 买1不会触发撤单 |
| | | return None, "" |
| | | # return cancel_data, cancel_msg |
| | |
| | | logger_l2_error.error(f"参数:buy_single_index-{_buy_single_index} buy_exec_index-{_buy_exec_index}") |
| | | logger_l2_error.exception(e) |
| | | finally: |
| | | l2_data_log.l2_time(code, round(t.time() * 1000) - _start_time, |
| | | "已下单-s级大单估算") |
| | | # l2_data_log.l2_time(code, round(t.time() * 1000) - _start_time, |
| | | # "已下单-s级大单估算") |
| | | pass |
| | | return None, "" |
| | | |
| | | # H撤 |
| | |
| | | logger_l2_error.error(f"参数:buy_single_index-{_buy_single_index} buy_exec_index-{_buy_exec_index}") |
| | | logger_l2_error.exception(e) |
| | | finally: |
| | | l2_data_log.l2_time(code, round(t.time() * 1000) - _start_time, "已下单-H撤大单计算") |
| | | # l2_data_log.l2_time(code, round(t.time() * 1000) - _start_time, "已下单-H撤大单计算") |
| | | pass |
| | | return None, "" |
| | | |
| | | # L撤 |
| | |
| | | logger_l2_error.error(f"参数:buy_single_index-{_buy_single_index} buy_exec_index-{_buy_exec_index}") |
| | | logger_l2_error.exception(e) |
| | | finally: |
| | | l2_data_log.l2_time(code, round(t.time() * 1000) - _start_time, "已下单-L撤大单计算") |
| | | # l2_data_log.l2_time(code, round(t.time() * 1000) - _start_time, "已下单-L撤大单计算") |
| | | pass |
| | | return None, "" |
| | | |
| | | # 板上卖撤 |
| | |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | finally: |
| | | l2_data_log.l2_time(code, round(t.time() * 1000) - _start_time, "已下单-板上卖耗时") |
| | | # l2_data_log.l2_time(code, round(t.time() * 1000) - _start_time, "已下单-板上卖耗时") |
| | | pass |
| | | return None, "" |
| | | |
| | | # 是否需要撤销 |
| | |
| | | if not cancel_data: |
| | | cancel_data, cancel_msg = l_cancel(buy_single_index, buy_exec_index) |
| | | l2_log.debug(code, "撤单计算结束") |
| | | _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, |
| | | "已下单-撤单 判断是否需要撤单") |
| | | # _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, |
| | | # "已下单-撤单 判断是否需要撤单") |
| | | if cancel_data: |
| | | l2_log.debug(code, "触发撤单,撤单位置:{} ,撤单原因:{}", cancel_data["index"], cancel_msg) |
| | | l2_log.trade_record(code, "撤单", "'index':{} , 'msg':'{}'", cancel_data["index"], cancel_msg) |
| | | # 撤单 |
| | | if cls.cancel_buy(code, cancel_msg): |
| | | _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, |
| | | "已下单-撤单 耗时") |
| | | # _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, |
| | | # "已下单-撤单 耗时") |
| | | # 撤单成功,继续计算下单 |
| | | cls.__process_not_order(code, cancel_data["index"] + 1, end_index, capture_time, is_first_code) |
| | | _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, |
| | | "处理剩余数据 耗时") |
| | | # _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, |
| | | # "处理剩余数据 耗时") |
| | | else: |
| | | # 撤单尚未成功 |
| | | pass |
| | |
| | | # lp.disable() |
| | | # logger_profile.info(output.getvalue()) |
| | | |
| | | _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, |
| | | "已虚拟下单-执行真实下单 外部耗时") |
| | | # _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, |
| | | # "已虚拟下单-执行真实下单 外部耗时") |
| | | |
| | | @classmethod |
| | | def __buy(cls, code, capture_timestamp, last_data, last_data_index, is_first_code): |
| | |
| | | else: |
| | | can, need_clear_data, reason = cls.__can_buy_first(code) |
| | | |
| | | __start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - __start_time, "最后判断是否能下单", force=True) |
| | | # __start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - __start_time, "最后判断是否能下单", force=True) |
| | | # 删除虚拟下单 |
| | | if code in cls.unreal_buy_dict: |
| | | cls.unreal_buy_dict.pop(code) |
| | |
| | | # 可以下单 |
| | | return True, False, None |
| | | finally: |
| | | l2_data_log.l2_time(code, round((t.time() - __start_time) * 1000), "是否可以下单计算") |
| | | # l2_data_log.l2_time(code, round((t.time() - __start_time) * 1000), "是否可以下单计算") |
| | | pass |
| | | |
| | | @classmethod |
| | | def __can_buy_first(cls, code): |
| | |
| | | # 如果是今天第一次有下单开始信号,需要设置大单起始点 |
| | | cls.l2BigNumForMProcessor.set_begin_pos(code, buy_single_index) |
| | | |
| | | _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "下单信号计算时间") |
| | | # _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "下单信号计算时间") |
| | | |
| | | if buy_single_index is None: |
| | | # 未获取到买入信号,终止程序 |
| | |
| | | compute_end_index, |
| | | gpcode_manager.get_limit_up_price(code)) |
| | | |
| | | _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "计算m值大单") |
| | | # _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "计算m值大单") |
| | | |
| | | threshold_money, msg = cls.__get_threshmoney(code) |
| | | |
| | | _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "m值阈值计算") |
| | | # _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "m值阈值计算") |
| | | |
| | | # 买入纯买额统计 |
| | | compute_index, buy_nums, buy_count, rebegin_buy_pos, max_num_set_new = cls.__sum_buy_num_for_order_3(code, |
| | |
| | | threshold_money, |
| | | buy_single_index, |
| | | max_num_set) |
| | | _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "纯买额统计时间") |
| | | # _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "纯买额统计时间") |
| | | |
| | | l2_log.debug(code, "m值-{} 量比:{} rebegin_buy_pos:{}", threshold_money, cls.volume_rate_info[code][0], |
| | | rebegin_buy_pos) |
| | |
| | | # buy_single_index, |
| | | # buy_exec_index, False) |
| | | |
| | | l2_log.debug(code, "记录执行买入数据结束") |
| | | # l2_log.debug(code, "记录执行买入数据结束") |
| | | |
| | | _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, |
| | | "记录执行买入数据", force=True) |
| | | # _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, |
| | | # "记录执行买入数据", force=True) |
| | | |
| | | # 数据是否处理完毕 |
| | | if compute_index >= compute_end_index: |
| | |
| | | cls.volume_rate_info[code][1], |
| | | cls.volume_rate_info[code][1], |
| | | True) |
| | | _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, |
| | | "S级大单处理耗时", force=True) |
| | | # _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, |
| | | # "S级大单处理耗时", force=True) |
| | | l2_log.debug(code, "数据处理完毕,下单, 数据截图时间-{}", capture_time) |
| | | # 数据已经处理完毕,如果还没撤单就实际下单 |
| | | if need_cancel: |
| | |
| | | cls.volume_rate_info[code][1], False) |
| | | l2_log.debug(code, "S级大单处理") |
| | | |
| | | _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, |
| | | "S级大单处理耗时", force=True) |
| | | # _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, |
| | | # "S级大单处理耗时", force=True) |
| | | # 数据尚未处理完毕,进行下一步处理 |
| | | l2_log.debug(code, "数据尚未处理完毕,进行下一步处理,处理进度:{} end_index:{}", compute_index, compute_end_index) |
| | | # 处理撤单步骤 |
| | |
| | | # lp.disable() |
| | | # logger_profile.info(output.getvalue()) |
| | | |
| | | _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, |
| | | f"处理撤单步骤耗时,范围:{compute_index + 1}-{compute_end_index}", force=True) |
| | | # _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, |
| | | # f"处理撤单步骤耗时,范围:{compute_index + 1}-{compute_end_index}", force=True) |
| | | else: |
| | | # 未达到下单条件,保存纯买额,设置纯买额 |
| | | # 记录买入信号位置 |
| | |
| | | __start_time = round(time.time() * 1000) |
| | | if datas: |
| | | RedisUtils.setex_async(_redisManager.getRedis(), "l2-data-latest-{}".format(code), tool.get_expire(), json.dumps(datas)) |
| | | l2_data_log.l2_time(code, round(time.time() * 1000) - __start_time, "保存最近l2数据用时") |
| | | # l2_data_log.l2_time(code, round(time.time() * 1000) - __start_time, "保存最近l2数据用时") |
| | | # 设置进内存 |
| | | local_latest_datas[code] = datas |
| | | set_l2_data_latest_count(code, len(datas)) |
| | |
| | | from log_module.log import logger_l2_process_time |
| | | import logging |
| | | import threading |
| | | import time |
| | | |
| | | from log_module.log import logger_l2_process_time, logger_debug |
| | | |
| | | |
| | | class TestInstance: |
| | | __instance = None |
| | | |
| | | def __new__(cls, *args, **kwargs): |
| | | if not cls.__instance: |
| | | cls.__instance = super(TestInstance, cls).__new__(cls, *args, **kwargs) |
| | | print(cls.__instance) |
| | | return cls.__instance |
| | | |
| | | def test(self): |
| | | i=2 |
| | | i+=1 |
| | | |
| | | |
| | | _dict = {} |
| | | |
| | | |
| | | def test_time(instance, i): |
| | | start = time.time() |
| | | instance.test() |
| | | logger_debug.info(f"开始:{i}") |
| | | time.time() |
| | | # round(time.time() * 1000) - round(start * 1000) |
| | | logger_debug.info(f"结束:{i}") |
| | | if len(_dict.keys()) == 99: |
| | | total = 0 |
| | | for k in _dict: |
| | | total += _dict[k] |
| | | print(total / len(_dict)) |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | logger_l2_process_time.info("test123") |
| | | # for k in range(1): |
| | | # _dict.clear() |
| | | # for i in range(0, 100): |
| | | # threading.Thread(target=lambda: test_time(TestInstance(), i), daemon=True).start() |
| | | # time.sleep(2) |
| | | print(time.time()) |
| | | for i in range(1000): |
| | | # logger_debug.debug("结束") |
| | | TestInstance().test() |
| | | pass |
| | | print(time.time()) |
| | | for i in range(1000): |
| | | logger_debug.debug("结束") |
| | | pass |
| | | print(time.time()) |
| | | |
| | | input() |
| | |
| | | print("开始买入") |
| | | logger_trade.info("{}开始买入".format(code)) |
| | | CodesTradeStateManager().set_trade_state(code, TRADE_STATE_BUY_PLACE_ORDER) |
| | | _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "买入判断时间", force=True) |
| | | # _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "买入判断时间", force=True) |
| | | __buy(code, price, trade_state, capture_timestamp, last_data, last_data_index) |
| | | l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "异步买入时间", force=True) |
| | | # l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "异步买入时间", force=True) |
| | | |
| | | |
| | | # 中断买入 |