Data Sources Overview

Historical bars for backtest: synthetic, dict, custom

The DataSource protocol is what the backtest loop iterates over. Horizon ships three built-ins and makes it trivial to add your own.

Built-in sources

Protocol

python
from typing import Iterator, Protocol
from horizon.data import Bar

class DataSource(Protocol):
    def markets(self) -> list[str]: ...
    def iter_bars(self) -> Iterator[Bar]: ...

Implementations yield Bar objects in chronological order across all markets. The backtest loop buffers bars at the same timestamp and flushes them as a single tick.

The Bar type

python
@dataclass(frozen=True)
class Bar:
    market_id: str
    timestamp: datetime
    price: float
    open: float | None = None
    high: float | None = None
    low: float | None = None
    close: float | None = None
    volume: float | None = None
    bid: float | None = None
    ask: float | None = None

Next