Pro Feature. Requires a Pro or Ultra subscription. Get started at api.mathematicalcompany.com
Optimal Stopping
When should you exit a prediction market position? Holding too long risks giving back profits; exiting too early leaves money on the table. The Longstaff-Schwartz algorithm solves this as an optimal stopping problem: at each time step, compare the immediate exit value to the expected value of continuing. Horizon implements the full backward-induction algorithm in Rust, including path generation and basis-function regression.Path Generation
hz.generate_gbm_paths() simulates geometric Brownian motion paths for Monte Carlo valuation.Longstaff-Schwartz
hz.longstaff_schwartz() computes the optimal stopping policy via backward regression on simulated paths.Live Decision
hz.should_exit() evaluates the learned policy against current state to produce a hold/exit signal.Pipeline Integration
hz.exit_optimizer() runs the stopping policy each cycle and injects exit signals into your strategy context.hz.generate_gbm_paths
Generate simulated price paths using geometric Brownian motion for use in the Longstaff-Schwartz algorithm.
Returns
list[list[float]]: matrix of shape (n_paths, n_steps + 1) with simulated prices. Prices are clamped to [0.01, 0.99] to respect prediction market bounds.
hz.longstaff_schwartz
Compute the optimal stopping policy using backward regression on simulated paths. At each time step, the algorithm regresses the continuation value against polynomial basis functions of the current state, then compares the immediate exercise value to the fitted continuation value.StopPolicy Type
The
thresholds list has one entry per time step. The threshold typically decreases over time: as expiry approaches, the option to wait becomes less valuable, so you become willing to exit at lower prices.hz.should_exit
Evaluate the learned stopping policy against a current price and time fraction to produce a hold/exit signal. This is the lightweight evaluation function for use in live trading.Pipeline Integration
Thehz.exit_optimizer() pipeline function evaluates the stopping policy each cycle and injects exit signals into ctx.params["exit_signal"].
Parameters
Mathematical Background
Longstaff-Schwartz Algorithm
Longstaff-Schwartz Algorithm
The Longstaff-Schwartz (2001) algorithm solves the American option stopping problem via backward induction on simulated paths:
- Generate N price paths using Monte Carlo simulation.
- At the final time step T, the exercise value is known: max(S_T - K, 0) for a call (or simply S_T - entry for a position exit).
- Moving backward from T-1 to 0: for paths where immediate exercise has positive value, regress the discounted future continuation value against polynomial basis functions of the current price.
- At each step, exercise if the immediate value exceeds the fitted continuation value.
- The optimal stopping time for each path is recorded, and the policy is the set of regression coefficients.
Why Not a Fixed Threshold?
Why Not a Fixed Threshold?
A fixed take-profit threshold ignores the time dimension. Early in a market’s life, a position at 0.60 (entered at 0.50) might be worth holding because there is time for further appreciation. Near expiry, the same position should be exited because the remaining optionality is small. The Longstaff-Schwartz approach produces a time-varying threshold that accounts for this.
Basis Functions
Basis Functions
The regression uses polynomial basis functions of the current price: 1, S, S^2, S^3, and so on. A degree of 3 is usually sufficient. Higher degrees can overfit to Monte Carlo noise. The Rust implementation uses Householder QR decomposition for numerically stable least-squares regression.