Python API¶
Generated by mkdocstrings from the docstrings of the fastmm package on every build. Start at
Python; the prose reference is Python strategy API.
Everything below is reachable as fastmm.<name>; the compiled core is documented from the type
stub python/fastmm/_core.pyi, so it is the same API the type checker sees.
Strategies¶
Strategy
¶
Base class of Python strategies.
Define any subset of the hooks (on_start(self, ctx), on_book(self, ctx, inst, book),
on_fill(self, ctx, fill), ... see HOOKS); undefined hooks are never called. Declare
parameters with Param; override validate() for cross-field checks.
state(self) -> bytes | str and restore(self, data: bytes) -> bool keep what the
strategy has learnt: in BacktestConfig.state_file, and in each journal checkpoint
([engine] checkpoint_s, ctx.request_checkpoint()), which a backtest with
[backtest] initial_state = "journal" and start_at restores. state_version = <int>
names the layout of those bytes (0 when absent); a state file or checkpoint of another version
is refused.
quote_context_names = ("fair", ...) names the values hot hooks publish with
ctx.quote_context(...), which the store keeps with every fill (fastmm-pnl
fill-context).
params
classmethod
¶
params() -> Dict[str, Param]
Every Param of the class and its bases, in declaration order (bases first).
schema
classmethod
¶
Parameter schema like fastmm.strategies(): [{'name', 'type', 'default', 'min', 'max', 'doc'}, ...]; an unbounded side is None.
hooks
classmethod
¶
The hooks this class defines, in table order. Raises TypeError for a hook with the wrong signature and warns about likely misspellings.
validate
¶
Optional cross-field check after parameters are applied; return an error message.
configure
¶
Apply parameters (like C++ StrategyBase::configure): keys in sorted order, the first error raises ValueError and leaves the previous values.
param_values
¶
Effective parameters as strings (BacktestResult.params).
publish
¶
New parameter values for a running strategy with slow methods, from any thread: the same checks and effect as ctx.publish(). False when no session of this instance runs or the session did not take the update.
Param
¶
Bases: property, Generic[T]
A strategy parameter: typed from its default (bool, int or float), with an optional range.
Values from cfg.params (strings) and run_backtest(params=...) (Python scalars) are
parsed like FASTMM_PARAM values in C++, with the same error messages. Read it as a plain
attribute: self.levels; an assignment is parsed and checked the same way.
Hooks read parameters on every event, so a read is the C-level get of a property whose getter
is operator.attrgetter("_fastmm_param_<name>"): the instance's value, else the default the
class holds under that name. No Python code runs.
Hot hooks¶
A @hot method is compiled with Numba and runs on the trading thread; see
Write hot hooks in Python.
hot
¶
Marks a strategy method as a hot hook: @fastmm.hot for on_book, on_fill, on_quoting and
on_connection, @fastmm.hot(every="100ms") for a timer hook. Every hot hook takes
(self, ctx, book).
State
¶
A per-instrument value that hot hooks read and write and that persists between calls. The type comes from the default: bool, int (int64) or float.
HotCompileError
¶
Bases: TypeError
A hot hook does not compile in Numba's nopython mode or fails the IR check.
Slow methods¶
An @every method runs on a second thread and publishes parameters to the hot hooks; see
Run slow methods beside hot hooks.
every
¶
Runs the decorated method, name(self, ctx), on the slow thread once per period of session
time, first when the session starts. In a live session a call that runs longer than timeout
stops the session. Units: ns, us, ms, s, m, h.
Running a strategy¶
run_backtest
¶
run_backtest(
config: "BacktestConfig",
data: Any = None,
strategy: StrategyArg = None,
params: Optional[Mapping[str, Any]] = None,
*,
hot_cache: bool = True,
slow_delay_ms: float = 0,
max_param_age_ms: Optional[int] = None,
fills_capacity: Optional[int] = None,
recent_rows: int = 4096,
) -> "BacktestResult"
Run one backtest.
data: None (config.source / config.path), 'synthetic', a .fmj or .csv path, or a dict of numpy columns (see docs/python.md).
strategy: None (config.strategy), a registered C++ strategy name, a module:Class name
(imported, as fastmm run does), a fastmm.Strategy subclass (a fresh instance per run) or an
unused Strategy instance.
params: overrides applied on top of config.params (str, int, float or bool values).
hot_cache: for a strategy with @fastmm.hot methods, whether compiled hooks are kept in FastMM's Numba cache directory; False compiles every hook afresh.
C++ strategies run with the GIL released. A Python strategy holds the GIL for the whole run; a hook that raises stops the run and raises StrategyError (original exception as cause, partial result as .result). KeyboardInterrupt and SystemExit from a hook propagate unchanged, with the partial result as .result. Hot hooks run with the GIL released; a failing hot hook trips the kill switch and raises StrategyError with .status, .fail_code and .kill_reason.
Slow methods (on_start, on_stop and @fastmm.every methods of a class with hot hooks) run at simulated times. slow_delay_ms: simulated delay before their publishes take effect. max_param_age_ms: overrides [strategy] max_param_age_ms (0 disables); None keeps the configured value, or when that is 0 uses 3 x the shortest @fastmm.every period and at least 1000 ms. fills_capacity: fills the ring holds (None: from the order rate limit). recent_rows: rows per instrument that ctx.recent() keeps. A slow method that raises, or a full fills ring, stops the run and raises StrategyError with .slow_failure.
run_live
¶
run_live(
strategy: Any,
config: Union[str, "os.PathLike[str]"],
params: Optional[Mapping[str, Any]] = None,
*,
duration: Union[None, str, int, float] = None,
dry_run: bool = False,
journal: Optional[str] = None,
no_journal: bool = False,
status: Optional[str] = None,
no_status: bool = False,
log: Optional[str] = None,
record_raw: Optional[str] = None,
allow_inline_secrets: bool = False,
standby: bool = False,
takeover: bool = False,
fills_capacity: Optional[int] = None,
recent_rows: int = 4096,
slow_tier_timeout_ms: int = SLOW_TIER_TIMEOUT_MS,
) -> int
Run a strategy with @fastmm.hot methods against the venues in config until SIGINT,
SIGTERM, duration, a kill switch or a slow-tier failure; returns the exit code.
strategy: a fastmm.Strategy subclass or an unused instance. Parameters come from the
configuration's [strategy.params] when [strategy] name names the class (py:Class,
module:Class or Class), then from params.
The keyword arguments up to takeover are the fastmm-live options:
duration in seconds or as "60s", journal, status and log paths,
record_raw directory, standby and takeover (wait for, or take over, the instance
lock of a running session of the same engine name). fills_capacity and recent_rows size the slow methods' fills
ring and recent rows as in run_backtest. slow_tier_timeout_ms: how long to wait for the slow
thread after the session stops; run_live warns and returns if it has not returned by then.
Configuration, parameter and compilation errors and an exception in on_start print a message and return 3. Raises ImportError without fastmm_live, TypeError for a strategy that is not a fastmm.Strategy and RuntimeError when a session already runs in this process.
replay
¶
replay(
journal: Any,
strategy: type,
*,
verify: bool = True,
config: "Optional[BacktestConfig]" = None,
params: Optional[Mapping[str, Any]] = None,
param_updates: bool = True,
hot_cache: bool = True,
) -> ReplayResult
Replay a journal recorded with a Python hot strategy.
The engine consumes the journal's inputs, ParamUpdate messages included, with strategy's hot
hooks; slow methods do not run. verify compares every outbound message with the recording.
The parameters start at the values the journal records (a backtest journal), else at the
configuration's [strategy.params]; params overrides them. max_param_age_ms is the recorded one.
config defaults to the configuration the journal embeds. A different class, hot-hook source,
fastmm or numba version, params or param_updates=False makes it a what-if replay:
ReplayResult.what_if is True and the reasons are listed.
sweep
¶
sweep(
config: Any,
grid: Grid,
data: Any = None,
strategy: Any = None,
threads: int = 0,
verify: int = 0,
summary_only: bool = False,
) -> List[Tuple[Dict[str, Any], Any]]
Parameter sweep. grid: {name: [values]}, every combination, or a list of points [{name: value}]. A name is a strategy parameter or one of the settings latency_fixed_us, latency_jitter_us, latency_md_us, latency_md_jitter_us, p_drop, queue_conservatism, maker_fee_bps and taker_fee_bps, set as the BacktestConfig attribute of that name. Returns [(point, BacktestResult)] in the points' order, a grid's first name varying slowest.
data: the forms run_backtest takes. strategy: None (config.strategy), a registered C++
strategy name, a fastmm.Strategy subclass or a module:Class name. Under the queue fill
models (l2_queue, l3_queue) the points share passes over the data, each with the result
run_backtest gives it: a C++ strategy or a class with @fastmm.hot hooks runs on threads with
the GIL released. A plain Python strategy, or any strategy under the matching model, runs one
backtest per point (a Python class in worker processes forked from this one, so it may live in
__main__ or a notebook). threads: workers; <= 0 uses every CPU, 1 runs in this thread.
verify: run that many points, spread over the grid, again as backtests of their own and raise RuntimeError naming the first difference (fill, order, equity bar, count or outbound hash) if one gives another result.
summary_only: each result keeps its metrics, counts and markouts but not its fills, equity bars and orders.
walk_forward
¶
walk_forward(
config: Any,
grid: Grid,
folds: int,
data: Any = None,
metric: Metric = "net_pnl",
strategy: Any = None,
threads: int = 0,
summary_only: bool = False,
) -> Dict[str, Any]
Walk-forward sweep: the grid's points run on folds consecutive time slices of data, and
the best point of fold i-1 by metric is scored on fold i. metric: net_pnl, realized_pnl,
sharpe_bar, spread_captured_bps, or a function of a BacktestResult returning a number, higher
is better.
Returns a dict: metric, folds (start_ts, end_ts, best, chosen, in_sample, out_of_sample, hindsight, scores, points), mean_in_sample, mean_out_of_sample, mean_hindsight, choice_changes and table (the printable report). folds=1 runs exactly fastmm.sweep. The synthetic market needs fill_model='l2_queue' for folds > 1. grid, strategy, threads and summary_only are as in fastmm.sweep, each fold's points sharing their passes as a sweep's do.
What a hook is given¶
Context
¶
What a Python strategy sees (mirrors the C++ StrategyContext). Usable only inside a hook.
instruments
property
¶
Every instrument of the table, as a tuple of Instrument (index == id).
account_usable
¶
The account's order link is live and its kill switch is not engaged.
balance
¶
One asset of a venue's account: the venue's last report (on_balance) moved by this engine's orders and fills since. known is False until the venue reports it.
balance_room
¶
Largest quantity of the instrument on side at price the balance covers, rounded down to the lot; None while the venue has not reported the balance that side draws on.
balances_live
¶
A venue has reported balances (before that, balance() is unknown everywhere).
best_ex_self
¶
(price, qty) of the book's best level on one side after own_qty is taken out; a level that was only ours is skipped. (0.0, 0.0) when none is left.
cancel
¶
Request a cancel; False when the order is unknown or cannot be cancelled.
capacity
¶
What an order slot on that account costs now ([capacity]): a new order or a move is sent when its value covers price.
every
¶
Repeating timer; on_timer(ctx, timer_id, tag) fires every period_ns. Returns the id.
fees
¶
fees(inst: Any) -> Fees
Maker and taker rates of the instrument: the ones the simulated venue charges.
funding
¶
The venue's funding of a perpetual (on_perp_state), with its age.
funding_need
¶
The funding need set_funding_need last declared for the instrument.
holding
¶
What one account of the pool holds of the instrument: a spot instrument's base balance there, a derivative's position on that account.
home
¶
The venue id of the account the instrument's position lives on in its account pool ([venues.
index
¶
The venue's index price of a derivative (on_perp_state), with its age.
margin
¶
The venue's margin: its account-wide margin where it reports one, else the settlement asset of its first derivative.
mark
¶
The venue's mark price of a derivative (on_perp_state), with its age.
once
¶
One-shot timer after delay_ns. Returns the id.
order
¶
Snapshot of an open order, or None once it is terminal.
order_budget
¶
What the [risk] rate limit and the venue's own limits still admit now; a backtest knows only the local token bucket.
own_qty
¶
own_qty(
inst: Any,
side: SupportsInt | SupportsIndex,
price: SupportsFloat | SupportsIndex,
at_ns: Any = None,
) -> float
Our resting quantity that the venue's feed shows at this price, as of the book's last update or of venue time at_ns. 0.0 where the feed does not show our orders (a backtest).
pieces
¶
Accounts other than the home that hold a tradable amount of the instrument.
placement
¶
The pool's placement: "home" (adding orders to the home, reducing orders to the accounts holding the instrument) or "balance" (the default).
pool
¶
The venue ids of the accounts that take orders for a venue's instruments ([venues.
queue_ahead
¶
Estimated quantity resting ahead of an open order at its price (the l2_queue model on the market data the strategy sees), or None before the ack and once it is terminal. Queues are tracked from the first call on; call it in on_start to cover every order from its ack.
randint
¶
Integer in [lo, hi] inclusive from the engine's seeded RNG.
reduce_only
¶
Reduce-only mode holds the instrument: an order that would take its position further from zero is refused (fastmm-ctl reduce-only, [risk] reduce_only).
replace
¶
Cancel-replace (venues that support it). Raises OrderRejected.
request_checkpoint
¶
Write a checkpoint to the journal once the events waiting now are handled: state(), the positions and the open orders. A backtest with [backtest] initial_state = "journal" and start_at starts from it.
risk_headroom
¶
risk_headroom(inst: Any) -> RiskHeadroom
What each [risk] limit still admits on the instrument now; an order of exactly a room passes that limit.
send
¶
send(
inst: Any,
side: SupportsInt | SupportsIndex,
price: Any,
qty: Any,
*,
post_only: bool = False,
reduce_only: bool = False,
ioc: bool = False,
tag: SupportsInt | SupportsIndex = 0,
value: Any = None,
) -> int
Send a limit order (risk-checked, journaled); returns its client order id. The price is rounded passively to the tick and the quantity down to the lot. With a value ([capacity]) it is sent only if that covers the price of its order slot. Raises OrderRejected.
set_funding_need
¶
The quote the instrument's bids want resting (its quote asset; a derivative's settlement asset); zero or less clears it. A pool treasury with targets = "demand" funds each account for the needs of the instruments whose home it is.
set_home
¶
Move the instrument's home to that account of its pool (journaled). With placement = "home" its adding orders on other accounts are cancelled. False when its pool keeps no homes or the venue is not of the pool.
set_quotes
¶
Desired ladder as sequences of (price, qty) floats, level 0 first (at most 8 per side); (price, qty, value) or (price, qty, value, keep) also state what the level is worth ([capacity]). Prices are rounded to the tick passively (bids down, asks up), quantities down to the lot; a non-positive price or quantity drops the level. Returns False when quoting is disabled.
set_quotes_raw
¶
Like set_quotes with raw int prices and quantities (1e-8 scale), used as given.
sweeping
¶
The pool sweeps now ([venues.
venue_health
¶
venue_health(
venue: SupportsInt | SupportsIndex = 0,
) -> VenueHealth
Feed lag, order round trip and the feed-lag gate of a venue.
working_quote
¶
working_quote(
inst: Any,
side: SupportsInt | SupportsIndex,
level: SupportsInt | SupportsIndex = 0,
) -> Any
The open order in a quote slot, or None.
BookView
¶
OrderBook
¶
Price-aggregated L2 book (up to 256 levels per side) for research. Prices and quantities are floats, stored in 1e-8 fixed point; best-first level order.
apply_delta
¶
Set/update levels; qty 0 deletes the level.
apply_snapshot
¶
Replace the book. bids/asks: (n, 2) float arrays or sequences of (price, qty).
asks
¶
Top n ask levels (all if None) as an (k, 2) float64 array, best first.
bids
¶
Top n bid levels (all if None) as an (k, 2) float64 array, best first.
imbalance
¶
(bid qty - ask qty) / (bid qty + ask qty) over the top levels, in [-1, 1].
weighted_mid
¶
Mid of the per-side quantity-weighted prices over the top levels.
Instrument
¶
Portfolio
¶
PnL totals over every instrument (a copy).
Order
¶
Fees
¶
Maker and taker rates of one instrument (a copy).
RiskHeadroom
¶
What each [risk] limit still admits on one instrument (a copy). None where the limit is off.
VenueHealth
¶
Feed lag and order round trip of one venue (a copy); durations in ns, 0 before the first sample.
Configuration and results¶
BacktestConfig
¶
Everything one backtest needs: engine, instruments, strategy and parameters, simulated venue (fill model, latency, fees) and the synthetic market. Build one with from_toml() or single_instrument().
cancel_rate_per_order_s
property
writable
¶
Synthetic per-order cancel hazard per second.
duration_s
property
writable
¶
Synthetic horizon in seconds (data files run to their end).
fill_model
property
writable
¶
'matching' (orders rest in the simulated book) or 'l2_queue' (queue position on L2 data).
initial_capital
property
writable
¶
Quote currency; only used for max_drawdown_pct.
keep_orders
property
writable
¶
The result keeps a row for every order sent (BacktestResult.orders); a sweep's points do not, their metrics and outbound hash cover the orders.
latency_fixed_us
property
writable
¶
Fixed order and ack latency, microseconds.
latency_jitter_us
property
writable
¶
Mean lognormal order and ack latency jitter, microseconds.
latency_md_jitter_us
property
writable
¶
Mean market-data latency jitter, microseconds.
limit_rate_per_s
property
writable
¶
Synthetic limit order arrivals per second.
maker_fee_bps
property
writable
¶
Maker fee in bps (negative = rebate), 0.01 bps resolution. Setting it makes every instrument pay the same schedule; use set_instrument_fees() afterwards for per-instrument rates.
market_rate_per_s
property
writable
¶
Synthetic market order arrivals per second.
markout_horizons_s
property
writable
¶
Post-fill markout horizons in seconds; [] turns markouts off.
max_param_age_ms
property
writable
¶
[strategy] max_param_age_ms: quoting is disabled before the first parameter update and while none was applied for this long (0 disables).
measure_wall_clock
property
writable
¶
Measure wall-clock tick-to-order per engine step.
mid_step_rate_per_s
property
writable
¶
Synthetic latent mid steps per second (each one mid_step_ticks ticks).
output_dir
property
writable
¶
Where the apps write results; run_backtest() never writes (see BacktestResult.write_all).
params
property
writable
¶
Strategy parameters as {name: str} (a copy; assign a dict or use set_param()).
queue_conservatism
property
writable
¶
L2 queue model: 0 = cancels ahead always help, 1 = never.
source
property
writable
¶
'synthetic' | 'journal' | 'csv' | '' (used when run_backtest(data=None)).
start_mid
property
writable
¶
Synthetic market initial mid (exact decimal string; float accepted).
state_file
property
writable
¶
[strategy] state_file: the strategy's restore() reads it at the start and its state() is written to it at the end ('' = none).
state_interval_s
property
writable
¶
[strategy] state_interval_s: how often the state is taken, seconds of engine time (default 300).
supports_replace
property
writable
¶
Venue and engine use native replace instead of cancel-then-new.
taker_fee_bps
property
writable
¶
Taker fee in bps, 0.01 bps resolution. Setting it clears per-instrument schedules.
warnings
property
¶
Unknown keys and sections of the TOML file, each with its line.
from_toml
staticmethod
¶
from_toml(path: Any) -> BacktestConfig
Load a FastMM TOML config (sections [engine] [[instruments]] [strategy] [risk] [venues.
from_toml_string
staticmethod
¶
from_toml_string(text: str) -> BacktestConfig
Parse a TOML document held in a string. Raises ConfigError, also for an unknown key or section.
single_instrument
staticmethod
¶
single_instrument(
symbol: str, tick: str, lot: str
) -> BacktestConfig
One instrument on venue 0 with exact decimal tick/lot (e.g. '0.01', '0.00001'); matching fill model, no strategy selected, and the synthetic market of a TOML [sim] with no keys: a book about 2.7 bps wide (set_market changes it).
clear_params
¶
Remove every strategy parameter (use before switching strategy).
instrument_fees
¶
(maker_bps, taker_bps) charged to one instrument.
set_instrument_fees
¶
set_instrument_fees(
instrument: SupportsInt | SupportsIndex,
maker_bps: SupportsFloat | SupportsIndex,
taker_bps: SupportsFloat | SupportsIndex,
) -> None
Charge one instrument its own maker / taker schedule (negative = rebate).
set_market
¶
set_market(
spread_bps: SupportsFloat | SupportsIndex = 4.0,
mid_step_bps: SupportsFloat | SupportsIndex = 0.5,
depth_bps: SupportsFloat | SupportsIndex = 1.0,
) -> None
Shape the synthetic market in bps of start_mid, as [sim] spread_bps, mid_step_bps and depth_bps do: the spread from touch to touch, the size of a latent mid step and the mean distance of a new limit order behind the touch. Set start_mid first.
set_param
¶
Set one strategy parameter (str / int / float / bool).
BacktestResult
¶
Outcome of one backtest. fills / equity / orders are dicts of read-only numpy views over the C++ vectors (prices, quantities, fees and PnL are raw int64 with a 1e-8 scale); to_pandas() converts them.
equity_by_instrument
property
¶
Per instrument id, its equity bar columns: pnl (realized + unrealized - fees, its settlement currency), position, mid, quoted.
instruments
property
¶
Each instrument as 'venue:symbol', indexed by instrument id.
md_late
property
¶
Recorded events that came after a later-stamped one (beyond reorder_window_ms).
md_reordered
property
¶
Recorded events the simulated venue took ahead of one recorded before them.
outbound_sha256
property
¶
SHA-256 of every message the engine sent (determinism fingerprint).
slow_methods
property
¶
Wall time of each slow method of a Python strategy: {name: {'calls', 'p50_ms', 'p99_ms', 'max_ms'}}; empty for other strategies.
markouts
¶
Post-fill markouts, one entry per horizon: {'horizon_ns', 'label', 'excluded_fills', 'total', 'buy', 'sell', 'maker', 'taker', 'instrument'}. Each bucket has markout / capture / adverse_selection in quote currency and in bps of notional, over the fills that had a mid at the horizon.
stats
¶
Every summary metric: PnL / volume / inventory in quote or base units (float), counts and latency percentiles (int, ns); kill_reason and kill_at_s (seconds after the start) when the kill switch stopped the run, else None.
to_pandas
¶
fills / equity / orders as pandas DataFrames with float prices and datetime64 timestamps (see fastmm.results.to_pandas).
write_all
¶
Write equity.csv, fills.csv, orders.csv and summary.json into dir (created).
ReplayResult
dataclass
¶
ReplayResult(
ok: bool,
outbound_sha256: str,
recorded_sha256: str,
outbound_messages: int,
recorded_messages: int,
events: int,
first_mismatch: int,
expected_message: str,
actual_message: str,
what_if: bool,
what_if_reasons: List[str] = list(),
)
Outcome of fastmm.replay(). ok: the replayed engine sent the recorded messages. what_if:
the replay differs from the recording (what_if_reasons), so a mismatch is a result, not an
error.
to_pandas
¶
to_pandas(result: BacktestResult) -> Dict[str, DataFrame]
{"fills", "equity", "orders", "markouts"} DataFrames with float prices / quantities /
PnL and datetime64[ns] timestamps.
sweep_frame
¶
sweep_frame(
points: Iterable[Tuple[Dict[str, Any], BacktestResult]],
stats: Sequence[str] = SWEEP_STATS,
) -> DataFrame
One row per sweep point: its values, the stats() columns named by stats (default
SWEEP_STATS), the markout of every horizon in bps of notional (markout_bps_<label>, as
markout_bps_1s) and the outbound hash. Row order follows the sweep.
markout_frame
¶
markout_frame(result: BacktestResult) -> DataFrame
One row per (horizon, bucket) of result.markouts(): markout and spread capture in
quote currency and in bps of notional, plus the adverse selection between them.
load_csv
¶
Read a ts_ns,type,inst,side,price,qty,seq CSV (the CsvSource format, e.g. from
tools/gen_synthetic_data.py) into the column dict :func:fastmm.run_backtest accepts.
Prices and quantities are parsed exactly into raw int64 (1e-8), so the arrays replay bit-identically to running the CSV by path.
data_sources
¶
The registered market-data sources, their options and what each one carries.
convert_data
¶
convert_data(
data: str, out: str, config: BacktestConfig
) -> int
Decode a data source into an .fmj journal, the format a backtest replays fastest, and return the number of events written. The instruments come from config; 'synthetic' records the synthetic market of config's [sim] and [backtest] duration_s.
fetch
¶
fetch(
symbol: str,
dates: Sequence[str],
*,
market: str = "um",
kinds: Optional[Iterable[str]] = None,
cache: Optional[Cache] = None,
extract: bool = True,
force: bool = False,
quiet: bool = False,
) -> List[Path]
Download and unpack the daily dumps of symbol.
Returns the extracted CSV paths (the ZIPs when extract is false). Every file is checked
against the archive's published SHA-256 before it is accepted and a partial download
resumes. A day the archive does not have raises :class:DownloadError naming the URL.
fetch
¶
fetch(
exchange: str,
symbol: str,
dates: Sequence[str],
*,
kinds: Optional[Iterable[str]] = None,
cache: Optional[Cache] = None,
extract: bool = True,
force: bool = False,
quiet: bool = False,
api_key: Optional[str] = None,
) -> List[Path]
Download and gunzip the datasets of symbol on exchange.
Returns the extracted CSV paths. api_key defaults to $TARDIS_API_KEY; without one
only the first day of a month is served and anything else raises
:class:DownloadError with the HTTP 401.
Cache
¶
Files under a root directory, addressed by the archive's own key.
download
¶
download(
url: str,
key: str,
*,
sha256: Optional[str] = None,
force: bool = False,
headers: Optional[Dict[str, str]] = None,
progress: Optional[
Callable[[str, int, int], None]
] = None,
) -> Path
Download url to key, resuming a partial file and verifying sha256.
A file already in the cache is returned untouched unless force. The download goes
to <key>.part and is renamed only after the checksum matches, so an interrupted
run never leaves a truncated file that looks complete. A server that refuses the range
request starts the file again.
extract
¶
Unpack a cached .zip (one member) or .gz next to it, delete the archive, and
return the file. A fetch skips a day whose unpacked file is in the cache.
write_report
¶
write_report(
source: Any,
out: Optional[PathLike] = None,
config: Optional[PathLike] = None,
) -> str
Write the report for a run directory, a journal or a result, and return the path.
source is a path to a backtest output directory or a .fmj journal, or a
:class:fastmm.BacktestResult. out defaults to report.html beside the input.
Trading records¶
open_store
¶
open_store(path: Union[str, 'os.PathLike[str]']) -> Store
Opens a FastMM store read-only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, 'os.PathLike[str]']
|
the store file, |
required |
Returns:
| Type | Description |
|---|---|
Store
|
A |
Store
|
the connection. |
Raises:
| Type | Description |
|---|---|
ValueError
|
the file is not a FastMM store, or was written by a newer FastMM. |
OperationalError
|
the file does not exist or cannot be read. |
Store
¶
A read-only connection to a FastMM SQLite store.
Every method returns a pandas DataFrame with raw fixed-point columns decoded to floats and
nanosecond timestamps decoded to UTC datetimes, so fee_raw reads back as fee.
query
¶
Runs sql and returns the rows, decoding _raw and _ns columns as above.
sessions
¶
sessions(
engine: Optional[str] = None,
since: Optional[str] = None,
until: Optional[str] = None,
) -> "pd.DataFrame"
One row per session: when it ran, what it made and how it ended.
fills
¶
fills(
session: Optional[int] = None,
instrument: Optional[str] = None,
since: Optional[str] = None,
until: Optional[str] = None,
) -> "pd.DataFrame"
One row per execution, in time order.
fill_context
¶
fill_context(
session: Optional[int] = None,
instrument: Optional[str] = None,
since: Optional[str] = None,
until: Optional[str] = None,
) -> "pd.DataFrame"
One row per execution with its decision record (schema 16), as fastmm-pnl
fill-context shows it: the order's state when the fill arrived (order_state), when it
was placed and at what price, when its working leg went out (sent, after amends
accepted replaces) and was acknowledged, the cancel or replace in flight (in_flight,
sent at in_flight_since), and the strategy's quote context when the order went out
(sent_<name>) and when the fill arrived (fill_<name>), named after the session's
quote_context_names (ctx0 .. ctx3 where the sessions name a slot differently).
orders
¶
orders(
session: Optional[int] = None,
instrument: Optional[str] = None,
open_only: bool = False,
) -> "pd.DataFrame"
One row per order, in the last state the session recorded for it.
positions
¶
The last position snapshot of each session and instrument.
pnl
¶
pnl(
since: Optional[str] = None,
until: Optional[str] = None,
instrument: Optional[str] = None,
by: str = "instrument",
books: bool = False,
) -> "pd.DataFrame"
Realised (with the funding in it, schema 4), fees and net by UTC day.
by is "instrument" (a row per day, account and symbol) or "currency" (a row per day and
settlement currency). Unrealised PnL is not summed across days: it is a mark, not a flow.
From schema 10 the PnL is recomputed from the stored fills, as fastmm-pnl pnl does:
average cost, by the account holding the position and symbol, carried across sessions;
an execution counts once however many records hold it, and an estimate (a synthetic fill)
only for what no execution named. estimates counts the estimates still open and
books_net is the engine's own net for the day. books=True (or an older store) reads the
engine's day roll-up of its position records instead.
funding
¶
funding(
session: Optional[int] = None,
instrument: Optional[str] = None,
since: Optional[str] = None,
until: Optional[str] = None,
) -> "pd.DataFrame"
Every perpetual funding payment booked: amount in asset, negative paid.
Funding is realized PnL; pnl() and positions() carry it inside realized and say how
much of it in funding. A store from before schema 4 has none.
duplicates
¶
One row per venue execution or funding payment that more than one session stored.
A restart booked it a second time: the positions, fees and PnL of the sessions holding
the copies count it twice. An execution is the venue's id of it on one venue, symbol and
side. Empty in a sound store. The columns are those of fastmm-pnl duplicates: kind
(fill or funding), engine, venue, symbol, id, copies, side, qty (the amount of a
funding payment), ts (of the first copy) and sessions (oldest first, space separated).
kill_events
¶
Every kill switch trip, global or per venue, with the reason and the PnL at the time.
journals
¶
The journal parts each session wrote, in order.
Errors¶
StrategyError
¶
StrategyError(
message: str,
result: "Optional[BacktestResult]" = None,
hook: str = "",
now_ns: int = 0,
events: int = 0,
status: int = 0,
fail_code: int = 0,
kill_reason: str = "",
slow_failure: str = "",
symbol: str = "",
)
Bases: RuntimeError
A strategy hook raised. The original exception (with its traceback) is __cause__;
result is the partial BacktestResult of the run, which stopped after the failing event.
OrderRejected
¶
Bases: Exception
ctx.send() or ctx.replace() was rejected; .reason is the RejectReason name.
StaleViewError
¶
Bases: RuntimeError
A view (book, fill, trade, ...) was read outside the hook that received it.
ConfigError
¶
Bases: ValueError
Logging and build information¶
enable_logging
¶
Start writing C++ log records at level or above to path (appending) or to stderr. Calling it again restarts the logger with the new settings.
disable_logging
¶
Flush pending log records, stop the logger thread and close the log file.
strategies
¶
Registered strategies and their parameter schemas: {name: [{'name', 'type', 'default', 'min', 'max', 'doc'}, ...]}.