20#define QUARTER_MS (15LL * 60LL * 1000LL)
25 {100.00, 100.20, 99.90, 100.10, 10.0, 0 *
QUARTER_MS},
26 {100.10, 100.30, 100.05, 100.20, 10.0, 1 *
QUARTER_MS},
27 {100.20, 100.80, 100.15, 100.70, 10.0, 2 *
QUARTER_MS},
28 { 99.40, 99.45, 99.20, 99.30, 10.0, 3 *
QUARTER_MS},
29 { 99.45, 99.65, 99.35, 99.60, 10.0, 4 *
QUARTER_MS},
30 { 99.60, 99.62, 99.30, 99.40, 10.0, 5 *
QUARTER_MS}
40static void submit(
struct host_state* state, uint32_t intent,
double value,
41 uint32_t trigger,
double price,
const char* label) {
43 memset(&request, 0,
sizeof(request));
51 request.
label = label;
59 struct host_state* state = (
struct host_state*)user;
65 if (state->bars == 1) {
67 }
else if (state->bars == 2) {
71 }
else if (state->bars == 4) {
73 }
else if (state->bars == 5) {
85 memset(&spec, 0,
sizeof(spec));
93 spec.
type =
"futures";
116 memset(&ext, 0,
sizeof(ext));
143 {100.10, 100.75, 99.40}, {100.10, 100.75, 99.40}, 2},
144 {
"QuantizeFills/HalfUp", 1u, 0u, 3,
145 {100.10, 100.75, 99.40}, {100.00, 100.75, 99.50}, 2},
146 {
"QuantizeFills/Directional", 1u, 1u, 3,
147 {100.10, 100.75, 99.40}, {100.25, 100.75, 99.25}, 2},
148 {
"QuantizeFillsAndTriggers/HalfUp", 2u, 0u, 5,
149 {100.10, 100.75, 99.40, 99.75, 99.60}, {100.00, 100.75, 99.50, 99.75, 99.50}, 3}
156 struct host_state state;
167 memset(&state, 0,
sizeof(state));
168 memset(&report, 0,
sizeof(report));
169 memset(&callbacks, 0,
sizeof(callbacks));
170 callbacks.struct_size = (uint32_t)
sizeof(callbacks);
172 callbacks.user = &state;
173 callbacks.on_bar =
on_bar;
177 fprintf(stderr,
"%s create: the runtime refused the callback table\n", mode->name);
192 printf(
"%-34s", mode->name);
193 memset(events, 0,
sizeof(events));
195 for (i = 0; i < written; ++i) {
197 printf(
" %.2f->%.2f", events[i].raw_price, events[i].resolved_price);
198 if (fills < mode->fills
199 && (events[i].raw_price != mode->raw[fills]
205 && events[i].
resolved_price != round(events[i].resolved_price / 0.25) * 0.25) {
211 printf(
" | trades=%d\n", trades);
212 if (fills != mode->fills || trades != mode->trades || state.refused != 0) ok = 0;
217 fprintf(stderr,
"%s: the fills are not the hand-computed ones\n", mode->name);
226 struct host_state state;
232 memset(&state, 0,
sizeof(state));
233 memset(&callbacks, 0,
sizeof(callbacks));
234 callbacks.struct_size = (uint32_t)
sizeof(callbacks);
236 callbacks.user = &state;
237 callbacks.on_bar =
on_bar;
239 if (!state.handle)
return 0;
241 printf(
"QuantizeFills with price_tick = 0: %s\n",
242 rc !=
PF_NATIVE_OK ?
"refused at configure" :
"NOT refused");
253 if (trades < 0)
return 1;
258 printf(
"closed trades: %d\n", closed);
259 return closed > 0 ? 0 : 1;
const char * strategy_get_last_error(pf_strategy_t s)
Returns the error message captured by the most recent run_backtest / run_backtest_full call on this s...
int strategy_native_run_v1(pf_strategy_t s, const pf_bar_t *bars, int n, pf_report_t *out)
Run n bars as one batch and fill out.
void strategy_native_host_free(pf_strategy_t s)
Release a handle from strategy_native_host_create_v1.
int strategy_native_position_v1(pf_strategy_t s, double *signed_units, double *average_price, uint64_t *lots)
Read the physical position.
int strategy_configure_native_ext_v1(pf_strategy_t s, const pf_native_run_spec_v1 *base, const pf_native_run_spec_ext_v1 *ext)
Configure a native run from the v1 specification plus the extension.
int strategy_native_events_v1(pf_strategy_t s, uint64_t after_ordinal, pf_native_event_v1 *out, int cap)
Copy up to cap events with an ordinal strictly greater than after_ordinal into out,...
pf_strategy_t strategy_native_host_create_v1(const pf_native_callbacks_v1 *callbacks)
Allocate a native host that forwards every kernel callback to callbacks.
int strategy_native_submit_v1(pf_strategy_t s, const pf_native_request_v1 *request, uint64_t *incarnation, uint32_t *reject)
Submit request.
void strategy_native_report_free_v1(pf_report_t *report)
Free the heap arrays inside a report filled by strategy_native_run_v1.
@ PF_NATIVE_INTENT_REDUCE
Reduce; see pf_native_reduction_t.
@ PF_NATIVE_INTENT_TRANSACT
intent_value signed units.
@ PF_NATIVE_INTENT_FLATTEN
Close the whole book.
@ PF_NATIVE_SPEC_EXT_PRICE_GRID
@ PF_NATIVE_TRIGGER_LIMIT
p1 = price; fill_through makes it market-if-touched.
@ PF_NATIVE_TRIGGER_STOP
p1 = price.
@ PF_NATIVE_TRIGGER_MARKET
p1, p2 ignored.
@ PF_NATIVE_REDUCE_EXPLICIT_UNITS
intent_value units.
@ PF_NATIVE_EVENT_APPLIED
#define PF_NATIVE_OK
Success.
static const pf_bar_t kBars[]
static pf_native_run_spec_v1 make_spec(void)
static int on_bar(void *user, const pf_bar_t *bar, const pf_native_decision_v1 *at)
#define PF_NATIVE_API_VERSION
Monotonic version of this header's native-C layouts.
static void submit(struct host_state *state, uint32_t intent, double value, uint32_t trigger, double price, const char *label)
static pf_native_run_spec_ext_v1 make_ext(uint32_t grid, uint32_t rounding)
static int on_bar(void *user, const pf_bar_t *bar, const pf_native_decision_v1 *at)
static const struct mode kModes[]
static int run_mode(const struct mode *mode)
static int grid_without_a_ladder_is_refused(void)
void * pf_strategy_t
Opaque handle to a compiled strategy instance.
Single OHLCV bar pushed into the engine.
The C host's strategy logic.
Where the kernel is, presented to every callback.
One recorded event, read back by strategy_native_events_v1.
double p1
Limit/stop price, or trail offset.
uint32_t struct_size
sizeof(pf_native_request_v1).
uint32_t intent
pf_native_intent_t.
uint32_t version
PF_NATIVE_API_VERSION.
double intent_value
The intent's own scalar; see pf_native_intent_t.
uint32_t trigger
pf_native_trigger_t.
uint32_t reduce_size
pf_native_reduction_t, REDUCE only.
The run-specification fields pf_native_run_spec_v1 predates.
uint32_t price_grid
NativePriceGrid.
uint32_t present_mask
pf_native_spec_ext_mask_t bits.
uint32_t struct_size
sizeof(pf_native_run_spec_ext_v1).
uint32_t version
PF_NATIVE_API_VERSION.
uint32_t grid_rounding
NativeGridRounding.
Versioned native run specification.
uint32_t allowed_open_directions
const char * basecurrency
const char * chart_timezone
Backtest report filled by run_backtest / run_backtest_full.
int total_trades
Closed-trade count (== trades_len), including the range-end close of a position still open after the ...