Python API reference / 2.2.1

Monte Carlo price paths

Try it in Python

Simulate 32 paths and read the 5th, 50th and 95th percentiles of the final close. The middle value is the median across those paths at the final bar.

from pricegenerator import GenerationConfig
from pricegenerator.monte_carlo import RunEnsemble

report = RunEnsemble(GenerationConfig(seed=42, horizon=120), path_count=32)
print([(level, row[-1]) for level, row in zip(report.quantile_levels, report.close_quantiles)])

pricegenerator.monte_carlo

Generate many OHLCV histories under one model and set of assumptions. Summarize the spread of closing prices at each bar and the maximum price drawdown along each simulated history.

EnsembleReport

Summary of all simulated paths. For each bar it stores three close-price quantiles, and for each path it stores its maximum close-to-close price drawdown. Any individual OHLCV history can be regenerated from the configuration and its zero-based path index.

EnsembleReport(
    configuration: GenerationConfig,
    path_count: int,
    close_quantiles: tuple[tuple[float, ...], ...],
    maximum_drawdowns: tuple[float, ...],
    drawdown_quantiles: tuple[float, ...],
    environment: tuple[tuple[str, str], ...],
    quantile_levels: tuple[float, ...] = QUANTILE_LEVELS,
    quantile_method: str = QUANTILE_METHOD,
    metric_version: str = METRIC_VERSION,
    seed_derivation: str = SEED_DERIVATION,
    rng: str = RNG_ID,
    library_version: str = __version__,
)
Fields (12)
configuration
Full generation settings shared by all paths, including the root seed, bar interval and horizon.
path_count
Number of paths generated under the same settings, an integer from 2 to 10,000. The product with horizon must be at most 1,000,000.
close_quantiles
Three sequences of closing prices in P05, P50, P95 order. Each position compares the closes from every path at that bar. A quantile line can draw its values from different paths at different times.
maximum_drawdowns
Maximum price drawdown for each path in index order, expressed as a nonnegative fraction of its running peak close. A one-bar path has drawdown zero. Strategy drawdown requires the strategy’s own equity series.
drawdown_quantiles
P05, median and P95 across the per-path maximum price drawdowns, in fractional units.
environment
Recorded name/value pairs for Python, platform, pandas and NumPy used for exact replay.
quantile_levels
Quantile probabilities shared by both summaries, in output order (0.05, 0.50, 0.95).
quantile_method
Sample quantile interpolation rule, linear in this version.
metric_version
Identifier for the ensemble metric definitions, close-ensemble-v1 in this release.
seed_derivation
Versioned rule that maps the root seed and path index to the path seed.
rng
Identifier of the random number generator that draws innovations along each path.
library_version
Version of PriceGenerator that produced the report.

Source ↗

EnsembleReport.ToDict

Return the numeric summaries, full generation configuration and software environment in a JSON-compatible dictionary.

EnsembleReport.ToDict() -> dict[str, Any]

Source ↗

DerivePathSeed

Derive one unsigned 256-bit seed from an integer root seed and a nonnegative path index using SHA-256. Increasing path count or changing worker order preserves the seed for an existing index.

DerivePathSeed(root_seed: int, path_index: int) -> int
Parameters (2)
root_seed
Integer seed shared by the experiment. Its sign is preserved by the derivation scheme.
path_index
Zero-based path number, a nonnegative integer. Use the same index to regenerate that member.

Source ↗

RunEnsemble

Generate 2–10,000 OHLCV paths under the same configuration. Return the 5th, 50th and 95th percentiles of closes at each bar and the distribution of per-path maximum price drawdowns. Total bars across all paths are limited to 1,000,000. Use one worker in an interactive session, or up to eight processes from a guarded script.

RunEnsemble(config: GenerationConfig, *, path_count: int, workers: int = 1) -> EnsembleReport
Parameters (3)
config
GenerationConfig carrying the root seed and common settings such as the number of bars per path.
path_count
Number of paths generated under the same settings, an integer from 2 to 10,000. The product with horizon must be at most 1,000,000.
workers
Integer from 1 to 8. One runs serially, larger values request separate worker processes. Worker count does not affect the results.

Source ↗

CreateEnsembleManifest

Return a replay manifest containing the configuration, path count and software versions, plus SHA-256 digests of the experiment identity and reported quantiles and drawdowns. Individual OHLCV rows can be verified separately with a single-path manifest.

CreateEnsembleManifest(report: EnsembleReport) -> dict[str, Any]
Parameters (1)
report
EnsembleReport produced by RunEnsemble or ReplayEnsemble.

Source ↗

ReplayEnsemble

Regenerate an EnsembleReport from its manifest. By default the recorded environment must match. Allowing an environment change still requires compatible manifest and metric versions and an exact match of the output digest. A failed verification raises ValueError.

ReplayEnsemble(
    manifest: Mapping[str, Any],
    *,
    workers: int = 1,
    strict_environment: bool = True,
) -> EnsembleReport
Parameters (3)
manifest
Complete ensemble manifest from CreateEnsembleManifest, including both experiment and output digests.
workers
Integer from 1 to 8. One runs serially, larger values request separate worker processes. Worker count does not affect the results.
strict_environment
True requires the recorded software environment to match exactly. False permits a different environment while keeping the result-digest check mandatory.

Source ↗

ChartEnsemble

Save the chart and return its Path. Candles and volume come from path 0. Overlays show the three close-price quantiles across every path at each bar, as points for a one-bar horizon or lines otherwise. P05–P95 describes the spread of simulated closes at each bar. Estimating the probability that a whole path stays inside this band requires a separate calculation. Use HTML or SVG directly, or install the export extra for PNG or JPEG.

ChartEnsemble(report: EnsembleReport, output: str | Path) -> Path
Parameters (2)
report
EnsembleReport produced by RunEnsemble or ReplayEnsemble.
output
Output filename or Path. The extension selects HTML, SVG, PNG or JPEG. PNG and JPEG use the optional export dependencies.

Source ↗

Module constants

SEED_DERIVATION = 'pricegenerator-monte-carlo-path-seed-v1'
Identifier for the SHA-256 scheme that converts a root seed and a path index into a reproducible path seed.
QUANTILE_LEVELS = (0.05, 0.5, 0.95)
Ordered levels (0.05, 0.50, 0.95) used both for pointwise closing prices and for the sample of per-path maximum drawdowns.
QUANTILE_METHOD = 'linear'
Linear interpolation between neighboring sorted values at rank (n−1)·q.
MAX_ENSEMBLE_PATHS = 10000
Maximum number of paths accepted in a single ensemble run (10,000).
MAX_ENSEMBLE_CANDLES = 1000000
Maximum total number of bars per run (1,000,000), counted as path_count × horizon.
MAX_ENSEMBLE_WORKERS = 8
Maximum number of worker processes in one run (8).
METRIC_VERSION = 'close-ensemble-v1'
Version identifier for the ensemble’s price-quantile and drawdown conventions. A replay checks this value.
MANIFEST_VERSION = 1
Schema version of the ensemble replay manifest, currently 1.