Twenty-eight rounds of movement strategy

Use the stock market sector to take turns to see rising prices, and choose stocks that are rising in rotation.

Select target: SSE 50, CSI 300 Index Fund

Judging indicator: Momentum (closing price of the day-closing price of the 20th day before) / closing price of the day

Judgment condition: when the momentum is greater than 0 and there is no position, buy and hold the one with the greater momentum; close the position when the momentum is less than 0.

Position adjustment cycle: Position adjustment at the close of each trading day.

The code on ptrade is as follows:

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)
    

 

Guess you like

Origin blog.csdn.net/u012724887/article/details/105413427