首页龙虾技能列表 › rqalpha — 技能工具

🍚 rqalpha — 技能工具

v1.0.2

RQAlpha 米筐开源事件驱动回测框架 - 支持A股和期货,模块化架构,可自由扩展。

1· 276·0 当前·0 累计
by @coderwpf·MIT-0
下载技能包
License
MIT-0
最后更新
2026/3/23
安全扫描
VirusTotal
无害
查看报告
OpenClaw
安全
high confidence
The skill is internally consistent with a backtesting framework: it only requires python3, documents installing rqalpha via pip, and the instructions/files match that purpose.
评估建议
This package appears to be a straightforward wrapper/documentation for the RQAlpha backtesting library. Before installing, consider: (1) install and run it inside a virtualenv or container to isolate dependencies; (2) verify you trust the pip package source and version (rqalpha>=5.0.0) and review its upstream repository if you need assurance; (3) note that rqalpha download-bundle will fetch market data from external sources — ensure you are comfortable with network access and data provenance; (4...
详细分析 ▾
用途与能力
Name/description, README, SKILL.md and requirements.txt all describe an RQAlpha backtesting skill. Required binaries (python3) and listed Python package dependencies are appropriate for the stated purpose.
指令范围
SKILL.md instructs installing rqalpha via pip, downloading data bundles, running backtests, and using the library API. It does not direct reading unrelated files, collecting environment secrets, or sending data to unexpected endpoints.
安装机制
There is no automatic install spec (instruction-only). The docs instruct the user to pip install rqalpha and to run rqalpha download-bundle — standard for a Python library. requirements.txt lists expected packages.
凭证需求
The skill requests no environment variables, credentials, or config paths. The dependencies are typical Python packages required for a backtesting toolkit.
持久化与权限
always is false and the skill does not request persistent or elevated agent privileges, nor does it modify other skills or system-wide agent settings.
安全有层次,运行前请审查代码。

License

MIT-0

可自由使用、修改和再分发,无需署名。

运行时依赖

无特殊依赖

版本

latestv1.0.22026/3/13

- Added example directory demo_project with sample files. - Included demo_project/README.md for documentation of the example project. - Provided demo_project/demo.py as a code example for quickstart or demonstration purposes.

● 无害

安装命令 点击复制

官方npx clawhub@latest install rqalpha
镜像加速npx clawhub@latest install rqalpha --registry https://cn.clawhub-mirror.com

技能文档

RQAlpha米筐科技 开发的开源事件驱动回测框架。提供A股和期货市场的策略开发、回测和模拟交易完整解决方案。高度模块化,支持插件(Mod)系统扩展。

Docs: https://rqalpha.readthedocs.io/

安装

pip install rqalpha

# 下载内置数据包(A股日线数据) rqalpha download-bundle

策略结构

def init(context):
    """策略启动时调用一次 — 设置订阅和参数"""
    context.stock = '000001.XSHE'
    context.fired = False

def handle_bar(context, bar_dict): """每根K线调用 — 主要交易逻辑""" if not context.fired: order_shares(context.stock, 1000) context.fired = True

def before_trading(context): """每个交易日开盘前调用""" pass

def after_trading(context): """每个交易日收盘后调用""" pass

运行回测

命令行

rqalpha run \
    -f strategy.py \
    -s 2024-01-01 \
    -e 2024-06-30 \
    --account stock 100000 \
    --benchmark 000300.XSHG \
    --plot

Python API

from rqalpha.api import 
from rqalpha import run_func

config = { "base": { "start_date": "2024-01-01", "end_date": "2024-06-30", "accounts": {"stock": 100000}, "benchmark": "000300.XSHG", "frequency": "1d", }, "extra": { "log_level": "warning", }, "mod": { "sys_analyser": {"enabled": True, "plot": True}, }, }

result = run_func(init=init, handle_bar=handle_bar, config=config) print(result)


代码格式

市场后缀示例
上海A股.XSHG600000.XSHG(浦发银行)
深圳A股.XSHE000001.XSHE(平安银行)
指数.XSHG/.XSHE000300.XSHG(沪深300)
期货.XSGE/.XDCE/.XZCE/.CCFXIF2401.CCFX(沪深300期货)

下单函数

# 按股数买卖
order_shares('000001.XSHE', 1000)       # 买入1000股
order_shares('000001.XSHE', -500)       # 卖出500股

# 按手买入(1手=100股) order_lots('000001.XSHE', 10) # 买入10手(1000股)

# 按金额买入 order_value('000001.XSHE', 50000) # 买入5万元

# 按组合比例买入 order_percent('000001.XSHE', 0.5) # 买入组合值50%的仓位

# 目标仓位 order_target_value('000001.XSHE', 100000) # 调整到10万元 order_target_percent('000001.XSHE', 0.3) # 调整到组合的30%

# 撤单 cancel_order(order_id)

数据查询函数

def handle_bar(context, bar_dict):
    # 当前K线数据
    bar = bar_dict['000001.XSHE']
    price = bar.close
    volume = bar.volume
    dt = bar.datetime

# 历史数据(返回DataFrame) prices = history_bars('000001.XSHE', bar_count=20, frequency='1d', fields=['close', 'volume', 'open', 'high', 'low'])

# 检查股票是否可交易 tradable = is_valid_price(bar.close)

# 检查是否停牌 suspended = is_suspended('000001.XSHE')

投资组合与持仓

def handle_bar(context, bar_dict):
    # 组合信息
    cash = context.portfolio.cash                    # 可用资金
    total = context.portfolio.total_value            # 总资产
    market_value = context.portfolio.market_value    # 持仓市值
    pnl = context.portfolio.pnl                      # 总盈亏
    returns = context.portfolio.daily_returns        # 日收益率

# 持仓信息 positions = context.portfolio.positions for stock, pos in positions.items(): print(f'{stock}: quantity={pos.quantity}, ' f'sellable={pos.sellable}, ' f'avg_price={pos.avg_price:.2f}, ' f'market_value={pos.market_value:.2f}, ' f'pnl={pos.pnl:.2f}')

定时调度

from rqalpha.api import 

def init(context): # 每个交易日指定时间运行函数 scheduler.run_daily(rebalance, time_rule=market_open(minute=5)) # 每周运行(每周一) scheduler.run_weekly(weekly_task, tradingday=1, time_rule=market_open(minute=5)) # 每月运行(首个交易日) scheduler.run_monthly(monthly_task, tradingday=1, time_rule=market_open(minute=5))

def rebalance(context, bar_dict): pass


Mod系统(插件)

RQAlpha的模块化架构允许通过Mod扩展功能:

config = {
    "mod": {
        "sys_analyser": {
            "enabled": True,
            "plot": True,
            "benchmark": "000300.XSHG",
        },
        "sys_simulation": {
            "enabled": True,
            "matching_type": "current_bar",    # 撮合方式:current_bar或next_bar
            "slippage": 0.01,                  # 滑点(元)
        },
        "sys_transaction_cost": {
            "enabled": True,
            "commission_rate": 0.0003,         # 手续费率
            "tax_rate": 0.001,                 # 印花税(仅卖出)
            "min_commission": 5,               # 最低手续费
        },
    },
}

可用内置Mod

Mod说明
sys_analyser绩效分析和图表绘制
sys_simulation撮合模拟
sys_transaction_cost手续费和税费计算
sys_accounts账户管理
sys_benchmark基准追踪
sys_progress进度条显示
sys_risk风险管理检查

进阶示例

双均线交叉策略

import numpy as np
from rqalpha.api import 

def init(context): context.stock = '600000.XSHG' context.fast = 5 context.slow = 20 scheduler.run_daily(trade_logic, time_rule=market_open(minute=5))

def trade_logic(context, bar_dict): prices = history_bars(context.stock, context.slow + 1, '1d', fields=['close']) if len(prices) < context.slow: return

closes = prices['close'] fast_ma = np.mean(closes[-context.fast:]) slow_ma = np.mean(closes[-context.slow:])

pos = context.portfolio.positions.get(context.stock) has_position = pos is not None and pos.quantity > 0

if fast_ma > slow_ma and not has_position: order_target_percent(context.stock, 0.9) logger.info(f'BUY: fast_ma={fast_ma:.2f} > slow_ma={slow_ma:.2f}') elif fast_ma < slow_ma and has_position: order_target_percent(context.stock, 0) logger.info(f'SELL: fast_ma={fast_ma:.2f} < slow_ma={slow_ma:.2f}')

def handle_bar(context, bar_dict): pass

多股等权重调仓

from rqalpha.api import 

def init(context): context.stocks = ['600000.XSHG', '000001.XSHE', '601318.XSHG', '600036.XSHG', '000858.XSHE'] scheduler.run_monthly(rebalance, tradingday=1, time_rule=market_open(minute=30))

def rebalance(context, bar_dict): # 卖出不在目标列表中的股票 for stock in list(context.portfolio.positions.keys()): if stock not in context.stocks: order_target_percent(stock, 0)

# 等权分配 weight = 0.95 / len(context.stocks) for stock in context.stocks: if not is_suspended(stock): order_target_percent(stock, weight) logger.info(f'Rebalance: {stock} -> {weight:.1%}')

def handle_bar(context, bar_dict): pass


使用技巧

  • RQAlpha是纯本地框架,无云端依赖,适合离线研究。
  • 使用 rqalpha download-bundle 获取免费内置A股日线数据。
  • Mod系统允许插入自定义数据源、券商接口和风控模块。
  • 实盘交易可通过 rqalpha-mod-vnpy 连接vn.py的券商网关。
  • 支持日线和分钟级回测。
  • Docs: https://rqalpha.readthedocs.io/

常见错误处理

错误原因解决方法
Bundle not found未下载数据包运行 rqalpha download-bundle
Insufficient cash可用资金不足检查 context.portfolio.cash
Order Creation Failed: suspended股票停牌is_suspended() 提前检查
No data for instrument股票代码错误检查代码格式(如 .XSHG / .XSHE

绩效分析输出

运行回测后,sys_analyser Mod会输出以下指标:

指标说明
total_returns总收益率
annualized_returns年化收益率
benchmark_total_returns基准总收益率
alphaAlpha值
betaBeta值
sharpe夏普比率
sortinoSortino比率
max_drawdown最大回撤
tracking_error跟踪误差
information_ratio信息比率
volatility波动率

进阶示例:RSI均值回归策略

import numpy as np
from rqalpha.api import 

def init(context): context.stock = '000001.XSHE' context.rsi_period = 14 context.oversold = 30 context.overbought = 70

def handle_bar(context, bar_dict): prices = history_bars(context.stock, context.rsi_period + 2, '1d', fields=['close']) if len(prices) < context.rsi_period + 1: return

closes = prices['close'] deltas = np.diff(closes) gains = np.where(deltas > 0, deltas, 0) losses = np.where(deltas < 0, -deltas, 0) avg_gain = np.mean(gains[-context.rsi_period:]) avg_loss = np.mean(losses[-context.rsi_period:])

if avg_loss == 0: rsi = 100 else: rsi = 100 - 100 / (1 + avg_gain / avg_loss)

pos = context.portfolio.positions.get(context.stock) has_pos = pos is not None and pos.quantity > 0

if rsi < context.oversold and not has_pos: order_target_percent(context.stock, 0.9) logger.info(f'RSI={rsi:.1f} 超卖买入') elif rsi > context.overbought and has_pos: order_target_percent(context.stock, 0) logger.info(f'RSI={rsi:.1f} 超买卖出')

进阶示例:止损止盈策略

from rqalpha.api import 
import numpy as np

def init(context): context.stock = '600519.XSHG' context.entry_price = 0 context.stop_loss = 0.05 context.take_profit = 0.15 scheduler.run_daily(trade, time_rule=market_open(minute=5))

def trade(context, bar_dict): bar = bar_dict[context.stock] price = bar.close prices = history_bars(context.stock, 21, '1d', fields=['close']) ma20 = np.mean(prices['close'][-20:])

pos = context.portfolio.positions.get(context.stock) has_pos = pos is not None and pos.quantity > 0

if not has_pos: if price > ma20: order_target_percent(context.stock, 0.9) context.entry_price = price logger.info(f'买入: price={price:.2f}, ma20={ma20:.2f}') else: if context.entry_price > 0: pnl = (price - context.entry_price) / context.entry_price if pnl <= -context.stop_loss: order_target_percent(context.stock, 0) logger.info(f'止损: pnl={pnl:.2%}') context.entry_price = 0 elif pnl >= context.take_profit: order_target_percent(context.stock, 0) logger.info(f'止盈: pnl={pnl:.2%}') context.entry_price = 0

def handle_bar(context, bar_dict): pass

进阶示例:期货双均线CTA策略

import numpy as np
from rqalpha.api import 

def init(context): context.symbol = 'IF2401.CCFX' context.fast = 5 context.slow = 20

def handle_bar(context, bar_dict): prices = history_bars(context.symbol, context.slow + 1, '1d', fields=['close']) if len(prices) < context.slow: return

closes = prices['close'] fast_ma = np.mean(closes[-context.fast:]) slow_ma = np.mean(closes[-context.slow:]) prev_fast = np.mean(closes[-context.fast-1:-1]) prev_slow = np.mean(closes[-context.slow-1:-1])

pos = context.portfolio.positions.get(context.symbol) long_qty = pos.buy_quantity if pos else 0

if prev_fast <= prev_slow and fast_ma > slow_ma and long_qty == 0: buy_open(context.symbol, 1) logger.info(f'开多: fast_ma={fast_ma:.2f} > slow_ma={slow_ma:.2f}') elif prev_fast >= prev_slow and fast_ma < slow_ma and long_qty > 0: sell_close(context.symbol, long_qty) logger.info(f'平多: fast_ma={fast_ma:.2f} < slow_ma={slow_ma:.2f}')



🤖 AI Agent 高阶使用指南

对于 AI Agent,在使用该量化/数据工具时应遵循以下高阶策略和最佳实践,以确保任务的高效完成:

1. 数据校验与错误处理

在获取数据或执行操作后,AI 应当主动检查返回的结果格式是否符合预期,以及是否存在缺失值(NaN)或空数据。 示例策略:在通过 API 获取数据框(DataFrame)后,使用 if df.empty: 进行校验;捕获 Exception 以防网络或接口错误导致进程崩溃。

2. 多步组合分析

AI 经常需要进行宏观经济分析或跨市场对比。应善于将当前接口与其他数据源或工具组合使用。 示例策略:先获取板块或指数的宏观数据,再筛选成分股,最后对具体标的进行深入的财务或技术面分析,形成完整的决策链条。

3. 构建动态监控与日志

对于交易和策略类任务,AI 可以定期拉取数据并建立监控机制。
示例策略:使用循环或定时任务检查特定标的的异动(如涨跌停、放量),并在发现满足条件的信号时输出结构化日志或触发预警。


社区与支持

大佬量化 维护 — 量化交易教学与策略研发团队。

微信客服: bossquant1 · Bilibili · 搜索 大佬量化 — 微信公众号 / Bilibili / 抖音

数据来源:ClawHub ↗ · 中文优化:龙虾技能库
OpenClaw 技能定制 / 插件定制 / 私有工作流定制

免费技能或插件可能存在安全风险,如需更匹配、更安全的方案,建议联系付费定制

了解定制服务