-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathstrategy.cpp
More file actions
49 lines (48 loc) · 2.04 KB
/
Copy pathstrategy.cpp
File metadata and controls
49 lines (48 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// SPDX-License-Identifier: Apache-2.0
// A hand-written native strategy. Codegen output exposes the same lifecycle.
#include <pineforge/engine.hpp>
class NativeExample final : public pineforge::BacktestEngine {
public:
NativeExample() {
initial_capital_ = 100000;
default_qty_value_ = 1;
}
void on_bar(const pineforge::Bar &) override {
// Alternating market entry/close gives a small deterministic test.
if (bar_index_ % 4 == 1)
strategy_entry("Long", true);
if (bar_index_ % 4 == 3)
strategy_close_all();
}
};
extern "C" {
PF_API void *strategy_create(const char *) {
try {
return new NativeExample;
} catch (...) {
return nullptr;
}
}
PF_API void strategy_free(void *s) { delete static_cast<NativeExample *>(s); }
PF_API void strategy_set_input(void *s, const char *key, const char *value) {
static_cast<NativeExample *>(s)->set_input(key, value);
}
PF_API void strategy_set_override(void *, const char *, const char *) {}
PF_API void run_backtest(void *s, pineforge::Bar *bars, int n, pineforge::ReportC *out) {
auto *strategy = static_cast<NativeExample *>(s);
strategy->run(bars, n);
strategy->fill_report(out);
}
PF_API void run_backtest_full(void *s, pineforge::Bar *bars, int n, const char *input_tf,
const char *script_tf, int magnifier, int samples, int distribution,
pineforge::ReportC *out) {
auto *strategy = static_cast<NativeExample *>(s);
strategy->run(bars, n, input_tf ? input_tf : "", script_tf ? script_tf : "", magnifier != 0,
samples, static_cast<pineforge::MagnifierDistribution>(distribution));
strategy->fill_report(out);
}
PF_API void report_free(pineforge::ReportC *out) { pineforge::BacktestEngine::free_report(out); }
// Retain the core object that exports native streaming symbols from the static
// engine library; no per-strategy streaming implementation is necessary.
PF_API int native_example_abi_version() { return pf_abi_version(); }
}