  
等级: 新手上路 
- 注册: 
 - 2025-2-24
 
- 曾用名: 
 
 
 
 | 
 
请求支援 
 
def init(context): 
    # 策略核心参数 
    context.short_ma = 5       # 短期均线周期 
    context.long_ma = 20      # 长期均线周期 
    context.rsi_period = 14   # RSI周期 
    context.atr_period = 14   # ATR周期 
    context.risk_ratio = 0.02 # 单笔风险比例 
    context.symbol = "SZ000001" # 交易品种 
 
    # 初始化运行时变量 
    context.position = 0      # 当前持仓方向(0:无仓 1:多仓 -1:空仓) 
    context.entry_price = 0   # 入场价格 
    context.stop_loss = 0     # 动态止损位 
 
def handle_bar(context): 
        # 获取必要数据 
    bars = history_bars(context.symbol, max(context.long_ma, context.rsi_period, context.atr_period)+1, '5m', ['close', 'high', 'low']) 
 
    if len(bars) < max(context.long_ma, context.rsi_period, context.atr_period)+1: 
        return 
 
    close = bars['close'] 
    high = bars['high'] 
    low = bars['low'] 
 
    # 计算双均线 
    short_ma = close[-context.short_ma:].mean() 
    long_ma = close[-context.long_ma:].mean() 
 
    # 计算RSI 
    delta = np.diff(close) 
    gain = delta[delta > 0].sum() 
    loss = abs(delta[delta < 0].sum()) 
    rs = gain / loss if loss != 0 else 1 
    rsi = 100 - (100 / (1 + rs)) 
 
    # 计算ATR 
    tr = [max(h-l, abs(h-c), abs(l-c)) for h,l,c in zip(high[-context.atr_period:],  
                                                        low[-context.atr_period:],  
                                                        close[-context.atr_period-1:-1])] 
    atr = np.mean(tr) 
 
    # 获取账户信息 
    portfolio = get_portfolio(context.symbol, 2) 
    current_price = get_dynainf(context.symbol, 7) 
 
    # 趋势跟踪逻辑 
    if short_ma > long_ma and context.position <= 0: 
        # 计算风险仓位 
        max_risk = context.portfolio_value * context.risk_ratio 
        volume = int(max_risk / (atr * 2)) 
 
        if portfolio.volume > 0:  # 先平空仓 
            buy_close(context.symbol, "Market", volume=portfolio.volume,serial_id = 1) 
        buy_open(context.symbol, "Market", volume=volume,serial_id = 2) 
        context.position = 1 
        context.entry_price = current_price 
        context.stop_loss = current_price - atr * 2 
 
    # 均值回归逻辑 
    elif rsi < 30 and context.position == 0: 
        volume = int(context.portfolio_value * 0.2 / current_price) 
        buy_open(context.symbol, "Limit", price=current_price*0.998, volume=volume,serial_id = 3) 
        context.position = 1 
        context.entry_price = current_price 
        context.stop_loss = current_price * 0.995 
 
    # 动态止损 
    if context.position != 0: 
        if current_price < context.stop_loss: 
            if context.position == 1: 
                sell_close(context.symbol, "Market", volume=portfolio.volume,serial_id = 4) 
            else: 
                buy_close(context.symbol, "Market", volume=portfolio.volume,serial_id = 5) 
            context.position = 0 
 
    # 时间止损(收盘前5分钟强制平仓) 
    if context.time.hour == 14 and context.time.minute >= 55: 
        if context.position == 1: 
            sell_close(context.symbol, "Market", volume=portfolio.volume,serial_id = 6) 
        elif context.position == -1: 
            buy_close(context.symbol, "Market", volume=portfolio.volume,serial_id = 7) 
        context.position = 0 
 
def before_trading(context): 
    # 重置运行时变量 
    context.position = 0 
    context.entry_price = 0 
    context.stop_loss = 0 
 
def parameter(): 
    # 可优化参数 
    input_par("short_ma", 5, 3, 10, 1) 
    input_par("long_ma", 20, 15, 30, 1) 
    input_par("rsi_period", 14, 10, 20, 1) 
    input_par("risk_ratio", 0.02, 0.01, 0.05, 0.005) 
 
 
 
 
 
请帮我看一下是哪个索引有问题,我实在是找不出来 
 |   
 
 
 
 |