gitmyhub

riskguard

Python ★ 64 updated 16d ago

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

RiskGuard is a Python library that enforces trading discipline like position caps, drawdown circuit breakers, and dynamic position sizing for algorithmic trading.

Pythonsetup: moderatecomplexity 3/5

RiskGuard is an open-source Python library that adds a risk control layer to an algorithmic trading system. The author frames it as the missing piece in the usual quant trading stack: tools already exist for data, research, backtesting, and execution, but not for enforcing hard trading discipline, so RiskGuard fills that gap with a small, dependency-free core.

The core idea is that risk rules work best when they are written into code ahead of time rather than relied on as willpower in the moment, since a trader's judgment tends to fail exactly when losses are mounting. RiskGuard enforces a cap on how much of an account's equity can go into a single position, and a drawdown circuit breaker that blocks new trades once losses cross a set threshold, while always still allowing trades that reduce an existing position. It also offers position sizing formulas, including the Kelly criterion and a volatility-targeting method, so bet size is calculated by formula rather than guessed emotionally, and it automatically avoids sizing a trade at all when the expected edge is negative.

Beyond static rules, a background RiskMonitor thread can watch an account continuously and automatically cut positions if a drawdown limit is breached while no one is actively watching. Every decision, breach, and trade can be recorded to a tamper resistant audit log using a hash chain, with an optional cryptographic key so the log's integrity can be verified later. Account state, including drawdown and circuit breaker status, can be saved to disk so that restarting the trading process does not accidentally bypass a triggered circuit breaker.

The library connects to a paper trading simulator with configurable slippage and fees, or to a real broker such as Alpaca, through a pluggable broker interface. It is not yet published to PyPI, so installation is currently done directly from the GitHub repository with pip. The project is written in Python 3.10 or newer, has continuous integration set up, and is released under the MIT license. Documentation is provided in both Chinese and English.

Where it fits