Add classifier to the code
This commit is contained in:
+29
-37
@@ -6,7 +6,7 @@ sys.path.append(os.path.abspath("/Users/jack/Project/freqtrade/user_data/Chan"))
|
||||
import numpy as np
|
||||
from datetime import timedelta
|
||||
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 ChanKLC import ChanKLC
|
||||
from ChanBI import ChanBI
|
||||
@@ -279,31 +279,24 @@ class ChanLunClassifier:
|
||||
:return: 特征矩阵X和标签y
|
||||
"""
|
||||
# 使用ChanLun获取bi_list
|
||||
bi_list = self.chan.cal_bi_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)
|
||||
# 筛选方向为UP的bi的起始klc
|
||||
feature_data = []
|
||||
labels = []
|
||||
|
||||
bi_index = 0
|
||||
bi_index = 1
|
||||
sample_list = []
|
||||
for klc in klc_list:
|
||||
if klc.pre and klc.next:
|
||||
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:
|
||||
if klc.klc_fx_type != Chan_KLC_FX.UNKNOWN:
|
||||
sample_list.append(klc)
|
||||
for klc in sample_list:
|
||||
if bi_index >= len(bi_list):
|
||||
bi_index = len(bi_list) - 1
|
||||
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:
|
||||
klc.set_bi(bi)
|
||||
#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:
|
||||
#klc.set_bi(bi)
|
||||
|
||||
# 提取特征
|
||||
features = klc.get_feature_data()
|
||||
@@ -318,17 +311,21 @@ class ChanLunClassifier:
|
||||
|
||||
# 判断这个bi是否赚钱(这里简单定义为:如果bi的结束价格高于起始价格,则标记为1,否则为0)
|
||||
# 这个标签定义可以根据实际需求修改
|
||||
bi = bi_list[bi_index]
|
||||
if bi.start_klc.index == klc.index:
|
||||
#if klc.index == seg.start_bi.start_klc.index and seg.dir == Chan_SEG_DIR.UP:
|
||||
label = 1
|
||||
bi_index += 2
|
||||
else:
|
||||
matched = False
|
||||
for bi in bi_list:
|
||||
if bi.end_klc and bi.end_klc.index == klc.index:
|
||||
label = 1
|
||||
matched = True
|
||||
break
|
||||
if not matched:
|
||||
label = 0
|
||||
|
||||
feature_data.append(feature_vec)
|
||||
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)
|
||||
def get_validate_feature_data(self, dataframe):
|
||||
"""
|
||||
@@ -337,23 +334,16 @@ class ChanLunClassifier:
|
||||
:return: 特征矩阵X和标签y
|
||||
"""
|
||||
# 使用ChanLun获取bi_list
|
||||
bi_list = self.chan.cal_bi_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)
|
||||
# 筛选方向为UP的bi的起始klc
|
||||
feature_data = []
|
||||
labels = []
|
||||
bi_index = 0
|
||||
bi_index = 1
|
||||
sample_list = []
|
||||
for klc in klc_list:
|
||||
if klc.pre and klc.next:
|
||||
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:
|
||||
if klc.klc_fx_type != Chan_KLC_FX.UNKNOWN:
|
||||
sample_list.append(klc)
|
||||
for klc in sample_list:
|
||||
if bi_index >= len(bi_list):
|
||||
@@ -371,16 +361,18 @@ class ChanLunClassifier:
|
||||
else:
|
||||
feature_vec.append(0)
|
||||
seg = seg_list[bi_index]
|
||||
if bi.start_klc.index == klc.index:
|
||||
#if klc.index == seg.start_bi.start_klc.index and seg.dir == Chan_SEG_DIR.UP:
|
||||
label = 1
|
||||
bi_index += 2
|
||||
else:
|
||||
matched = False
|
||||
for bi in bi_list:
|
||||
if bi.end_klc and bi.end_klc.index == klc.index:
|
||||
label = 1
|
||||
matched = True
|
||||
break
|
||||
if not matched:
|
||||
label = 0
|
||||
|
||||
feature_data.append(feature_vec)
|
||||
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)
|
||||
def validate_model(self, dataframe=None):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user