Workflows¶
The workflows package provides high-level classes that orchestrate
multi-step processes like daily model calibration, historical backtesting and
per-ticker tuning of the calibration settings.
BacktestWorkflow ¶
BacktestWorkflow(ticker: str, model_config: dict, *, settings: CalibrationSettings | None = None, allow_mc: bool = False, dates: list[str] | None = None, warm_start: bool = True)
Walk-forward backtest of one model on one ticker.
Snapshot dates are processed in ascending order; the model is calibrated on
day t and evaluated, with day t+1's own market parameters, on the
liquid options of day t+1. Everything is offline (no network calls).
Two baselines accompany every period:
BSM-ATM- a flat Black-Scholes volatility equal to dayt's ATM implied volatility;Stale- the model parameters calibrated one snapshot earlier (t-1) applied ont+1, i.e. what skipping the daily re-calibration costs.
Initializes the backtest workflow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ticker
|
str
|
The stock ticker to run the backtest for. |
required |
model_config
|
dict
|
A dictionary "recipe" defining how to calibrate the model. |
required |
settings
|
CalibrationSettings | None
|
Calibration settings (filters, objective, optimiser). |
None
|
allow_mc
|
bool
|
Permit Monte Carlo pricing for simulation-only models. |
False
|
dates
|
list[str] | None
|
Snapshot dates to use (any order); defaults to every stored date. |
None
|
warm_start
|
bool
|
Seed each calibration with the previous period's parameters. |
True
|
Source code in src/optpricing/workflows/backtest_workflow.py
run ¶
Executes the full backtesting loop.
For each consecutive pair of snapshot dates, calibrates on the earlier
day and records out-of-sample metrics on the later day in
self.results (one row per period) and self.bucket_results.
Source code in src/optpricing/workflows/backtest_workflow.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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 | |
save_results ¶
Saves the per-period table (CSV), the bucket table (CSV) and the summary with parameters (JSON) in the backtest-logs directory.
Returns:
| Type | Description |
|---|---|
tuple[Path, Path] | None
|
|
Source code in src/optpricing/workflows/backtest_workflow.py
summary ¶
Aggregates the per-period rows.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
|
Source code in src/optpricing/workflows/backtest_workflow.py
DailyWorkflow ¶
DailyWorkflow(market_data: DataFrame, model_config: dict[str, Any], *, settings: CalibrationSettings | None = None, allow_mc: bool = False, warm_start: dict[str, float] | None = None)
Orchestrates the calibration of a single model for a single snapshot of market data.
The workflow is fully offline:
- Reads the market parameters (spot, dividend yield, rate) stored in the snapshot (implied from the chain for pre-v2 files).
- Selects the calibration set with the settings' liquidity filter and the (wider) evaluation set.
- Builds the initial guess from the recipe, the ATM implied volatility and, for jump models with a local returns history, a historical prior.
- Calibrates with :class:
~optpricing.calibration.Calibrator(IV-space weighted objective, multi-start) and evaluates the fit.
Initializes the daily workflow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
market_data
|
DataFrame
|
A DataFrame containing the option chain for a single snapshot date. |
required |
model_config
|
dict[str, Any]
|
A recipe: |
required |
settings
|
CalibrationSettings | None
|
Filter / objective / optimiser settings; defaults apply if omitted. |
None
|
allow_mc
|
bool
|
Permit Monte Carlo pricing for simulation-only models. |
False
|
warm_start
|
dict[str, float] | None
|
A previous calibration used as an extra starting point. |
None
|
Source code in src/optpricing/workflows/daily_workflow.py
evaluate ¶
evaluate(model: BaseModel, stock: Stock | None = None, rate: Rate | None = None, data: DataFrame | None = None) -> tuple[dict[str, float], pd.DataFrame, pd.DataFrame]
Fit metrics of model on the evaluation set (or data).
Returns:
| Type | Description |
|---|---|
tuple[dict[str, float], DataFrame, DataFrame]
|
Summary metrics, bucket table and per-option residuals (see
:func: |
Source code in src/optpricing/workflows/daily_workflow.py
run ¶
Executes the full calibration and evaluation workflow.
Populates self.results with the status, the calibrated parameters and
the fit metrics; never raises (failures are recorded with Status =
"Failed" and an Error message).
Source code in src/optpricing/workflows/daily_workflow.py
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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
TuningResult
dataclass
¶
TuningResult(ticker: str, created: str, dates: list[str], leaderboard: list[dict[str, Any]] = list(), settings: dict[str, dict[str, Any]] = dict(), trials: list[dict[str, Any]] = list())
Cached outcome of :func:tune_ticker.
Attributes:
| Name | Type | Description |
|---|---|---|
ticker |
str
|
Ticker symbol. |
created |
str
|
ISO timestamp of the run. |
dates |
list[str]
|
Snapshot dates used (ascending). |
leaderboard |
list[dict[str, Any]]
|
One row per model, best first: |
settings |
dict[str, dict[str, Any]]
|
Best settings (serialised) per model. |
trials |
list[dict[str, Any]]
|
Every (model, settings) score. |
load_tuned_settings ¶
Best cached settings for (ticker, model), or None when not tuned.
Source code in src/optpricing/workflows/tuning.py
load_tuning ¶
The cached tuning result for a ticker, or None.
Source code in src/optpricing/workflows/tuning.py
tune_ticker ¶
tune_ticker(ticker: str, *, models: list[str] | None = None, candidates: list[str] | str | None = None, dates: list[str] | None = None, max_pairs: int | None = None, allow_mc: bool = False, progress: Callable[[str], None] | None = None, save: bool = True) -> TuningResult
Walk-forward tuning of calibration settings and model ranking for a ticker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ticker
|
str
|
Ticker with at least two stored snapshots. |
required |
models
|
list[str] | None
|
Recipe names; defaults to every calibratable model. |
None
|
candidates
|
list[str] | str | None
|
Candidate setting names from :data: |
None
|
dates
|
list[str] | None
|
Snapshot dates to use; defaults to all stored dates. |
None
|
max_pairs
|
int | None
|
Use only the most recent |
None
|
allow_mc
|
bool
|
Include simulation-only models (slow). |
False
|
progress
|
Callable[[str], None] | None
|
Called with a short message before each trial. |
None
|
save
|
bool
|
Write the result to |
True
|
Returns:
| Type | Description |
|---|---|
TuningResult
|
Best settings per model and the leaderboard. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two snapshot dates are available. |
Source code in src/optpricing/workflows/tuning.py
136 137 138 139 140 141 142 143 144 145 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 | |
Calibration recipes¶
calibratable_model_names ¶
Recipe keys whose model can be fitted to a chain: models with a vectorised
closed form or a characteristic function, plus (with allow_mc) the
simulation-only ones.
Source code in src/optpricing/workflows/configs/__init__.py
get_model_config ¶
A copy of the recipe for name (see :func:resolve_model_name).
resolve_model_name ¶
Canonical recipe key for a user-supplied model name (case-insensitive,
aliases such as "Variance Gamma" accepted).
Raises:
| Type | Description |
|---|---|
KeyError
|
If the name matches no recipe. |