Professional risk management system for quantitative trading.
bash
pip install pandas numpy scipy plotly dash
python
from quant_risk import RiskDashboard
dashboard = RiskDashboard(
initial_capital=1000000,
var_confidence=0.95,
max_position_pct=0.15,
max_drawdown_pct=0.20
)
python
dashboard.add_position(
symbol='600519',
shares=1000,
entry_price=1800.0,
current_price=1850.0
)
dashboard.add_position(
symbol='000858',
shares=5000,
entry_price=45.0,
current_price=48.0
)
python
metrics = dashboard.get_risk_metrics()
print(f"VaR (95%): {metrics['var_95']:,.2f}")
print(f"CVaR (95%): {metrics['cvar_95']:,.2f}")
print(f"Sharpe Ratio: {metrics['sharpe_ratio']:.2f}")
print(f"Max Drawdown: {metrics['max_drawdown']:.2%}")
print(f"Total Exposure: {metrics['total_exposure']:,.0f}")
python
scenarios = {
'2008 Crash': -0.50,
'2020 Covid': -0.30,
'Rate Hike': -0.15,
'Custom': -0.25
}
results = dashboard.stress_test(scenarios)
for name, result in results.items():
print(f"{name}: P&L = {result['pnl']:,.2f}")
python
dashboard.start_dashboard(port=8050)
# Open http://localhost:8050
MethodDescriptionadd_position(symbol, shares, entry, current)Add positionremove_position(symbol)Close positionupdate_price(symbol, price)Update market priceget_positions()Get all positionsget_risk_metrics()Calculate risk metrics
MethodDescriptioncalculate_var(method='historical')Calculate VaRcalculate_cvar()Calculate CVaRstress_test(scenarios)Run stress testsfactor_exposure()Calculate factor exposuresector_allocation()Get sector breakdown
MethodDescriptionadd_alert(condition, message)Create alertget_alerts()Get active alertsclear_alerts()Clear alerts
MethodDescriptiongenerate_report(format='pdf')Generate reportget_daily_summary()Daily summary
python
limits = {
'max_position_pct': 0.15, # 15% per position
'max_sector_pct': 0.30, # 30% per sector
'max_leverage': 1.5, # 1.5x leverage
'max_drawdown': 0.20, # 20% stop loss
'max_var_pct': 0.05, # 5% VaR limit
}
python
alerts = {
'drawdown_warning': 0.10, # 10% drawdown warning
'drawdown_critical': 0.15, # 15% critical
'var_warning': 0.03, # 3% VaR warning
'volatility_spike': 2.0, # 2x normal volatility
}
python
dashboard.start_dashboard()
# Features:
# - Real-time position table
# - P&L charts
# - Risk metrics gauges
# - Sector pie chart
# - Drawdown curve
# - Factor exposure bar chart
python
# P&L Chart
chart = dashboard.plot_pnl_history()
# Risk Decomposition
chart = dashboard.plot_risk_attribution()
# Scenario Comparison
chart = dashboard.plot_scenarios()
python
# From trading system
import asyncio
async def update_positions():
while True:
positions = await trading_system.get_positions()
for pos in positions:
dashboard.update_price(pos.symbol, pos.current_price)
await asyncio.sleep(60) # Update every minute
asyncio.run(update_positions())
python
# Send alerts to Slack/WeChat
def on_alert(alert):
send_webhook(
url=os.getenv('ALERT_WEBHOOK'),
message=f"Risk Alert: {alert['message']}"
)
dashboard.set_alert_callback(on_alert)