很不错的策略,简单高效。出于学习的目的,将
#取需要的只数
check_out_lists = check_out_lists[:g.buy_stock_count]
改写为:
#此函数用于生成交易日可操作股票列表,a为持仓股票,b为过滤停牌、涨停、跌停及st和退市后的股票
def find_elements(a, b):
if not a: #判断a是否为空列表
return b[:g.buy_stock_count]
a_in_b = [x for x in a if x in b] # 找出a中在b中出现的元素
if len(a_in_b) == len(a):
# 如果a中所有元素都在b中,则直接返回a
return a
elif len(a_in_b) > 0:
# 如果a中部分元素在b中,则从b中选择凑齐g.buy_stock_count个元素
b_remaining = [x for x in b if x not in a_in_b]
b_selected = b_remaining[:g.buy_stock_count - len(a_in_b)]
return a_in_b + b_selected
else:
# 如果a中没有元素在b中,则从B中选择任意g.buy_stock_count个元素
return b[:g.buy_stock_count]
更改后,2022年全年累计交易费用由原策略3319元下降至491元。虽然全年收益99.46%高于原策略的49.90%,但若时间线拉长为2008-11-10 到 2019-11-10,收益依然不及原策略。