Run a Python strategy live¶
A class with hot hooks runs against venues inside the Python process, through fastmm run or fastmm.run_live. The session opens its own venue connections; it cannot attach to fastmm-gateway. Reference: Live sessions.
Before a keyed session, work through the Go-live checklist.
Install¶
To build both packages from a checkout instead: Install from source.
Run against the simulated exchange¶
The steps use configs/ and examples/ from a checkout. With only the pip package, the project fastmm init writes has sim.toml for the same session (Quick start).
-
Start the simulated exchange. It listens on 127.0.0.1:9080.
You have Run the pip package fastmm sim(no TLS)a checkout ./build/release/bin/fastmm-sim-exchange --config configs/sim.tomlthe release tarball, in fastmm-<version>-x86_64/bin/fastmm-sim-exchange --config configs/sim.toml -
Copy
configs/sim-local.tomltosim-py.tomland setname = "py:BasicMMHot"in its[strategy]table, so its[strategy.params]apply to the class. -
In another terminal, run the class:
FASTMM_SIM_API_KEY=sim-key FASTMM_SIM_API_SECRET=sim-secret \ PYTHONPATH=examples/python/strategies \ python -m fastmm run basic_mm_hot:BasicMMHot --config sim-py.toml --duration 60sThe hooks compile and run once on scratch data before the session connects. The log is then the one
fastmm-livewrites. -
Press Ctrl-C to stop it. The kill switch trips, open orders are cancelled and the process exits with code 0 after this line (Kill switch and shutdown):
python -m fastmm run is the same as fastmm run. From a script, run_live returns the exit code:
import fastmm
from basic_mm_hot import BasicMMHot
raise SystemExit(fastmm.run_live(BasicMMHot, "sim-py.toml", duration="60s"))
Run slow methods live¶
A class with slow methods (Run slow methods beside hot hooks) runs the same way. on_start runs before the session connects; the session does not quote before the first publish:
FASTMM_SIM_API_KEY=sim-key FASTMM_SIM_API_SECRET=sim-secret \
PYTHONPATH=examples/python/strategies \
python -m fastmm run hot_slow_mm:HotSlowMM \
--config configs/sim-local.toml --duration 60s
The basic_mm [strategy.params] in configs/sim-local.toml do not apply to the class; stderr says note: ignoring [strategy.params].
The session stops with exit code 7 when a slow method raises, a call runs past its timeout, the fills ring is full or the slow thread ends (Slow methods live). To keep it running:
- Set each
timeoutabove the longest wall time the method can take;run_backtestreports it inresult.slow_methods. - Keep
max_param_age_msbelow the timeouts, so a stalled model pulls the quotes before the session stops. The log showsno parameter update for <n> ms (max_param_age_ms): quotes pulled until the next. - Make
on_stopreturn:python -m fastmm runwaits--slow-tier-timeout-ms(10000 ms by default) for it after the session stops, then exits.
Keep other threads off the engine's core¶
- Pin the engine thread with
[engine] cpu, and the venue threads withnet_cpus(Cores). Beforeon_start, the calling thread moves to the other CPUs, so the slow thread and threadson_startstarts inherit them. When the session starts, all other threads of the process move too. - Set thread-count variables before Python starts, for example
OPENBLAS_NUM_THREADS=2 python my_session.py; numpy reads them when it loads.python -m fastmm runsetsOPENBLAS_NUM_THREADS,OMP_NUM_THREADSandMKL_NUM_THREADSto 1 unless they are set. - Start worker processes with
multiprocessing.set_start_method("forkserver")or"spawn". A child forked during a session cannot run a session or publish parameters.
Check the journal¶
The journal header records the class, a hash of the hot-hook source and the package versions:
python -c "import fastmm
j = fastmm.inspect_journal('runs/sim-local-<session id>.fmj')
print(j['strategy_meta'])"
Replay a Python strategy's journal with its class. Slow methods do not run; the parameter updates come from the journal (Replay):
PYTHONPATH=examples/python/strategies python -c "import fastmm, hot_slow_mm
print(fastmm.replay('runs/sim-local-<session id>.fmj', hot_slow_mm.HotSlowMM))"
BasicMMHot sends the orders C++ basic_mm sends, so the C++ strategy also replays its journal. Pass a configuration that names basic_mm with the same parameters. fastmm-replay warns that the configuration hash differs, then compares all messages: