Skip to content

Commit 6e9b908

Browse files
committed
Add the intraday-cap limit setter to the Pine source host (R4-C L3b)
Add the public PineStrategyHost risk setter and route integer limits through adapter_.cap, preserving IntradayCap attachment and ledger semantics (R4-C A32). Add a focused source-host test and register it in the CTest suite. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016iF71UAEuQ7F6eqWgecAHm (cherry picked from commit 3d6035f1572bd1928a35b364103d38f43173b2c0)
1 parent 2a84db7 commit 6e9b908

4 files changed

Lines changed: 135 additions & 0 deletions

File tree

‎include/pineforge/source/pine_strategy_host.hpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class PineStrategyHost : public BacktestEngine, protected PineLanguageState {
2525
void set_pine_risk_max_cons_loss_days(int value);
2626
void set_pine_risk_max_drawdown(double value, bool percent);
2727
void set_pine_risk_max_intraday_loss(double value, bool percent);
28+
void set_pine_risk_max_intraday_filled_orders(int limit);
2829
void set_pine_risk_max_position_size(double value);
2930

3031
void strategy_entry(const std::string& id, bool is_long,

‎src/source/pine_strategy_host.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ void source::PineStrategyHost::set_pine_risk_max_intraday_loss(
7272
risk_max_intraday_loss_is_pct_ = percent;
7373
}
7474

75+
void source::PineStrategyHost::set_pine_risk_max_intraday_filled_orders(int limit) {
76+
adapter_.cap = limit;
77+
}
78+
7579
void source::PineStrategyHost::set_pine_risk_max_position_size(double value) {
7680
risk_max_position_size_ = value;
7781
}

‎tests/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ set(TEST_SOURCES
3838
test_native_host_repairs
3939
test_source_layer_native_defaults
4040
test_source_fingerprint
41+
test_source_host_intraday_cap_setter
4142
test_order_action_integration
4243
test_placement_facts
4344
test_order_cancellation
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// The explicit source risk setter must retain IntradayCap's assignment
2+
// semantics: attach the Pine policy, update its limit, and preserve its day
3+
// ledger when a script changes the limit at statement time.
4+
#include <pineforge/source/pine_strategy_host.hpp>
5+
6+
#include <cstdio>
7+
8+
using namespace pineforge;
9+
10+
namespace {
11+
12+
int checks = 0;
13+
int failures = 0;
14+
15+
#define CHECK(value) do { \
16+
++checks; \
17+
if (!(value)) { \
18+
++failures; \
19+
std::printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #value); \
20+
} \
21+
} while (0)
22+
23+
using Cap = compat::pine::IntradayCap;
24+
using CapAttachment = compat::pine::CapAttachment;
25+
26+
class Fixture final : public source::PineStrategyHost {
27+
public:
28+
void on_source_bar(const Bar&) override {}
29+
30+
const Cap& cap() const { return adapter_.cap; }
31+
32+
void seed_latched_day() {
33+
compat::pine::CapClock clock{};
34+
clock.timestamp = 1700000000000LL;
35+
clock.session = "24x7";
36+
clock.timezone = "UTC";
37+
clock.chart_day = 14;
38+
clock.chart_month = 9;
39+
40+
compat::pine::Calculation calculation{};
41+
calculation.stream_idle = true;
42+
calculation.fifo = true;
43+
calculation.bar = 11;
44+
45+
compat::pine::MatchedAttempt attempt{};
46+
attempt.kind = compat::pine::OrderKind::Entry;
47+
attempt.incarnation = 1;
48+
attempt.created_bar = calculation.bar;
49+
attempt.is_long = true;
50+
attempt.live_side = compat::pine::Side::Flat;
51+
attempt.pyramiding = 1;
52+
53+
const auto admission = adapter_.cap.pre_dispatch(
54+
clock, calculation, attempt, 0);
55+
(void)adapter_.cap.post_dispatch(
56+
admission, calculation, attempt, compat::pine::Side::Flat, 0,
57+
compat::pine::Prices{100.0, 100.0, 100.0, 100.0});
58+
}
59+
};
60+
61+
void check_cap_matches(const Cap& actual, const Cap& expected) {
62+
CHECK(actual.attachment() == expected.attachment());
63+
CHECK(actual.configuration().limit == expected.configuration().limit);
64+
CHECK(actual.budget().day().has_value() == expected.budget().day().has_value());
65+
if (actual.budget().day() && expected.budget().day())
66+
CHECK(*actual.budget().day() == *expected.budget().day());
67+
CHECK(actual.budget().charged_slots() == expected.budget().charged_slots());
68+
CHECK(actual.budget().latched() == expected.budget().latched());
69+
CHECK(actual.budget().transfer().has_value() == expected.budget().transfer().has_value());
70+
CHECK(actual.due_cause().has_value() == expected.due_cause().has_value());
71+
CHECK(actual.next_action() == expected.next_action());
72+
}
73+
74+
void test_setter_matches_integer_assignment() {
75+
Fixture host;
76+
Cap direct;
77+
78+
host.set_pine_risk_max_intraday_filled_orders(4);
79+
direct = 4;
80+
check_cap_matches(host.cap(), direct);
81+
CHECK(host.cap().attachment() == CapAttachment::LegacySource);
82+
CHECK(host.cap().configuration().limit == 4);
83+
CHECK(host.cap().budget().charged_slots() == 0);
84+
85+
// Build an identical, already-latched day ledger on both objects. The
86+
// second assignment must change only the configured limit.
87+
host.set_pine_risk_max_intraday_filled_orders(1);
88+
direct = 1;
89+
host.seed_latched_day();
90+
91+
compat::pine::CapClock clock{};
92+
clock.timestamp = 1700000000000LL;
93+
clock.session = "24x7";
94+
clock.timezone = "UTC";
95+
clock.chart_day = 14;
96+
clock.chart_month = 9;
97+
compat::pine::Calculation calculation{};
98+
calculation.stream_idle = true;
99+
calculation.fifo = true;
100+
calculation.bar = 11;
101+
compat::pine::MatchedAttempt attempt{};
102+
attempt.kind = compat::pine::OrderKind::Entry;
103+
attempt.incarnation = 1;
104+
attempt.created_bar = calculation.bar;
105+
attempt.is_long = true;
106+
attempt.live_side = compat::pine::Side::Flat;
107+
attempt.pyramiding = 1;
108+
const auto admission = direct.pre_dispatch(clock, calculation, attempt, 0);
109+
(void)direct.post_dispatch(
110+
admission, calculation, attempt, compat::pine::Side::Flat, 0,
111+
compat::pine::Prices{100.0, 100.0, 100.0, 100.0});
112+
113+
CHECK(host.cap().budget().day().has_value());
114+
CHECK(host.cap().budget().charged_slots() == 1);
115+
CHECK(host.cap().budget().latched());
116+
host.set_pine_risk_max_intraday_filled_orders(7);
117+
direct = 7;
118+
check_cap_matches(host.cap(), direct);
119+
CHECK(host.cap().budget().charged_slots() == 1);
120+
CHECK(host.cap().budget().latched());
121+
}
122+
123+
} // namespace
124+
125+
int main() {
126+
test_setter_matches_integer_assignment();
127+
std::printf("checks=%d failures=%d\n", checks, failures);
128+
return failures == 0 ? 0 : 1;
129+
}

0 commit comments

Comments
 (0)