Skip to content

ENH: Support time-varying bid-ask spreads - #1393

Open
AtomicGlance wants to merge 1 commit into
kernc:masterfrom
AtomicGlance:codex/1300-time-varying-spread
Open

AtomicGlance wants to merge 1 commit into
kernc:masterfrom
AtomicGlance:codex/1300-time-varying-spread

Conversation

@AtomicGlance

Copy link
Copy Markdown

Summary

Fixes #1300.

Backtest(..., spread=...) now accepts either the existing scalar rate or a one-dimensional per-bar array-like value. A pandas Series must use the same index as the OHLC data. Values are validated as finite, numeric, and non-negative, and are applied at the bar where an order is filled. Scalar spreads keep the existing execution path and results.

The spread remains a relative rate, consistent with the current API. Data-source spreads expressed in price units can be converted to rates before passing them to Backtest.

Tests

  • scalar and constant Series spreads produce identical trades and equity curves
  • variable spreads affect market, limit, and stop fills at the fill bar
  • long and short fills use the correct direction
  • invalid length, shape, type, index, NaN, infinite, and negative values are rejected
  • full backtesting/test/_test.py suite: 83 passed, 1 skipped

@AtomicGlance

Copy link
Copy Markdown
Author

I reran the full backtesting.py test module on this branch (83 passed, 1 skipped). Before I make any further API changes, could you confirm that a spread sequence/Series aligned to the input bars is the preferred interface alongside the existing scalar value? I鈥檝e kept the current spread convention and apply the value from the actual fill bar for market, limit, and stop orders. I鈥檇 also appreciate confirmation on whether a Series must have an identical index and how missing values should be handled.

@AtomicGlance
AtomicGlance force-pushed the codex/1300-time-varying-spread branch from 8045baf to 89afc8f Compare September 9, 2026 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spread is Float but spread is typically different per row of the OHLC dataframe

1 participant