
Rqalpha
Install this when you need copy-paste RQAlpha Python patterns to backtest A-share strategies before risking real capital.
Overview
rqalpha is an agent skill for the Validate phase that provides RQAlpha Python templates for scheduled backtest strategies including dual moving-average entries and multi-stock rebalancing.
Install
npx skills add https://github.com/lzwme/finance-quant-skills --skill rqalphaWhat is this skill?
- Dual moving-average crossover with scheduler.run_daily at market_open plus order_target_percent sizing
- Monthly equal-weight rebalance across a fixed multi-stock universe with suspension checks
- Position-aware entries and exits using history_bars closes and portfolio quantity checks
- Two strategy patterns: single-stock MA cross and multi-name monthly rebalance templates
- 2 documented strategy patterns: dual MA crossover and monthly equal-weight rebalance
- Examples use scheduler.run_daily and scheduler.run_monthly with market_open timing rules
Adoption & trust: 1 installs on skills.sh; 84 GitHub stars; 2/3 security scanners passed (skills.sh audits).
What problem does it solve?
You have a systematic trading idea but keep stumbling on RQAlpha schedulers, history_bars, and order_target_percent when turning it into a backtest.
Who is it for?
Solo quant developers prototyping China A-share rules in RQAlpha who want scheduler-driven MA or rebalance starters their agent can extend.
Skip if: Builders who do not use RQAlpha, need only live brokerage automation with no backtest, or want generic pandas signals without the RQAlpha API.
When should I use this skill?
When implementing or extending RQAlpha backtest strategies that need scheduled trade logic, history_bars signals, or order_target_percent rebalancing.
What do I get? / Deliverables
You get runnable init and scheduled trade_logic modules with position checks and target weights ready to run inside RQAlpha backtests.
- RQAlpha strategy module with init context and scheduler hooks
- Scheduled trade_logic or rebalance functions using order_target_percent
- Backtest-ready examples for single-name MA cross and multi-stock rebalance
Recommended Skills
Journey fit
Canonical shelf is Validate because the README centers on backtest strategy prototypes (signals, scheduling, rebalance) rather than production brokerage ops or distribution. Prototype fits scheduled dual-MA and monthly rebalance examples that prove a rule set in simulation before you ship or trade live.
How it compares
Use as RQAlpha-specific strategy templates instead of ad-hoc chat snippets that omit scheduler and order_target_percent semantics.
Common Questions / FAQ
Who is rqalpha for?
rqalpha is for solo and indie builders and small teams who backtest systematic strategies in RQAlpha on Python, especially A-share tickers, and want agent-assisted strategy scaffolding.
When should I use rqalpha?
Use rqalpha during Validate when you are prototyping backtests: wiring dual moving-average daily entries, monthly equal-weight rebalance, or teaching your agent the correct RQAlpha init and scheduler patterns before you commit to live trading or a full build.
Is rqalpha safe to install?
Treat it like any third-party agent skill: review the Security Audits panel on this Prism catalog page and inspect the skill package in your repo before running backtests or granting shell and filesystem access.
SKILL.md
READMESKILL.md - Rqalpha
# RQAlpha 进阶策略示例 ## 双均线交叉策略 ```python 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) elif fast_ma < slow_ma and has_position: order_target_percent(context.stock, 0) def handle_bar(context, bar_dict): pass ``` ## 多股等权重调仓 ```python 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) def handle_bar(context, bar_dict): pass ``` ## RSI均值回归策略 ```python 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) elif rsi > context.overbought and has_pos: order_target_percent(context.stock, 0) ``` ## 止损止盈策略 ```python 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 else: if context.entry_price > 0: pnl = (price - context.entry_price) / context.entry_price if pnl <= -context.stop_loss or pnl >= context.take_profit: order_target_percent(context.stock, 0) context.entry_price = 0 def handle_bar(context, bar_dict): pass ``` ## 期货双均线CTA策略 ```python 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