Skip to content

Commit ecbcf55

Browse files
committed
Introduce source::PineStrategyHost and the on_source_bar hook; port Pine tests and the example (R4-C L1)
Port Pine-facing test fixtures and helper headers to source::PineStrategyHost, preserving direct engine.on_bar calls through the final forwarding hook. Retarget the handwritten runner example and committed tutorial consumers; leave NativeStrategyHost and pure native request/settlement/position/account fixtures untouched. Contract rules: R4-C §2 P1/P2, §3.1, §7 L1; amendments A5 and A8. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016iF71UAEuQ7F6eqWgecAHm
1 parent 51f6c4c commit ecbcf55

210 files changed

Lines changed: 1418 additions & 1208 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎runner/examples/strategy.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// A hand-written native strategy. Codegen output exposes the same lifecycle.
33
#include <pineforge/engine.hpp>
4+
#include <pineforge/source/pine_strategy_host.hpp>
45

5-
class NativeExample final : public pineforge::BacktestEngine {
6+
class NativeExample final : public pineforge::source::PineStrategyHost {
67
public:
78
NativeExample() {
89
initial_capital_ = 100000;
910
default_qty_value_ = 1;
1011
}
11-
void on_bar(const pineforge::Bar &) override {
12+
void on_source_bar(const pineforge::Bar &) override {
1213
// Alternating market entry/close gives a small deterministic test.
1314
if (bar_index_ % 4 == 1)
1415
strategy_entry("Long", true);

‎tests/admission_literal_book.hpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <pineforge/pineforge.h>
33
#include <pineforge/engine.hpp>
4+
#include <pineforge/source/pine_strategy_host.hpp>
45
#include <pineforge/pending_order_mirror.hpp>
56
#include <algorithm>
67
#include <stdexcept>
@@ -18,11 +19,11 @@ POINT(Dispatch,apply_filled_order_to_state);
1819
POINT(Compact,compact_filled_pending_orders);
1920
POINT(Reduce,reduce_oca_group);
2021
#undef POINT
21-
class Book:public BacktestEngine{
22+
class Book:public pineforge::source::PineStrategyHost{
2223
public:
2324
Book(){initial_capital_=1000;commission_value_=0;slippage_=0;pyramiding_=2;qty_step_=1;
2425
current_bar_={100,100,100,100,1,0};next_order_incarnation_=41;next_order_seq_=4;}
25-
void on_bar(const Bar&)override{}
26+
void on_source_bar(const Bar&)override{}
2627
void add(const char* id,double qty,bool buy=true,double stop=missing){strategy_entry(id,buy,missing,stop,qty);}
2728
void raw(const char* id,double qty,bool buy=true){strategy_order(id,buy,qty);}
2829
void cancel(const char* id){strategy_cancel(id);}

‎tests/reservation_expansion_test_access.hpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <pineforge/pineforge.h>
33
#include <pineforge/engine.hpp>
4+
#include <pineforge/source/pine_strategy_host.hpp>
45
#include <pineforge/pending_order_mirror.hpp>
56
#include <algorithm>
67
#include <stdexcept>
@@ -31,15 +32,15 @@ CHECKPOINT(Revive, revive_position_brackets_after_margin_call_partial);
3132

3233
using namespace pineforge;
3334
constexpr double missing = std::numeric_limits<double>::quiet_NaN();
34-
class Book : public BacktestEngine {
35+
class Book : public pineforge::source::PineStrategyHost {
3536
public:
3637
Book() {
3738
initial_capital_=100000; commission_value_=0; margin_long_=margin_short_=0;
3839
pyramiding_=20; process_orders_on_close_=true; qty_step_=std::ldexp(1.0,-32);
3940
current_bar_={100,100,100,100,1,0}; next_position_cycle_seq_=7;
4041
next_order_incarnation_=40;
4142
}
42-
void on_bar(const Bar&) override {}
43+
void on_source_bar(const Bar&) override {}
4344
std::vector<uint64_t> retired;
4445
double trail=missing; int closed_bar=-1; uint64_t closed_id=0; bool was_long=false;
4546
PendingOrder& get(const std::string& id) {

‎tests/test_aapl15_margin_brackets.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565

6666
#include <pineforge/bar.hpp>
6767
#include <pineforge/engine.hpp>
68+
#include <pineforge/source/pine_strategy_host.hpp>
6869

6970
using namespace pineforge;
7071

@@ -339,7 +340,7 @@ static const BarRow kFordBouga0808[] = {
339340
// the next open, integer lots, mintick 0.01, no commission. FIXED default
340341
// sizing by default (the tapes' fixed lots); PERCENT_OF_EQUITY 100 for the
341342
// all-in reversal shapes.
342-
class Probe : public BacktestEngine {
343+
class Probe : public pineforge::source::PineStrategyHost {
343344
public:
344345
explicit Probe(double capital, double default_qty = 1.0) {
345346
initial_capital_ = capital;
@@ -359,7 +360,7 @@ class Probe : public BacktestEngine {
359360
set_margin_call_enabled(true);
360361
}
361362
std::function<void(Probe&, int)> script;
362-
void on_bar(const Bar& /*bar*/) override {
363+
void on_source_bar(const Bar& /*bar*/) override {
363364
if (script) script(*this, bar_index_);
364365
}
365366
void all_in() {

‎tests/test_accounting_reconciliation.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <vector>
2828

2929
#include <pineforge/engine.hpp>
30+
#include <pineforge/source/pine_strategy_host.hpp>
3031
#include <pineforge/bar.hpp>
3132
#include <pineforge/na.hpp>
3233

@@ -52,7 +53,7 @@ static bool near(double a, double b, double tol = 1e-6) {
5253
namespace {
5354

5455
// Momentum-flip generator (wins + losses), exposing the protected accumulators.
55-
class ReconProbe : public BacktestEngine {
56+
class ReconProbe : public pineforge::source::PineStrategyHost {
5657
public:
5758
double prev_close_ = std::numeric_limits<double>::quiet_NaN();
5859

@@ -64,7 +65,7 @@ class ReconProbe : public BacktestEngine {
6465
commission_value_ = 0; // exact recompute
6566
pyramiding_ = 1;
6667
}
67-
void on_bar(const Bar& bar) override {
68+
void on_source_bar(const Bar& bar) override {
6869
if (!std::isnan(prev_close_)) {
6970
if (bar.close > prev_close_)
7071
strategy_entry("L", true, std::numeric_limits<double>::quiet_NaN(),

‎tests/test_adversarial_ohlcv.cpp‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <vector>
2323

2424
#include <pineforge/engine.hpp>
25+
#include <pineforge/source/pine_strategy_host.hpp>
2526
#include <pineforge/bar.hpp>
2627
#include <pineforge/na.hpp>
2728

@@ -44,21 +45,21 @@ namespace {
4445
constexpr double kNaN = std::numeric_limits<double>::quiet_NaN();
4546
constexpr double kInf = std::numeric_limits<double>::infinity();
4647

47-
class QtyProbe : public BacktestEngine {
48+
class QtyProbe : public pineforge::source::PineStrategyHost {
4849
public:
4950
QtyProbe() {
5051
initial_capital_ = 100'000;
5152
default_qty_type_ = QtyType::PERCENT_OF_EQUITY;
5253
default_qty_value_ = 10.0; // 10% of equity
5354
slippage_ = 0; commission_value_ = 0; pyramiding_ = 1;
5455
}
55-
void on_bar(const Bar&) override {}
56+
void on_source_bar(const Bar&) override {}
5657
double cq(double fp, double qv, int qt) { return calc_qty_for_type(fp, qv, qt); }
5758
double cq_default(double fp) { return calc_qty(fp); }
5859
};
5960

6061
// Momentum %-equity strategy used to stress degenerate feeds end-to-end.
61-
class StressProbe : public BacktestEngine {
62+
class StressProbe : public pineforge::source::PineStrategyHost {
6263
public:
6364
double prev_ = kNaN;
6465
StressProbe() {
@@ -67,7 +68,7 @@ class StressProbe : public BacktestEngine {
6768
default_qty_value_ = 5.0;
6869
slippage_ = 0; commission_value_ = 0; pyramiding_ = 1;
6970
}
70-
void on_bar(const Bar& b) override {
71+
void on_source_bar(const Bar& b) override {
7172
if (!std::isnan(prev_)) {
7273
if (b.close > prev_) strategy_entry("L", true, kNaN, kNaN, kNaN, "up");
7374
else if (b.close < prev_) strategy_entry("S", false, kNaN, kNaN, kNaN, "dn");

‎tests/test_affordability_fx.cpp‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <pineforge/bar.hpp>
2929
#include <pineforge/engine.hpp>
30+
#include <pineforge/source/pine_strategy_host.hpp>
3031

3132
using namespace pineforge;
3233

@@ -57,7 +58,7 @@ namespace {
5758
// because process_orders_on_close is on), then closes it on bar 1. Whether the
5859
// entry survives the affordability gate is observable as trade_count() == 1 (or
5960
// 0 if rejected).
60-
class FxProbe : public BacktestEngine {
61+
class FxProbe : public pineforge::source::PineStrategyHost {
6162
public:
6263
explicit FxProbe(double fx_or_nan, double commission_percent = 0.0) {
6364
initial_capital_ = 1000.0;
@@ -70,7 +71,7 @@ class FxProbe : public BacktestEngine {
7071
if (!std::isnan(fx_or_nan))
7172
set_syminfo_metadata("account_currency_fx", fx_or_nan);
7273
}
73-
void on_bar(const Bar& /*bar*/) override {
74+
void on_source_bar(const Bar& /*bar*/) override {
7475
if (bar_index_ == 0)
7576
strategy_entry("L", true, kNaN, kNaN, 1.0); // qty=1 market long
7677
else if (bar_index_ == 1)
@@ -84,7 +85,7 @@ class FxProbe : public BacktestEngine {
8485
// A default 100%-of-equity order is placed under FX=1.0 and fills on the
8586
// next bar after FX rolls to 1.001. TV admits the frozen signal snapshot, then
8687
// revalues the live fill and emits a broker margin trim at the new rate.
87-
class FrozenFxRolloverProbe : public BacktestEngine {
88+
class FrozenFxRolloverProbe : public pineforge::source::PineStrategyHost {
8889
public:
8990
FrozenFxRolloverProbe() {
9091
initial_capital_ = 10000.0;
@@ -96,7 +97,7 @@ class FrozenFxRolloverProbe : public BacktestEngine {
9697
qty_step_ = 0.0001;
9798
process_orders_on_close_ = false;
9899
}
99-
void on_bar(const Bar& /*bar*/) override {
100+
void on_source_bar(const Bar& /*bar*/) override {
100101
if (bar_index_ == 0) strategy_entry("L", true);
101102
}
102103
int trades() const { return trade_count(); }
@@ -109,7 +110,7 @@ class FrozenFxRolloverProbe : public BacktestEngine {
109110
// A live 1x long crosses a timestamped FX epoch on bar 2. The broker must
110111
// consume that epoch and emit any required trim at bar OPEN, before on_bar can
111112
// observe the position or place another order.
112-
class CarriedFxRolloverOrderingProbe : public BacktestEngine {
113+
class CarriedFxRolloverOrderingProbe : public pineforge::source::PineStrategyHost {
113114
public:
114115
CarriedFxRolloverOrderingProbe() {
115116
initial_capital_ = 10000.0;
@@ -120,7 +121,7 @@ class CarriedFxRolloverOrderingProbe : public BacktestEngine {
120121
qty_step_ = 0.0001;
121122
process_orders_on_close_ = true;
122123
}
123-
void on_bar(const Bar& /*bar*/) override {
124+
void on_source_bar(const Bar& /*bar*/) override {
124125
++on_bar_calls_;
125126
if (bar_index_ == 0) {
126127
strategy_entry("L", true, kNaN, kNaN, 100.0);
@@ -150,7 +151,7 @@ class CarriedFxRolloverOrderingProbe : public BacktestEngine {
150151
// liquidation rule. They must reject before on_bar instead of silently
151152
// falling through to the end-of-bar adverse-price pass. (1x short is
152153
// supported by cell A1; keep this probe for leveraged-only fail-closed.)
153-
class UnsupportedCarriedFxRolloverProbe : public BacktestEngine {
154+
class UnsupportedCarriedFxRolloverProbe : public pineforge::source::PineStrategyHost {
154155
public:
155156
UnsupportedCarriedFxRolloverProbe(bool is_long, double margin_pct)
156157
: is_long_(is_long) {
@@ -161,7 +162,7 @@ class UnsupportedCarriedFxRolloverProbe : public BacktestEngine {
161162
margin_short_ = is_long ? 100.0 : margin_pct;
162163
process_orders_on_close_ = true;
163164
}
164-
void on_bar(const Bar& /*bar*/) override {
165+
void on_source_bar(const Bar& /*bar*/) override {
165166
++on_bar_calls_;
166167
if (bar_index_ == 0) {
167168
strategy_entry(is_long_ ? "L" : "S", is_long_,
@@ -177,7 +178,7 @@ class UnsupportedCarriedFxRolloverProbe : public BacktestEngine {
177178

178179
// Cell A1 dual of CarriedFxRolloverOrderingProbe: carried 1x short under a
179180
// timestamped FX epoch change. Mirrors long sizing (qty=100, capital=10000).
180-
class CarriedShortFxRolloverProbe : public BacktestEngine {
181+
class CarriedShortFxRolloverProbe : public pineforge::source::PineStrategyHost {
181182
public:
182183
CarriedShortFxRolloverProbe() {
183184
initial_capital_ = 10000.0;
@@ -189,7 +190,7 @@ class CarriedShortFxRolloverProbe : public BacktestEngine {
189190
qty_step_ = 0.0001;
190191
process_orders_on_close_ = true;
191192
}
192-
void on_bar(const Bar& /*bar*/) override {
193+
void on_source_bar(const Bar& /*bar*/) override {
193194
++on_bar_calls_;
194195
if (bar_index_ == 0) {
195196
strategy_entry("S", /*is_long=*/false, kNaN, kNaN, 100.0);
@@ -221,7 +222,7 @@ class CarriedShortFxRolloverProbe : public BacktestEngine {
221222
// crossed that epoch while still flat. The flat crossing must consume the
222223
// rollover permanently: once margin calls are enabled after the fill, the next
223224
// bar must not replay the old epoch against the newly opened position.
224-
class FlatEpochConsumptionProbe : public BacktestEngine {
225+
class FlatEpochConsumptionProbe : public pineforge::source::PineStrategyHost {
225226
public:
226227
FlatEpochConsumptionProbe() {
227228
initial_capital_ = 10000.0;
@@ -233,7 +234,7 @@ class FlatEpochConsumptionProbe : public BacktestEngine {
233234
qty_step_ = 0.1;
234235
process_orders_on_close_ = false;
235236
}
236-
void on_bar(const Bar& /*bar*/) override {
237+
void on_source_bar(const Bar& /*bar*/) override {
237238
if (bar_index_ == 0) {
238239
// Signal under FX=1.0; the frozen admission tuple lets this fill at
239240
// the next open after FX=1.001 becomes effective.
@@ -263,7 +264,7 @@ class FlatEpochConsumptionProbe : public BacktestEngine {
263264
// Entry fees are paid in account currency at the entry fill. A later FX epoch
264265
// changes open gross PnL and exit-time trade reporting, but must not reprice the
265266
// already-paid fee exposed by strategy.opentrades.* while the slice is live.
266-
class EntryFeeAccessorLifecycleProbe : public BacktestEngine {
267+
class EntryFeeAccessorLifecycleProbe : public pineforge::source::PineStrategyHost {
267268
public:
268269
EntryFeeAccessorLifecycleProbe(CommissionType type, double value,
269270
double qty)
@@ -275,7 +276,7 @@ class EntryFeeAccessorLifecycleProbe : public BacktestEngine {
275276
margin_long_ = 0.0; // isolate accounting from broker liquidation
276277
process_orders_on_close_ = true;
277278
}
278-
void on_bar(const Bar& /*bar*/) override {
279+
void on_source_bar(const Bar& /*bar*/) override {
279280
if (bar_index_ == 0) {
280281
strategy_entry("L", true, kNaN, kNaN, qty_);
281282
} else if (bar_index_ == 1) {
@@ -299,7 +300,7 @@ class EntryFeeAccessorLifecycleProbe : public BacktestEngine {
299300

300301
// Exercises lifecycle transitions that retain or replace PyramidEntry slices:
301302
// a rate-1 entry, a rate-2 pyramid add, a FIFO partial exit, then a reversal.
302-
class PyramidEntryFeeLifecycleProbe : public BacktestEngine {
303+
class PyramidEntryFeeLifecycleProbe : public pineforge::source::PineStrategyHost {
303304
public:
304305
PyramidEntryFeeLifecycleProbe() {
305306
initial_capital_ = 10000.0;
@@ -311,7 +312,7 @@ class PyramidEntryFeeLifecycleProbe : public BacktestEngine {
311312
process_orders_on_close_ = true;
312313
pyramiding_ = 3;
313314
}
314-
void on_bar(const Bar& /*bar*/) override {
315+
void on_source_bar(const Bar& /*bar*/) override {
315316
if (bar_index_ == 0) {
316317
strategy_entry("L1", true, kNaN, kNaN, 2.0);
317318
} else if (bar_index_ == 1) {
@@ -355,7 +356,7 @@ class PyramidEntryFeeLifecycleProbe : public BacktestEngine {
355356
// With a 2x FX rollover, repricing the old 10% entry fee would manufacture a
356357
// broker-open deficit and an 0.08-contract margin row. The paid rate-1 fee
357358
// leaves the carried 2.75-contract position affordable.
358-
class CarriedEntryFeeSnapshotProbe : public BacktestEngine {
359+
class CarriedEntryFeeSnapshotProbe : public pineforge::source::PineStrategyHost {
359360
public:
360361
CarriedEntryFeeSnapshotProbe() {
361362
initial_capital_ = 600.0;
@@ -366,7 +367,7 @@ class CarriedEntryFeeSnapshotProbe : public BacktestEngine {
366367
qty_step_ = 0.01;
367368
process_orders_on_close_ = true;
368369
}
369-
void on_bar(const Bar& /*bar*/) override {
370+
void on_source_bar(const Bar& /*bar*/) override {
370371
if (bar_index_ == 0) {
371372
strategy_entry("L", true, kNaN, kNaN, 2.75);
372373
} else if (bar_index_ == 1) {
@@ -385,7 +386,7 @@ class CarriedEntryFeeSnapshotProbe : public BacktestEngine {
385386
// Post-fill affordability must sum each live slice's paid fee: rate-1 L1 costs
386387
// 20 and rate-2 L2 costs 10. Repricing both at rate 2 would use 50 instead of
387388
// 30 and manufacture a margin trim from an otherwise affordable position.
388-
class PostFillEntryFeeSnapshotProbe : public BacktestEngine {
389+
class PostFillEntryFeeSnapshotProbe : public pineforge::source::PineStrategyHost {
389390
public:
390391
PostFillEntryFeeSnapshotProbe() {
391392
initial_capital_ = 540.0;
@@ -397,7 +398,7 @@ class PostFillEntryFeeSnapshotProbe : public BacktestEngine {
397398
process_orders_on_close_ = true;
398399
pyramiding_ = 2;
399400
}
400-
void on_bar(const Bar& /*bar*/) override {
401+
void on_source_bar(const Bar& /*bar*/) override {
401402
if (bar_index_ == 0) {
402403
strategy_entry("L1", true, kNaN, kNaN, 2.0);
403404
} else if (bar_index_ == 1) {
@@ -816,7 +817,7 @@ int main() {
816817
};
817818
const int64_t timestamps[] = {1000, 2000};
818819
const double rates[] = {1.0, 1.001};
819-
class FlatEpochShortProbe : public BacktestEngine {
820+
class FlatEpochShortProbe : public pineforge::source::PineStrategyHost {
820821
public:
821822
FlatEpochShortProbe() {
822823
initial_capital_ = 10000.0;
@@ -829,7 +830,7 @@ int main() {
829830
qty_step_ = 0.1;
830831
process_orders_on_close_ = false;
831832
}
832-
void on_bar(const Bar& /*bar*/) override {
833+
void on_source_bar(const Bar& /*bar*/) override {
833834
if (bar_index_ == 0) {
834835
strategy_entry("S", /*is_long=*/false);
835836
} else if (bar_index_ == 1) {

0 commit comments

Comments
 (0)