| | |
| | | |
| | | |
| | | class TickDataProcess: |
| | | # 初始化原始价格,取9点25的价格 |
| | | __last_price_infos = {} |
| | | |
| | | __last_15s_price_infos = {} |
| | | |
| | | __reference_price_info = {} |
| | | # 最高价 |
| | | __highest_price_infos = {} |
| | | |
| | | @classmethod |
| | | def init_origin_price(cls, code, price, rate_percent): |
| | | cls.__last_price_infos[code] = [("09:25:00", price, rate_percent)] |
| | | cls.__last_15s_price_infos[code] = [("09:25:00", price, rate_percent)] |
| | | cls.__reference_price_info[code] = ("09:25:00", price, rate_percent) |
| | | |
| | | # 获取15s最终的价格 |
| | | @classmethod |
| | | def get_15_seconds_price_info(cls, price_infos, max_amplitude_rate_percent): |
| | | final_price_info = price_infos[-1] |
| | | max_rate_info = None |
| | | for price_info in price_infos: |
| | | if price_info - price_infos[0][2] > max_amplitude_rate_percent: |
| | | if not max_rate_info: |
| | | max_rate_info = price_info |
| | | if max_rate_info[2] < price_info[2]: |
| | | max_rate_info = price_info |
| | | if max_rate_info: |
| | | final_price_info = max_rate_info |
| | | return final_price_info |
| | | cls.__highest_price_infos[code] = [("09:25:00", price, rate_percent)] |
| | | |
| | | # 价格信息(时间,现价,涨幅) |
| | | # return 是否减仓 |
| | | @classmethod |
| | | def process_tick_data(cls, code, time_str, price, rate_percent, max_amplitude_rate_percent=0.6): |
| | | # 非交易时间 |
| | | if tool.trade_time_sub(time_str, "09:30:00") < 0: |
| | | return False |
| | | now_price_info = (time_str, price, rate_percent) |
| | | latest_price_info = cls.__last_price_infos.get(code)[-1] |
| | | # 填充中间缺失的数据 |
| | | latest_time = latest_price_info[0] |
| | | if tool.trade_time_sub(latest_time, "09:29:57") < 0: |
| | | latest_time = "09:29:57" |
| | | |
| | | sub_time = tool.trade_time_sub(time_str, latest_time) |
| | | fill_middle_count = sub_time // 3 - 1 |
| | | if fill_middle_count > 0: |
| | | for i in range(0, fill_middle_count): |
| | | cls.__last_price_infos.get(code).append( |
| | | (tool.trade_time_add_second(latest_price_info[0], 3), latest_price_info[1], latest_price_info[2])) |
| | | cls.__last_price_infos.get(code).append(now_price_info) |
| | | latest_15s_price_info = cls.__last_15s_price_infos[code][-1] |
| | | if now_price_info[2] - latest_15s_price_info[2] > max_amplitude_rate_percent: |
| | | # 涨幅超过振幅且比参考点的振幅大 |
| | | if now_price_info[2] > cls.__reference_price_info[code][2]: |
| | | cls.__reference_price_info[code] = now_price_info |
| | | elif latest_15s_price_info[2] - now_price_info[2] > max_amplitude_rate_percent: |
| | | # 跌幅超过振幅 |
| | | cls.__reference_price_info[code] = now_price_info |
| | | # 当前点是否比参考点下降振幅就触发卖 |
| | | if cls.__reference_price_info[code][2] - now_price_info[2] > max_amplitude_rate_percent: |
| | | # 触发卖 |
| | | cls.__reference_price_info[code] = now_price_info |
| | | return True |
| | | # 如果比最高价高就取最高价 |
| | | if cls.__highest_price_infos.get(code)[2] < now_price_info[2]: |
| | | cls.__highest_price_infos[code] = now_price_info |
| | | else: |
| | | # 尚未触发卖 |
| | | if len(cls.__last_price_infos.get(code)) >= 5: |
| | | # 满足15s数据 |
| | | reference_price_info = cls.get_15_seconds_price_info(cls.__last_price_infos.get(code), max_amplitude_rate_percent) |
| | | cls.__last_price_infos.get(code).clear() |
| | | cls.__reference_price_info[code] = reference_price_info |
| | | # 低于最高点阈值 |
| | | if cls.__highest_price_infos.get(code)[2] - now_price_info[2] > max_amplitude_rate_percent: |
| | | # 触发卖 |
| | | cls.__highest_price_infos[code] = now_price_info |
| | | return True |
| | | return False |
| | | |
| | | |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | pass |