|
1 | 1 | #pragma once |
2 | 2 |
|
3 | | -#include <pineforge/native_host.hpp> |
4 | | -#include <pineforge/source/pine_adapter.hpp> |
5 | | -#include <pineforge/source/pine_scheduler.hpp> |
6 | | - |
7 | | -#include <cstdint> |
8 | | -#include <limits> |
9 | | -#include <string> |
10 | | -#include <utility> |
11 | | -#include <vector> |
| 3 | +// Compatibility spelling retained for the L2 fixture twins. L3a has one |
| 4 | +// source host: generated strategies and these fixtures both lower through |
| 5 | +// PineStrategyHost's native provider and scheduler. |
| 6 | +#include <pineforge/source/pine_strategy_host.hpp> |
12 | 7 |
|
13 | 8 | namespace pineforge::source { |
14 | 9 |
|
15 | | -// L2-only fixture host. It is deliberately a distinct native host rather than |
16 | | -// a base of, alias for, or edit to the live PineStrategyHost. Consequently no |
17 | | -// generated route can reach this lowering before L3a switches inheritance. |
18 | | -class PineNativeHost : public NativeStrategyHost { |
19 | | -public: |
20 | | - enum class FixturePendingOrderType { MARKET, EXIT, ENTRY, RAW_ORDER }; |
21 | | - struct FixturePendingOrder { |
22 | | - std::string id; |
23 | | - FixturePendingOrderType type = FixturePendingOrderType::MARKET; |
24 | | - double default_stop_placement_qty = std::numeric_limits<double>::quiet_NaN(); |
25 | | - double default_stop_sizing_price = std::numeric_limits<double>::quiet_NaN(); |
26 | | - }; |
27 | | - |
28 | | - // A read-only map-shaped facade lets fixture twins express legacy ledger |
29 | | - // observations against the adapter's live source cohort facts without |
30 | | - // retaining the executable legacy id ledger. It is deliberately protected |
31 | | - // below rather than part of the generated surface. |
32 | | - class SourceIdLedgerView { |
33 | | - public: |
34 | | - struct value_type { double second = 0.0; }; |
35 | | - class const_iterator { |
36 | | - public: |
37 | | - const value_type* operator->() const noexcept { return &value_; } |
38 | | - bool operator==(const const_iterator& other) const noexcept { |
39 | | - return present_ == other.present_; |
40 | | - } |
41 | | - bool operator!=(const const_iterator& other) const noexcept { |
42 | | - return !(*this == other); |
43 | | - } |
44 | | - private: |
45 | | - friend class SourceIdLedgerView; |
46 | | - bool present_ = false; |
47 | | - value_type value_{}; |
48 | | - }; |
49 | | - |
50 | | - const_iterator find(const std::string& id) const noexcept { |
51 | | - const double units = host_ ? host_->adapter_.source_unclosed_qty_for(id) : 0.0; |
52 | | - const_iterator result; |
53 | | - result.present_ = units > 0.0; |
54 | | - result.value_.second = units; |
55 | | - return result; |
56 | | - } |
57 | | - const_iterator end() const noexcept { return {}; } |
58 | | - |
59 | | - private: |
60 | | - friend class PineNativeHost; |
61 | | - explicit SourceIdLedgerView(const PineNativeHost* host) noexcept : host_(host) {} |
62 | | - const PineNativeHost* host_ = nullptr; |
63 | | - }; |
64 | | - |
65 | | - explicit PineNativeHost( |
66 | | - compat::pine::CapAttachment cap = compat::pine::CapAttachment::None); |
67 | | - ~PineNativeHost() override; |
68 | | - |
69 | | - PineNativeHost(const PineNativeHost&) = delete; |
70 | | - PineNativeHost& operator=(const PineNativeHost&) = delete; |
71 | | - |
72 | | - void prepare_native_begin(const NativeBeginArgs&) final; |
73 | | - void on_native_run_begin() final; |
74 | | - void on_native_bar_open(const Bar&, const NativeDecisionContext&) final; |
75 | | - void on_native_bar(const Bar&, const NativeDecisionContext&) final; |
76 | | - void on_native_applied(const native_order::ExecutionAppliedEvent&, |
77 | | - const NativeDecisionContext&) final; |
78 | | - native_order::ExecutionTerms resolve_execution_terms( |
79 | | - const NativeExecutionTermsFacts&) const final; |
80 | | - NativePrecommitVerdict validate_execution_precommit( |
81 | | - const NativePrecommitView&) const final; |
82 | | - |
83 | | - virtual void on_source_bar(const Bar&) = 0; |
84 | | - |
85 | | - // Generated source surface, forwarded into the adapter. These signatures |
86 | | - // intentionally match the live source host until L3a joins the routes. |
87 | | - void configure_pine_strategy(const PineStrategyConfig&); |
88 | | - void set_strategy_override(const StrategyOverrides&); |
89 | | - void set_pine_risk_direction(int direction); |
90 | | - void set_pine_risk_max_cons_loss_days(int value); |
91 | | - void set_pine_risk_max_drawdown(double value, bool percent); |
92 | | - void set_pine_risk_max_intraday_loss(double value, bool percent); |
93 | | - void set_pine_risk_max_intraday_filled_orders(int limit); |
94 | | - void set_pine_risk_max_position_size(double value); |
95 | | - void enable_pine_intraday_cap(); |
96 | | - void attach_pine_execution_adapter(); |
97 | | - void set_syminfo_metadata(const std::string& key, double value) override; |
98 | | - |
99 | | - void strategy_entry(const std::string& id, bool is_long, |
100 | | - double limit_price = std::numeric_limits<double>::quiet_NaN(), |
101 | | - double stop_price = std::numeric_limits<double>::quiet_NaN(), |
102 | | - double qty = std::numeric_limits<double>::quiet_NaN(), |
103 | | - const std::string& comment = {}, const std::string& oca_name = {}, |
104 | | - int oca_type = 0, int qty_type = -1); |
105 | | - void strategy_close(const std::string& id, const std::string& comment = {}, |
106 | | - double qty = std::numeric_limits<double>::quiet_NaN(), |
107 | | - double qty_percent = std::numeric_limits<double>::quiet_NaN(), |
108 | | - bool immediately = false); |
109 | | - void strategy_close(const std::string& id, const std::string& comment, |
110 | | - double qty, double qty_percent, bool immediately, |
111 | | - std::uint64_t callsite_token); |
112 | | - void strategy_close_all(); |
113 | | - void strategy_exit(const std::string& id, const std::string& from_entry, |
114 | | - double limit_price, double stop_price, |
115 | | - double trail_points = std::numeric_limits<double>::quiet_NaN(), |
116 | | - double trail_offset = std::numeric_limits<double>::quiet_NaN(), |
117 | | - double trail_price = std::numeric_limits<double>::quiet_NaN(), |
118 | | - double qty_percent = 100.0, const std::string& comment = {}, |
119 | | - double qty = std::numeric_limits<double>::quiet_NaN(), |
120 | | - const std::string& oca_name = {}, |
121 | | - double profit_ticks = std::numeric_limits<double>::quiet_NaN(), |
122 | | - double loss_ticks = std::numeric_limits<double>::quiet_NaN()); |
123 | | - void strategy_exit_cancel_bracket(const std::string& exit_id, |
124 | | - const std::string& from_entry, |
125 | | - const std::string& comment = {}); |
126 | | - void strategy_cancel(const std::string& id); |
127 | | - void strategy_cancel_all(); |
128 | | - void strategy_order(const std::string& id, bool is_long, double qty, |
129 | | - double limit_price = std::numeric_limits<double>::quiet_NaN(), |
130 | | - double stop_price = std::numeric_limits<double>::quiet_NaN(), |
131 | | - const std::string& oca_name = {}, int oca_type = 0); |
132 | | - |
133 | | - bool is_first_tick() const noexcept { return scheduler_.is_first_tick(); } |
134 | | - bool is_last_tick() const noexcept { return scheduler_.is_last_tick(); } |
135 | | - bool history_advances_new_bar() const noexcept { |
136 | | - return scheduler_.history_advances_new_bar(); |
137 | | - } |
138 | | - bool security_series_slot_is_new(int slot) const noexcept { |
139 | | - return scheduler_.security_series_slot_is_new(slot); |
140 | | - } |
141 | | - int pine_bar_index() const noexcept { return source_bar_index_; } |
142 | | - int pine_last_bar_index() const noexcept { return source_last_bar_index_; } |
143 | | - double prev_chart_close() const noexcept { return scheduler_.previous_chart_close(); } |
144 | | - int last_bar_dual_entry_path() const noexcept { |
145 | | - return adapter_.pending_intent_view().last_bar_dual_entry_path(); |
146 | | - } |
147 | | - const PendingIntentView& pending_intent_view() const noexcept { |
148 | | - return adapter_.pending_intent_view(); |
149 | | - } |
150 | | - int short_seed_collision_role_v1(native_order::RequestHandle handle) const noexcept { |
151 | | - return adapter_.short_seed_collision_role_v1(std::move(handle)); |
152 | | - } |
153 | | - double live_position_size() const override { return physical_position().signed_units; } |
154 | | - int pending_order_count() const noexcept { return pending_intent_view().size(); } |
155 | | - int probe_fill_qty(int index, double fill_price, double* qty, int* close_only, |
156 | | - int* partition) const noexcept { |
157 | | - return pending_intent_view().probe_fill_qty(index, fill_price, qty, close_only, partition); |
158 | | - } |
159 | | - int pending_order_level_resolved(int index) const noexcept { |
160 | | - return pending_intent_view().level_resolved(index); |
161 | | - } |
162 | | - int pending_order_effective_levels(int index, double* stop, double* limit, |
163 | | - double* trail_activation) const noexcept { |
164 | | - return pending_intent_view().effective_levels(index, stop, limit, trail_activation); |
165 | | - } |
166 | | - |
167 | | -protected: |
168 | | - // Fixture-compatible source setting used by direct C++ oracle fixtures. |
169 | | - // It is translated at command lowering; it is not a generic-kernel field. |
170 | | - enum class RiskDirection { BOTH, LONG_ONLY, SHORT_ONLY }; |
171 | | - SourceIdLedgerView source_id_ledger_view() const noexcept { |
172 | | - return SourceIdLedgerView(this); |
173 | | - } |
174 | | - const std::vector<FixturePendingOrder>& source_pending_view() const { |
175 | | - source_pending_view_cache_.clear(); |
176 | | - source_pending_view_cache_.reserve(adapter_.pending_same_bar_commands_.size() |
177 | | - + adapter_.source_shadow_pending_.size() + adapter_.live_handles_.size()); |
178 | | - const auto append = [&](const PlacementSnapshot& snapshot, const std::string& label) { |
179 | | - FixturePendingOrderType type = FixturePendingOrderType::MARKET; |
180 | | - switch (snapshot.family) { |
181 | | - case PineOrderFamily::Close: |
182 | | - case PineOrderFamily::CloseAll: |
183 | | - case PineOrderFamily::ExitLimit: |
184 | | - case PineOrderFamily::ExitStop: |
185 | | - case PineOrderFamily::ExitTrail: |
186 | | - case PineOrderFamily::Margin: |
187 | | - type = FixturePendingOrderType::EXIT; |
188 | | - break; |
189 | | - case PineOrderFamily::Order: |
190 | | - type = FixturePendingOrderType::RAW_ORDER; |
191 | | - break; |
192 | | - case PineOrderFamily::Entry: |
193 | | - type = FixturePendingOrderType::MARKET; |
194 | | - break; |
195 | | - } |
196 | | - const std::string& id = snapshot.frozen_market_targeted_close ? label : snapshot.source_id; |
197 | | - source_pending_view_cache_.push_back({id, type, |
198 | | - snapshot.sizing.frozen_units, snapshot.sizing.price}); |
199 | | - }; |
200 | | - for (const auto& command : adapter_.pending_same_bar_commands_) { |
201 | | - append(command.snapshot, command.request.label); |
202 | | - } |
203 | | - for (const auto& shadow : adapter_.source_shadow_pending_) { |
204 | | - append(shadow.snapshot, shadow.label); |
205 | | - } |
206 | | - for (const auto& handle : adapter_.live_handles_) { |
207 | | - const auto found = adapter_.placement_.find(handle.incarnation); |
208 | | - if (found != adapter_.placement_.end()) append(found->second, found->second.source_id); |
209 | | - } |
210 | | - return source_pending_view_cache_; |
211 | | - } |
212 | | - void hash_source_extension(BrokerStateHashSink&) const override; |
213 | | - |
214 | | -private: |
215 | | - friend class PineScheduler; |
216 | | - |
217 | | - StagedConfiguration staged_configuration() const; |
218 | | - void scheduler_prepare_script_run(const std::vector<Bar>& bars, bool static_eligible, |
219 | | - int expected_script_bars); |
220 | | - void scheduler_configure_security_evaluators(); |
221 | | - void scheduler_prepare_chart_day_partition(const std::vector<Bar>& bars); |
222 | | - void scheduler_record_range_end(const Bar&); |
223 | | - void scheduler_publish_source_bar(const Bar&, bool first_tick, bool advance_source_index = true); |
224 | | - void project_short_seed_report_rows(const native_order::ExecutionAppliedEvent&); |
225 | | - bool scheduler_coof_enabled() const noexcept { return config_.calc_on_order_fills; } |
226 | | - static PineStrategyConfig apply_overrides(PineStrategyConfig, const StrategyOverrides&); |
227 | | - |
228 | | - // Read-only, derived fixture cache for exact oracle observations. It does |
229 | | - // not participate in behavior or persistence; the durable adapter facts |
230 | | - // it projects are hashed by PineExecutionAdapter::hash_state. |
231 | | - mutable std::vector<FixturePendingOrder> source_pending_view_cache_; |
232 | | - |
233 | | -protected: |
234 | | - // @source-state begin |
235 | | - PineStrategyConfig config_{}; |
236 | | - StrategyOverrides override_{}; |
237 | | - QtyType default_qty_type_ = QtyType::FIXED; |
238 | | - double default_qty_value_ = 1.0; |
239 | | - int pyramiding_ = 1; |
240 | | - bool close_entries_rule_any_ = false; |
241 | | - RiskDirection risk_direction_ = RiskDirection::BOTH; |
242 | | - PineExecutionAdapter adapter_; |
243 | | - PineScheduler scheduler_; |
244 | | - int source_bar_index_ = -1; |
245 | | - int source_last_bar_index_ = -1; |
246 | | - std::uint64_t source_callback_count_ = 0; |
247 | | - // The generic consumer deliberately clears the retired legacy scheduler |
248 | | - // flags when it applies a NativeRunSpec. A fixture host must therefore |
249 | | - // retain its source configuration instead of sampling those fields again |
250 | | - // on a subsequent public begin. |
251 | | - bool source_configuration_captured_ = false; |
252 | | - // @source-state end |
253 | | -}; |
254 | | - |
255 | | -using FixturePendingOrder = PineNativeHost::FixturePendingOrder; |
256 | | -using FixturePendingOrderType = PineNativeHost::FixturePendingOrderType; |
| 10 | +using PineNativeHost = PineStrategyHost; |
| 11 | +using FixturePendingOrder = PineStrategyHost::FixturePendingOrder; |
| 12 | +using FixturePendingOrderType = PineStrategyHost::FixturePendingOrderType; |
257 | 13 |
|
258 | 14 | } // namespace pineforge::source |
0 commit comments