Repository navigation
Round closed-trade statistics with SmartRounding instead of two decimals - #10204
quantconnect-kronos[bot] merged 5 commits into
Conversation
TradeBuilder rounded ProfitLoss, MAE, MFE and EndTradeDrawdown to two decimal places, which zeroes ordinary amounts in accounts denominated in currencies like BTC and misclassifies profitable trades as losses. Keep the calculated values; formatting rounds them for display. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Approving. The change removes the two-decimal rounding of ProfitLoss, MAE, MFE and EndTradeDrawdown in all three grouping methods, which is exactly what #10137 asks for, and nothing else.
What I checked
- CI: every run on
e50c7acpassed (Build & Test, Regression, Research Regression, Report Generator, Syntax, Benchmarks, Python Virtual Environments). API Tests was skipped, as it is for pull requests. KeepsAccountCurrencyPrecisionfails on master in all three cases (Expected: 0.00025m But was: 0m) and passes with the change. All 483QuantConnect.Tests.Common.Statisticstests pass locally.- End to end: I ran a backtest with a BTC account currency that trades SPY minute bars for a week. On master all 32 closed trades report
ProfitLoss = MAE = MFE = EndTradeDrawdown = 0.00and 0 wins, while 17 trades closed above entry. With this change no trade is zero, and the 17 wins are exactly the 17 trades that closed above entry (for examplepnl=0.000493…,mfe=0.000576…). - Consumers:
TradeStatisticsandExtensions.IsWinonly compare and sum these values, so full precision changes nothing except the intended win/loss classification. For option exercises,IsWinnow compares an unrounded P&L with the unrounded ITM amount, which is more consistent than before.ReturnsPerTradeReportElementuses only entry and exit prices. No rounding is lost in serialization that wasn't already lost forEntryPrice. - Decimal noise near zero: the averaged values in FlatToFlat and FlatToReduced are computed as
sum / quantityon both sides, so a trade with a true P&L of zero gives identical decimals and still comes out as0, not a win from noise.
Note, not blocking: in USD accounts, closed-trade values in results now carry trailing digits such as 999.999999999999999999988 instead of 1000.00. The description says so, EntryPrice already does this for averaged entries, and keeping precision until formatting is what the issue proposes.
Other repositories: none need a change. There is no API or stub change.
Risk: risk:low. It's a 10-line bug fix that is easy to revert, with no API change and nothing new that can throw. It's backed by a test that fails without the fix, green CI including regression tests, and the end-to-end comparison above.
|
Keeping up to 28 significant digits on every closed trade is far more than we need, but rounding to 2 dp is what breaks BTC accounts.
|
Replace the full-precision values with SmartRounding, which keeps small account-currency amounts such as BTC profits while dropping the decimal noise of averaged and inverse prices. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Applied the
The PR description is updated to match. |
There was a problem hiding this comment.
Approving 3898122. It applies the SmartRounding() decision from the discussion above to the same ten assignments, and nothing else changed.
What changed since my last review
TradeBuildernow storesProfitLoss,MAE,MFEandEndTradeDrawdownthroughSmartRounding()in all three grouping methods, instead of at full precision. That resolves the trailing-digit note from my earlier review:1000stays1000.- Small account-currency amounts are kept.
RoundToSignificantDigitsreturns0for zero and otherwise keeps 7 significant digits at any scale, so0.00025BTC is stored exactly, asKeepsAccountCurrencyPrecisionasserts. Nothing here can newly throw: the scale it computes is a power of ten within decimal range for any trade value. - Tests: the excursion test now expects
SmartRounding()of the exact fractions. Its long case covers the negative branch (-1222.2222…becomes-1222.222). The inverse scale-in total is now compared within0.001because each trade is rounded on its own. Both are the expected consequence of the decided rounding, not looser checks of the fix. - As the description says, losses at or below -1000 round to 7 significant digits instead of 4 decimals. That was decided above and matches charts, so it doesn't block.
CI: all runs on 38981222b passed: Build & Test, Regression, Research Regression, Report Generator, Syntax, Benchmarks and Python Virtual Environments. API Tests was skipped, as it is for pull requests.
Other repositories: none need a change. There's no API or stub change.
Risk: risk:low, unchanged. It's a ten-line bug fix that's easy to revert, with no API change. Evidence: a test that fails without the fix, green CI including regression tests, and the end-to-end BTC-account run in the description.
There was a problem hiding this comment.
Requesting changes on 00e295e. The reversal fix works, but inverse crypto futures can still turn a breakeven trade into a tiny win. It has the same cause as the two earlier findings, so I'm asking for a decision on the approach (below) instead of suggesting another patch.
What changed since my last review
- FlatToFlat now computes
(exit total / exit quantity − entry total / entry quantity) × entry quantity. Equal averages divide to the same decimal, so a breakeven reversal is exactly0. The new reversal cases inBreakevenAveragedFillsHaveZeroProfitcover it. Keeping master's reversal weighting out of this change, and leaving it to #9938, is the right scope. My reversal finding is resolved.
Still open: residue on inverse contracts (blocking). For inverse crypto futures, GetProfitPrice returns -1 / price. That value rarely fits in a decimal exactly, so the summed totals of a true breakeven don't cancel. SmartRounding() keeps the leftover as 7 significant digits. I reproduced this with a local test on this head, using BTCUSD on Binance with multiplier 100, rate 1 and zero fees, run FIFO and LIFO: buy 100 @ 40000, buy 100 @ 60000, sell 200 @ 48000. 48000 is the harmonic mean, so the trade's true profit is exactly 0.
- FlatToFlat records
ProfitLoss = 8.8E-20withIsWin = true. - FlatToReduced records
4.4E-20withIsWin = true. - On master both round to
0.00and don't count as a win.
The short version of that trade comes out as a loss of the same size, which then inflates the profit-loss ratio. Other inverse sequences I tried, such as scaling in and out at the same prices in a different order, gave 0. FillToFill is unaffected.
Cause: this is the third time a decimal residue shows up. The first came from running averages, the second from the reversal ratio, and this one from reciprocal prices. SmartRounding() keeps 7 significant digits whatever the value's size, so any residue survives. Exact sums can't remove it for inverse contracts, because their per-fill values are already rounded.
@Martin-Molinero Decision needed: how should closed-trade P&L drop decimal residue on inverse contracts, now that SmartRounding() keeps tiny values?
This pull request replaces two-decimal rounding of closed-trade P&L with SmartRounding(), as you decided on #10137. That fixes BTC-denominated accounts, and the linear-instrument breakeven cases are now exact. For inverse crypto futures, a breakeven trade can still record a residue of about 1e-20 and count as a win or a loss. Removing that needs a tolerance, and choosing one is a policy call.
- A. Drop residue relative to the trade's own size (recommended): treat a profit below about
1e-20× the trade's entry value as0beforeSmartRounding(), in FlatToFlat and FlatToReduced. Real BTC-scale profits are many orders of magnitude above that, so the original fix is kept. It takes a few lines plus inverse breakeven tests. Why recommended: it keeps your rounding and has no fixed account-currency epsilon. - B. Accept it and merge as is: inverse-contract trades that close exactly at breakeven count as tiny wins or losses, tracked as a separate issue. No more work.
- C. Round to a fixed number of decimals first (for example 10) and then apply
SmartRounding(): simple, but it's an absolute epsilon that would zero real profits in currencies scaled below it.
Reply with the option you choose, or another way to go.
Other repositories: none need a change. There's no API or stub change.
Risk: risk:high stays. A valid fill sequence on an inverse contract still gets a different win/loss classification than on master. Once that's resolved, with its regression test, the rest of the evidence (green CI and the end-to-end runs) is in place.
|
Confirmed on 00e295e: on inverse BTCUSD (Binance, multiplier 100, zero fees), buy I'm not changing the code until @Martin-Molinero picks an option (A, B, C or another) from the review above. Once he does, I'll implement it with an inverse breakeven regression test and record the decision in the description. |
|
A. Treat a profit below |
An inverse contract's reciprocal prices aren't exact decimals, so a breakeven FlatToFlat or FlatToReduced trade kept a residue that SmartRounding preserves and counted as a win or loss. Treat a profit below 1e-20 of the trade's entry value as zero, which keeps small real profits such as those of a BTC account. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Implemented option A, as @Martin-Molinero decided, in 3400c9d:
The decision and the new evidence are in the description. |
There was a problem hiding this comment.
Approving 3400c9d. It implements option A as decided: in FlatToFlat and FlatToReduced, a profit below 1e-20 × the trade's entry value is 0 before SmartRounding(). That resolves the inverse breakeven finding.
What I checked
- The four new inverse cases in
BreakevenAveragedFillsHaveZeroProfit(BTCUSD, long and short, both grouping methods) fail locally with the previous commit'sTradeBuilder(±8.8E-20,±4.4E-20) and pass on this head. All 492QuantConnect.Tests.Common.Statisticstests pass,KeepsAccountCurrencyPrecisionincluded. - Headroom on both sides: profit and entry value are compared in the same units, before the account-currency rate and multiplier. In the test, the raw residue is about
2e-26against a threshold of about4e-23. A-1/pricevalue is off by at most5e-29, so the residue relative to entry value grows with price and nears1e-20only at prices around1e8, far above any coin-margined contract. A real profit would have to be under1e-20of the position's value to be dropped, far below one price tick. Linear instruments were already exactly0at breakeven, so the check changes nothing for them, and FillToFill is unaffected. - CI: all runs on
3400c9dpassed. API Tests was skipped, as it is for pull requests.
Other repositories: none need a change. There's no API or stub change.
Risk: I set risk:low. The breakeven cases that blocked earlier commits are now covered by tests that fail without the fix. The rounding and the tolerance are what was decided on #10137 and on this pull request. It's a small bug fix that's easy to revert, with no API change and nothing new that can throw, backed by green CI including regression tests and the end-to-end runs in the description.
There was a problem hiding this comment.
Approving. The averaged-fill totals and agreed relative-residue threshold resolve my earlier breakeven/statistics-generation finding while retaining small account-currency profits.
- Release build and all 492 statistics tests pass locally.
- Tested end to end through algorithm orders in both FlatToFlat and FlatToReduced: averaged linear fills, same-price reversals, and the inverse BTCUSD harmonic-mean round trip remain exactly zero and are not wins. Genuine profits of
0.00025 BTCand0.0002272727 BTCremain wins. The runs produce the expected 8 and 7 closed trades, respectively, and all 27 summary statistics.
Risk: risk:low. The previously failing paths now have regression coverage and end-to-end verification. This remains a small, reversible bug fix with no public API change; the existing reversal-weighting issue stays outside its scope.
Description
TradeBuilderrounded each closed trade'sProfitLoss,MAE,MFEandEndTradeDrawdownto two decimal places, whatever the account currency. In an account denominated in a currency like BTC, ordinary amounts are far below 0.01. For example, a $10 profit at a USD→BTC rate of0.000025is0.00025 BTC, and it was stored as0.00. The trade was then also classified as a loss (IsWin = false), which skews the trade statistics built from these values.This replaces the two-decimal
Math.Round(x, 2)on those ten assignments, in all three fill-grouping methods (FillToFill, FlatToFlat, FlatToReduced), withSmartRounding(), as decided on #10137. It's the roundingChartPointandCandlestickalready use: 4 decimal places above 1000, and 7 significant digits otherwise. Small account-currency amounts such as0.00025 BTCare kept, and the decimal noise of averaged or inverse prices is dropped, so1000stays1000, not999.999999999999999999988.SmartRounding()checksinput > 1000rather than the absolute value, so values at or below -1000 round to 7 significant digits. For example, an inverse future's MAE of-1222.2222…becomes-1222.222. Charts behave the same way, and this was left as is by decision.FlatToFlat and FlatToReduced computed
ProfitLossfrom running averages of the fill values (avg += (v - avg) * q / total). Each step's division leaves residue, so a breakeven trade could come out as, say,-3.6e-27instead of0. The old two-decimal rounding hid that, butSmartRounding()keeps seven significant digits of it. The trade then counted as a win or a loss, and the following trade statistics could overflow, dividing by that residue in the profit-loss ratio. These two paths now sumquantity × valueover the entry and exit fills. FlatToReduced takes the profit from those totals directly, with no division. FlatToFlat takes it as(exit total / exit quantity − entry total / entry quantity) × entry quantity, the same formula as before but with each average from a single division. When the closing fill also reverses the position, the two quantities differ, as before. A breakeven trade has equal averages, and the two divisions round to the same decimal, so its profit is exactly0. The reportedEntryPriceandExitPriceaverages are unchanged, and so is how a closing fill that also reverses the position is weighted (#9938 tracks that separately).For inverse crypto futures, the per-fill values are
-1/price, whichdecimalcan't hold exactly. So even the summed totals of a true breakeven don't cancel. For example, buy100 @ 40000, buy100 @ 60000, sell200 @ 48000(the harmonic mean) came out as8.8E-20. As decided on this pull request (option A), FlatToFlat and FlatToReduced treat a profit below1e-20× the trade's entry value as0beforeSmartRounding(). The bound is relative to the trade's size, not a fixed account-currency epsilon, so small real profits such as a BTC account's are kept.Related Issue
Closes #10137
Motivation and Context
In non-USD-scale account currencies, profit, excursion and drawdown are zero, and profitable trades count as losses.
How Has This Been Tested?
TradeBuilderTests.KeepsAccountCurrencyPrecision, one case per grouping method. It covers a USD-quoted trade in a BTC account and assertsProfitLoss = 0.00025,IsWin = true, and, for FillToFill,MAE,MFEandEndTradeDrawdownexactly. It fails on master, where all three cases report0.AreEqualassertions. The inverse crypto-future excursion test expectsSmartRounding()of the exact fractions (11000m / 9and so on) instead ofMath.Round(…, 2). The inverse scale-in test sums trades that are each rounded on their own, so it compares the total1900/3within0.001.TradeBuilderTests.BreakevenAveragedFillsHaveZeroProfit(FlatToFlat and FlatToReduced, FIFO and LIFO). It covers breakeven sequences such as buy1 @ 3,2 @ 11,1 @ 1, sell4 @ 6.5, and buy9 @ 1,4 @ 18,5 @ 4, then sell4 @ 18,5 @ 4,9 @ 1. In FlatToFlat it also covers breakeven reversals: buy3 @ 50, sell9 @ 50, buy6 @ 50, and buy1 @ 3,2 @ 11,1 @ 1, sell6 @ 6.5, buy2 @ 6.5. It also covers the inverse BTCUSD breakeven above, long and short, in FlatToFlat and FlatToReduced. Each sequence is run FIFO and LIFO and followed by a profitable trade. The test asserts every breakeven trade is exactly0and not a win, and thatTradeStatisticsbuilds. Without the relative bound, the inverse cases come out as±8.8E-20(FlatToFlat) and±4.4E-20(FlatToReduced). With running averages these trades came out as8E-28and-3.6E-27, and with the earlier quantity-ratio form the reversals came out as-2E-26and1E-27.dotnet build /p:Configuration=Release QuantConnect.Lean.sln. Full test suite with the CI filter (TestCategory!=TravisExclude&TestCategory!=ResearchRegressionTests):Passed! - Failed: 0, Passed: 39832, Skipped: 100, Total: 39932.pnl=-9.710185e-06,mfe=4.924451e-05), and the win/loss classification matches each trade's price move. The same algorithm without this change reports zero profit on those trades. A second end-to-end backtest uses a FlatToFlat trade builder throughSetTradeBuilderand fills the breakeven sequence above, followed by a10000profit. The breakeven trade reports0and isn't a win, and all 27 summary statistics and the trade statistics are generated. The same holds for a breakeven reversal (buy3 @ 50, sell9 @ 50, buy6 @ 50): both of its trades report0. A third backtest trades coin-margined BTCUSD with a FlatToFlat trade builder. The inverse breakeven trade above reports0and isn't a win, and a following buy1 @ 40000, sell1 @ 44000keeps its profit of18.94815USD and counts as a win.Types of changes
Checklist:
bug-<issue#>-<description>orfeature-<issue#>-<description>