3#include <pineforge/pineforge.h>
4#include <pineforge/execution_observer.h>
5#include <pineforge/selected_window.h>
39 const std::optional<pf_version_t>& numbers,
40 const std::optional<std::string>& full,
41 const std::optional<std::string>& expected_manifest_version = std::nullopt);
80 const std::filesystem::path&
path() const noexcept {
return path_; }
82 int abi_version() const noexcept {
return abi_version_value_; }
96 void set_input(pf_strategy_t strategy,
const std::string& key,
const std::string& value)
const;
103 const std::string& key,
104 const std::string& value)
const;
142 const pf_bar_t* bars,
144 const std::string& input_timeframe,
145 const std::string& script_timeframe,
147 int magnifier_samples,
148 pf_magnifier_distribution_t magnifier_distribution,
149 pf_report_t* report)
const;
168 const std::optional<std::
string>& expected_manifest_version = std::nullopt) const;
173 const std::optional<std::
string>& expected_manifest_version = std::nullopt) const;
180 std::int64_t end_ms) const;
196 const pf_execution_observer_v1* observer) const;
209 using StrategyCreateFn = pf_strategy_t (*)(const
char*);
210 using StrategyFreeFn =
void (*)(pf_strategy_t);
211 using StrategySetInputFn =
void (*)(pf_strategy_t, const
char*, const
char*);
212 using StrategySetOverrideFn =
void (*)(pf_strategy_t, const
char*, const
char*);
213 using StrategySetChartTimezoneFn =
void (*)(pf_strategy_t, const
char*);
214 using StrategySetSymbolDoubleFn =
void (*)(pf_strategy_t,
double);
215 using StrategySetSymbolMetadataFn =
void (*)(pf_strategy_t, const
char*,
double);
216 using StrategySetSymbolStringFn =
void (*)(pf_strategy_t, const
char*);
217 using StrategySetSymbolFactsFn =
int (*)(pf_strategy_t, const
char*, const
char*, const
char*);
218 using StrategySetSymbolFeedFn =
int (*)(pf_strategy_t, const
char*, const
char*,
219 const pf_bar_t*, const std::int64_t*, std::int32_t);
221 struct ScratchTablesV1 {
222 std::uint32_t struct_size;
223 std::uint32_t version;
224 std::uint32_t backend;
225 std::uint32_t reserved;
226 std::uint64_t maxima[11];
228 using StrategySetScratchTablesFn = int (*)(pf_strategy_t,
const ScratchTablesV1*);
229 using StrategyRequireScratchArenaFn = int (*)(pf_strategy_t);
230 using RunBacktestFullFn = void (*)(pf_strategy_t,
237 pf_magnifier_distribution_t,
239 using StrategyGetLastErrorFn =
const char* (*)(pf_strategy_t);
240 using StrategyLastRunStatusFn = int (*)(pf_strategy_t);
241 using StrategyCreateCheckedFn = int (*)(
const char*, pf_strategy_t*,
char*, std::size_t);
242 using StrategySetCheckedFn =
243 int (*)(pf_strategy_t,
const char*,
const char*,
char*, std::size_t);
244 using ReportFreeFn = void (*)(pf_report_t*);
245 using AbiVersionFn = int (*)();
246 using ProtocolVersionFn = std::uint32_t (*)();
247 using SetSelectedWindowFn = int (*)(pf_strategy_t,
const pf_selected_window_config_v1*);
248 using SelectedWindowCountsFn = int (*)(pf_strategy_t, pf_selected_window_counts_v1*);
249 using SetExecutionObserverFn = int (*)(pf_strategy_t,
const pf_execution_observer_v1*);
250 using ExecutionObservationFn = int (*)(pf_strategy_t, pf_execution_observation_v1*);
251 using VersionGetFn = pf_version_t (*)();
252 using VersionStringFn =
const char* (*)();
253 using EffectiveSettingsFn =
254 int (*)(pf_strategy_t,
char*, std::size_t, std::size_t*,
char*, std::size_t);
256 void close() noexcept;
258 std::filesystem::
path path_;
259 void* library_handle_ =
nullptr;
260 int abi_version_value_ = 0;
261 StrategyCreateFn strategy_create_ =
nullptr;
262 StrategyFreeFn strategy_free_ =
nullptr;
263 StrategySetInputFn strategy_set_input_ =
nullptr;
264 StrategySetOverrideFn strategy_set_override_ =
nullptr;
265 StrategySetChartTimezoneFn strategy_set_chart_timezone_ =
nullptr;
266 StrategySetSymbolDoubleFn strategy_set_syminfo_mintick_ =
nullptr;
267 StrategySetSymbolDoubleFn strategy_set_syminfo_pointvalue_ =
nullptr;
268 StrategySetSymbolMetadataFn strategy_set_syminfo_metadata_ =
nullptr;
269 StrategySetSymbolStringFn strategy_set_syminfo_timezone_ =
nullptr;
270 StrategySetSymbolStringFn strategy_set_syminfo_session_ =
nullptr;
271 StrategySetSymbolFactsFn strategy_set_symbol_facts_ =
nullptr;
272 StrategySetSymbolFeedFn strategy_set_symbol_feed_ =
nullptr;
273 StrategySetScratchTablesFn strategy_set_scratch_tables_v1_ =
nullptr;
274 StrategyRequireScratchArenaFn strategy_require_report_scratch_arena_v1_ =
nullptr;
275 RunBacktestFullFn run_backtest_full_ =
nullptr;
276 StrategyGetLastErrorFn strategy_get_last_error_ =
nullptr;
277 StrategyGetLastErrorFn strategy_get_last_error_code_ =
nullptr;
278 StrategyGetLastErrorFn strategy_get_last_error_args_ =
nullptr;
279 StrategyLastRunStatusFn strategy_last_run_status_ =
nullptr;
280 StrategyCreateCheckedFn strategy_create_checked_ =
nullptr;
281 StrategySetCheckedFn strategy_set_input_checked_ =
nullptr;
282 StrategySetCheckedFn strategy_set_override_checked_ =
nullptr;
283 ReportFreeFn report_free_ =
nullptr;
284 AbiVersionFn pf_abi_version_ =
nullptr;
285 ProtocolVersionFn pf_selected_window_version_ =
nullptr;
286 SetSelectedWindowFn strategy_set_selected_window_v1_ =
nullptr;
287 SelectedWindowCountsFn strategy_selected_window_counts_v1_ =
nullptr;
288 ProtocolVersionFn pf_execution_observer_version_ =
nullptr;
289 SetExecutionObserverFn strategy_set_execution_observer_v1_ =
nullptr;
290 ExecutionObservationFn strategy_execution_observation_v1_ =
nullptr;
291 VersionGetFn pf_version_get_ =
nullptr;
292 VersionStringFn pf_version_string_ =
nullptr;
293 EffectiveSettingsFn strategy_get_effective_settings_ =
nullptr;
RAII wrapper around one compiled PineForge strategy plugin.
void set_execution_observer(pf_strategy_t strategy, const pf_execution_observer_v1 *observer) const
Registers an observer, or removes it when observer is null.
std::optional< std::string > effective_settings(pf_strategy_t strategy) const
Returns optional version-1 effective-settings JSON using the bounded two-call protocol.
StrategyPlugin & operator=(StrategyPlugin &&other) noexcept
void set_chart_timezone(pf_strategy_t strategy, const std::string &timezone) const
Sets the chart timezone when non-empty and supported by the plugin.
bool selected_window_available() const noexcept
Returns whether all six selected-window/observer exports exist; does not admit a revision.
pf_strategy_t create_strategy() const
Creates a fresh strategy handle owned by the caller.
void set_symbol_info(pf_strategy_t strategy, const SymbolInfo &info) const
Applies the lot grid (metadata keys qty_step, then mincontract) first, then the metadata key series_o...
const std::filesystem::path & path() const noexcept
Returns the dynamic-library path supplied at construction.
void require_scratch_arena(pf_strategy_t strategy) const
Admits the inherited arena using the engine's environment and backing checks.
EngineVersion engine_version(const std::optional< std::string > &expected_manifest_version=std::nullopt) const
Reads and validates this plugin's embedded runtime identity, optionally against a manifest.
std::optional< std::string > last_error_code(pf_strategy_t strategy) const
Returns an engine code, or nullopt when the optional getter is unavailable.
std::string last_error(pf_strategy_t strategy) const
Returns the plugin's last error for strategy, or an empty string when none exists.
void free_report(pf_report_t *report) const noexcept
Releases report-owned allocations populated by run_backtest_full().
std::optional< std::string > last_error_args(pf_strategy_t strategy) const
Returns raw engine argument JSON, or nullopt when its getter is unavailable.
pf_selected_window_counts_v1 selected_window_counts(pf_strategy_t strategy) const
Reads native counts with initialized and checked engine headers.
void run_backtest_full(pf_strategy_t strategy, const pf_bar_t *bars, int bar_count, const std::string &input_timeframe, const std::string &script_timeframe, bool bar_magnifier, int magnifier_samples, pf_magnifier_distribution_t magnifier_distribution, pf_report_t *report) const
Invokes the plugin's full backtest entry point using immutable OHLCV input.
void set_selected_window(pf_strategy_t strategy, std::int64_t start_ms, std::int64_t end_ms) const
Configures the next selected run's half-open interval using the engine's v1 descriptor.
void set_scratch_tables_bounded_default(pf_strategy_t strategy) const
Installs backend 1 with all-zero maxima; the engine expands its default tier at begin.
void free_strategy(pf_strategy_t strategy) const noexcept
Releases a handle returned by create_strategy(); safe to call during cleanup.
int abi_version() const noexcept
Returns the ABI version reported by the validated plugin.
void clear_selected_window(pf_strategy_t strategy) const
Clears pending selection; subject to the same checked status rules as its setter.
void set_input(pf_strategy_t strategy, const std::string &key, const std::string &value) const
Applies one serialized Pine input before the backtest starts.
void require_selected_window(const std::optional< std::string > &expected_manifest_version=std::nullopt) const
Requires all six exports in protocol order, ABI versions 1, then an admitted engine.
StrategyPlugin(std::filesystem::path path)
Loads path and resolves the required PineForge strategy symbols.
pf_execution_observation_v1 execution_observation(pf_strategy_t strategy) const
Reads the execution snapshot; a refusal or changed header is hpo_invariant.
StrategyPlugin(StrategyPlugin &&other) noexcept
std::optional< std::int32_t > last_run_status(pf_strategy_t strategy) const
Returns the optional run status; status 1 denotes a failed execution.
void set_override(pf_strategy_t strategy, const std::string &key, const std::string &value) const
Applies one serialized runtime strategy override before the backtest starts.
StrategyPlugin & operator=(const StrategyPlugin &)=delete
void set_symbol_feeds(pf_strategy_t strategy, const SymbolFeeds &symbols) const
Installs fixed other-symbol facts and bars into a fresh strategy handle.
StrategyPlugin(const StrategyPlugin &)=delete
std::vector< SymbolData > SymbolFeeds
Study-owned collection shared read-only by all trial workers.
EngineVersion validate_selected_engine_version(const std::optional< pf_version_t > &numbers, const std::optional< std::string > &full, const std::optional< std::string > &expected_manifest_version=std::nullopt)
Parses and cross-checks embedded metadata without loading a plugin or consulting build headers.
Embedded runtime identity admitted for selected-window execution.
std::uint32_t patch
Numeric patch release from the plugin's own metadata.
std::uint32_t major
Numeric major release from the plugin's own metadata.
std::string commit_sha
Commit reported by the plugin; empty when the runtime does not know it.
std::string full
Complete version, including any git-describe suffix.
std::uint32_t minor
Numeric minor release from the plugin's own metadata.
Optional instrument metadata applied after trial inputs and runtime overrides.
std::optional< double > pointvalue
Positive money-per-price-point multiplier.
std::optional< double > mintick
Positive instrument price tick size.
std::string session
Trading session; empty means unchanged.
std::optional< std::int64_t > series_origin_ms
Positive epoch milliseconds (below 2^53) of the first bar of the reference series.
std::optional< double > mincontract
Instrument lot-size grid (TradingView syminfo.mincontract); unset means no grid.
std::string timezone
Exchange timezone, distinct from the chart timezone; empty means unchanged.