Skip to content

Commit

Permalink
fix: 优化计算性能
Browse files Browse the repository at this point in the history
  • Loading branch information
Vespa314 committed Feb 19, 2024
1 parent 65ea874 commit 1789cbf
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
24 changes: 19 additions & 5 deletions Bi/Bi.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def __init__(self, begin_klc: CKLine, end_klc: CKLine, idx: int, is_sure: bool):
self.__is_sure = is_sure
self.__sure_end = None

self.__klc_lst: List[CKLine] = []
self.__seg_idx: Optional[int] = None

from Seg.Seg import CSeg
Expand Down Expand Up @@ -57,7 +56,22 @@ def is_sure(self): return self.__is_sure
def sure_end(self): return self.__sure_end

@property
def klc_lst(self): return self.__klc_lst
def klc_lst(self):
klc = self.begin_klc
while True:
yield klc
klc = klc.next
if not klc or klc.idx > self.end_klc.idx:
break

@property
def klc_lst_re(self):
klc = self.end_klc
while True:
yield klc
klc = klc.pre
if not klc or klc.idx < self.begin_klc.idx:
break

@property
def seg_idx(self): return self.__seg_idx
Expand Down Expand Up @@ -248,7 +262,7 @@ def Cal_MACD_half_reverse(self):
_s = 1e-7
begin_klu = self.get_end_klu()
peak_macd = begin_klu.macd.macd
for klc in self.klc_lst[::-1]:
for klc in self.klc_lst_re:
for klu in klc[::-1]:
if klu.idx > begin_klu.idx:
continue
Expand Down Expand Up @@ -304,5 +318,5 @@ def Cal_MACD_trade_metric(self, metric: str, cal_avg=False) -> float:
_s += metric_res
return _s / self.get_klu_cnt() if cal_avg else _s

def set_klc_lst(self, lst):
self.__klc_lst = lst
# def set_klc_lst(self, lst):
# self.__klc_lst = lst
2 changes: 1 addition & 1 deletion Bi/BiList.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def update_peak(self, klc: CKLine, for_virtual=False):
if not self.can_update_peak(klc):
return False
_tmp_last_bi = self.bi_list[-1]
self.bi_list = self.bi_list[:-1]
self.bi_list.pop()
if not self.try_update_end(klc, for_virtual=for_virtual):
self.bi_list.append(_tmp_last_bi)
return False
Expand Down
12 changes: 7 additions & 5 deletions BuySellPoint/BSPointList.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class CBSPointList(Generic[LINE_TYPE, LINE_LIST_TYPE]):
def __init__(self, bs_point_config: CBSPointConfig):
self.lst: List[CBS_Point[LINE_TYPE]] = []
self.lst_dict = {}
self.bsp1_lst: List[CBS_Point[LINE_TYPE]] = []
self.config = bs_point_config
self.last_sure_pos = -1
Expand All @@ -39,6 +40,7 @@ def __getitem__(self, index: Union[slice, int]) -> Union[List[CBS_Point], CBS_Po

def cal(self, bi_list: LINE_LIST_TYPE, seg_list: CSegListComm[LINE_TYPE]):
self.lst = [bsp for bsp in self.lst if bsp.klu.idx <= self.last_sure_pos]
self.lst_dict = {bsp.bi.get_end_klu(): bsp for bsp in self.lst}
self.bsp1_lst = [bsp for bsp in self.bsp1_lst if bsp.klu.idx <= self.last_sure_pos]

self.cal_seg_bs1point(seg_list, bi_list)
Expand Down Expand Up @@ -66,11 +68,10 @@ def add_bs(
feature_dict=None,
):
is_buy = bi.is_down()
for exist_bsp in self.lst:
if exist_bsp.klu.idx == bi.get_end_klu().idx:
assert exist_bsp.is_buy == is_buy
exist_bsp.add_another_bsp_prop(bs_type, relate_bsp1)
return
if exist_bsp := self.lst_dict.get(bi.get_end_klu().idx):
assert exist_bsp.is_buy == is_buy
exist_bsp.add_another_bsp_prop(bs_type, relate_bsp1)
return
if bs_type not in self.config.GetBSConfig(is_buy).target_types:
is_target_bsp = False

Expand All @@ -86,6 +87,7 @@ def add_bs(
return
if is_target_bsp:
self.lst.append(bsp)
self.lst_dict[bi.get_end_klu().idx] = bsp
if bs_type in [BSP_TYPE.T1, BSP_TYPE.T1P]:
self.bsp1_lst.append(bsp)

Expand Down
6 changes: 0 additions & 6 deletions KLine/KLine_List.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ def cal_seg_and_zs(self):
self.segzs_list.cal_bi_zs(self.seg_list, self.segseg_list)
update_zs_in_seg(self.seg_list, self.segseg_list, self.segzs_list) # 计算segseg的zs_lst,以及中枢的bi_in, bi_out

self.update_klc_in_bi() # 计算每一笔里面的 klc列表

# 计算买卖点
self.seg_bs_point_lst.cal(self.seg_list, self.segseg_list) # 线段线段买卖点
self.bs_point_lst.cal(self.bi_list, self.seg_list) # 再算笔买卖点
Expand All @@ -134,10 +132,6 @@ def klu_iter(self, klc_begin_idx=0):
for klc in self.lst[klc_begin_idx:]:
yield from klc.lst

def update_klc_in_bi(self):
for bi in self.bi_list:
bi.set_klc_lst(self[bi.begin_klc.idx:bi.end_klc.idx+1])


def cal_seg(bi_list, seg_list):
seg_list.update(bi_list)
Expand Down
4 changes: 2 additions & 2 deletions Seg/SegListChan.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ def __init__(self, seg_config=CSegConfig(), lv=SEG_TYPE.BI):
def do_init(self):
# 删除末尾不确定的线段
while len(self) and not self.lst[-1].is_sure:
self.lst = self.lst[:-1]
self.lst.pop()
if len(self):
assert self.lst[-1].eigen_fx and self.lst[-1].eigen_fx.ele[-1]
if not self.lst[-1].eigen_fx.ele[-1].lst[-1].is_sure:
# 如果确定线段的分形的第三元素包含不确定笔,也需要重新算,不然线段分形元素的高低点可能不对
self.lst = self.lst[:-1]
self.lst.pop()

def update(self, bi_lst: CBiList):
self.do_init()
Expand Down
3 changes: 2 additions & 1 deletion ZS/ZSList.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def try_construct_zs(self, lst, is_sure, zs_algo):
return CZS(lst, is_sure=is_sure) if min_high > max_low else None

def cal_bi_zs(self, bi_lst: Union[CBiList, CSegListComm], seg_lst: CSegListComm):
self.zs_lst = [zs for zs in self.zs_lst if zs.begin_bi.idx < self.last_sure_pos]
while self.zs_lst and self.zs_lst[-1].begin_bi.idx >= self.last_sure_pos:
self.zs_lst.pop()
if self.config.zs_algo == "normal":
for seg in seg_lst:
if not self.seg_need_cal(seg):
Expand Down

0 comments on commit 1789cbf

Please sign in to comment.