Skip to content

Commit dbcca85

Browse files
luisleo526claude
andcommitted
Kernel+Source: a current execution converts at its cursor's rate, threaded; it neither writes nor restores the host's clock (R5 lane B-ADAPTER, item 5)
R5 lane F3's finding 2. consume_matched_request presented the execution cursor as the engine's bar clock (current_bar_.timestamp) so that the terms, the inspection, the admission, the preview and the settlement all converted at the cursor's instant; F3 made execute_current save the frame's clock and restore it after the call. E21's ruling for a host command is to thread the rate, never the clock -- which F3 could not do without engine_execution.cpp ("fourteen clock-based FX reads and build_close_trade_with_costs' four"). Kernel (on INT23's engine_execution.cpp / engine_settlement_stage.hpp): - consume_matched_request takes the execution's rate once -- account_currency_fx_at(cursor effective time), the instant it presented -- and threads it: the terms facts' active_fx (already that instant), the sized basis (marked_equity_at), the candidate inspection (inspect_native_reversal_at, inspect_native_settlement_selected_at, inspect_native_settlement_scoped_at), the preview and settlement context (PhysicalExecutionContext::account_fx), the account observation and the risk note (risk_note_fill / risk_roll_day take the rate). It writes the clock only when matching a driver point, whose own instant it is; a current execution presents nothing. - The one-lot and staged settlements take their quote rate from the context (settle_with_membership, settle_native_reversal_at_v1, OneLot::preview_keeping), a projection converts at its stage's rate (OneLot::project, project_native_settlement_stage: notional, marked equity and the rows it builds), and project_native_settlement_at projects at an explicit rate. A context that names no rate converts at the presented clock's, exactly as before. - execute_current's save and restore is deleted. - open_profit_at(price, fx) beside open_profit. ADR-0001 rule 2. (a) Mechanism: an execution converting at its own instant's rate, handed to what it runs through its facts and context, is the same for every host and venue; the host's presented clock stays its own; nothing branches on a platform. (b) Knob: none -- no spec field; the rate is the one the run already chose (the cursor's instant), carried as data. For a host, one thing changes and it is the point: inside a current execution its own hooks now see its frame's clock, not the cursor's, and the execution's rate is in their facts (NativeExecutionTermsFacts:: active_fx, ClosedLotExcursionFacts::account_fx). Source: the Pine callbacks that run inside an execution read the facts' rate: the at-fill default percentage's equity and the affordability fallback in resolve_terms, and a typed percentage's money (percent_commission_live_equity(mark, fx); a typed reversal's flatten projected at the rate, project_native_settlement_at, which retires the now-unused PineStrategyHost::adapter_project_flatten). Its excursion owner already converts at facts.account_fx (item 3). Witness tests/test_native_current_execution_rate.cpp (new row, source-free): F3's bar-open geometry (the command executes at the decision floor, a bar later, across a 1.0 -> 2.5 step): inside the execution's own terms hook the clock is the frame's (+2 min) and the facts' rate 2.5 at +3 min; the new unit's fee is 0.11 x 2.5 = 0.275 and after the call the clock, rate and marked equity are the frame's; a current close books 369.375 P&L, 5.625 commission and a 373.5 run-up, all at 2.5; and the kernel sources: execute_current names no clock, consume_matched_request writes it once and only under !current, the candidate inspection and the sized basis read no clock, the settle and preview entries take the context's rate. Fail-before (inside a git archive of db98990): inside the terms hook: clock +3 min, facts rate 2.50 at +3 min FAIL ...:194 host.hook_clock == kT0 + 2 * kMinute execute_current names the bar clock consume_matched_request writes the clock 1 time(s), unguarded NativeExecutionConsumer::resolve_sized_units( reads a clock (settle_with_membership, settle_native_reversal_at_v1, preview_keeping x2) does not take the context's rate R5 B-ADAPTER current execution rate: 47 checks, 8 failures The economics rows (0.275; 369.375 / 5.625 / 373.5) pass before and after: the threaded rate is the presented one. After: 47 checks, 0 failures; local ctest 661/661 before the row was added (every FX, margin-clock, settlement carry, C-host and adapter suite, including F3's item-5 and E21's rows); check_native_cpp_versions, check_broker_state_hash_coverage, check_native_c_api_surface, check_c_abi_runtime exit 0. Not changed (recorded for B-DOCS): NativeStrategyHost:: inspect_current_execution, the host-facing preview of a current execution, still converts at the presented clock -- in a bar-open frame across a step it previews at the frame's rate what executes at the cursor's. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> (cherry picked from commit 1bdd55f87c2bd41fc3e091f2bb1e3561b512f1fc)
1 parent 4b00da9 commit dbcca85

10 files changed

Lines changed: 525 additions & 75 deletions

‎include/pineforge/engine.hpp‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,12 @@ class BacktestEngine {
658658
const execution::Action& action,
659659
const execution::Fill& fill,
660660
const execution::SelectedOpeningSet& selection) const;
661+
// The selected inspection at an explicit account-currency rate, as
662+
// inspect_native_settlement_scoped_at is the scoped one.
663+
execution::SettlementInspection inspect_native_settlement_selected_at(
664+
const execution::Action& action,
665+
const execution::Fill& fill,
666+
const execution::SelectedOpeningSet& selection, double fx) const;
661667
execution::Result settle_native_execution_selected_at(
662668
const execution::Action& action,
663669
const execution::Fill& fill,
@@ -675,6 +681,13 @@ class BacktestEngine {
675681
execution::AccountEffectProjection project_native_settlement_v1(
676682
const execution::Action& action,
677683
const execution::Fill& fill) const;
684+
// The same projection at an explicit account-currency rate: its rows, its
685+
// quoted charges, its notional and its marked equity convert at `fx`, and
686+
// no clock is read. project_native_settlement_v1() is this at the
687+
// presented clock's rate.
688+
execution::AccountEffectProjection project_native_settlement_at(
689+
const execution::Action& action,
690+
const execution::Fill& fill, double fx) const;
678691
execution::AccountEffectProjection project_native_settlement_scoped_v1(
679692
const execution::Action& action,
680693
const execution::Fill& fill,
@@ -690,6 +703,10 @@ class BacktestEngine {
690703
execution::SettlementInspection inspect_native_reversal_v1(
691704
const execution::ReverseTo& reversal,
692705
const execution::Fill& fill) const;
706+
// The reversal inspection at an explicit account-currency rate.
707+
execution::SettlementInspection inspect_native_reversal_at(
708+
const execution::ReverseTo& reversal,
709+
const execution::Fill& fill, double fx) const;
693710
execution::AccountEffectProjection project_native_reversal_v1(
694711
const execution::ReverseTo& reversal,
695712
const execution::Fill& fill) const;
@@ -1006,6 +1023,11 @@ class BacktestEngine {
10061023
// (build_close_trade_with_costs)
10071024
// so equity = capital + net_profit + open_profit stays in one unit.
10081025
double open_profit(double current_price) const {
1026+
if (position_side_ == PositionSide::FLAT) return 0.0;
1027+
return open_profit_at(current_price, active_account_currency_fx());
1028+
}
1029+
// open_profit at an explicit account-currency rate `fx`.
1030+
double open_profit_at(double current_price, double fx) const {
10091031
if (position_side_ == PositionSide::FLAT) return 0.0;
10101032
double diff = (position_side_ == PositionSide::LONG)
10111033
? (current_price - position_entry_price_)
@@ -1014,7 +1036,7 @@ class BacktestEngine {
10141036
// open_trade_profit —
10151037
// callers combine this with initial_capital_ + net_profit_sum_ (both
10161038
// account-currency) to get total equity. fx=1.0 is a no-op.
1017-
return diff * position_qty_ * syminfo_.pointvalue * active_account_currency_fx();
1039+
return diff * position_qty_ * syminfo_.pointvalue * fx;
10181040
}
10191041

10201042
int count_wintrades() const { return win_trades_count_; }
@@ -1502,7 +1524,7 @@ class BacktestEngine {
15021524
const execution::Action& action,
15031525
const execution::Fill& fill,
15041526
execution::CloseScope book_or_opening,
1505-
const execution::SelectedOpeningSet* selected) const;
1527+
const execution::SelectedOpeningSet* selected, double fx) const;
15061528
execution::SettlementInspection inspect_with_membership(
15071529
const execution::Action& action,
15081530
const execution::Fill& fill,

‎include/pineforge/source/pine_adapter.hpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,10 @@ class PineExecutionAdapter {
17091709
double cohort_exposure_for(const SourceId&) const noexcept;
17101710
bool from_entry_filled_this_cycle(const SourceId&) const noexcept;
17111711
double percent_commission_live_equity(double) const noexcept;
1712+
// The same equity at an explicit account-currency rate: the rate an
1713+
// execution converts at, which its terms facts carry
1714+
// (NativeExecutionTermsFacts::active_fx); none, the presented clock's.
1715+
double percent_commission_live_equity(double, std::optional<double> fx) const noexcept;
17121716
double quantize_close_units(double basis, double percent) const noexcept;
17131717
double quantize_percent_exit_units(double requested,
17141718
double available) const noexcept;

‎include/pineforge/source/pine_strategy_host.hpp‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -753,9 +753,6 @@ class PineStrategyHost : public NativeStrategyHost, public BrokerStateHashProvid
753753
// The one writer of the three session flags below: it selects the
754754
// kernel's session-day facts of the script bar being published.
755755
void scheduler_update_session_state();
756-
execution::AccountEffectProjection adapter_project_flatten(
757-
double price, const std::string& id, const std::string& comment,
758-
std::uint64_t incarnation) const;
759756
void adapter_label_bracket_trades(
760757
const native_order::ExecutionAppliedEvent&, bool from_bracket);
761758
bool adapter_has_open_entry_id(const std::string&) const;

‎src/engine_execution.cpp‎

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,9 @@ execution::AccountEffectProjection BacktestEngine::NativeSettlementStage::OneLot
442442

443443
std::optional<Trade> built;
444444
if (closes && !row) {
445-
built.emplace(close_row(engine, fill, execution::PhysicalExecutionContext{}));
445+
execution::PhysicalExecutionContext at_rate;
446+
at_rate.account_fx = fx;
447+
built.emplace(close_row(engine, fill, at_rate));
446448
row = &*built;
447449
}
448450
double realized = engine.net_profit_sum_;
@@ -455,7 +457,7 @@ execution::AccountEffectProjection BacktestEngine::NativeSettlementStage::OneLot
455457
if (!std::isfinite(realized))
456458
return invalid_projection(Status::InvalidAccounting);
457459

458-
const double account_fx = engine.active_account_currency_fx();
460+
const double account_fx = fx;
459461
const double pv = engine.syminfo_.pointvalue;
460462
const PositionSide resulting_side = opening > 0.0 ? incoming : PositionSide::FLAT;
461463
const double direction = resulting_side == PositionSide::SHORT ? -1.0 : 1.0;
@@ -873,12 +875,16 @@ void BacktestEngine::finish_native_settlement_stage(
873875

874876
execution::SettlementInspection BacktestEngine::inspect_native_reversal_v1(
875877
const execution::ReverseTo& reversal, const execution::Fill& fill) const {
878+
return inspect_native_reversal_at(reversal, fill, active_account_currency_fx());
879+
}
880+
881+
execution::SettlementInspection BacktestEngine::inspect_native_reversal_at(
882+
const execution::ReverseTo& reversal, const execution::Fill& fill, double fx) const {
876883
NativeSettlementStage::OneLot one;
877-
if (one.admit(*this, internal::SettlementEntry::Inspect, reversal, fill, nullptr,
878-
active_account_currency_fx()))
884+
if (one.admit(*this, internal::SettlementEntry::Inspect, reversal, fill, nullptr, fx))
879885
return one.inspect(*this, fill);
880886
NativeSettlementStage stage;
881-
stage_native_settlement(stage, reversal, fill, nullptr, active_account_currency_fx());
887+
stage_native_settlement(stage, reversal, fill, nullptr, fx);
882888
return inspect_native_settlement_stage(stage, fill);
883889
}
884890

@@ -897,12 +903,12 @@ execution::Result BacktestEngine::settle_native_reversal_at_v1(
897903
const execution::ReverseTo& reversal, const execution::Fill& fill,
898904
const execution::PhysicalExecutionContext& context) {
899905
const execution::LifecycleEffects lifecycle;
906+
const double fx = context.account_fx ? *context.account_fx : active_account_currency_fx();
900907
NativeSettlementStage::OneLot one;
901-
if (one.admit(*this, internal::SettlementEntry::Settle, reversal, fill, &lifecycle,
902-
active_account_currency_fx()))
908+
if (one.admit(*this, internal::SettlementEntry::Settle, reversal, fill, &lifecycle, fx))
903909
return one.settle(*this, fill, context);
904910
NativeSettlementStage stage;
905-
stage_native_settlement(stage, reversal, fill, &lifecycle, active_account_currency_fx());
911+
stage_native_settlement(stage, reversal, fill, &lifecycle, fx);
906912
return commit_native_settlement_stage(stage, fill, lifecycle, context);
907913
}
908914

@@ -995,13 +1001,13 @@ execution::Result BacktestEngine::settle_with_membership(
9951001
const execution::PhysicalExecutionContext& context,
9961002
execution::CloseScope book_or_opening,
9971003
const execution::SelectedOpeningSet* selected) {
1004+
const double fx = context.account_fx ? *context.account_fx : active_account_currency_fx();
9981005
NativeSettlementStage::OneLot one;
9991006
if (one.admit(*this, internal::SettlementEntry::Settle, action, fill, book_or_opening,
1000-
selected, &lifecycle, active_account_currency_fx()))
1007+
selected, &lifecycle, fx))
10011008
return one.settle(*this, fill, context);
10021009
NativeSettlementStage stage;
1003-
stage_native_settlement(stage, action, fill, book_or_opening, selected, &lifecycle,
1004-
active_account_currency_fx());
1010+
stage_native_settlement(stage, action, fill, book_or_opening, selected, &lifecycle, fx);
10051011
return commit_native_settlement_stage(stage, fill, lifecycle, context);
10061012
}
10071013

@@ -1111,17 +1117,18 @@ execution::Status BacktestEngine::NativeSettlementStage::OneLot::preview_keeping
11111117
execution::CloseScope scope, const execution::SelectedOpeningSet* selected,
11121118
execution::AccountEffectProjection& account, std::vector<double>& row_pnl,
11131119
std::optional<OneLot>* kept) {
1120+
const double fx = context.account_fx ? *context.account_fx
1121+
: engine.active_account_currency_fx();
11141122
OneLot one;
11151123
if (one.admit(engine, internal::SettlementEntry::Preview, action, fill, scope, selected,
1116-
nullptr, engine.active_account_currency_fx())) {
1124+
nullptr, fx)) {
11171125
const auto readiness =
11181126
one.preview(engine, fill, context, account, row_pnl, /*reserve_rows=*/true);
11191127
if (kept) kept->emplace(one);
11201128
return readiness;
11211129
}
11221130
NativeSettlementStage stage;
1123-
engine.stage_native_settlement(stage, action, fill, scope, selected, nullptr,
1124-
engine.active_account_currency_fx());
1131+
engine.stage_native_settlement(stage, action, fill, scope, selected, nullptr, fx);
11251132
NativeSettlementRows rows;
11261133
const auto readiness = engine.prepare_native_settlement_commit(stage, fill, context, rows);
11271134
account = engine.project_native_settlement_stage(stage, fill);
@@ -1138,17 +1145,17 @@ execution::Status BacktestEngine::NativeSettlementStage::OneLot::preview_keeping
11381145
const execution::Fill& fill, const execution::PhysicalExecutionContext& context,
11391146
execution::AccountEffectProjection& account, std::vector<double>& row_pnl,
11401147
std::optional<OneLot>* kept) {
1148+
const double fx = context.account_fx ? *context.account_fx
1149+
: engine.active_account_currency_fx();
11411150
OneLot one;
1142-
if (one.admit(engine, internal::SettlementEntry::Preview, reversal, fill, nullptr,
1143-
engine.active_account_currency_fx())) {
1151+
if (one.admit(engine, internal::SettlementEntry::Preview, reversal, fill, nullptr, fx)) {
11441152
const auto readiness =
11451153
one.preview(engine, fill, context, account, row_pnl, /*reserve_rows=*/false);
11461154
if (kept) kept->emplace(one);
11471155
return readiness;
11481156
}
11491157
NativeSettlementStage stage;
1150-
engine.stage_native_settlement(stage, reversal, fill, nullptr,
1151-
engine.active_account_currency_fx());
1158+
engine.stage_native_settlement(stage, reversal, fill, nullptr, fx);
11521159
NativeSettlementRows rows;
11531160
const auto readiness = engine.prepare_native_settlement_commit(stage, fill, context, rows);
11541161
account = engine.project_native_settlement_stage(stage, fill);
@@ -1266,6 +1273,12 @@ execution::SettlementInspection BacktestEngine::inspect_native_settlement_select
12661273
active_account_currency_fx());
12671274
}
12681275

1276+
execution::SettlementInspection BacktestEngine::inspect_native_settlement_selected_at(
1277+
const execution::Action& action, const execution::Fill& fill,
1278+
const execution::SelectedOpeningSet& selection, double fx) const {
1279+
return inspect_with_membership(action, fill, execution::Book{}, &selection, fx);
1280+
}
1281+
12691282
execution::SettlementInspection BacktestEngine::inspect_with_membership(
12701283
const execution::Action& action, const execution::Fill& fill,
12711284
execution::CloseScope book_or_opening,
@@ -1312,29 +1325,34 @@ execution::AccountEffectProjection BacktestEngine::project_native_settlement_v1(
13121325
return project_native_settlement_scoped_v1(action, fill, execution::Book{});
13131326
}
13141327

1328+
execution::AccountEffectProjection BacktestEngine::project_native_settlement_at(
1329+
const execution::Action& action, const execution::Fill& fill, double fx) const {
1330+
return project_with_membership(action, fill, execution::Book{}, nullptr, fx);
1331+
}
1332+
13151333
execution::AccountEffectProjection BacktestEngine::project_native_settlement_scoped_v1(
13161334
const execution::Action& action, const execution::Fill& fill,
13171335
execution::CloseScope scope) const {
1318-
return project_with_membership(action, fill, scope, nullptr);
1336+
return project_with_membership(action, fill, scope, nullptr, active_account_currency_fx());
13191337
}
13201338

13211339
execution::AccountEffectProjection BacktestEngine::project_native_settlement_selected_v1(
13221340
const execution::Action& action, const execution::Fill& fill,
13231341
const execution::SelectedOpeningSet& selection) const {
1324-
return project_with_membership(action, fill, execution::Book{}, &selection);
1342+
return project_with_membership(action, fill, execution::Book{}, &selection,
1343+
active_account_currency_fx());
13251344
}
13261345

13271346
execution::AccountEffectProjection BacktestEngine::project_with_membership(
13281347
const execution::Action& action, const execution::Fill& fill,
13291348
execution::CloseScope book_or_opening,
1330-
const execution::SelectedOpeningSet* selected) const {
1349+
const execution::SelectedOpeningSet* selected, double fx) const {
13311350
NativeSettlementStage::OneLot one;
13321351
if (one.admit(*this, internal::SettlementEntry::Project, action, fill, book_or_opening,
1333-
selected, nullptr, active_account_currency_fx()))
1352+
selected, nullptr, fx))
13341353
return one.project(*this, fill, nullptr);
13351354
NativeSettlementStage stage;
1336-
stage_native_settlement(stage, action, fill, book_or_opening, selected, nullptr,
1337-
active_account_currency_fx());
1355+
stage_native_settlement(stage, action, fill, book_or_opening, selected, nullptr, fx);
13381356
return project_native_settlement_stage(stage, fill);
13391357
}
13401358

@@ -1347,7 +1365,7 @@ execution::AccountEffectProjection BacktestEngine::project_native_settlement_sta
13471365
const auto unchanged_account = [&]() -> execution::AccountEffectProjection {
13481366
execution::AccountEffectProjection out;
13491367
out.status = Status::NoEffect;
1350-
const double fx = active_account_currency_fx();
1368+
const double fx = stage.fx;
13511369
const double pv = syminfo_.pointvalue;
13521370
double abs_units = 0.0;
13531371
for (const auto& lot : pyramid_entries_) abs_units += lot.qty;
@@ -1360,7 +1378,7 @@ execution::AccountEffectProjection BacktestEngine::project_native_settlement_sta
13601378
remaining += open_entry_commission(lot);
13611379
out.realized_balance = initial_capital_ + net_profit_sum_;
13621380
out.remaining_entry_cost = remaining;
1363-
out.marked_equity = marked_equity(fill.price);
1381+
out.marked_equity = marked_equity_at(fill.price, stage.fx);
13641382
out.cycle_after = pyramid_entries_.empty() ? 0 : position_cycle_seq_;
13651383
out.signed_units_after = position_side_ == PositionSide::SHORT
13661384
? -abs_units
@@ -1393,6 +1411,7 @@ execution::AccountEffectProjection BacktestEngine::project_native_settlement_sta
13931411
throw std::overflow_error("position cycle sequence exhausted");
13941412

13951413
execution::PhysicalExecutionContext context;
1414+
context.account_fx = stage.fx;
13961415
NativeSettlementRows rows;
13971416
build_native_settlement_close_rows(stage, fill, context, rows);
13981417

@@ -1406,7 +1425,7 @@ execution::AccountEffectProjection BacktestEngine::project_native_settlement_sta
14061425
if (!std::isfinite(realized))
14071426
return invalid_projection(Status::InvalidAccounting);
14081427

1409-
const double fx = active_account_currency_fx();
1428+
const double fx = stage.fx;
14101429
const double pv = syminfo_.pointvalue;
14111430
const PositionSide resulting_side = !stage.survivors.empty()
14121431
? position_side_

0 commit comments

Comments
 (0)