Simulation creation with the API
Create a simulation over HTTP, bare or with its data and algorithms in one request, as a single account, a comparison or an instrument sweep.
You want to create a simulation from a script: one account with one
strategy, a comparison of several strategies, or one strategy swept across
several instruments. In the API a simulation is a runner, and one
request, POST /v1/runner, creates it. This guide shows the request, its
fields, and the two ways to use it.
To do the same in the desktop app, see Simulation creation in the app.
The examples use http://127.0.0.1:8080 as the engine's address and
$TOKEN for your access token. See
Authentication.
Kind
The type field of the request decides the kind of simulation. It is fixed
once the runner exists.
type | Kind | Accounts |
|---|---|---|
simulation | Single account | One, shared by every algorithm on the runner |
multi_simulation | Comparison | One per algorithm instance, all with the same starting balances |
sweep_simulation | Instrument sweep | One per instrument, all with the same starting balances |
The three kinds are explained in Simulations.
The same three arrangements exist for live prices, with the types paper,
multi_paper and sweep_paper. They take the same fields, and their data
sources are live instead of historical. See
Live simulations.
Plan requirement for comparisons and sweeps
multi_simulation and sweep_simulation need a plan that includes
comparisons. Without it the request is refused with 403 and
details.reason set to feature_not_in_plan. See
Plan limits.
Steps
Ids
The request names assets and instruments by id. Look up each instrument with
GET /v1/ticker; its id is the instrument and its quote is the asset it
is priced in.
curl -H "Authorization: Bearer $TOKEN" \
"http://127.0.0.1:8080/v1/ticker?symbol=BTCUSDT&market=binance"Look up algorithms with GET /v1/algorithms. Each entry has an id, a
parameter_schema and an available flag.
Bare or whole
- Bare. Send only the account. Add data sources and algorithms later with their own requests. This suits a script that builds a setup step by step, and it is what the app does.
- Whole. Send the account, the data sources and the algorithms in one request. This suits a script that knows the full setup in advance.
Both produce the same runner, and you can mix them: create a runner whole and change it later with the incremental requests.
Request
Send POST /v1/runner with the fields described below. The engine answers
201 with the runner, including its id.
Start
Send "start" to POST /v1/runner/{simulation_id}/operate. See
Run control.
Fields
| Field | Required | Meaning |
|---|---|---|
type | Yes | The kind. See Kind. |
name | Yes | The name shown in lists. |
initial_balances | Yes | The starting money: a list of { "asset": id, "amount": number }. In a comparison or a sweep, every account starts with these balances. |
primary_quote_asset | Yes | The asset id every result is measured in. It must be one of the assets in initial_balances. |
maker_fee | Yes | The fee on an order that rests before it fills, as a fraction: 0.001 is 0.1%. |
taker_fee | Yes | The fee on an order that fills at once, as a fraction. |
margin | No | Margin settings for the account. Leave it out for a spot account. See Margin and leverage. |
data | No | Data sources, as a map from an id you choose to a data source. |
algorithms | No | Algorithm instances, as a list. |
The primary quote asset cannot change after creation. Every run's results are measured in it, and changing it would make new runs impossible to compare with old ones. See Primary quote asset.
Data source fields
| Field | Meaning |
|---|---|
type | historical for a simulation kind. |
ticker | The instrument's id. |
interval | The candle interval: 1s, 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w or 1mo. |
start | The first instant of the range, ISO 8601 in UTC, such as 2024-01-01T00:00:00Z. |
end | The end of the range, exclusive. |
Algorithm fields
| Field | Meaning |
|---|---|
id | The algorithm's id, from GET /v1/algorithms. |
label | A name for this instance, to tell it from others of the same algorithm. Optional. |
parameters | The instance's parameters, shaped as the algorithm's parameter_schema says. Optional. |
data_sources | The ids of the data sources this instance reads. |
Bare runner
Send the account alone:
curl -X POST http://127.0.0.1:8080/v1/runner \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"type": "simulation",
"name": "SMA crossover on BTCUSDT",
"initial_balances": [
{ "asset": "4e058fc7-4509-4bb4-a276-8f35aa87e5cc", "amount": 10000 }
],
"primary_quote_asset": "4e058fc7-4509-4bb4-a276-8f35aa87e5cc",
"maker_fee": 0.001,
"taker_fee": 0.001
}'{
"type": "simulation",
"id": "0d6c3f0e-5a55-4b0e-9a57-3f2a1b7c9e11",
"name": "SMA crossover on BTCUSDT",
"status": "ready",
"data": {},
"algorithms": {}
}Then fill it in:
POST /v1/runner/{simulation_id}/dataadds a data source and answers with the id the engine gave it.POST /v1/runner/{simulation_id}/algorithmadds an algorithm instance that reads data sources by those ids.
First simulation with the API walks through both requests. For the details, see Data source setup and Algorithm setup.
Whole runner
Send data and algorithms with the account. Because the algorithms must
name their data sources in the same request, you choose the data source ids
yourself: any UUIDs, unique within the request. The engine still assigns the
algorithm instance ids.
This request creates a comparison of SMA Crossover and Buy and Hold
over the same hourly BTCUSDT candles, each with its own 10,000 USDT:
curl -X POST http://127.0.0.1:8080/v1/runner \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"type": "multi_simulation",
"name": "Crossover against buy and hold",
"initial_balances": [
{ "asset": "4e058fc7-4509-4bb4-a276-8f35aa87e5cc", "amount": 10000 }
],
"primary_quote_asset": "4e058fc7-4509-4bb4-a276-8f35aa87e5cc",
"maker_fee": 0.001,
"taker_fee": 0.001,
"data": {
"11111111-1111-4111-8111-111111111111": {
"type": "historical",
"ticker": "1f8b39a6-6ed8-4557-babe-6fa3b2372e07",
"interval": "1h",
"start": "2024-01-01T00:00:00Z",
"end": "2025-01-01T00:00:00Z"
}
},
"algorithms": [
{
"id": "3f2b6d1e-9b0a-4d5c-8f7e-1a2c4b6d8e05",
"label": "SMA 9/21",
"parameters": { "fast_period": 9, "slow_period": 21 },
"data_sources": ["11111111-1111-4111-8111-111111111111"]
},
{
"id": "f2f674e0-2158-4335-9bf1-d210f67f8b54",
"label": "Buy and Hold",
"data_sources": ["11111111-1111-4111-8111-111111111111"]
}
]
}'The answer is the runner. Its algorithms map is keyed by the instance ids
the engine assigned, which you need when you read each instance's results:
{
"type": "simulation",
"id": "7b1e2c94-6d0a-4f38-a5c2-9e4d0b8f3a61",
"name": "Crossover against buy and hold",
"status": "ready",
"data": {
"11111111-1111-4111-8111-111111111111": {
"type": "historical",
"ticker": "1f8b39a6-6ed8-4557-babe-6fa3b2372e07",
"interval": "1h",
"start": "2024-01-01T00:00:00Z",
"end": "2025-01-01T00:00:00Z"
}
},
"algorithms": {
"a4f0c2d8-1e57-4b93-8c6a-0d7e5f2b9a13": {
"id": "3f2b6d1e-9b0a-4d5c-8f7e-1a2c4b6d8e05",
"label": "SMA 9/21"
},
"c9d3e6b1-7a20-4c84-9f5b-2e8a1d4f6c07": {
"id": "f2f674e0-2158-4335-9bf1-d210f67f8b54",
"label": "Buy and Hold"
}
},
"config": { "type": "multi_simulated" },
"sweep": false
}The runner's own type reads simulation for all three kinds. The
arrangement shows in config.type: simulated for a single account,
multi_simulated for a comparison or a sweep. A sweep also has
"sweep": true.
Instrument sweep
A sweep is created like a comparison, with "type": "sweep_simulation", and
it must keep one promise: the setup is identical on every instrument. Give
it one data source per instrument and one algorithm instance per data
source.
{
"type": "sweep_simulation",
"name": "SMA crossover across instruments",
"initial_balances": [
{ "asset": "4e058fc7-4509-4bb4-a276-8f35aa87e5cc", "amount": 10000 }
],
"primary_quote_asset": "4e058fc7-4509-4bb4-a276-8f35aa87e5cc",
"maker_fee": 0.001,
"taker_fee": 0.001,
"data": {
"11111111-1111-4111-8111-111111111111": {
"type": "historical",
"ticker": "1f8b39a6-6ed8-4557-babe-6fa3b2372e07",
"interval": "4h",
"start": "2024-01-01T00:00:00Z",
"end": "2025-01-01T00:00:00Z"
},
"22222222-2222-4222-8222-222222222222": {
"type": "historical",
"ticker": "e36fc907-fc5d-445d-84af-c8f24bbb9377",
"interval": "4h",
"start": "2024-01-01T00:00:00Z",
"end": "2025-01-01T00:00:00Z"
}
},
"algorithms": [
{
"id": "3f2b6d1e-9b0a-4d5c-8f7e-1a2c4b6d8e05",
"label": "BTCUSDT",
"parameters": { "fast_period": 9, "slow_period": 21 },
"data_sources": ["11111111-1111-4111-8111-111111111111"]
},
{
"id": "3f2b6d1e-9b0a-4d5c-8f7e-1a2c4b6d8e05",
"label": "ETHUSDT",
"parameters": { "fast_period": 9, "slow_period": 21 },
"data_sources": ["22222222-2222-4222-8222-222222222222"]
}
]
}A sweep starts only when all of these hold:
- Every instance runs the same algorithm with the same parameters. Labels may differ.
- Every instance reads exactly one data source, and no two instances share one.
- Every data source has the same candle interval.
You can edit a sweep freely with the incremental requests, but a sweep that has drifted from these rules is refused when you start it.
Checks at creation and at start
Creation checks very little, on purpose: a runner that cannot run yet is a normal state while you are still building it.
| Case | Check |
|---|---|
| Creation | The primary quote asset is funded in initial_balances. A data source sent with the request matches the kind: historical for the simulation types, live for the paper types. Your plan includes comparisons, for the multi_simulation and sweep_simulation types. |
| Start | Everything else: there is at least one data source and one algorithm, every algorithm's data sources exist, at least one instrument is quoted in the primary quote asset, the candle intervals fit together, the parameters are valid, and the sweep rules hold. |
A refusal at start is a 409 with details.reason set to not_runnable
and the cause in details.message. See
Errors.
Changes after creation
| Change | Request |
|---|---|
The name, the favourite flag, the account settings (config) | PATCH /v1/runner/{id} |
| The data sources | POST and DELETE /v1/runner/{simulation_id}/data |
| The algorithm instances | POST /v1/runner/{simulation_id}/algorithm, PATCH and DELETE /v1/runner/{simulation_id}/algorithm/{algorithm_id} |
| Everything, back to how an earlier run had it | POST /v1/runner/{id}/run/{run_id}/restore |
The kind and the primary quote asset stay as they were created. To try a different kind, create a new runner.
Related
- Simulation creation in the app
- First simulation with the API: a full walkthrough from token to results
- Simulations: the three kinds and when to use each
- Run control: start, stop, pause and resume
create_runnerin the API reference- Errors
Simulation creation in the app
The five steps of the New simulation dialog: Details, Balances, Data, Algorithms and Review, with what each step requires and what you can fill in later.
Data source setup
Add and remove data sources on a simulation's Data card, and change the date range and interval that every source of the simulation shares.