# 二八輪動策略

``````def initialize(context):
# 初始化此策略
# 設置我們要操作的股票池
g.security = ['510050.SS', '510300.SS']
set_universe(g.security)

def handle_data(context, data):
security = g.security
his_50 = get_history(20, frequency = '1d', field = 'close', security_list = security, fq = 'pre')
his_300 = get_history(20, frequency = '1d', field = 'close', security_list = security, fq = 'pre')

dong_50 = (data['510050.SS']['close'] - his_50['510050.SS'][0]) / data['510050.SS']['close']
dong_300 = (data['510300.SS']['close'] - his_300['510300.SS'][0]) / data['510300.SS']['close']

amount_50 = context.portfolio.positions['510050.SS']['amount']
amount_300 = context.portfolio.positions['510300.SS']['amount']

cash = context.portfolio.cash
if dong_50 < 0 and amount_50 > 0:
order_target('510050.SS', 0)
if dong_300 < 0 and amount_300 > 0:
order_target('510300.SS', 0)
if dong_50 > 0 and dong_50 > dong_300 and amount_50 == 0:
order_target_value('510050.SS', cash)
if dong_300 > 0 and dong_300 > dong_50 and amount_300 == 0:
order_target_value('510300.SS', cash)
``````