Risk & Safety Playbook

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

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.

Updated: By Eterna Engineering (Security & Agent Architecture Team)Read as Markdown
Direct Answer

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.

Core Sizing Formula(Equity * Risk%) / StopDistComputed deterministically in TypeScript
Mandatory BracketsstopLoss + takeProfitAttached atomically in placeOrder()
Account BoundaryIsolated Sub-AccountMax loss capped at funded balance
Emergency ExitcancelAllOrders + closePositionSingle-call panic flatten
https://mcp.eterna.exchange/mcp

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)
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.
FAQ

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.
Primary Sources

References & verification links

  • Eterna SDK ReferenceExact parameters for setLeverage, placeOrder, setTradingStop, and closePosition.
Start Trading via MCP

Connect your AI agent in 30 seconds

One remote MCP URL. 800+ USDT perpetuals, 380+ spot pairs, 0.014% maker / 0.035% taker, no KYC.

View on GitHub