You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG: Return 0 from Trade.pl_pct when entry_price is 0 or NaN - #1416
Trade.pl_pct divided by entry_price unconditionally, raising ZeroDivisionError when it was 0 and returning NaN when it was NaN.
It now returns 0 in both cases.
Added a regression test test_trade_pl_pct_zero_entry_price
covering both inputs; verified it fails without the fix.
The guard fixes the ZeroDivisionError, but returning 0. changes what the stats see. _stats.py:71 builds ReturnPct from t.pl_pct, and _stats.py:91 takes returns = trades_df['ReturnPct'] for the trade-level numbers (win rate, average and best/worst trade, expectancy, SQN). A NaN pl_pct was skipped by pandas' mean/std, so a bad-entry trade dropped out of those; a 0. is counted as a real break-even trade, which pulls win rate down and shrinks the mean and SQN, and nothing in the output says a trade had an unusable entry price.
So for the NaN case the new behaviour is arguably worse than before: it turns "no information" into "a trade that returned exactly 0%". Returning float('nan') for both inputs (and keeping the guard so zero no longer raises) would keep the stats honest, and the test could assert np.isnan(trade.pl_pct) plus that stats['# Trades'] still counts it while Win Rate [%] is computed over the non-NaN trades. If you do want 0., a docstring line saying such trades count as break-even would at least make it a documented choice.
Smaller point: Trade.pl (line 663) still uses entry_price without a guard, so with NaN it returns NaN while pl_pct now says 0; the two disagree for the same trade.
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1318.
Trade.pl_pctdivided byentry_priceunconditionally, raisingZeroDivisionErrorwhen it was 0 and returning NaN when it was NaN.It now returns 0 in both cases.
Added a regression test
test_trade_pl_pct_zero_entry_pricecovering both inputs; verified it fails without the fix.