Skip to main content

What you’ll build

In this tutorial, you will add one risk-control layer around an existing DerivaDEX strategy so it:
  1. tracks one explicit risk budget,
  2. watches live mark-price and account updates,
  3. blocks new entries when risk limits are hit,
  4. forces one controlled de-risking path before liquidation pressure takes over.

Prerequisites

Step 1: Start with one existing strategy

Reuse a strategy that already:
  1. receives mark-price updates,
  2. submits signed and encrypted orders,
  3. records order outcomes.
Do not change the signal logic first. This lesson changes the risk wrapper around the strategy, not the trading thesis.

Step 2: Define one explicit risk budget

Choose a small, fixed rule set for the first version:
  • one maximum position size,
  • one maximum gross notional cap,
  • one minimum margin-buffer target above maintenance,
  • one session loss limit that stops new entries.
Keep the thresholds simple and deterministic. You are building the discipline layer, not an adaptive optimizer.

Step 3: Track the live risk state

Consume the same public market and account signals your strategy already needs:
  1. MARK_PRICE for the active symbol,
  2. order and account updates for fills and balance effects,
  3. current strategy state from the REST bootstrap or local position record.
For each update, recalculate:
  1. current direction and size,
  2. current notional exposure,
  3. estimated remaining margin buffer,
  4. whether the strategy is still allowed to open more risk.

Step 4: Block new entries when the budget is exhausted

Before submitting any new entry:
  1. check the maximum size rule,
  2. check the maximum notional rule,
  3. check the minimum margin-buffer rule,
  4. check the session loss-stop rule.
If any one of those fails, skip the new trade and record the reason instead of trying to force the signal through.

Step 5: Add one controlled de-risk action

When risk is too high:
  1. cancel stale working orders that would increase exposure,
  2. submit one smaller reducing order or exit action,
  3. keep the de-risk order inside normal product increments and price protections,
  4. wait for the lifecycle result before sending another reduction.
The first version should prefer one clean reduction over many overlapping emergency actions.

Step 6: Separate “stop trading” from “panic exit”

Use two different responses:
  • stop trading when the risk budget is full but the position is still orderly,
  • panic exit when the margin buffer is collapsing toward the liquidation zone.
For this lesson, make the panic path simple:
  1. stop all new entries,
  2. cancel risk-increasing orders,
  3. send one reducing action,
  4. monitor the resulting account updates closely.

Step 7: Run one controlled stress pass

Drive the strategy through one session where:
  1. the normal signal tries to add exposure,
  2. the risk budget blocks the extra trade,
  3. the market moves enough to shrink the margin buffer,
  4. the de-risk action fires,
  5. the resulting lifecycle events confirm the risk layer worked.

Step 8: Finish when the strategy stands down cleanly

This tutorial is complete when you observe one full cycle:
  1. normal trading begins,
  2. the risk budget becomes binding,
  3. new entries stop,
  4. one reducing action executes,
  5. the strategy remains out of liquidation danger.

Next routes

Last modified on April 12, 2026