|
| 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