RiskGuard is an open-source, zero-dependency risk management library that enforces predefined trading discipline with configurable rules and state persistence.

Stars

64

7-day growth

No data

Forks

15

Open issues

1

License

MIT

Last updated

2026-07-10

AI repository intelligence
FR-AI / ANALYSIS

Why it is worth attention

It fills a missing open-source layer in the quantitative trading stack—enforcing hard-coded risk rules (position limits, drawdown circuit breaker, quarantine, etc.) that are often implemented ad hoc, and provides features like tamper-proof audit logs and persistent state that survive process restarts.

Who it is for

  • Quantitative traders using backtesting frameworks like backtesting.py or vectorbt who need a framework-agnostic risk layer
  • Developers building AI-driven trading agents that require a deterministic, fail-closed risk gate before executing any real order
  • Individual traders who have experienced a blow-up and want to automate discipline into code rather than relying on willpower
  • Teams needing a pluggable risk middleware compatible with Alpaca or custom execution backends

Use cases

  • Overlay risk rules on existing backtesting scripts to compare drawdown and returns with and without risk controls
  • Generate daily structured digests and run read-only stress tests for portfolio health checks
  • Set up a real-time kill-switch monitor that automatically triggers circuit breakers and liquidation when drawdown exceeds thresholds
  • Integrate with Alpaca to enforce position limits and drawdown caps on live trading accounts

Strengths

  • Zero core dependencies and composable design: install and use the core package without any extras
  • Fail-closed safety: never silently proceed with stale or guessed prices, always throws on missing data
  • Tamper-proof audit log with optional HMAC verification and chain-of-hash integrity
  • State persistence with optimistic locking prevents restart-based bypass of circuit breakers

Considerations

  • Not yet published to PyPI; currently requires git-based installation
  • Audit integrity requires an external anchor (HMAC key stored separately) and expected count for full verification
  • Does not include anomaly detection or market sentiment analysis; it strictly executes hard-coded rules as configured

README quick start

🛡️ RiskGuard

给你的交易系统,配一个 7×24、永不情绪化的风控官。

"Rule No.1:永远不要亏钱。Rule No.2:永远不要忘记第一条。" —— 沃伦·巴菲特

简体中文 · English

量化交易的五层积木——数据 → 研究 → 回测 → 风控 → 执行——数据有 OpenBB、研究有 Qlib、回测有 backtesting.py/vectorbt、执行有 Alpaca,唯独风控层没有一个成熟的开源标准件。RiskGuard 就是来补这块空白的。

风控的本质不是技术,而是提前写死的纪律。RiskGuard 把那几条能挡住绝大多数爆仓惨剧的规则,做成一个核心零依赖、可组合的库,让系统自动执行——因为等你真的亏钱上头那一刻,意志力是这世界上最靠不住的东西。

📖 目录


🔥 看它拦下一次爆仓(30 秒)

同一个"满仓做多"信号,喂给同一段 −45% 的崩盘行情。一个不设风控,一个套上 RiskGuard(单笔 10% 上限 + 15% 回撤熔断)。看最大回撤的差距:

裸奔账户🛡️ RiskGuard 账户
单笔仓位占比100%10%(自动缩单)
期末权益(起始 10 万)55,00094,377
最大回撤−45.0%−5.6%
同一段崩盘,风控把最大回撤从 45% 压到 5.6%。
纪律不是让你多赚,是让你别在最坏的一天把本金亏光。

这段数字是 examples/06_with_vs_without.py 的真实输出,是"仓位封顶"这条纪律的机械结果、可一键复现——不是收益承诺。git clone 下来自己跑一遍就知道。

但纪律不是免费的

同一档配置,换成 +200% 的单边牛市,风控账户只吃到裸奔涨幅的 10%——120,000 vs 300,000。仓位上限截断的是分布的两端:既拦暴跌,也拦暴涨。这是真实的保费,不是隐藏条款(见 examples/09)。值不值,取决于你更怕错过暴涨,还是更怕活不到下一次机会。


🤔 为什么不自己写几个 if 判断就好?

你当然可以写。但风控真正难的不是"能不能判断",而是分析质量和决策纪律——尤其在你亏钱上头、最不该动手的那一刻。

你以为够了现实会发生什么RiskGuard 怎么做
记在脑子里"别超过 10%"上头时,脑子里的规则第一个失效写进配置、系统强制,越界自动缩单
"跌多了我就手动止损"你会犹豫、舍不得、甚至加仓摊平回撤触线自动熔断,拉闸停手
回测年化 30% 很香滑点/手续费一扣,实盘常年亏模拟盘内建滑点 + 手续费,先扣摩擦再看
拿不到价就用上一个价用错价 = 敞口算错 = 风控失效拿不到价直接拒单(fail-closed)
平仓单和开仓单一视同仁熔断时把平仓也拦了,风险无法收敛减仓单永远放行
日志记个 txt能被随手改,出事查无对证哈希链留痕,verify() 一键验真伪
重启一下继续交易高水位/熔断状态全部归零,纪律形同虚设状态可持久化,重启也扛得住熔断

这不是"能不能判断"的问题,而是"纪律能不能被系统强制执行"的问题。 RiskGuard 把这七件事全部焊死进代码。


🧱 一套风控,这些能力

能力一句话
🚫 单笔仓位闸MaxPositionLimit任一标的名义敞口 ≤ 权益的 max_position_pct(默认 10%)—— 别把身家压一注
🧯 回撤熔断DrawdownCircuitBreaker回撤触及 max_drawdown_pct(默认 15%)即停新仓,减仓永远放行
🐣 新兵隔离区`Strate

Description

券商无关的开源交易风控层:仓位上限 · 回撤熔断 · 动态仓位(Kelly/波动率) · 防篡改审计 · 实时监控守护。补上量化五层积木唯一缺的一层,核心零依赖。

Related repositories

Similar projects matched by category, topics, and programming language.

lopopolo
Featured
lopopolo GitHub avatar

harness-engineering

Harness Engineering is a methodology for improving coding agent outputs by carefully crafting the environment around them—providing curated context, tools, and executable constraints that encode an organization’s nonfunctional requirements and cumulative lessons.

AI & Machine LearningAI Agents
2,390
slvDev
Featured
slvDev GitHub avatar

esp32-ai

A 28.9 million parameter language model runs on an $8 ESP32-S3 microcontroller entirely on-device, generating simple stories at about 9.5 tokens per second.

AI & Machine LearningLarge Language Models
1,960
littledivy
Featured
littledivy GitHub avatar

mimic

mimic captures traffic from any iOS or web app and automatically generates a Python client library that lets you call the app's API like a regular library.

AI & Machine Learning
1,482