# AI Crypto Trading Risk Management: Position Sizing, TP/SL & Kill Switches

> Never delegate risk math to free-form LLM intuition. Compute position size deterministically inside `execute_code` (`qty = (equity * riskPct) / |entry - stopLoss|`), cap leverage (`eterna.setLeverage`), attach `stopLoss` and `takeProfit` directly on every `eterna.placeOrder`, and limit capital at the isolated sub-account boundary.

- Canonical URL: https://ai.eterna.exchange/guides/ai-trading-risk-management-guardrails
- Last updated: November 23, 2026
- Author: Eterna Engineering (Security & Agent Architecture Team)
- MCP endpoint: https://mcp.eterna.exchange/mcp

LLMs are strong at synthesizing market structure and terrible at mental arithmetic under stress. Moving all sizing, drawdown checks, and bracket orders into deterministic TypeScript inside `execute_code` turns a fragile prompt bot into a disciplined trading system.

## Key Facts

| Metric | Value | Notes |
|--------|-------|-------|
| Core Sizing Formula | (Equity * Risk%) / StopDist | Computed deterministically in TypeScript |
| Mandatory Brackets | stopLoss + takeProfit | Attached atomically in placeOrder() |
| Account Boundary | Isolated Sub-Account | Max loss capped at funded balance |
| Emergency Exit | cancelAllOrders + closePosition | Single-call panic flatten |

## 1. Deterministic 1%-Risk Position Sizing Script

Copy this reusable TypeScript wrapper into your agent instructions so every order validates balance, open exposure, leverage, and stop distance before execution.

### safe-order-guard.ts (execute_code)

```typescript
const symbol = "BTCUSDT";
const side = "Buy" as const;
const riskPct = 0.01; // 1% of account equity
const maxLeverage = 5;
const stopDistancePct = 0.015; // 1.5% stop
const rewardMultiple = 2.0; // 2R take profit

const [balance, positions, tickers] = await Promise.all([
  eterna.getBalance(),
  eterna.getPositions(),
  eterna.getTickers({ symbol }),
]);

if (positions.some((p: any) => p.symbol === symbol && Number(p.size) > 0)) {
  return { skipped: true, reason: "Position already open on " + symbol };
}

const equity = Number(balance.totalEquity ?? balance.availableBalance ?? 0);
const lastPrice = Number(tickers[0].lastPrice);
const riskUsd = equity * riskPct;
const stopPrice = lastPrice * (1 - stopDistancePct);
const takeProfitPrice = lastPrice * (1 + stopDistancePct * rewardMultiple);
const stopDistUsd = Math.abs(lastPrice - stopPrice);
const rawQty = riskUsd / stopDistUsd;

return {
  equity,
  lastPrice,
  riskUsd: Number(riskUsd.toFixed(2)),
  suggestedQty: Number(rawQty.toFixed(4)),
  stopLoss: Number(stopPrice.toFixed(2)),
  takeProfit: Number(takeProfitPrice.toFixed(2)),
  maxLeverage,
};
```

**Returns:** Verified sizing plan with exact riskUsd, suggestedQty, stopLoss, and takeProfit before submitting placeOrder.

## 2. The 5 Non-Negotiable Agent Risk Rules

- Fund the agent sub-account only with capital you are prepared to risk; never keep long-term cold storage in an active bot account.
- Enforce a daily drawdown circuit breaker: if unrealized + realized PnL drops below -3% on the day, refuse new opens.
- Cap concurrent open positions (e.g., max 3 correlated crypto pairs) so a BTC flush cannot liquidate 10 altcoin longs simultaneously.
- Always read `eterna.getInstruments({ symbol })` to respect `minOrderQty` and `qtyStep` before placing live orders.
- Keep a single-prompt kill switch ready: `cancelAllOrders()` followed by `closePosition()` for every open symbol.

## Frequently Asked Questions

### How do I build an instant emergency kill switch on Eterna MCP?

Run one `execute_code` call that invokes `await eterna.cancelAllOrders()`, reads `await eterna.getPositions()`, and loops `await eterna.closePosition({ symbol })` across every non-zero position.

### Can prompt injection trick my agent into over-leveraging?

Never feed untrusted external web text or social posts directly into an order-placing prompt without deterministic code limits on leverage, max notional, and allowlisted symbols.

## References & Primary Sources

- [Eterna SDK Reference](https://ai.eterna.exchange/sdk): Exact parameters for setLeverage, placeOrder, setTradingStop, and closePosition.

## Related Guides

- [Eterna MCP SDK Reference: All 30+ eterna.* Methods](https://ai.eterna.exchange/sdk)
- [RSI Mean-Reversion AI Crypto Trading Bot (Runnable MCP Script)](https://ai.eterna.exchange/strategies/rsi-mean-reversion-agent)
- [Running Multiple AI Trading Agents Safely with Isolated Sub-Accounts](https://ai.eterna.exchange/guides/multi-agent-crypto-portfolio-isolation)
- [OAuth 2.1 & Device Flow for Automated Crypto Trading Agents](https://ai.eterna.exchange/guides/mcp-oauth-authentication-for-agents)
