Skip to content

Fix double fee_rate application in risk_to_qty (fixes #614) - #651

Closed
hakarrr wants to merge 2 commits into
jesse-ai:masterfrom
hakarrr:fix-614-double-fee
Closed

hakarrr wants to merge 2 commits into
jesse-ai:masterfrom
hakarrr:fix-614-double-fee

Conversation

@hakarrr

@hakarrr hakarrr commented Nov 8, 2026

Copy link
Copy Markdown

Fixes #614.

risk_to_qty reduced the position size by (1 - fee_rate * 3) and then passed fee_rate on to size_to_qty, which applies the same adjustment again — so with fee_rate set, the fee was charged twice.

Reproduction from the issue:

utils.risk_to_qty(10000, 10, 100, 90, fee_rate=0.005)
# before: 97.0225
# after:  98.5

Change: drop the pre-adjustment in risk_to_qty and let size_to_qty apply the fee exactly once. size_to_qty itself is unchanged, so its other callers keep the same behavior.

Tests (tests/test_utils.py):

  • The existing fee case in test_risk_to_qty expected 4.97, which encoded the double application; with the fee charged once the value is 4.985. Updated.
  • Added the reporter's example as a regression assertion (98.5, not 97.0225). It fails on current master and passes with this change.
  • python -m pytest tests/test_utils.py → 14 passed.

Note: #620 had the same fix but was closed by the stale bot; this revives it with the test expectations updated.

hakarrr and others added 2 commits November 9, 2026 01:00
risk_to_qty reduced the position size by (1 - fee_rate * 3) and then passed fee_rate on to size_to_qty, which applies the same adjustment again, so the fee was charged twice.

Drop the pre-adjustment in risk_to_qty; size_to_qty applies the fee exactly once. size_to_qty itself is unchanged, so other callers keep the same behavior.

Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
The existing fee case expected 4.97, which encoded the fee being applied twice. With the fee charged once it is 4.985. Also adds the reporter's example from jesse-ai#614 (10000, 10, 100, 90, fee_rate=0.005) as a regression assertion: 98.5 instead of the double-haircut 97.0225.

Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
@saleh-mir

Copy link
Copy Markdown
Member

Thanks @hakarrr for picking this up, with the reproduction and the tests.

We went a different way for #614 in 3.6.1, so I'm closing this one. Charging the fee once still treats it as a flat share of the size, while the risk is only the distance to the stop, so a stop-out still loses more than the chosen risk: in the issue's example, about 1,079 instead of 1,000. 3.6.1 sizes the position as risk / (|entry - stop| + fee_rate * (entry + stop)), which counts the fee on entry and on exit at the stop, so a stop-out loses exactly the risk.

Appreciate the help!

@saleh-mir saleh-mir closed this Nov 11, 2026
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.

utils.risk_to_qty() function calculates fee_rate twice

2 participants