PERP_MAX_POSITION_USD: Cap Your Trading Bot's Position Size to Prevent Catastrophic Loss
Building a profitable trading bot is hard enough — you don't need a catastrophic position size error wiping out your entire account. The PERP_MAX_POSITION_USD policy in WAIaaS caps your bot's maximum position size in perpetual futures, creating an automated safety net that prevents runaway trades from destroying your capital.
Why Position Limits Matter for Trading Bots
Algorithmic trading systems can execute hundreds of trades per minute. A single bug in position sizing logic, a misconfigured leverage parameter, or an unexpected market condition can lead your bot to open positions far larger than intended. Traditional trading infrastructure leaves you vulnerable — by the time you notice the problem, significant damage may already be done.
Professional trading firms solve this with sophisticated risk management systems that cost millions to build. WAIaaS brings enterprise-grade position limits to individual developers through its policy engine, automatically blocking trades that exceed your predefined risk parameters before they reach the blockchain.
Implementing Position Size Controls
WAIaaS's PERP_MAX_POSITION_USD policy integrates directly into the transaction pipeline. When your bot attempts to open or modify a position, the policy engine calculates the resulting position size and blocks execution if it exceeds your limit.
Here's how to set up position size limits for your trading bot:
curl -X POST http://localhost:3100/v1/policies \
-H 'Content-Type: application/json' \
-H 'X-Master-Password: <password>' \
-d '{
"walletId": "<wallet-uuid>",
"type": "PERP_MAX_POSITION_USD",
"rules": {
"maxPositionUsd": 10000,
"markets": ["BTC-PERP", "ETH-PERP", "SOL-PERP"]
}
}'
This policy prevents your bot from holding more than $10,000 in any single perpetual futures position across Bitcoin, Ethereum, or Solana markets. The limit applies to the notional value, not just the margin posted.
For more granular control, you can set different limits per market:
curl -X POST http://localhost:3100/v1/policies \
-H 'Content-Type: application/json' \
-H 'X-Master-Password: <password>' \
-d '{
"walletId": "<wallet-uuid>",
"type": "PERP_MAX_POSITION_USD",
"rules": {
"marketLimits": {
"BTC-PERP": 25000,
"ETH-PERP": 15000,
"SOL-PERP": 5000,
"MATIC-PERP": 2000
}
}
}'
Multi-Protocol Position Tracking
WAIaaS tracks positions across multiple perpetual futures protocols automatically. Currently supporting Hyperliquid and Drift, with the policy engine aggregating exposure to provide accurate position sizing regardless of where your bot trades.
When your bot executes a trade on Hyperliquid, WAIaaS:
curl -X POST http://127.0.0.1:3100/v1/actions/hyperliquid/place-order \
-H "Content-Type: application/json" \
-H "Authorization: Bearer wai_sess_<token>" \
-d '{
"symbol": "BTC",
"side": "long",
"size": 0.5,
"price": 45000,
"orderType": "limit"
}'
The system automatically:
- Calculates the resulting position size in USD terms
- Checks against your PERP_MAX_POSITION_USD policy
- Blocks execution if the position would exceed limits
- Updates internal position tracking for future trades
This works seamlessly across protocols — your bot can trade BTC-PERP on Hyperliquid and SOL-PERP on Drift with unified position limits enforced across both venues.
Integration with Leverage Controls
Position size limits work in conjunction with WAIaaS's PERP_MAX_LEVERAGE policy to create comprehensive risk controls. While leverage limits prevent excessive borrowing, position limits cap absolute exposure:
{
"policies": [
{
"type": "PERP_MAX_LEVERAGE",
"rules": {"maxLeverage": 5}
},
{
"type": "PERP_MAX_POSITION_USD",
"rules": {"maxPositionUsd": 10000}
}
]
}
With a 5x leverage limit and $10,000 position cap, your bot can use maximum $2,000 margin per position while maintaining controlled risk exposure. The policies compound to create multiple layers of protection.
Real-Time Position Monitoring
Your trading bot can query current positions and policy status through the REST API:
curl http://127.0.0.1:3100/v1/wallet/defi-positions \
-H "Authorization: Bearer wai_sess_<token>"
This returns detailed position data across all integrated protocols:
{
"positions": [
{
"protocol": "hyperliquid",
"market": "BTC-PERP",
"side": "long",
"size": 0.25,
"notionalUsd": 11250,
"unrealizedPnl": 156.25,
"leverage": 3.2
}
],
"totalNotionalUsd": 11250
}
Your bot can use this data to make intelligent position sizing decisions, ensuring trades stay within policy limits even as market prices fluctuate.
Handling Policy Violations
When a trade violates position limits, WAIaaS returns a clear error response:
{
"error": {
"code": "POLICY_DENIED",
"message": "Position would exceed PERP_MAX_POSITION_USD limit of $10,000",
"domain": "POLICY",
"retryable": false,
"details": {
"currentPositionUsd": 8500,
"requestedIncrease": 3000,
"resultingPositionUsd": 11500,
"maxAllowed": 10000
}
}
}
Your bot can catch these errors and implement fallback logic — perhaps splitting the trade into smaller chunks, reducing position size, or waiting for existing positions to close.
Quick Start: Protecting Your Trading Bot
Here's how to add position limits to your existing trading bot in under 5 minutes:
- Install the CLI and start WAIaaS:
npm install -g @waiaas/cli
waiaas init
waiaas start
waiaas quickset --mode mainnet
- Create your position limit policy:
curl -X POST http://localhost:3100/v1/policies \
-H 'Content-Type: application/json' \
-H 'X-Master-Password: <your-password>' \
-d '{
"walletId": "<your-wallet-id>",
"type": "PERP_MAX_POSITION_USD",
"rules": {"maxPositionUsd": 5000}
}'
- Update your bot to use WAIaaS endpoints:
// Replace direct protocol calls with WAIaaS actions
const result = await fetch('http://localhost:3100/v1/actions/hyperliquid/place-order', {
method: 'POST',
headers: {
'Authorization': `Bearer ${sessionToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
symbol: 'BTC',
side: 'long',
size: 0.1,
orderType: 'market'
})
});
- Add error handling for policy violations:
if (!result.ok) {
const error = await result.json();
if (error.code === 'POLICY_DENIED') {
console.log('Trade blocked by position limit - implementing fallback');
// Your fallback logic here
}
}
- Monitor positions in real-time:
curl http://localhost:3100/v1/wallet/defi-positions \
-H "Authorization: Bearer wai_sess_<token>"
Your trading bot now has enterprise-grade position limits protecting against catastrophic losses while maintaining full trading functionality.
Advanced Configuration
For sophisticated trading strategies, WAIaaS supports complex position limit configurations. You can set different limits by market category, time of day, or volatility conditions.
Market-specific limits allow higher exposure to liquid markets:
{
"type": "PERP_MAX_POSITION_USD",
"rules": {
"marketLimits": {
"BTC-PERP": 50000, // Major crypto - higher limit
"ETH-PERP": 30000,
"SOL-PERP": 10000, // Alt crypto - lower limit
"DOGE-PERP": 2000 // Meme token - strict limit
}
}
}
Combined with WAIaaS's time restriction policies, you can implement dynamic risk management:
{
"policies": [
{
"type": "PERP_MAX_POSITION_USD",
"rules": {"maxPositionUsd": 20000}
},
{
"type": "TIME_RESTRICTION",
"rules": {
"allowedHours": {"start": 9, "end": 16},
"timezone": "America/New_York"
}
}
]
}
This configuration allows higher position limits during US market hours when liquidity is typically better, with automatic trade blocking outside those windows.
The policy engine evaluates all active policies before allowing trade execution, creating layered protection that adapts to market conditions and your risk preferences.
Position size control is just one component of comprehensive trading bot risk management. WAIaaS's 21 policy types work together to create sophisticated guardrails around your automated trading strategies while maintaining the speed and flexibility required for profitable algorithmic trading.
Ready to protect your trading bot from catastrophic losses? Check out the complete WAIaaS setup guide and start implementing bulletproof position controls today.
What's Next
Position limits are most effective when combined with other risk management policies. WAIaaS offers 21 different policy types that work together to create comprehensive protection for your trading operations.
Get started with WAIaaS position controls at the GitHub repository or learn more about the complete platform at waiaas.ai.