Backtest Run
DSL (Domain Specific Language) Backtest is an advanced trading feature provided by Deepcoin, allowing users to backtest complex market-triggered strategies via JSON against historical data.
1. API Information
- Endpoint Name: DSL Backtest Run (Backtest Run)
- Request Method:
POST - Request URL:
/deepcoin/v2/trade/backtest-run - Content-Type:
application/json
2. Overall Request Architecture
The request body consists of two core objects: strategy logic definition (dsl) and historical data
configuration (data_source):
{
"dsl": { ... }, // Defines: Strategy logic, indicators, execution actions, risk control
"data_source": { ... } // Defines: Which trading pair? What time period for backtesting?
}
3. Detailed Parameter Definitions
3.1 Strategy Logic Layer (dsl)
| Parameter | Required | Type | Description |
|---|---|---|---|
version | No | String | DSL version, e.g., "1.0" |
indicators | Yes | Array | Array of technical indicators and their trigger conditions (see 3.1.1) |
then | Yes | Object | Execution actions after conditions are met: open/close position (see 3.1.2) |
risk | No | Object | Order's built-in take-profit/stop-loss protection: stop_loss, take_profit (see 3.1.3) |
execution | No | Object | Execution configuration (optional, see 3.1.4) |
3.1.1 Technical Indicators (indicators)
The system evaluates the logic based on the conditions defined in indicators (multiple indicators default to AND
logic).
| Parameter | Required | Type | Description |
|---|---|---|---|
name | Yes | String | Unique identifier for the indicator (e.g., ma5, boll) |
type | Yes | String | Indicator Type: BOLL, MA, EMA, KDJ, RSI, WR |
params | Yes | Object | Indicator calculation parameters (see 3.1.1.1) |
condition | Yes | Object | Trigger threshold (see 3.1.1.2) |
scope | Yes | String | Scope of action: entry (for opening position), exit (for closing position) |
3.1.1.1 Indicator Calculation Parameters (params)
| Indicator Type | params Included Parameters | Notes |
|---|---|---|
| Common | interval | Required. Candlestick period: 1m, 5m, 15m, 30m, 1h, 4h, 1d, 1w |
| BOLL | period, std ,interval | std is the standard deviation multiplier for Bollinger Bands |
| MA / EMA | period,interval | - |
| KDJ | n, k_smoothing, d_smoothing,interval | n is the KDJ parameter, k_smoothing for K smoothing, d_smoothing for D smoothing |
| RSI / WR | period,interval | - |
3.1.1.2 Trigger Condition Fields & Examples (condition)
The condition object defines precise trigger logic by combining different fields.
| Parameter | Required | Type | Description |
|---|---|---|---|
ref | Yes | String | Indicator reference field (e.g., boll.lower) |
op | Yes | String | Comparison operator (<,>) |
right | No | Number | Comparison value (required for KDJ, RSI, WR indicators) |
diff_price | No | Number | Price difference between current price and indicator value |
| Scenario | condition Example | Business Meaning |
|---|---|---|
| Indicator vs. Price Comparison ( boll omits right, only supports market price comparison) | {"ref": "boll.lower", "op": "<"} | Current market price is higher than the Bollinger Bands lower band. |
| Indicator vs. Fixed Value Comparison (using right) | {"ref": "kdj.k", "op": ">", "right": 80} | The KDJ indicator's K value is greater than 80. |
| Indicator vs. Price Difference Comparison (using diff_price) | {"ref": "ma5.value", "op": ">", "diff_price": 100} | The MA line value is 100 points higher than the current price. |
3.1.2 Execution Actions (then)
Contains entry (for opening) or exit (for closing) logic blocks.
Action Branch (on_true):
| Parameter | Required | Type | Description |
|---|---|---|---|
action | No | String | Action: open(open position), close(close position) |
side | No | String | Direction: long, short |
volume | Yes | Number | Order quantity in contracts |
3.1.3 Risk Control (risk)
| Parameter | Required | Type | Description |
|---|---|---|---|
stop_loss | No | Object | Stop-loss configuration.{"value": 0.1} means 10% |
take_profit | No | Object | Take-profit configuration.{"value": 0.2} means 20% |
3.1.4 Execution Configuration (execution)
| Parameter | Required | Type | Description |
|---|---|---|---|
fee_bps | No | Number | Fee in basis points (e.g., 5 means 0.05%) |
3.2 Data Source Configuration (data_source)
| Parameter | Required | Type | Description |
|---|---|---|---|
instId | Yes | String | Trading pair identifier (e.g., BTC-USDT-SWAP) |
from_ts | Yes | Integer | Start timestamp (Unix timestamp in seconds) |
to_ts | Yes | Integer | End timestamp (Unix timestamp in seconds) |
3.3 Additional Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
include_trades | No | Boolean | Whether to include detailed trade records in response; defaults to true |
4. Response Parameters
4.1 Basic Response
| Field | Type | Description |
|---|---|---|
code | Integer | Business status code. 0: success; others: failure |
msg | String | Error or prompt message |
data | Object | Backtest result data (see 4.2) |
4.2 Backtest Result Data
| Field | Type | Description |
|---|---|---|
summary | Object | Backtest summary statistics (see 4.2.1) |
trades | Array | Array of trade records (see 4.2.2) |
4.2.1 Backtest Summary
| Field | Type | Description |
|---|---|---|
realized_pnl | Number | Realized profit and loss |
instId | String | Trading pair instId |
total_fee | Number | Total fees paid |
trades | Integer | Total number of trades |
4.2.2 Trade Records
| Field | Type | Description |
|---|---|---|
fee | Number | Fee for this trade |
order_id | String | Order ID |
price | Number | Trade price |
qty | Number | Trade quantity |
reason | String | Reason for the trade |
side | String | Trade direction: buy, sell |
ts | Integer | Timestamp of the trade |
5. Typical Application Examples
5.1 Complete Request Example
A comprehensive example showing all available parameters for a BOLL strategy backtest.
{
"dsl": {
"version": "1.0",
"indicators": [
{
"name": "boll",
"type": "BOLL",
"scope": "entry",
"params": {
"period": 15,
"std": 2,
"interval": "1m"
},
"condition": {
"ref": "boll.lower",
"op": "<",
"right": "price.close",
"diff_price": 10
}
}
],
"then": {
"entry": {
"on_true": {
"action": "open",
"side": "long",
"volume": 0.1
},
"on_false": {
"action": "none",
"side": "long",
"volume": 0
}
},
"exit": {
"on_true": {
"action": "close",
"side": "long",
"volume": 100
},
"on_false": {
"action": "none",
"side": "long",
"volume": 0
}
}
},
"execution": {
"fee_bps": 5
},
"risk": {
"stop_loss": {
"value": 0.1
},
"take_profit": {
"value": 0.1
}
}
},
"data_source": {
"instId": "BTC-USDT-SWAP",
"from_ts": 1772054911,
"to_ts": 1772090911
}
}
5.2 Combined Indicators (BOLL + KDJ) Backtest
Backtest a strategy where a long position is opened when the price breaks below the Bollinger lower band and the KDJ K-value is below 30.
{
"dsl": {
"version": "1.0",
"indicators": [
{
"name": "boll",
"type": "BOLL",
"scope": "entry",
"params": {
"period": 20,
"std": 2,
"interval": "1m"
},
"condition": {
"ref": "boll.lower",
"op": "<",
"right": "price.close"
}
},
{
"name": "kdj",
"type": "KDJ",
"scope": "entry",
"params": {
"n": 9,
"k_smoothing": 3,
"d_smoothing": 3,
"interval": "1m"
},
"condition": {
"ref": "kdj.k",
"op": "<",
"right": 30
}
}
],
"then": {
"entry": {
"on_true": {
"action": "open",
"side": "long",
"volume": 100
},
"on_false": {
"action": "none",
"side": "long",
"volume": 0
}
},
"exit": {
"on_true": {
"action": "close",
"side": "long",
"volume": 0
},
"on_false": {
"action": "none",
"side": "long",
"volume": 0
}
}
},
"risk": {
"stop_loss": {
"value": 0.1
},
"take_profit": {
"value": 0.5
}
}
},
"data_source": {
"instId": "BTC-USDT-SWAP",
"from_ts": 1704067200,
"to_ts": 1706745600
}
}
5.3 MA Crossover Strategy with Full Risk Control
Backtest a strategy using MA crossover for entry and exit signals.
{
"dsl": {
"version": "1.0",
"indicators": [
{
"name": "ma5",
"type": "MA",
"scope": "entry",
"params": {
"period": 5,
"interval": "1h"
},
"condition": {
"ref": "ma5.value",
"op": "<",
"right": "price.close"
}
},
{
"name": "ma20",
"type": "MA",
"scope": "exit",
"params": {
"period": 20,
"interval": "1h"
},
"condition": {
"ref": "ma20.value",
"op": ">",
"right": "price.close"
}
}
],
"then": {
"entry": {
"on_true": {
"action": "open",
"side": "long",
"volume": 50
},
"on_false": {
"action": "none",
"side": "long",
"volume": 0
}
},
"exit": {
"on_true": {
"action": "close",
"side": "long",
"volume": 50
},
"on_false": {
"action": "none",
"side": "long",
"volume": 0
}
}
},
"execution": {
"fee_bps": 5
},
"risk": {
"stop_loss": {
"value": 0.05
},
"take_profit": {
"value": 0.15
}
}
},
"data_source": {
"instId": "ETH-USDT-SWAP",
"from_ts": 1704067200,
"to_ts": 1706745600
}
}
6. Usage Notes
- Indicator Logic: All indicators with
scope: "entry"must be met simultaneously (whenentry_condition_modeisALL) to trigger thethen.entryaction. - Exit Logic: Similarly, all indicators with
scope: "exit"must be met to trigger thethen.exitaction. - Data Range: Ensure
from_tsandto_tscover sufficient historical data for meaningful backtesting results. - Risk Management: Always configure appropriate stop-loss and take-profit levels to manage risk in your strategy.
- Backtesting Limitations: Past performance does not guarantee future results. Use backtesting as one tool among many for strategy validation.