Add classifier to the code

This commit is contained in:
jackyu66git
2025-04-24 20:34:16 +08:00
parent 656484e0ca
commit 63852efe47
7 changed files with 42 additions and 50 deletions
+1 -1
View File
@@ -32,7 +32,7 @@ class ChanKLC():
self.distance = 0 self.distance = 0
self.klc_fx_type = Chan_KLC_FX.UNKNOWN self.klc_fx_type = Chan_KLC_FX.UNKNOWN
def set_klc_fx_type(self, klc_fx_type): def set_klc_fx_type(self, klc_fx_type):
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']) #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): 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): def check_klc_fx_type(self, klc_fx_type):
+27 -35
View File
@@ -6,7 +6,7 @@ sys.path.append(os.path.abspath("/Users/jack/Project/freqtrade/user_data/Chan"))
import numpy as np import numpy as np
from datetime import timedelta from datetime import timedelta
from pandas import DataFrame from pandas import DataFrame
from ChanEnum import Chan_FX_TYPE, Chan_KLINE_DIR, Chan_BI_DIR, Chan_SEG_DIR, Chan_ZS_DIR, Chan_BSP_DIR, Chan_BSP_TYPE from ChanEnum import Chan_FX_TYPE, Chan_KLINE_DIR, Chan_BI_DIR, Chan_SEG_DIR, Chan_ZS_DIR, Chan_BSP_DIR, Chan_BSP_TYPE, Chan_KLC_FX
from ChanKLU import ChanKLU from ChanKLU import ChanKLU
from ChanKLC import ChanKLC from ChanKLC import ChanKLC
from ChanBI import ChanBI from ChanBI import ChanBI
@@ -279,31 +279,24 @@ class ChanLunClassifier:
:return: 特征矩阵X和标签y :return: 特征矩阵X和标签y
""" """
# 使用ChanLun获取bi_list # 使用ChanLun获取bi_list
bi_list = self.chan.cal_bi_list(self.chan.get_klc_list(dataframe))
klc_list = self.chan.get_klc_list(dataframe) klc_list = self.chan.get_klc_list(dataframe)
bi_list = self.chan.cal_bi_list(klc_list)
seg_list = self.chan.get_seg_list(bi_list) seg_list = self.chan.get_seg_list(bi_list)
# 筛选方向为UP的bi的起始klc # 筛选方向为UP的bi的起始klc
feature_data = [] feature_data = []
labels = [] labels = []
bi_index = 0 bi_index = 1
sample_list = [] sample_list = []
for klc in klc_list: for klc in klc_list:
if klc.pre and klc.next: if klc.klc_fx_type != Chan_KLC_FX.UNKNOWN:
if klc.high > klc.pre.high and klc.high > klc.next.high:
klc.set_fx(Chan_FX_TYPE.TOP)
elif klc.low < klc.pre.low and klc.low < klc.next.low:
klc.set_fx(Chan_FX_TYPE.BOTTOM)
else:
klc.set_fx(Chan_FX_TYPE.UNKNOWN)
if klc.fx != Chan_FX_TYPE.UNKNOWN:
sample_list.append(klc) sample_list.append(klc)
for klc in sample_list: for klc in sample_list:
if bi_index >= len(bi_list): if bi_index >= len(bi_list):
bi_index = len(bi_list) - 1 bi_index = len(bi_list) - 1
bi = bi_list[bi_index] #bi = bi_list[bi_index]
if klc.end_klu and bi.end_klc and klc.start_klu.index >= bi.start_klc.start_klu.index and klc.end_klu.index <= bi.end_klc.end_klu.index: #if klc.end_klu and bi.end_klc and klc.start_klu.index >= bi.start_klc.start_klu.index and klc.end_klu.index <= bi.end_klc.end_klu.index:
klc.set_bi(bi) #klc.set_bi(bi)
# 提取特征 # 提取特征
features = klc.get_feature_data() features = klc.get_feature_data()
@@ -318,17 +311,21 @@ class ChanLunClassifier:
# 判断这个bi是否赚钱(这里简单定义为:如果bi的结束价格高于起始价格,则标记为1,否则为0) # 判断这个bi是否赚钱(这里简单定义为:如果bi的结束价格高于起始价格,则标记为1,否则为0)
# 这个标签定义可以根据实际需求修改 # 这个标签定义可以根据实际需求修改
bi = bi_list[bi_index] matched = False
if bi.start_klc.index == klc.index: for bi in bi_list:
#if klc.index == seg.start_bi.start_klc.index and seg.dir == Chan_SEG_DIR.UP: if bi.end_klc and bi.end_klc.index == klc.index:
label = 1 label = 1
bi_index += 2 matched = True
else: break
if not matched:
label = 0 label = 0
feature_data.append(feature_vec) feature_data.append(feature_vec)
labels.append(label) labels.append(label)
print("Trainning data: ", klc_list[-1].start_time, klc_list[-1].fx) # 在return前添加
positive_count = np.sum(labels)
print(f"正样本数量: {positive_count}, 负样本数量: {len(labels) - positive_count}")
print("Trainning data: ", len(feature_data), klc_list[-1].start_time, klc_list[-1].klc_fx_type , "---------------------")
return np.array(feature_data), np.array(labels) return np.array(feature_data), np.array(labels)
def get_validate_feature_data(self, dataframe): def get_validate_feature_data(self, dataframe):
""" """
@@ -337,23 +334,16 @@ class ChanLunClassifier:
:return: 特征矩阵X和标签y :return: 特征矩阵X和标签y
""" """
# 使用ChanLun获取bi_list # 使用ChanLun获取bi_list
bi_list = self.chan.cal_bi_list(self.chan.get_klc_list(dataframe))
klc_list = self.chan.get_klc_list(dataframe) klc_list = self.chan.get_klc_list(dataframe)
bi_list = self.chan.cal_bi_list(klc_list)
seg_list = self.chan.get_seg_list(bi_list) seg_list = self.chan.get_seg_list(bi_list)
# 筛选方向为UP的bi的起始klc # 筛选方向为UP的bi的起始klc
feature_data = [] feature_data = []
labels = [] labels = []
bi_index = 0 bi_index = 1
sample_list = [] sample_list = []
for klc in klc_list: for klc in klc_list:
if klc.pre and klc.next: if klc.klc_fx_type != Chan_KLC_FX.UNKNOWN:
if klc.high > klc.pre.high and klc.high > klc.next.high:
klc.set_fx(Chan_FX_TYPE.TOP)
elif klc.low < klc.pre.low and klc.low < klc.next.low:
klc.set_fx(Chan_FX_TYPE.BOTTOM)
else:
klc.set_fx(Chan_FX_TYPE.UNKNOWN)
if klc.fx != Chan_FX_TYPE.UNKNOWN:
sample_list.append(klc) sample_list.append(klc)
for klc in sample_list: for klc in sample_list:
if bi_index >= len(bi_list): if bi_index >= len(bi_list):
@@ -371,16 +361,18 @@ class ChanLunClassifier:
else: else:
feature_vec.append(0) feature_vec.append(0)
seg = seg_list[bi_index] seg = seg_list[bi_index]
if bi.start_klc.index == klc.index: matched = False
#if klc.index == seg.start_bi.start_klc.index and seg.dir == Chan_SEG_DIR.UP: for bi in bi_list:
if bi.end_klc and bi.end_klc.index == klc.index:
label = 1 label = 1
bi_index += 2 matched = True
else: break
if not matched:
label = 0 label = 0
feature_data.append(feature_vec) feature_data.append(feature_vec)
labels.append(label) labels.append(label)
print("Validating data: ", len(feature_data), klc_list[-1].start_time, klc_list[-1].klc_fx_type , "---------------------")
return np.array(feature_data), np.array(labels) return np.array(feature_data), np.array(labels)
def validate_model(self, dataframe=None): def validate_model(self, dataframe=None):
""" """
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -43,7 +43,7 @@
"ccxt_config": {}, "ccxt_config": {},
"ccxt_async_config": {}, "ccxt_async_config": {},
"pair_whitelist": [ "pair_whitelist": [
"BTC/USDT:USDT", "SOL/USDT:USDT",
], ],
"pair_blacklist": [ "pair_blacklist": [
"BNB/.*" "BNB/.*"
+11 -11
View File
@@ -21,10 +21,10 @@ logger = logging.getLogger(__name__)
### Now you can use logger.info('asfd') to log ### Now you can use logger.info('asfd') to log
# freqtrade plot-dataframe --strategy ChanLun_SOL_5 --datadir user_data/data/binance -c ./user_data/ChanLun_SOL.json --timerange=20250309- # freqtrade plot-dataframe --strategy ChanLun_SOL_5 --datadir user_data/data/binance -c ./user_data/ChanLun_SOL.json --timerange=20250309-
# freqtrade trade -c ./user_data/ChanLun_SOL.json --strategy ChanLun_SOL_5 --strategy-path ./user_data/strategies # freqtrade trade -c ./user_data/Chan/config/ChanLun_SOL.json --strategy ChanLun_SOL_5 --strategy-path ./user_data/Chan/strategies
# freqtrade backtesting -c ./user_data/ChanLun_SOL.json --strategy ChanLun_SOL_5 --strategy-path ./user_data/strategies --timerange=20250416- # freqtrade backtesting -c ./user_data/Chan/config/ChanLun_SOL.json --strategy ChanLun_SOL_5 --strategy-path ./user_data/Chan/strategies --timerange=20250416-
# freqtrade download-data -c ./user_data/ChanLun_SOL.json -t 1m --pairs SOL/USDT:USDT --timerange=20250405- # freqtrade download-data -c ./user_data/Chan/config/ChanLun_SOL.json -t 1m --pairs SOL/USDT:USDT --timerange=20250405-
# freqtrade hyperopt --hyperopt-loss SharpeHyperOptLossDaily --spaces roi stoploss --strategy ChanLun_SOL_5 --strategy-path ./user_data/strategies -c ./user_data/ChanLun_SOL.json -e 200 --timerange=20250201-20250401 # freqtrade hyperopt --hyperopt-loss SharpeHyperOptLossDaily --spaces roi stoploss --strategy ChanLun_SOL_5 --strategy-path ./user _data/Chan/strategies -c ./user_data/Chan/config/ChanLun_SOL.json -e 200 --timerange=20250201-20250401
# sudo docker compose run --rm chan_btc backtesting -c ./user_data/ChanLun_SOL.json --strategy ChanLun_SOL --strategy-path ./user_data/strategies --timerange=20250101- # sudo docker compose run --rm chan_btc backtesting -c ./user_data/ChanLun_SOL.json --strategy ChanLun_SOL --strategy-path ./user_data/strategies --timerange=20250101-
# sudo docker compose run --rm chan_btc download-data -c ./user_data/ChanLun_SOL.json --pairs SOL/USDT:USDT -t 1m --timerange 20240101- # sudo docker compose run --rm chan_btc download-data -c ./user_data/ChanLun_SOL.json --pairs SOL/USDT:USDT -t 1m --timerange 20240101-
@@ -118,9 +118,9 @@ class ChanLun_SOL_5(IStrategy):
self.classifier.train_model(dataframe_1d, model_name="1d_model") self.classifier.train_model(dataframe_1d, model_name="1d_model")
""" """
"""
if self.classifier.model is None: if self.classifier.model is None:
self.classifier.train_model(dataframe_30, model_name="30m_model") #self.classifier.train_model(dataframe_30, model_name="30m_model")
self.classifier.load_model(model_name="30m_model") self.classifier.load_model(model_name="30m_model")
klc_list = self.chan.get_klc_list(dataframe_30) klc_list = self.chan.get_klc_list(dataframe_30)
top_avg = 0 top_avg = 0
@@ -129,12 +129,12 @@ class ChanLun_SOL_5(IStrategy):
bottom_count = 0 bottom_count = 0
for index in range(int(len(klc_list) * 0.8), len(klc_list)): for index in range(int(len(klc_list) * 0.8), len(klc_list)):
klc = klc_list[index] klc = klc_list[index]
if self.classifier.predict(klc) > 0.01 and klc.fx == Chan_FX_TYPE.BOTTOM: if self.classifier.predict(klc) > 0.45 and klc.fx == Chan_FX_TYPE.BOTTOM:
features = klc.get_feature_data() 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.end_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_avg += self.classifier.predict(klc)
bottom_count += 1 bottom_count += 1
if self.classifier.predict(klc) > 0.05 and klc.fx == Chan_FX_TYPE.TOP: if self.classifier.predict(klc) > 0.44 and klc.fx == Chan_FX_TYPE.TOP:
features = klc.get_feature_data() 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.end_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_avg += self.classifier.predict(klc)
@@ -143,7 +143,7 @@ class ChanLun_SOL_5(IStrategy):
top_avg /= top_count top_avg /= top_count
print(bottom_avg, top_avg) print(bottom_avg, top_avg)
print("-------------------------------------------------------------------------------") print("-------------------------------------------------------------------------------")
"""
""" """
self.print_xgb(dataframe, "1m_model") self.print_xgb(dataframe, "1m_model")
self.print_xgb(dataframe_5, "5m_model") self.print_xgb(dataframe_5, "5m_model")
@@ -163,11 +163,11 @@ class ChanLun_SOL_5(IStrategy):
#dataframe_60['state'] = self.chan.cal_klu_state(dataframe_60) #dataframe_60['state'] = self.chan.cal_klu_state(dataframe_60)
#dataframe_4h['state'] = self.chan.resample_klc_list(dataframe_4h) #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) dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
#self.print_macd_div_list(dataframe) #self.print_macd_div_list(dataframe)
#self.print_resample_df(dataframe, 1, 50) #self.print_resample_df(dataframe, 1, 50)
self.chan.get_bi_list(dataframe_30) #self.chan.get_bi_list(dataframe_30)
if self.last_time + timedelta(minutes=1) < datetime.now(): if self.last_time + timedelta(minutes=1) < datetime.now():
#print(informative.iloc[-1]) #print(informative.iloc[-1])
#self.log_macd_div_list(dataframe) #self.log_macd_div_list(dataframe)