Continuous
Build continuous contract symbology from individual futures contract data.
Supports multiple roll rules
- Calendar (.c.) — rolls based on contract expiry dates
- Volume (.v.) — rolls when the next contract's daily volume exceeds the current
- Open interest (.n.) — deferred, raises NotImplementedError
Usage
python -m onesecondtrader.secmaster.continuous --db secmaster.db --publisher-id 1 --root MNQ --rule c --roll-offset 0
logger = logging.getLogger(__name__)
module-attribute
_MONTH_CODES = {'F': 1, 'G': 2, 'H': 3, 'J': 4, 'K': 5, 'M': 6, 'N': 7, 'Q': 8, 'U': 9, 'V': 10, 'X': 11, 'Z': 12}
module-attribute
_FUTURES_RE = re.compile('^([A-Z]+)([FGHJKMNQUVXZ])(\\d{1,2})$')
module-attribute
_parse_futures_symbol(symbol)
Parse 'MNQH5' -> ('MNQ', 3, 2025). Returns None for spreads or unparseable symbols.
Source code in src/onesecondtrader/secmaster/continuous.py
_third_friday(year, month)
Return the 3rd Friday of the given month.
Source code in src/onesecondtrader/secmaster/continuous.py
_discover_contracts(con, publisher_id, root_symbol)
Discover individual outright contracts from symbology for a given root symbol.
Returns a list of dicts with keys: symbol, root, month, year, expiry, source_instrument_id, start_date, end_date.
Source code in src/onesecondtrader/secmaster/continuous.py
_get_or_create_continuous_instrument(con, publisher_id, continuous_symbol)
Ensure an instrument record exists for the continuous symbol mapping.
Source code in src/onesecondtrader/secmaster/continuous.py
build_continuous_symbology(db_path, publisher_id, root_symbol, roll_rule='c', roll_offset_days=0, rebuild_coverage=True)
Build front-month continuous symbology from individual contract data.
Creates symbology entries mapping '{root}.{rule}.0' to the correct underlying contract for each roll period.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
Path
|
Path to the security master SQLite database. |
required |
publisher_id
|
int
|
Publisher ID for the futures data. |
required |
root_symbol
|
str
|
Root symbol (e.g., 'MNQ'). |
required |
roll_rule
|
str
|
Roll rule: 'c' = calendar, 'v' = volume, 'n' = open interest. |
'c'
|
roll_offset_days
|
int
|
For calendar roll: days before expiry to roll. |
0
|
rebuild_coverage
|
bool
|
Whether to rebuild symbol_coverage after creating entries. Set to False when calling in a loop to avoid redundant rebuilds. |
True
|
Returns:
| Type | Description |
|---|---|
int
|
The number of symbology entries created. |
Source code in src/onesecondtrader/secmaster/continuous.py
_build_calendar_continuous(db_path, publisher_id, root_symbol, roll_offset_days, rebuild_coverage=True)
Build continuous symbology using calendar-based roll (3rd Friday of contract month).
Source code in src/onesecondtrader/secmaster/continuous.py
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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
_build_volume_continuous(db_path, publisher_id, root_symbol, rebuild_coverage=True)
Build continuous symbology using volume-based roll detection.
Source code in src/onesecondtrader/secmaster/continuous.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |
_discover_root_symbols(con, publisher_id)
Discover unique futures root symbols from symbology for a given publisher.
Queries all raw_symbol entries, parses each with _FUTURES_RE, and collects unique root symbols. Skips spreads (symbols containing '-').
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of root symbol strings (e.g., {'MNQ', 'ES'}). |
Source code in src/onesecondtrader/secmaster/continuous.py
build_all_continuous(db_path, publisher_id, rebuild_coverage=True)
Build calendar-roll and volume-roll continuous symbology for every futures root symbol discovered in the given publisher's symbology.
Safe to call on datasets with no futures — returns 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
Path
|
Path to the security master SQLite database. |
required |
publisher_id
|
int
|
Publisher ID to scan for futures root symbols. |
required |
rebuild_coverage
|
bool
|
Whether to rebuild symbol_coverage once at the end. |
True
|
Returns:
| Type | Description |
|---|---|
int
|
Total number of continuous symbology entries created. |
Source code in src/onesecondtrader/secmaster/continuous.py
main()
CLI entry point for building continuous symbology.