金字塔决策交易系统

 找回密码
 

微信登录

微信扫一扫,快速登录

搜索
查看: 279|回复: 1

请求支援

[复制链接]

11

主题

21

帖子

21

积分

Rank: 1

等级: 新手上路

注册:
2025-2-24
曾用名:
发表于 2025-2-25 16:13 | 显示全部楼层 |阅读模式
请求支援

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)





请帮我看一下是哪个索引有问题,我实在是找不出来
微信图片_20250225161219.png
微信图片_20250225161226.png
微信图片_20250225161231.png
回复

使用道具 举报

20

主题

1万

帖子

1万

积分

Rank: 8Rank: 8

等级: 超级版主

注册:
2021-5-18
曾用名:
FireScript
发表于 2025-2-25 16:37 | 显示全部楼层
本帖最后由 技术009 于 2025-2-25 17:36 编辑

history_bars 返回的是一个 numpy数组。你不能直接这样取值。
得用切片方式进行 获取。
金字塔提供一对一VIP专业技术指导服务,技术团队实时响应您的日常使用问题与策略编写。联系电话:021-20339086
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 微信登录

本版积分规则

手机版|小黑屋|上海金之塔信息技术有限公司 ( 沪ICP备13035422号 )

GMT+8, 2025-4-18 11:14 , Processed in 0.185828 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表