modelscope.cn

stock-backtest

股票交易策略回测技能。支持SMA交叉、RSI均值回归、突破等策略,计算胜率、收益率、CAGR、最大回撤、夏普比率。触发词:回测、策略回测、backtest、交易策略测试、SMA策略、RSI策略、均线策略、量化回测。

Installation

$ npx skills add https://modelscope.cn

Similar popular skills

Related neighbors and high-traction skills in the same topics — useful to compare before installing.

Also in this package

Other skills from modelscope.cn · top by installs.

npx skills add https://modelscope.cn

Browse all from modelscope.cn

More details

Agent compatibility

Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.

Claude Code Not declared
Cursor Not declared
Codex Not declared
GitHub Copilot Not declared
Windsurf Not declared
Gemini CLI Not declared
Cline Not declared
OpenCode Not declared

Package contents

Files included with this skill beyond the listing page.

  • skill md SKILL.md 4,281 B

History

  1. First recorded snapshot · 0 installs

SKILL.md

股票策略回测技能

执行交易策略的历史数据回测,输出完整的绩效报告。

快速开始

当用户请求回测时,按以下流程执行:

  1. 确认数据来源 - Yahoo Finance、akshare(A股)、或用户提供的CSV
  2. 确认策略类型 - 默认提供几种常见策略,也支持自定义
  3. 运行回测 - 使用Python计算
  4. 输出报告 - 总收益、年化收益、最大回撤、夏普比率、胜率、交易次数

内置策略

1. SMA交叉策略

  • 买入信号:短期均线上穿长期均线
  • 卖出信号:短期均线下穿长期均线
  • 默认参数:SMA20/SMA50

2. RSI均值回归策略

  • 买入信号:RSI < 30(超卖)
  • 卖出信号:RSI > 70(超买)
  • 默认参数:RSI周期14

3. 突破策略

  • 买入信号:收盘价突破N日最高价
  • 卖出信号:收盘价跌破N日最低价
  • 默认参数:20日突破

回测实现

使用Python编写回测脚本,依赖以下库:

  • yfinance - Yahoo Finance数据
  • akshare - A股数据
  • pandas - 数据处理
  • numpy - 数值计算

数据格式要求

CSV文件需包含以下列:

  • date - 日期
  • open - 开盘价
  • high - 最高价
  • low - 最低价
  • close - 收盘价
  • volume - 成交量

回测代码模板

import pandas as pd
import numpy as np

def backtest(df, strategy_func, initial_capital=100000, commission=0.001):
    """
    运行回测
    
    参数:
    - df: OHLCV数据DataFrame
    - strategy_func: 策略函数,返回买入/卖出信号
    - initial_capital: 初始资金
    - commission: 交易成本比例
    
    返回:
    - 交易日志
    - 绩效指标
    """
    df = df.copy()
    df['signal'] = strategy_func(df)
    df['position'] = df['signal'].shift(1).fillna(0)
    df['returns'] = df['close'].pct_change()
    df['strategy_returns'] = df['position'] * df['returns']
    
    # 计算交易成本
    trades = df['position'].diff().abs()
    df['strategy_returns'] -= trades * commission
    
    # 计算累计收益
    df['cumulative_returns'] = (1 + df['strategy_returns']).cumprod()
    
    # 计算绩效指标
    total_return = df['cumulative_returns'].iloc[-1] - 1
    years = (df['date'].iloc[-1] - df['date'].iloc[0]).days / 365
    cagr = (1 + total_return) ** (1/years) - 1
    
    # 最大回撤
    cummax = df['cumulative_returns'].expanding().max()
    drawdown = (df['cumulative_returns'] - cummax) / cummax
    max_drawdown = drawdown.min()
    
    # 夏普比率
    risk_free_rate = 0.02
    excess_returns = df['strategy_returns'] - risk_free_rate/252
    sharpe = np.sqrt(252) * excess_returns.mean() / excess_returns.std()
    
    # 胜率
    trade_returns = df[df['position'].diff() != 0]['strategy_returns']
    win_rate = (trade_returns > 0).sum() / len(trade_returns) if len(trade_returns) > 0 else 0
    
    return {
        'total_return': total_return,
        'cagr': cagr,
        'max_drawdown': max_drawdown,
        'sharpe_ratio': sharpe,
        'win_rate': win_rate,
        'num_trades': trades.sum()
    }

报告格式

# 策略回测报告

## 策略概述
- 策略名称:[策略名称]
- 回测区间:[开始日期] 至 [结束日期]
- 初始资金:¥[金额]

## 绩效指标
| 指标 | 数值 |
|------|------|
| 总收益率 | XX.XX% |
| 年化收益率(CAGR) | XX.XX% |
| 最大回撤 | XX.XX% |
| 夏普比率 | X.XX |
| 胜率 | XX.XX% |
| 交易次数 | XX |

## 交易日志
[列出主要交易记录]

## 分析总结
[对策略表现的评价和建议]

使用示例

用户: "帮我回测一下茅台的SMA策略"

执行步骤:

  1. 使用akshare获取茅台历史数据
  2. 计算SMA20和SMA50
  3. 生成买卖信号
  4. 运行回测
  5. 输出报告

注意事项

  • 回测结果不代表未来表现
  • 需考虑交易成本、滑点等现实因素
  • 建议进行参数敏感性分析
  • 注意数据质量和完整性