| | |
| | | 交易数据股那里器 |
| | | 用于对交易临时数据(交易状态,代码状态等)进行管理 |
| | | """ |
| | | |
| | | import datetime |
| | | import json |
| | | import time |
| | | |
| | | # 交易撤销数据管理器 |
| | | import constant |
| | | import global_util |
| | | import l2_data_util |
| | | import redis_manager |
| | | import tool |
| | |
| | | redis = cls.redisManager.getRedis() |
| | | val_str = redis.get("buy_position_info-{}".format(code)) |
| | | if val_str is None: |
| | | return None, None, None,None |
| | | return None, None, None, None |
| | | else: |
| | | val = json.loads(val_str) |
| | | return val[0], val[1], val[2], val[3] |
| | |
| | | # 间隔2s及其以上表示数据异常 |
| | | # 间隔2s以上的就以下单时间下一秒末尾作为确认点 |
| | | start_index = l2_data_index |
| | | if len(l2_today_datas)-1 > start_index: |
| | | if len(l2_today_datas) - 1 > start_index: |
| | | for i in range(start_index + 1, len(l2_today_datas)): |
| | | _time = l2_today_datas[i]["val"]["time"] |
| | | if l2_data_util.get_time_as_seconds(_time) - old_time_int >= 2: |
| | |
| | | cls.__set_buy_sure_position(code, l2_data_index, l2_data) |
| | | elif new_time_int - old_time_int >= 0: |
| | | # 间隔2s内表示数据正常,将其位置设置为新增数据的中间位置 |
| | | index = len(l2_today_datas)-1 - (len(l2_add_datas)) // 2 |
| | | index = len(l2_today_datas) - 1 - (len(l2_add_datas)) // 2 |
| | | data = l2_today_datas[index] |
| | | cls.__set_buy_sure_position(code, index, data) |
| | | else: |
| | |
| | | pass |
| | | |
| | | |
| | | # 代码实时价格管理器 |
| | | class CodeActualPriceProcessor: |
| | | __redisManager = redis_manager.RedisManager(0) |
| | | |
| | | def __get_redis(self): |
| | | return self.__redisManager.getRedis() |
| | | |
| | | # 保存跌价的时间 |
| | | def __save_down_price_time(self, code, time_str): |
| | | key = "under_water_last_time-{}".format(code) |
| | | self.__get_redis().setex(key, tool.get_expire(), time_str) |
| | | |
| | | def __remove_down_price_time(self, code): |
| | | key = "under_water_last_time-{}".format(code) |
| | | self.__get_redis().delete(key) |
| | | |
| | | def __get_last_down_price_time(self, code): |
| | | key = "under_water_last_time-{}".format(code) |
| | | return self.__get_redis().get(key) |
| | | |
| | | def __increment_down_price_time(self, code, seconds): |
| | | key = "under_water_seconds-{}".format(code) |
| | | self.__get_redis().incrby(key, seconds) |
| | | |
| | | def __get_down_price_time_as_seconds(self, code): |
| | | key = "under_water_seconds-{}".format(code) |
| | | val = self.__get_redis().get(key) |
| | | if val is None: |
| | | return None |
| | | else: |
| | | return int(val) |
| | | |
| | | def process_rate(self, code, rate, time_str): |
| | | # 9点半之前的数据不处理 |
| | | if int(time_str.replace(":", "")) < int("093000"): |
| | | return |
| | | # now_str = datetime.datetime.now().strftime("%H:%M:%S") |
| | | if rate >= 0: |
| | | down_start_time = self.__get_last_down_price_time(code) |
| | | if down_start_time is None: |
| | | return |
| | | else: |
| | | # 累计增加时间 |
| | | time_second = tool.trade_time_sub(time_str, down_start_time) |
| | | self.__increment_down_price_time(code, time_second) |
| | | # 删除起始时间 |
| | | self.__remove_down_price_time(code) |
| | | else: |
| | | # 记录开始值 |
| | | if self.__get_last_down_price_time(code) is None: |
| | | self.__save_down_price_time(code, time_str) |
| | | |
| | | # 保存现价 |
| | | def save_current_price(self, code, price, is_limit_up): |
| | | global_util.cuurent_prices[code] = (price, is_limit_up, round(time.time())) |
| | | pass |
| | | |
| | | # 是否为水下捞 |
| | | def is_under_water(self, code): |
| | | time_seconds = self.__get_down_price_time_as_seconds(code) |
| | | if time_seconds is None: |
| | | return False |
| | | else: |
| | | return time_seconds >= constant.UNDER_WATER_PRICE_TIME_AS_SECONDS |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | TradeBuyDataManager.set_buy_capture_time("123456", 178938828, 1232) |
| | | print(TradeBuyDataManager.get_buy_capture_time("123456")) |
| | | processor = CodeActualPriceProcessor() |
| | | processor.process_rate("123456", -0.2, "09:30:00") |
| | | processor.process_rate("123456", -0.3, "09:40:00") |
| | | processor.process_rate("123456", 0.3, "09:50:00") |
| | | |
| | | processor.is_under_water("123456") |