利用股市板塊輪流出現上漲行情,選擇輪動上漲的股票。
選擇標的:上證50、滬深300指數基金
判斷指標:動量(當日收盤價-之前第20天收盤價) / 當日收盤價
判斷條件:在動量大於0且沒有持倉時,買入其中動量較大的一個並持有;在動量小於0的時候平倉。
調倉週期:每個交易日收盤時調倉。
ptrade上代碼如下:
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)