Python API reference / 2.2.1

Bootstrap and scripted scenarios

Try it in Python

Insert an opening gap at the eleventh bar. A natural log jump of −0.05 lowers the opening price by about 4.88% relative to the preceding close.

from pricegenerator.extended_models import Scripted, ScriptedConfig, ScriptStep

candles = Scripted(ScriptedConfig(count=24, steps=(ScriptStep(index=10, log_jump=-0.05),)))
print(candles.loc[9, "close"], candles.loc[10, "open"])

pricegenerator.extended_models

Two alternatives for research and stress tests. Bootstrap resamples blocks from historical candles. Scripted paths follow a sequence of user-defined events.

BootstrapConfig

Immutable settings for a path built by sampling historical candles in contiguous blocks. Sets the output length (1 to 1,000,000), starting price, time grid and random seed. The source must have enough usable rows for one full block.

BootstrapConfig(
    count: int = 150,
    seed: int = 0,
    block_length: int = 1,
    initial_price: float = 100.0,
    start: datetime = datetime(2000, 1, 1, tzinfo=timezone.utc),
    timeframe_seconds: int = 3600,
)
Fields (6)
count
Number of candles to generate in the new series, from 1 to 1,000,000 inclusive.
seed
Nonnegative integer seed for NumPy's PCG64. It controls which historical blocks are drawn.
block_length
Number of consecutive historical candles per sampled block. Between 1 and the source row count minus one. Blocks do not wrap from the last row to the first.
initial_price
Positive starting price, in the chosen price units. It is the reference level before sampling a gap or applying a first-candle event.
start
Timestamp of the first candle, with an explicit time zone. The output timeline uses UTC.
timeframe_seconds
Positive whole seconds between candles of the new series. Source candle timestamps are used to verify regular spacing and are not resampled.

Source ↗

BootstrapConfig.ToDict

Return all bootstrap settings as a JSON-compatible dictionary, with the new series' start time encoded in ISO 8601 UTC.

BootstrapConfig.ToDict() -> dict

Source ↗

BootstrapConfig.FromDict

Rebuild the bootstrap settings from a complete JSON-compatible mapping. A missing or extra field, invalid time or invalid sample size raises ValueError.

BootstrapConfig.FromDict(values: Mapping) -> 'BootstrapConfig'
Parameters (1)
values
Complete JSON-compatible mapping produced by the configuration's ToDict method. Every field must be present, including an ISO 8601 start time with time zone.

Source ↗

Bootstrap

Build a new OHLCV DataFrame by jointly resampling the opening gap, body, wicks and observed volume in contiguous historical blocks. Requires at least three equally spaced candles with positive prices. The first source candle supplies the previous close. Joins between blocks can interrupt longer patterns.

Bootstrap(reference: pd.DataFrame, config: BootstrapConfig | None = None) -> pd.DataFrame
Parameters (2)
reference
Historical OHLCV table on a regular time grid with at least three rows and strictly positive prices. Sampled volumes keep the original units and may include fractions or zeros.
config
BootstrapConfig with the new time grid, initial price, sample size, seed and block length. None selects the defaults.

Source ↗

ScriptStep

Define an event on one indexed candle. A log jump changes only that candle's open relative to the preceding close. New drift or volume settings take effect on that candle and persist until another event changes them.

ScriptStep(
    index: int,
    log_jump: float = 0.0,
    drift: float | None = None,
    volume: float | None = None,
)
Fields (4)
index
Zero-based number of the candle where the event takes effect. Each event in the path must have its own number below count.
log_jump
One-candle opening gap in natural log units. The open equals the previous close × exp(log_jump). For a 10% gap up use log(1.10).
drift
New log return per candle, effective from this candle onward. None keeps the previous drift.
volume
New nonnegative volume per candle from this event onward. None keeps the previous setting. Fractional values are accepted.

Source ↗

ScriptedConfig

Immutable settings for a deterministic stress path of 1 to 1,000,000 candles. Events must have distinct, increasing zero-based indices inside the series. Invalid events, nonpositive starting price or a wick fraction outside [0, 1) raise ValueError.

ScriptedConfig(
    count: int = 150,
    initial_price: float = 100.0,
    start: datetime = datetime(2000, 1, 1, tzinfo=timezone.utc),
    timeframe_seconds: int = 3600,
    drift: float = 0.0,
    wick_fraction: float = 0.01,
    volume: float = 1000.0,
    steps: tuple[ScriptStep, ...] = (),
)
Fields (8)
count
Number of candles to generate in the new series, from 1 to 1,000,000 inclusive.
initial_price
Positive starting price, in the chosen price units. It is the reference level before sampling a gap or applying a first-candle event.
start
Timestamp of the first candle, with an explicit time zone. The output timeline uses UTC.
timeframe_seconds
Positive whole seconds between successive candle timestamps in the stress scenario.
drift
Constant added to the open-to-close log return each candle. A value of 0.01 corresponds to a price multiplier of exp(0.01) before other effects.
wick_fraction
Fraction from 0 inclusive to 1 exclusive. High equals the upper edge of the body × (1 + fraction), low equals the lower edge × (1 − fraction).
volume
Nonnegative volume per candle in the chosen units. The scripted model preserves the specified value, including any fractional part.
steps
Tuple of ScriptStep events, in ascending candle order and without duplicate indices. An empty tuple leaves the initial drift and volume in effect throughout.

Source ↗

ScriptedConfig.ToDict

Return a JSON-compatible dictionary with the full scenario, its UTC start time and a list of event dictionaries in execution order.

ScriptedConfig.ToDict() -> dict

Source ↗

ScriptedConfig.FromDict

Load a complete serialized scenario into a ScriptedConfig with validated ScriptStep objects. Missing or extra fields, malformed events or invalid event order raise ValueError.

ScriptedConfig.FromDict(values: Mapping) -> 'ScriptedConfig'
Parameters (1)
values
Complete JSON-compatible mapping produced by the configuration's ToDict method. Every field must be present, including an ISO 8601 start time with time zone.

Source ↗

Scripted

Return a deterministic OHLCV DataFrame. An event can create an opening gap. Each close equals open × exp(current drift), prices extend above and below the body by the chosen wick fraction, and volume stays at its current setting. Paths that exceed the supported price range raise ValueError.

Scripted(config: ScriptedConfig | None = None) -> pd.DataFrame
Parameters (1)
config
ScriptedConfig defining initial price, timeline, drift, wicks, volume and event schedule. None selects the default constant-price path.

Source ↗

CreateBootstrapManifest

Generate a bootstrap path and return a dictionary with the settings, software environment and hashes of both historical input and generated output. If supplied, data must match the generated candles or a ValueError is raised.

CreateBootstrapManifest(
    reference: pd.DataFrame,
    config: BootstrapConfig,
    data: pd.DataFrame | None = None,
) -> dict
Parameters (3)
reference
Historical OHLCV table on a regular time grid with at least three rows and strictly positive prices. Sampled volumes keep the original units and may include fractions or zeros.
config
BootstrapConfig used both to resample the source and to record every input required for replay.
data
Optional expected OHLCV series. Its fingerprint must match the new bootstrap run exactly.

Source ↗

ReplayBootstrap

Rebuild the sampled OHLCV DataFrame using the recorded settings and the original source candles. The model version, current environment, source fingerprint and output fingerprint must match. A mismatch raises ValueError.

ReplayBootstrap(manifest: Mapping, reference: pd.DataFrame) -> pd.DataFrame
Parameters (2)
manifest
The complete saved manifest for this model, including its settings, version and fingerprints. Use the dictionary returned by the corresponding creation function.
reference
Historical OHLCV table on a regular time grid with at least three rows and strictly positive prices. Sampled volumes keep the original units and may include fractions or zeros.

Source ↗

CreateScriptedManifest

Build the scripted path and return a dictionary with its events, configuration, environment and output fingerprint. Supplying a different candle series as data raises ValueError.

CreateScriptedManifest(config: ScriptedConfig, data: pd.DataFrame | None = None) -> dict
Parameters (2)
config
ScriptedConfig containing the initial settings and the complete event schedule to record.
data
Optional expected OHLCV series. Its fingerprint must match the result of executing the recorded events.

Source ↗

ReplayScripted

Regenerate a scripted OHLCV DataFrame from the recorded events. Verify the model and library versions, environment and exact output digest. A mismatch raises ValueError.

ReplayScripted(manifest: Mapping) -> pd.DataFrame
Parameters (1)
manifest
The complete saved manifest for this model, including its settings, version and fingerprints. Use the dictionary returned by the corresponding creation function.

Source ↗