What you’ll build
In this tutorial, you will add one risk-control layer around an existing DerivaDEX strategy so it:- tracks one explicit risk budget,
- watches live mark-price and account updates,
- blocks new entries when risk limits are hit,
- forces one controlled de-risking path before liquidation pressure takes over.
Prerequisites
- A working strategy skeleton from Building Your First Trading Bot.
- One signal-driven lesson already completed, such as Implementing a Moving Average Crossover Strategy or Building a Mean Reversion Bot.
- The lifecycle and reconciliation posture from How to Track an Order Through Its Lifecycle.
Step 1: Start with one existing strategy
Reuse a strategy that already:- receives mark-price updates,
- submits signed and encrypted orders,
- records order outcomes.
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.
Step 3: Track the live risk state
Consume the same public market and account signals your strategy already needs:MARK_PRICEfor the active symbol,- order and account updates for fills and balance effects,
- current strategy state from the REST bootstrap or local position record.
- current direction and size,
- current notional exposure,
- estimated remaining margin buffer,
- 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:- check the maximum size rule,
- check the maximum notional rule,
- check the minimum margin-buffer rule,
- check the session loss-stop rule.
Step 5: Add one controlled de-risk action
When risk is too high:- cancel stale working orders that would increase exposure,
- submit one smaller reducing order or exit action,
- keep the de-risk order inside normal product increments and price protections,
- wait for the lifecycle result before sending another reduction.
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.
- stop all new entries,
- cancel risk-increasing orders,
- send one reducing action,
- monitor the resulting account updates closely.
Step 7: Run one controlled stress pass
Drive the strategy through one session where:- the normal signal tries to add exposure,
- the risk budget blocks the extra trade,
- the market moves enough to shrink the margin buffer,
- the de-risk action fires,
- 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:- normal trading begins,
- the risk budget becomes binding,
- new entries stop,
- one reducing action executes,
- the strategy remains out of liquidation danger.