diff --git a/ChanKLC.py b/ChanKLC.py index 42b742c..f9339e8 100644 --- a/ChanKLC.py +++ b/ChanKLC.py @@ -35,18 +35,19 @@ class ChanKLC(): #print(self.start_time, klc_fx_type, self.get_feature_data()['klu_macd'], self.get_feature_data()['klu_macdhist'], self.get_feature_data()['klu_rsi']) if self.check_klc_fx_type(klc_fx_type): self.klc_fx_type = klc_fx_type + self.klc_fx_type = klc_fx_type def check_klc_fx_type(self, klc_fx_type): if klc_fx_type == Chan_KLC_FX.BOTTOM1: features = self.cal_klu_features() if features['klu_macd'] > 0: - print(self.start_time, klc_fx_type, features['klu_macd']) + #print(self.start_time, klc_fx_type, features['klu_macd']) return False else: return True elif klc_fx_type == Chan_KLC_FX.TOP1: features = self.get_feature_data() if features['klu_macd'] < 0: - print(self.start_time, klc_fx_type, features['klu_macd']) + #print(self.start_time, klc_fx_type, features['klu_macd']) return False else: return True diff --git a/ChanLun.py b/ChanLun.py index 90f7e19..d134eef 100644 --- a/ChanLun.py +++ b/ChanLun.py @@ -153,6 +153,36 @@ class ChanLun(): def get_bi_list(self, dataframe): bi_list = self.cal_bi_list(self.get_klc_list(dataframe)) return bi_list + # -------------------------------------------------------------------- + def get_kl_data(self, dataframe:DataFrame): + fields = "time,open,high,low,close,volume" + klu_list = [] + for i in range(0, len(dataframe)): + item = dataframe.iloc[i] + date = item['date'] + o = item['open'] + h = item['high'] + l = item['low'] + c = item['close'] + v = item['volume'] + #time_obj = date.fromtimestamp(date) + #date = date + timedelta(hours=8) + time_str = date.strftime('%Y-%m-%d %H:%M:%S') + item_data = [ + time_str, + o, + h, + l, + c, + v + ] + #klu = KLU(self.create_item_dict(item_data, GetColumnNameFromFieldList(fields))) + klu = ChanKLU(time_str, o, h, l, c, v) + klu.set_idx(i) + klu_list.append(klu) + if True: + klu.set_indicators(item) + return klu_list def calculate_zs(self, bi_list, seg_list): return self.get_zs_list(bi_list, seg_list) @@ -521,7 +551,7 @@ class ChanLun(): # 打印识别结果 print(f"笔中枢识别完成,共找到 {len(bi_zs_list)} 个笔中枢") return bi_zs_list - + #------------------------------------------------------------------- def cal_bi_list(self, klc_list): bi_list = [] last_top = None @@ -782,6 +812,8 @@ class ChanLun(): klc.state = "-10" #print(klc.time, klc.state) """ + #for index in range(0, 10): + #print(bi_list[index].start_time, bi_list[index].start_klc.start_time, bi_list[index].dir) return bi_list def get_zs_list(self, bi_list, seg_list): @@ -1102,35 +1134,6 @@ class ChanLun(): klc.set_end_klu(klu) return klc_list - def get_kl_data(self, dataframe:DataFrame): - fields = "time,open,high,low,close,volume" - klu_list = [] - for i in range(0, len(dataframe)): - item = dataframe.iloc[i] - date = item['date'] - o = item['open'] - h = item['high'] - l = item['low'] - c = item['close'] - v = item['volume'] - #time_obj = date.fromtimestamp(date) - #date = date + timedelta(hours=8) - time_str = date.strftime('%Y-%m-%d %H:%M:%S') - item_data = [ - time_str, - o, - h, - l, - c, - v - ] - #klu = KLU(self.create_item_dict(item_data, GetColumnNameFromFieldList(fields))) - klu = ChanKLU(time_str, o, h, l, c, v) - klu.set_idx(i) - klu_list.append(klu) - if True: - klu.set_indicators(item) - return klu_list def get_bsp_list1(self, big_df): big_bi_list = self.get_bi_list(big_df) @@ -1498,7 +1501,7 @@ class ChanLun(): area = histogram[start_idx:end_idx+1][histogram[start_idx:end_idx+1] < 0].sum() return abs(area) - + # -------------------------------------------------------------------- def plot_dual(self, small_df, big_df): """ 绘制双周期K线图表,包括两个周期的笔、线段、中枢和买卖点 diff --git a/ChanLun_Classifier.py b/ChanLun_Classifier.py index 4f44010..6d0d76f 100644 --- a/ChanLun_Classifier.py +++ b/ChanLun_Classifier.py @@ -314,6 +314,7 @@ class ChanLunClassifier: matched = False for bi in bi_list: if bi.end_klc and bi.end_klc.index == klc.index: + #print(bi.start_time, bi.start_klc.start_time, bi.dir) label = 1 matched = True break diff --git a/__pycache__/ChanKLC.cpython-312.pyc b/__pycache__/ChanKLC.cpython-312.pyc index 1408a12..abbe2d6 100644 Binary files a/__pycache__/ChanKLC.cpython-312.pyc and b/__pycache__/ChanKLC.cpython-312.pyc differ diff --git a/__pycache__/ChanLun.cpython-312.pyc b/__pycache__/ChanLun.cpython-312.pyc index c1f8b8e..793960b 100644 Binary files a/__pycache__/ChanLun.cpython-312.pyc and b/__pycache__/ChanLun.cpython-312.pyc differ diff --git a/__pycache__/ChanLun_Classifier.cpython-312.pyc b/__pycache__/ChanLun_Classifier.cpython-312.pyc index 93f78e8..439a339 100644 Binary files a/__pycache__/ChanLun_Classifier.cpython-312.pyc and b/__pycache__/ChanLun_Classifier.cpython-312.pyc differ diff --git a/strategies/ChanLun_SOL_5.py b/strategies/ChanLun_SOL_5.py index 546aa40..d140dbc 100644 --- a/strategies/ChanLun_SOL_5.py +++ b/strategies/ChanLun_SOL_5.py @@ -8,7 +8,7 @@ import os sys.path.append(os.path.abspath("/Users/jack/Documents/GitHub/freqtrade/user_data/Chan")) from ChanLun import ChanLun from ChanLun_Classifier import ChanLunClassifier -from ChanEnum import Chan_FX_TYPE +from ChanEnum import Chan_FX_TYPE, Chan_KLC_FX # -------------------------------- from technical.util import resample_to_interval, resampled_merge import talib.abstract as ta @@ -118,29 +118,33 @@ class ChanLun_SOL_5(IStrategy): self.classifier.train_model(dataframe_1d, model_name="1d_model") """ - + model_name = "60m_model" + df = dataframe_60 if self.classifier.model is None: - #self.classifier.train_model(dataframe_30, model_name="30m_model") - self.classifier.load_model(model_name="30m_model") - klc_list = self.chan.get_klc_list(dataframe_30) + #self.classifier.train_model(df, model_name=model_name) + self.classifier.load_model(model_name=model_name) + klc_list = self.chan.get_klc_list(df) + bi_list = self.chan.cal_bi_list(klc_list) top_avg = 0 bottom_avg = 0 top_count = 0 bottom_count = 0 for index in range(int(len(klc_list) * 0.8), len(klc_list)): klc = klc_list[index] - if self.classifier.predict(klc) > 0.45 and klc.fx == Chan_FX_TYPE.BOTTOM: + if self.classifier.predict(klc) > 0.37 and (klc.klc_fx_type == Chan_KLC_FX.BOTTOM1 or klc.klc_fx_type == Chan_KLC_FX.BOTTOM2): features = klc.get_feature_data() - print(klc.end_time, klc.fx, self.classifier.predict(klc), features['klc_macd'], features['klc_macd_hist'], features['klc_rsi'], features['klc_macd_signal']) + print(klc.bi.start_time, klc.start_time, klc.fx, self.classifier.predict(klc), features['klc_macd'], features['klc_macd_hist'], features['klc_rsi'], features['klc_macd_signal']) bottom_avg += self.classifier.predict(klc) bottom_count += 1 - if self.classifier.predict(klc) > 0.44 and klc.fx == Chan_FX_TYPE.TOP: + if self.classifier.predict(klc) > 0.42 and (klc.klc_fx_type == Chan_KLC_FX.TOP1 or klc.klc_fx_type == Chan_KLC_FX.TOP2): features = klc.get_feature_data() - print(klc.end_time, klc.fx, self.classifier.predict(klc), features['klc_macd'], features['klc_macd_hist'], features['klc_rsi'], features['klc_macd_signal']) + print(klc.bi.start_time, klc.start_time, klc.fx, self.classifier.predict(klc), features['klc_macd'], features['klc_macd_hist'], features['klc_rsi'], features['klc_macd_signal']) top_avg += self.classifier.predict(klc) top_count += 1 - bottom_avg /= bottom_count - top_avg /= top_count + if bottom_count > 0: + bottom_avg /= bottom_count + if top_count > 0: + top_avg /= top_count print(bottom_avg, top_avg) print("-------------------------------------------------------------------------------") @@ -163,7 +167,7 @@ class ChanLun_SOL_5(IStrategy): #dataframe_60['state'] = self.chan.cal_klu_state(dataframe_60) #dataframe_4h['state'] = self.chan.resample_klc_list(dataframe_4h) - #self.chan.plot_dual(dataframe_30, dataframe_60) + self.chan.plot_dual(dataframe_30, dataframe_60) dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14) #self.print_macd_div_list(dataframe) #self.print_resample_df(dataframe, 1, 50) diff --git a/strategies/ChanLun_XGB.py b/strategies/ChanLun_XGB.py index 85aa7cf..7f34efa 100644 --- a/strategies/ChanLun_XGB.py +++ b/strategies/ChanLun_XGB.py @@ -74,7 +74,7 @@ class ChanLun_XGB1(IStrategy): """Advanced feature engineering with Chan Lun and technical indicators.""" logger.info(f"Starting feature_engineering_standard for pair {metadata.get('pair', 'unknown')}") if dataframe.empty: - return dataframe + return dataframe try: df = dataframe.copy() diff --git a/web/app.py b/web/app.py index 42319e3..4299763 100644 --- a/web/app.py +++ b/web/app.py @@ -174,9 +174,10 @@ def analyze_chan(df): # 获取分析结果 klc_list = chan.get_klc_list(df) bi_list = chan.cal_bi_list(klc_list) + #for index in range(0, 10): + #print(bi_list[index].start_time, bi_list[index].start_klc.end_time, bi_list[index].dir) seg_list = chan.get_seg_list(bi_list) zs_list = chan.calculate_zs(bi_list, seg_list) - # 添加买卖点识别 buy_sell_points = identify_trade_points(bi_list, seg_list, zs_list) @@ -404,21 +405,21 @@ def analyze(): result.update({ 'kline_data': df.to_dict('records'), 'bi_list': [{ - 'start_time': bi.start_klc.start_time if isinstance(bi.start_klc.start_time, str) else bi.start_klc.start_time.astimezone(client_tz).isoformat(), + 'start_time': bi.start_klc.end_time if isinstance(bi.start_klc.end_time, str) else bi.start_klc.end_time.astimezone(client_tz).isoformat(), 'end_time': (bi.end_klc.end_time if isinstance(bi.end_klc.end_time, str) else bi.end_klc.end_time.astimezone(client_tz).isoformat()) if bi.end_klc else None, 'start_price': bi.start_klc.low if convert_direction(bi.dir) == 1 else bi.start_klc.high, 'end_price': bi.end_klc.high if convert_direction(bi.dir) == 1 else bi.end_klc.low if bi.end_klc else None, 'direction': convert_direction(bi.dir) } for bi in analysis_result['bi_list'] if bi.end_klc], 'seg_list': [{ - 'start_time': seg.start_bi.start_klc.start_time if isinstance(seg.start_bi.start_klc.start_time, str) else seg.start_bi.start_klc.start_time.astimezone(client_tz).isoformat(), + 'start_time': seg.start_bi.start_klc.end_time if isinstance(seg.start_bi.start_klc.end_time, str) else seg.start_bi.start_klc.end_time.astimezone(client_tz).isoformat(), 'end_time': (seg.end_bi.end_klc.end_time if isinstance(seg.end_bi.end_klc.end_time, str) else seg.end_bi.end_klc.end_time.astimezone(client_tz).isoformat()) if seg.end_bi else None, 'start_price': seg.start_bi.start_klc.low if convert_direction(seg.dir) == 1 else seg.start_bi.start_klc.high, 'end_price': seg.end_bi.end_klc.high if convert_direction(seg.dir) == 1 else seg.end_bi.end_klc.low if seg.end_bi else None, 'direction': convert_direction(seg.dir) } for seg in analysis_result['seg_list'] if seg.end_bi], 'zs_list': [{ - 'start_time': zs.start_klc.start_time if isinstance(zs.start_klc.start_time, str) else zs.start_klc.start_time.astimezone(client_tz).isoformat(), + 'start_time': zs.start_klc.end_time if isinstance(zs.start_klc.end_time, str) else zs.start_klc.end_time.astimezone(client_tz).isoformat(), 'end_time': (zs.end_klc.end_time if isinstance(zs.end_klc.end_time, str) else zs.end_klc.end_time.astimezone(client_tz).isoformat()) if zs.end_klc else None, 'zg': zs.zg, 'zd': zs.zd, @@ -426,7 +427,7 @@ def analyze(): } for zs in analysis_result['zs_list'] if zs.end_klc], # 添加未完成中枢列表 'uncompleted_zs_list': [{ - 'start_time': zs.start_klc.start_time if isinstance(zs.start_klc.start_time, str) else zs.start_klc.start_time.astimezone(client_tz).isoformat(), + 'start_time': zs.start_klc.end_time if isinstance(zs.start_klc.end_time, str) else zs.start_klc.end_time.astimezone(client_tz).isoformat(), 'end_time': None, # 未完成中枢没有结束时间 'zg': zs.zg, 'zd': zs.zd, @@ -463,7 +464,7 @@ def analyze(): # 添加小周期分析结果到返回数据 result['element_timeframe'] = element_timeframe result['element_bi_list'] = [{ - 'start_time': bi.start_klc.start_time if isinstance(bi.start_klc.start_time, str) else bi.start_klc.start_time.astimezone(client_tz).isoformat(), + 'start_time': bi.start_klc.end_time if isinstance(bi.start_klc.end_time, str) else bi.start_klc.end_time.astimezone(client_tz).isoformat(), 'end_time': (bi.end_klc.end_time if isinstance(bi.end_klc.end_time, str) else bi.end_klc.end_time.astimezone(client_tz).isoformat()) if bi.end_klc else None, 'start_price': bi.start_klc.low if convert_direction(bi.dir) == 1 else bi.start_klc.high, 'end_price': bi.end_klc.high if convert_direction(bi.dir) == 1 else bi.end_klc.low if bi.end_klc else None, @@ -471,7 +472,7 @@ def analyze(): } for bi in element_analysis['bi_list'] if bi.end_klc] result['element_seg_list'] = [{ - 'start_time': seg.start_bi.start_klc.start_time if isinstance(seg.start_bi.start_klc.start_time, str) else seg.start_bi.start_klc.start_time.astimezone(client_tz).isoformat(), + 'start_time': seg.start_bi.start_klc.end_time if isinstance(seg.start_bi.start_klc.end_time, str) else seg.start_bi.start_klc.end_time.astimezone(client_tz).isoformat(), 'end_time': (seg.end_bi.end_klc.end_time if isinstance(seg.end_bi.end_klc.end_time, str) else seg.end_bi.end_klc.end_time.astimezone(client_tz).isoformat()) if seg.end_bi else None, 'start_price': seg.start_bi.start_klc.low if convert_direction(seg.dir) == 1 else seg.start_bi.start_klc.high, 'end_price': seg.end_bi.end_klc.high if convert_direction(seg.dir) == 1 else seg.end_bi.end_klc.low if seg.end_bi else None, @@ -479,7 +480,7 @@ def analyze(): } for seg in element_analysis['seg_list'] if seg.end_bi] result['element_zs_list'] = [{ - 'start_time': zs.start_klc.start_time if isinstance(zs.start_klc.start_time, str) else zs.start_klc.start_time.astimezone(client_tz).isoformat(), + 'start_time': zs.start_klc.end_time if isinstance(zs.start_klc.end_time, str) else zs.start_klc.end_time.astimezone(client_tz).isoformat(), 'end_time': (zs.end_klc.end_time if isinstance(zs.end_klc.end_time, str) else zs.end_klc.end_time.astimezone(client_tz).isoformat()) if zs.end_klc else None, 'zg': zs.zg, 'zd': zs.zd, @@ -488,7 +489,7 @@ def analyze(): # 添加小周期未完成中枢列表 result['element_uncompleted_zs_list'] = [{ - 'start_time': zs.start_klc.start_time if isinstance(zs.start_klc.start_time, str) else zs.start_klc.start_time.astimezone(client_tz).isoformat(), + 'start_time': zs.start_klc.end_time if isinstance(zs.start_klc.end_time, str) else zs.start_klc.end_time.astimezone(client_tz).isoformat(), 'end_time': None, # 未完成中枢没有结束时间 'zg': zs.zg, 'zd': zs.zd,