二八轮动策略

利用股市板块轮流出现上涨行情,选择轮动上涨的股票。

选择标的:上证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)
    

猜你喜欢

转载自blog.csdn.net/u012724887/article/details/105413427