Backtest
Backtest execution logic for the dashboard.
Provides the request model, job tracking, and execution function for running backtests from the dashboard UI.
_executor = ThreadPoolExecutor(max_workers=1)
module-attribute
running_jobs = {}
module-attribute
_orchestrator_refs = {}
module-attribute
_running_metadata = {}
module-attribute
_jobs_lock = threading.Lock()
module-attribute
RTYPE_TO_BAR_PERIOD = {32: 'SECOND', 33: 'MINUTE', 34: 'HOUR', 35: 'DAY'}
module-attribute
BacktestRequest
Bases: BaseModel
Request model for backtest execution.
Attributes:
| Name | Type | Description |
|---|---|---|
strategy |
str
|
Name of the strategy class to run. |
strategy_params |
dict
|
Dictionary of parameter overrides for the strategy. |
symbols |
list[str]
|
List of instrument symbols to trade. |
rtype |
int
|
Bar period rtype code (32=second, 33=minute, 34=hour, 35=day). |
publisher_id |
int
|
Publisher ID for data source selection. |
start_date |
str | None
|
Optional start date in YYYY-MM-DD format. |
end_date |
str | None
|
Optional end date in YYYY-MM-DD format. |
Source code in src/onesecondtrader/dashboard/backtest.py
deserialize_params(params, param_specs)
Deserialize strategy parameters, converting enum strings to enum values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
dict
|
Dictionary of parameter names to values from the request. |
required |
param_specs
|
dict
|
Dictionary of parameter names to ParamSpec instances. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with enum strings converted to their enum values. |
Source code in src/onesecondtrader/dashboard/backtest.py
_ensure_db_status(db_run_id, status)
Force-update the run status in SQLite using a fresh connection.
Source code in src/onesecondtrader/dashboard/backtest.py
enqueue_backtest(request, run_id)
Submit a backtest to the bounded thread pool.
If all workers are busy the executor queues the task automatically.
Source code in src/onesecondtrader/dashboard/backtest.py
cancel_backtest(run_id)
Cancel a running or queued backtest.
Returns True if the backtest was found and cancellation was initiated.
Source code in src/onesecondtrader/dashboard/backtest.py
run_backtest(request, run_id)
Execute a backtest in a background task.
Configures and runs an Orchestrator with the specified strategy, symbols, and data source. Updates running_jobs with status during execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
BacktestRequest
|
Backtest configuration from the API request. |
required |
run_id
|
str
|
Unique identifier for tracking this backtest job. |
required |
Source code in src/onesecondtrader/dashboard/backtest.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |