Autonomous trading agents can generate impressive returns โ but without risk controls, a single bad market event can destroy weeks of profit. Purple Flea's risk management API lets you define hard limits your agent can never exceed: drawdown caps, position size limits, daily loss halts, and correlation guards โ enforced server-side, in real time.
Soft limits โ thresholds checked by your agent's own code โ fail under adverse conditions. If your agent crashes, hangs, or receives corrupted price data, its own logic cannot protect it. Purple Flea enforces risk limits server-side: they fire even if your agent is offline.
Four core controls that cover the vast majority of trading risk scenarios. Combine them to build a risk profile matching your strategy's tolerance.
Configure a complete risk profile for your trading agent in a single call, then query real-time exposure before every trade decision.
# pip install purpleflea
import purpleflea
risk = purpleflea.RiskClient(api_key="YOUR_KEY")
# Configure risk limits for a trading agent
limits = risk.set_limits(
agent_id="agent_trader_001",
max_drawdown_pct=15, # Stop if down 15% from peak
max_position_pct=20, # No single position > 20% of account
daily_loss_limit_usd=200, # Halt if daily loss exceeds $200
max_open_positions=5, # Max 5 concurrent positions
allowed_markets=["BTC-PERP", "ETH-PERP", "SOL-PERP"] # Whitelist
)
print(f"Risk profile: {limits['profile_id']}")
# Check current risk exposure before placing a trade
exposure = risk.check_exposure(agent_id="agent_trader_001")
print(f"Current drawdown: {exposure['current_drawdown_pct']:.1f}%")
print(f"Open positions: {exposure['open_position_count']}")
print(f"Safe to trade: {exposure['can_open_new_position']}")
# Register webhook for limit breach notifications
risk.set_webhook(
agent_id="agent_trader_001",
url="https://your-server.com/risk-webhook",
events=["drawdown.warning", "drawdown.breach",
"daily_loss.breach", "trading.halted"]
)
{
"agent_id": "agent_trader_001",
"can_open_new_position": true,
"current_drawdown_pct": 4.2,
"daily_pnl_usd": -147.50,
"open_position_count": 2,
"largest_position_pct": 12.3,
"correlation_exposure_pct": 55.1,
"limits_status": "within_bounds",
"checked_at": "2026-03-04T09:22:11Z"
}
When limits are approached or breached, Purple Flea fires structured webhook events โ so your agents, orchestrators, and monitoring systems can respond immediately.
{
"event": "drawdown.breach",
"agent_id": "agent_trader_001",
"timestamp": "2026-03-04T03:41:12Z",
"data": {
"peak_equity_usd": 1000.00,
"current_equity_usd": 847.50,
"drawdown_pct": 15.25,
"limit_pct": 15.00,
"action_taken": "trading_halted",
"open_positions_closed": 3
},
"resume_condition": "manual_lift_required"
}
Optimal position sizing is not arbitrary โ it can be calculated mathematically. Purple Flea integrates the Kelly Criterion directly into the order submission flow, so your agents can size positions optimally rather than guessing.
# Agent estimates 60% win rate,
# 2:1 reward-to-risk ratio
sizing = risk.kelly_size(
agent_id="agent_trader_001",
win_probability=0.60,
reward_to_risk=2.0,
kelly_fraction=0.5 # Half-Kelly for safety
)
print(f"Optimal size: {sizing['position_pct']:.1f}% of account")
print(f"USD amount: ${sizing['position_usd']:.2f}")
print(f"Ruin probability: {sizing['ruin_prob_pct']:.3f}%")
# Output:
# Optimal size: 10.0% of account
# USD amount: $100.00
# Ruin probability: 0.002%
Before deploying a risk profile to live trading, test it against historical data. Purple Flea's backtest API replays your agent's strategy through historical market conditions and reports exactly when each risk limit would have fired.
result = risk.backtest(
agent_id="agent_trader_001",
strategy_id="strat_momentum_v2",
from_date="2025-01-01",
to_date="2025-12-31",
risk_profile={
"max_drawdown_pct": 15,
"max_position_pct": 20,
"daily_loss_limit": 200
}
)
for event in result['limit_breach_events']:
print(f"{event['date']}: {event['type']}")
| Risk Profile | Max DD Hit | Halts Triggered | Annual Return |
|---|---|---|---|
| No limits | -42.3% | 0 | +87% |
| DD 30%, pos 40% | -28.1% | 2 | +71% |
| DD 15%, pos 20% | -12.4% | 5 | +58% |
| DD 10%, pos 10% | -7.2% | 9 | +34% |
Purple Flea's risk engine monitors all major dimensions of trading risk simultaneously, in real time.
Every autonomous trading agent should have hard risk limits before it goes live. Get a free API key and configure your agent's risk profile today โ it takes under 5 minutes.