Package a stock backtest for an independent rerun
A stock backtest is not independently reproducible when its instructions depend on the author’s chat, working directory or private files. Package the executable strategy, resolved configuration, data prerequisites, runtime and expected outputs, then actually run it from another directory. An archive containing a screenshot and a final profit number does not meet that requirement.
This guide builds and tests a small standard-library package with synthetic redistributable data. It is an external export method, not a verified CoTrading feature. Keep the research market and currency explicit. The example does not establish rights to distribute real market data or validate real historical performance.
Define the reproduction claim
State whether the recipient should reproduce exact fills and equity, only selected metrics within tolerance, or a method using different inputs. Those are different deliverables. Our fixture requires the exact synthetic fill list and ending value, using the same strategy, configuration and data bytes.
Record the source run and explain all prerequisites without referring to an earlier conversation. Include account initialization, signal timing, costs, terminal marking and any randomness. If the original strategy calls a model or live service, freeze its authorized outputs or disclose that the package still depends on an external unfrozen component.
Separate a complete offline fixture from a real-data reproduction requiring licensed acquisition. Do not silently replace unavailable historical prices with invented ones and claim that the original market result was reproduced. Synthetic data can demonstrate the method, but it must retain its own identity and limited claim.
Create the minimum executable layout
The example package contains strategy source, a runner, configuration, data, expected output and a README, plus a manifest. Six payload files are hashed; the manifest is an additional member. The runner resolves paths relative to its own file so another working directory cannot accidentally supply the author’s local inputs.
| File | Purpose |
|---|---|
| strategy.py | fixed signal and account function |
| runner.py | verify inputs, execute and compare |
| config.json | opening cash 1000 |
| data.json | prices 100, 102, 101, 104 |
| expected.json | exact fills and ending 1003 |
| README.txt | runtime, command and data status |
| manifest.json | runtime description and payload digests |
Use explicit encodings and structured values. Include dependency and interpreter requirements appropriate to the real experiment. This package needs Python 3.12 or later and only the standard library; it does not demonstrate installing a separate engine, restoring a database or recreating a model environment.
Preserve a lawful data-acquisition contract
When real input cannot be redistributed, omit it from the shared package and explain the authorized acquisition process. Identify provider dataset/version, security mapping, interval, calendar, adjustment policy, expected schema and available integrity reference. Keep credentials outside the archive and let the recipient provide access through an appropriate local mechanism.
Such a package is conditionally reproducible, not a fully offline rerun. If the provider no longer offers the exact snapshot, disclose that limitation. Do not promise that a public endpoint will reproduce an unavailable licensed revision, and do not embed someone else’s account token to bypass the prerequisite.
Our sample instead distributes its own invented four-price fixture. It also constructs a missing-data case and verifies that execution stops with a clear precondition error. That test models the boundary without contacting a provider or asserting that any real dataset may be shared.
Verify the archive before using its payload
The Python zipfile documentation supplies archive creation and reading interfaces. Our generator writes known flat filenames and rejects absolute or parent-traversal paths during extraction. It then checks required payload bytes against the manifest before strategy execution.
A digest detects changed content relative to its reference; it does not authenticate the publisher when an attacker could replace both data and manifest. Establish the package’s trusted provenance separately. Do not execute unknown strategy code merely because its self-supplied hashes agree.
Version the package when any payload changes, including configuration or expected output. Avoid editing expected results in place to make an unsuccessful rerun pass. A documentation-only change can also change its payload digest; distinguish that version update from a behavioral change without hiding either.
Run the synthetic strategy from its packaged inputs
The rule observes price rises and targets one share or zero, executing at the following price. On [100,102,101,104], it buys one share at index two for 101 and sells it at index three for 104. Starting cash 1000 becomes 1003, with no final shares and zero fees.
Expected fills are [[2,1,101],[3,-1,104]]. Quantity signs are positive for the buy and negative for the sale in this export. Preserve that convention rather than assuming every ledger uses the same sign. The expected result includes the fill sequence, so an offsetting execution error cannot pass merely by reaching the same ending value.
The runner loads the strategy through its packaged path, reads the local configuration and fixture, executes and compares the structured result. It does not rely on imports from the repository or user site packages. All necessary source is present in the downloadable generator.
Execute in an independent directory and child process
The Python subprocess documentation describes explicit argument lists, working directories, captured output and return codes. The sample creates another directory, restores the archive and launches python -I -B runner.py using an absolute runner path and a different working directory.
Isolation flags help avoid accidental user-path dependencies, but they do not pin operating system or Python implementation. Record the actual environment for a real claim. This test uses the current interpreter in a second process on the same machine; it is not a cross-platform certification.
Check return code and parse the actual output, then compare fills and ending equity. A child process that merely starts successfully has not necessarily produced its required artifacts. Capture errors with enough context to distinguish missing input, invalid data and strategy failures while keeping credentials out of logs.
Test tampered and unavailable data
After the successful independent run, the sample replaces its local data with [1,2,3]. The next child process fails with a data digest mismatch before executing the changed fixture. This is an integrity failure, not a legitimate alternative-market result to compare with the original.
A different case restores the package without data.json. It stops with a missing-required-file error, representing an unsatisfied acquisition prerequisite. It must not substitute zeros, sample internet prices or another author’s local cache. Both failures remain distinct from a strategy that executes correctly and loses money.
The script restores only its own temporary fixture and cleans its dedicated demonstration directories afterward. A production export should use an approved persistent location and retain the archive, manifest and rerun record. The temporary test is not an archival durability or backup audit.
Accept a package that stands on its own
Download the complete example. Use Python 3.12 or later and run python R626.py. It generates the full synthetic package, runs an isolated independent child, verifies ending 1003 and both fills, then checks tampered-data and missing-data rejection. No account credentials or external services are used.
Accept when a recipient can follow the README without chat context, required inputs are included or their prerequisites are explicit, and the independent rerun matches the declared output contract. Save the actual command, environment, return code, comparison and any remaining restrictions.
Rebuild when strategy, configuration, data, runtime or expected output changes, preserving the previous package. The example validates portable synthetic replay mechanics only; it does not demonstrate an existing product export button or guarantee that restricted real inputs will remain available offline.
FAQ
Is a package without licensed data fully offline reproducible?
No. Record the authorized acquisition prerequisite and the snapshot availability limitation.
Does running in another folder prove cross-platform compatibility?
No. It checks directory and process independence on the tested environment only.