Skip to main content
Pro Feature. Requires a Pro or Ultra subscription. Get started at api.mathematicalcompany.com
What is this? When should you exit a position? Hold too long and you give back profits; exit too early and you leave money on the table. The Longstaff-Schwartz algorithm solves this as an optimal stopping problem, computing an exercise boundary that tells you exactly when the expected value of holding drops below the value of exiting.

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

The hz.exit_optimizer() pipeline function evaluates the stopping policy each cycle and injects exit signals into ctx.params["exit_signal"].

Parameters


Mathematical Background

The Longstaff-Schwartz (2001) algorithm solves the American option stopping problem via backward induction on simulated paths:
  1. Generate N price paths using Monte Carlo simulation.
  2. 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).
  3. 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.
  4. At each step, exercise if the immediate value exceeds the fitted continuation value.
  5. The optimal stopping time for each path is recorded, and the policy is the set of regression coefficients.
For prediction markets, the “exercise value” is the profit from closing the position (current_price - entry_price - transaction_cost).
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.
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.
The quality of the stopping policy depends on the accuracy of the volatility estimate and the number of simulated paths. Use at least 5000 paths for reliable results. Recompute the policy periodically as market conditions change (the recompute_every parameter in the pipeline handles this automatically).