Skip to content

fix: correct quote-fee accounting and expose net backtest profit#2562

Open
grorge123 wants to merge 1 commit into
c9s:mainfrom
grorge123:fix_quote-fee
Open

fix: correct quote-fee accounting and expose net backtest profit#2562
grorge123 wants to merge 1 commit into
c9s:mainfrom
grorge123:fix_quote-fee

Conversation

@grorge123

Copy link
Copy Markdown
Contributor

Summary

This PR corrects quote-currency fee accounting and exposes net realized profit in BBGO backtest reports.

Changes

  • Correct quote-currency fee handling in Position.AddTrade:
    • Buy trades add the fee to the quote cash cost.
    • Sell trades subtract the fee from quote proceeds.
    • Futures behavior remains unchanged.
  • Fix TradeStats.TotalNetProfit so it consistently uses Profit.NetProfit.
  • Add SummaryReport.TotalNetProfit and aggregate profit across all symbols.
  • Preserve SummaryReport.TotalProfit for the existing PnL.Profit behavior.
  • Update the backtest report UI to display both gross profit and net profit.
  • Add regression tests for:
    • Quote-fee position accounting.
    • Net profit aggregation.
    • Partial closes and multiple quote-fee trades.
    • Profit fixer behavior.

Validation

  • go test ./pkg/types and go test ./pkg/backtest/ — Passed.

@grorge123
grorge123 requested a review from c9s as a code owner July 19, 2026 12:25
@CLAassistant

CLAassistant commented Jul 19, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@codecov

codecov Bot commented Jul 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.25000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 22.13%. Comparing base (55c1586) to head (927ca2e).
⚠️ Report is 149 commits behind head on main.

Files with missing lines Patch % Lines
pkg/cmd/backtest.go 0.00% 2 Missing ⚠️
pkg/types/position.go 91.66% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2562      +/-   ##
==========================================
+ Coverage   20.89%   22.13%   +1.24%     
==========================================
  Files        1154     1168      +14     
  Lines       80239    81006     +767     
==========================================
+ Hits        16766    17934    +1168     
+ Misses      61944    61513     -431     
- Partials     1529     1559      +30     
Files with missing lines Coverage Δ
pkg/backtest/report.go 0.00% <ø> (ø)
pkg/types/trade_stats.go 33.05% <100.00%> (+16.31%) ⬆️
pkg/types/position.go 38.51% <91.66%> (+0.76%) ⬆️
pkg/cmd/backtest.go 0.00% <0.00%> (ø)

... and 5 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2b436c1...927ca2e. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants