From 761d73cdac878b0a6c19cadc97ea646aa0eaffa5 Mon Sep 17 00:00:00 2001 From: jackyu66git Date: Fri, 25 Apr 2025 11:00:20 +0800 Subject: [PATCH] Change app and other py --- ChanKLC.py | 5 +- ChanLun.py | 65 +++++++++--------- ChanLun_Classifier.py | 1 + __pycache__/ChanKLC.cpython-312.pyc | Bin 20616 -> 20475 bytes __pycache__/ChanLun.cpython-312.pyc | Bin 112036 -> 112036 bytes .../ChanLun_Classifier.cpython-312.pyc | Bin 17001 -> 17001 bytes strategies/ChanLun_SOL_5.py | 28 ++++---- strategies/ChanLun_XGB.py | 2 +- web/app.py | 19 ++--- 9 files changed, 65 insertions(+), 55 deletions(-) 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 1408a12319a2733d901e1fed8f0966965e466e16..abbe2d6a9146fe4928434b4b60d50957a07468d6 100644 GIT binary patch delta 601 zcmY+B%WG3n5XSE`_m%rFv1x5cZeArMxvc~@jkZZ_Vyd>bH3-#(o1#%n6idAgVkLrw zA}P|Kt__NEL`vr`$wtE90ix2eoP{i{_849>z z*iga`#wwg-bEf5jQM~0Hhtq7++XTLKtandoNsCTxp^hnErf-&e-!)6sOAFI8rF6G` zn>oI1fIFUmLOu2!e$1Ph|9|H3e&2(c z1yEKL#m3R2?XXR*JXS=}7Noa1uF9PS1lMNVE83iL6{=hkSRXqToDg%M;d9X=zq5NA zJZJC6LW^qI2KZOhg#Z-`__=Ef&P$3k%yEWlO6Yg_RH#}q&_yj4`KA*GT!pV#t6*|i z+~jX6m33tszpm=vKvl8oLcA`YJ6}!~Qt6Cgp_U^FUBXq#-zr=%@>8bapgH^@of3O! zP^#g$+~@x2?6{SgE6x_@FFhK4dh$(R^sO`Y0WWV{dMMwAedI*T1Aqhg%eD#;eAPM* zhtO@;;V?$**I}5vw0n74{^AJ32X7ERdf*-3uYIHPp%bVyvJs zco+nRPKF7FAxwn#z<#_M9)tn%BJ2Vm^PTqme@HHCCeLJ3X4%LlGbuCmgft>)h3=e) y`?rUgMU>$P!%>D~49zKHEDGT5csB&`S^S{D3%zSH{)smnISD*#9HY@oyZ!-8?6i6S diff --git a/__pycache__/ChanLun.cpython-312.pyc b/__pycache__/ChanLun.cpython-312.pyc index c1f8b8e514b35b4862f746cb6aed50da1c88917d..793960b10fa3b8311ee8c9f27db5c96c97a0d444 100644 GIT binary patch delta 1959 zcmZux3ry8j82^9&`{pHFUU5N?S9lc#B{g+{N;79#fIKoQyuu~!yj<{HK!)U|AtZux zJLp+Xe3A$jL;hDTbD>z4HA@%Tva1=6r6uYdYAe~O^Bs!m=H2e^|DErA@5gB!*0v67 z{Vw?WdP)5E;Ull&zKRomL$cl)QY04!)vD>=a+bbUdON-_wpOC6ROt|-FjZuf^04m+ zgnHDaWmX4S&2`B;96Xu=d#Te(JI<`}_2}2zEAs6&XSoM#bW(qk#-rjKw%7nBAMf|A z<@xLF`GuBp3$^qI%PkVE>c2^QVjAI;k{ueb77E z1a>R$=Cp)EHyth89;TpYLq!=kX3bw`^Js~+q_omXdT7gr1XC{t*N7s%PVUrMZXxKp zfVk+54Hf&T|V0y8|v7*8;6VtDDit3~&zI(p%(F&t3diC%* zxTdrmS)z)NK$6Vc#L?jBWC0-MVN-L8NnOqtj^_fkXremGed?1 zda>(~2{oTBQ=zrMR${TwRIZ<>l(orfNpkkmjF2p)!!a9Q7Dh?jK-QvV4sUV*{eJmi~qR1FM-a8Lc zm7(4;(2!2fqH9m3z#66b)IX3j6I1pYFN7AwpBGW`M1>}%=l?QIL?L{Wmr!zy{{DG7 zyhjuJ&QsfBUz&g2pg7L%^!FFtn}j0%2t(9ozy@8kav+y)&i4Zg`Q{idpN2sC&E?fP zg2ju~(+!W!O6C>S3ouA??tbhq>Xe6(E)n6sTvJ8HCHj-e@h21E9)Q6(Qu20p3LE{e|oZjyEcv<(k;_)$pgS8i0X?A*!=mcIG()@)+qnsz zte{09M7}%F`m;$F)xlP-`9~e><6Wz(=Q+l(o_d%ecS!8Fde|=?mE7Atf=>Yw*z5*q z;1%g>;H{K_-D!Z8yfW_jJHUV?NpFPHa+}0dO}vne?&2nhHDNoO+8}`&KiLN7$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^ zIQo?xG9j3T)Id5oX;ux4FV^5O1cVN?@c~Il5L4Ml>Hh!i>n4^QF+{F(z^UmFLF1di zIA3{db}|#}vdS1OiOPL@WxJX?Bib|ACTZaM65`HSU-mp)0nZp0J@qzKmY&$ diff --git a/__pycache__/ChanLun_Classifier.cpython-312.pyc b/__pycache__/ChanLun_Classifier.cpython-312.pyc index 93f78e82c955e48c754371bcafb6f0b6818da59b..439a339e255af5fd63a7dc127c875a6ccad0f6e8 100644 GIT binary patch delta 116 zcmaFa!uYa3^y Si9er>QU4PIh%8bES^@xkc_W+v delta 95 zcmaFa!uYa0Ij+lv;Y7A 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,