ClawKit Logo
ClawKitReliability Toolkit
Back to Registry
Official Verified

rqalpha

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

skill-install — Terminal

Install via CLI (Recommended)

clawhub install openclaw/skills/skills/coderwpf/rqalpha
Or

RQAlpha(米筐开源回测框架)

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        # 日收益率

Metadata

Author@coderwpf
Stars3409
Views1
Updated2026-03-25
View Author Profile
AI Skill Finder

Not sure this is the right skill?

Describe what you want to build — we'll match you to the best skill from 16,000+ options.

Find the right skill
Add to Configuration

Paste this into your clawhub.json to enable this plugin.

{
  "plugins": {
    "official-coderwpf-rqalpha": {
      "enabled": true,
      "auto_update": true
    }
  }
}
Safety NoteClawKit audits metadata but not runtime behavior. Use with caution.