PineForge HPO 1.11.0
Native hyperparameter optimization for PineForge strategies
Loading...
Searching...
No Matches
strategy_plugin.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <pineforge/pineforge.h>
4#include <pineforge/execution_observer.h>
5#include <pineforge/selected_window.h>
7
8#include <cstddef>
9#include <cstdint>
10#include <filesystem>
11#include <optional>
12#include <string>
13
14namespace pineforge {
15namespace hpo {
16
20 std::uint32_t major = 0;
22 std::uint32_t minor = 0;
24 std::uint32_t patch = 0;
26 std::string full;
28 std::string commit_sha;
29};
30
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);
42
44struct SymbolInfo {
46 std::optional<double> mintick;
48 std::optional<double> pointvalue;
51 std::optional<double> mincontract;
53 std::string timezone;
55 std::string session;
58 std::optional<std::int64_t> series_origin_ms = std::nullopt;
59};
60
66class StrategyPlugin final {
67public:
71 explicit StrategyPlugin(std::filesystem::path path);
73
76 StrategyPlugin(StrategyPlugin&& other) noexcept;
78
80 const std::filesystem::path& path() const noexcept { return path_; }
82 int abi_version() const noexcept { return abi_version_value_; }
83
87 pf_strategy_t create_strategy() const;
88
90 void free_strategy(pf_strategy_t strategy) const noexcept;
91
96 void set_input(pf_strategy_t strategy, const std::string& key, const std::string& value) const;
97
102 void set_override(pf_strategy_t strategy,
103 const std::string& key,
104 const std::string& value) const;
105
112 void set_chart_timezone(pf_strategy_t strategy, const std::string& timezone) const;
113
120 void set_symbol_info(pf_strategy_t strategy, const SymbolInfo& info) const;
121
124 void set_symbol_feeds(pf_strategy_t strategy, const SymbolFeeds& symbols) const;
125
129 void set_scratch_tables_bounded_default(pf_strategy_t strategy) const;
130
135 void require_scratch_arena(pf_strategy_t strategy) const;
136
141 void run_backtest_full(pf_strategy_t strategy,
142 const pf_bar_t* bars,
143 int bar_count,
144 const std::string& input_timeframe,
145 const std::string& script_timeframe,
146 bool bar_magnifier,
147 int magnifier_samples,
148 pf_magnifier_distribution_t magnifier_distribution,
149 pf_report_t* report) const;
152 std::string last_error(pf_strategy_t strategy) const;
153
155 std::optional<std::string> last_error_code(pf_strategy_t strategy) const;
157 std::optional<std::string> last_error_args(pf_strategy_t strategy) const;
159 std::optional<std::int32_t> last_run_status(pf_strategy_t strategy) const;
160
162 bool selected_window_available() const noexcept;
163
168 const std::optional<std::string>& expected_manifest_version = std::nullopt) const;
169
173 const std::optional<std::string>& expected_manifest_version = std::nullopt) const;
174
179 void set_selected_window(pf_strategy_t strategy, std::int64_t start_ms,
180 std::int64_t end_ms) const;
184 void clear_selected_window(pf_strategy_t strategy) const;
189 pf_selected_window_counts_v1 selected_window_counts(pf_strategy_t strategy) const;
195 void set_execution_observer(pf_strategy_t strategy,
196 const pf_execution_observer_v1* observer) const;
200 pf_execution_observation_v1 execution_observation(pf_strategy_t strategy) const;
203 std::optional<std::string> effective_settings(pf_strategy_t strategy) const;
204
206 void free_report(pf_report_t* report) const noexcept;
207
208private:
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);
220 // Mirror the additive C descriptor without requiring its declaration in older engine headers.
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];
227 };
228 using StrategySetScratchTablesFn = int (*)(pf_strategy_t, const ScratchTablesV1*);
229 using StrategyRequireScratchArenaFn = int (*)(pf_strategy_t);
230 using RunBacktestFullFn = void (*)(pf_strategy_t,
231 pf_bar_t*,
232 int,
233 const char*,
234 const char*,
235 int,
236 int,
237 pf_magnifier_distribution_t,
238 pf_report_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);
255
256 void close() noexcept;
257
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;
294};
295
296} // namespace hpo
297} // namespace pineforge
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.