FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
session_state.hpp
Go to the documentation of this file.
1#pragma once
2// State that survives a process restart: the session epoch (client order ids) and the latched risk
3// state (kill switch and cumulative PnL). Both are text files next to the journal, written through
4// a temporary file + fsync + rename, so a crash leaves the old file or the new one, never a
5// truncated one. Both stores fail closed: a session that cannot read or bump them must not trade.
9
10#include <cstdint>
11#include <string>
12#include <string_view>
13
14namespace fastmm {
15
16// Writes `contents` to `path` atomically: <path>.tmp, fsync, rename, fsync of the directory.
17[[nodiscard]] Result<void, std::string> write_file_atomic(const std::string& path,
18 std::string_view contents);
20// The strategy state file ([strategy] state_file, written by write_file_atomic): its bytes, true
21// when it exists, false when there is no such file (a first start); an error when it cannot be
22// read. The bytes are the strategy's own: whether they are its is for restore() to say.
23[[nodiscard]] Result<bool, std::string> load_strategy_state(const std::string& path,
24 std::string& out);
26// The 16-bit session epoch is the high half of every ClientOrderId, so two sessions that share one
27// would let a venue's late message from the earlier session match an order of the later one.
29 public:
30 // Reads the durable counter, increments it, writes it back and returns the epoch to use
31 // (1..65535, never 0). Fails when the file cannot be read or written; the caller must not trade.
32 // Beyond 65535 sessions the epoch cycles and `wrapped` is set: ids of sessions that long ago
33 // can repeat.
34 [[nodiscard]] static Result<std::uint16_t, std::string> next_epoch(const std::string& path,
35 bool* wrapped = nullptr);
36};
37
38// Cumulative risk state across sessions. `realized` and `fees` are the sum over every session since
39// the file was last cleared, so [risk] max_loss is a budget for the deployment rather than per
40// process. Unrealized PnL is not carried; it is remeasured from the venue's position after the
41// restart.
42struct KillState {
43 bool latched = false; // a max-loss trip is latched: refuse to start until an operator clears it
47 std::uint64_t sessions = 0;
48 std::int64_t updated_ns = 0; // wall clock of the last write
50 // The carry [risk] max_loss is measured against, on top of the running session's PnL.
51 [[nodiscard]] Notional carry() const noexcept { return realized - fees; }
52};
54class KillStateStore {
55 public:
56 // "<journal_dir>/<engine name>.kill".
57 [[nodiscard]] static std::string default_path(std::string_view journal_dir,
58 std::string_view engine_name);
59 // A missing file is a cleared state; a file that cannot be read or parsed is an error.
60 [[nodiscard]] static Result<KillState, std::string> load(const std::string& path);
61 [[nodiscard]] static Result<void, std::string> store(const std::string& path, const KillState& s);
62 // Removes the file (an operator clearing a latched trip and the loss budget with it).
63 [[nodiscard]] static Result<void, std::string> clear(const std::string& path);
64};
66} // namespace fastmm
Definition session_state.hpp:56
static std::string default_path(std::string_view journal_dir, std::string_view engine_name)
"<journal_dir>/<engine name>.kill".
static Result< KillState, std::string > load(const std::string &path)
A missing file is a cleared state; a file that cannot be read or parsed is an error.
static Result< void, std::string > clear(const std::string &path)
Removes the file (an operator clearing a latched trip and the loss budget with it).
static Result< void, std::string > store(const std::string &path, const KillState &s)
Definition result.hpp:30
The 16-bit session epoch is the high half of every ClientOrderId, so two sessions that share one woul...
Definition session_state.hpp:30
static Result< std::uint16_t, std::string > next_epoch(const std::string &path, bool *wrapped=nullptr)
Reads the durable counter, increments it, writes it back and returns the epoch to use (1....
Minimal expected<T, E>: the hot path never throws, it returns Result.
All engine enumerations.
Fixed<Tag>: int64 fixed point with a global 1e-8 scale (ADR-0001).
Definition strategy.hpp:16
Result< bool, std::string > load_strategy_state(const std::string &path, std::string &out)
The strategy state file ([strategy] state_file, written by write_file_atomic): its bytes,...
KillReason
Why a kill switch (global or one venue's) was tripped.
Definition enums.hpp:523
Result< void, std::string > write_file_atomic(const std::string &path, std::string_view contents)
Writes contents to path atomically: <path>.tmp, fsync, rename, fsync of the directory.
Cumulative risk state across sessions.
Definition session_state.hpp:44
Notional carry() const noexcept
The carry [risk] max_loss is measured against, on top of the running session's PnL.
Definition session_state.hpp:53
Notional realized
Definition session_state.hpp:47
std::int64_t updated_ns
wall clock of the last write
Definition session_state.hpp:50
KillReason reason
Definition session_state.hpp:46
bool latched
a max-loss trip is latched: refuse to start until an operator clears it
Definition session_state.hpp:45
std::uint64_t sessions
Definition session_state.hpp:49
Notional fees
Definition session_state.hpp:48