114 for (
auto const& [sym, qty] :
positions) {
115 auto it = last_prices.find(sym);
116 if (it != last_prices.end()) {
117 gn += std::abs(qty) * it->second;
184 double commission = 0.001) noexcept
185 : initial_cash_(initial_cash)
186 , commission_(commission)
188 portfolio_.
cash = initial_cash;
193 strategy_ = std::move(strat);
198 queue_.push(std::move(event));
207 [[nodiscard]]
const std::vector<Fill>&
fills() const noexcept {
return fills_; }
217 std::optional<Fill> execute_order(
const Order& order,
223 double initial_cash_;
225 std::unique_ptr<
Strategy> strategy_;
227 using EventQueue = std::priority_queue<
234 std::vector<
Fill> fills_;
235 std::map<std::
string,
double> last_prices_;
254 double threshold = 0.001) noexcept
255 : base_qty_(base_qty)
256 , threshold_(threshold)
262 std::optional<Order> on_trade(
const BacktestEvent& event)
override;
263 std::optional<Order> on_bar (
const BacktestEvent& event)
override;
267 return spacelike_count_;
272 return timelike_count_;
282 int spacelike_count_ = 0;
283 int timelike_count_ = 0;
void add_event(BacktestEvent event)
Insert a market event into the priority queue.
const Portfolio & portfolio() const noexcept
Const accessor for the portfolio state after run().
void set_strategy(std::unique_ptr< Strategy > strat) noexcept
Set the strategy to drive order generation.
const std::vector< Fill > & fills() const noexcept
Const accessor for the fill history.
BacktestEngine(double initial_cash=100 '000.0, double commission=0.001) noexcept
int spacelike_rejections() const noexcept
Number of events rejected due to spacelike interval.
void on_start() override
Called at the start of the simulation before any events are dispatched.
int timelike_accepts() const noexcept
Number of events accepted due to timelike interval.
RelativisticStrategy(double base_qty=1.0, double threshold=0.001) noexcept
Abstract strategy interface. Subclass and override the virtual methods.
virtual void on_end()
Called at the end of the simulation after all events are processed.
virtual ~Strategy()=default
virtual std::optional< Order > on_bar(const BacktestEvent &)
virtual void on_start()
Called at the start of the simulation before any events are dispatched.
virtual std::optional< Order > on_trade(const BacktestEvent &)
Physical and financial constants for the SRFM system.
Spacetime Market Manifold — AGT-02 public API (implemented by AGT-06).
@ Market
Fill immediately at current price.
@ Limit
Fill only when price <= limit_price (buy) or >= (sell)
@ Trade
Individual trade execution tick.
@ Quote
Bid-ask quote update.
@ Bar
OHLCV bar (1-min, 5-min, daily, etc.)
A single market event with a timestamp.
bool operator>(const BacktestEvent &rhs) const noexcept
Priority comparison — earlier events have higher priority.
std::string symbol
Instrument identifier.
double volume
Volume for this event (0 for quotes)
EventType type
Trade, Quote, or Bar.
long long timestamp_ms
Unix epoch milliseconds.
double price
Trade price / bar close / mid-quote.
Aggregate performance statistics from a completed backtest.
double total_return
(final_equity - initial_equity) / initial
int num_trades
Total fills executed.
double profit_factor
Gross gains / gross losses.
double max_drawdown
Peak-to-trough equity drawdown fraction.
double win_rate
Fraction of profitable trades.
double sharpe_ratio
Annualised Sharpe (assuming 252 days)
Confirmation of an executed order.
An order emitted by a Strategy in response to a market event.
double limit_price
Only used for Limit orders.
Running portfolio state tracked by BacktestEngine.
double gross_notional(const std::map< std::string, double > &last_prices) const
Current gross notional exposure.
std::map< std::string, double > positions
symbol -> net quantity
std::vector< double > equity_curve
Equity snapshot after each fill.
Shared primitive types for the Special Relativity in Financial Modeling (SRFM) system.