8.1 大小盘轮动 · 新手上路 -- 二八ETF择时轮动策略2.0
start = '2013-05-01'
end = '2015-09-01'
benchmark = 'HS300'
universe = ['510300.XSHG','510500.XSHG']
HS300,SZ500=universe
capital_base = 100000
freq = 'd'
refresh_rate =1
max_retracement = 0.01
def initialize(account):
pass
def handle_data(account):
if account.current_date.weekday() != 4 :
return
if len(account.universe) < 2: return
hist = account.get_attribute_history('closePrice', 19)
if len(hist) < 2:
return
if hist[HS300][-1]/hist[HS300][0] > hist[SZ500][-1]/hist[ SZ500][0]:
if hist[HS300][-1]/hist[HS300][0] > 1:
if account.avail_secpos.has_key(SZ500):
order_pct_to(SZ500, 0)
order_pct_to(HS300, 0.99)
elif hist[HS300][-1]/hist[HS300][0] < 1- max_retracement:
if account.avail_secpos.has_key(SZ500):
order_pct_to(SZ500, 0)
if account.avail_secpos.has_key(HS300):
order_pct_to(HS300, 0)
elif hist[HS300][-1]/hist[HS300][0] < hist[SZ500][-1]/hist[ SZ500][0]:
if hist[SZ500][-1]/hist[SZ500][0] > 1:
if account.avail_secpos.has_key(HS300):
order_pct_to(HS300, 0)
order_pct_to(SZ500, 0.99)
elif hist[SZ500][-1]/hist[SZ500][0] < 1- max_retracement:
if account.avail_secpos.has_key(SZ500):
order_pct_to(SZ500, 0)
if account.avail_secpos.has_key(HS300):
order_pct_to(HS300, 0)