# Automated Trailing Stop & Partial Take-Profit AI Agent on Eterna MCP

> Run a lightweight portfolio-guardian agent every 15 minutes via Eterna CLI or n8n: read all open positions with `eterna.getPositions()`, move stops to breakeven once unrealized PnL exceeds +1R, and ratchet stops behind the 1h EMA 20 with `eterna.setTradingStop()`.

- Canonical URL: https://ai.eterna.exchange/strategies/trailing-stop-and-profit-lock-agent
- Last updated: November 23, 2026
- Author: Eterna Quant & AI Team (Agent Strategy Playbooks)
- MCP endpoint: https://mcp.eterna.exchange/mcp

Even manual traders lose edge by forgetting to trail stops overnight. An Eterna MCP guardian agent manages exits mechanically across every open crypto and xStocks position 24/7.

## Key Facts

| Metric | Value | Notes |
|--------|-------|-------|
| Primary SDK Methods | getPositions + setTradingStop | Zero new entry risk |
| Cadence | Every 15m or 1h | Ideal for CLI cron or n8n |
| Rule 1 | Never widen stops | Monotonic stop tightening only |
| Coverage | Crypto + 24/7 xStocks | Protects weekend equity moves |

## 1. Monotonic Trailing Stop Guard (`execute_code`)

### trailing-stop-guard.ts

```typescript
const positions = await eterna.getPositions();
const actions = [];

for (const pos of positions) {
  const ema20 = await eterna.getEma({
    symbol: pos.symbol,
    interval: "60",
    period: 20,
  });
  const entry = Number(pos.avgPrice);
  const mark = Number(pos.markPrice);
  const currentSl = Number(pos.stopLoss || 0);
  const isLong = pos.side === "Buy";

  const candidateSl = isLong
    ? Math.max(entry, ema20.value * 0.995)
    : Math.min(entry, ema20.value * 1.005);

  const shouldTighten = isLong
    ? mark > entry * 1.015 && (currentSl === 0 || candidateSl > currentSl)
    : mark < entry * 0.985 && (currentSl === 0 || candidateSl < currentSl);

  if (shouldTighten) {
    actions.push({
      symbol: pos.symbol,
      side: pos.side,
      currentSl,
      proposedSl: Number(candidateSl.toFixed(4)),
    });
  }
}

return { openCount: positions.length, actions };
```

## Frequently Asked Questions

### Can this agent manage trades that I opened manually in the Eterna web app?

If opened in the same account or sub-account, yes. Keeping a dedicated read-and-manage guardian script lets humans pick entries while the AI enforces disciplined exits.

## References & Primary Sources

- [Risk Management Guardrails](https://ai.eterna.exchange/guides/ai-trading-risk-management-guardrails): Core stop-loss and kill-switch patterns.

## Related Guides

- [AI Crypto Trading Risk Management: Position Sizing, TP/SL & Kill Switches](https://ai.eterna.exchange/guides/ai-trading-risk-management-guardrails)
- [Eterna CLI Guide: Terminal TypeScript Strategies with `@eterna-hybrid-exchange/cli`](https://ai.eterna.exchange/guides/eterna-cli-trading-automation)
- [Eterna MCP SDK Reference: All 30+ eterna.* Methods](https://ai.eterna.exchange/sdk)
