Repository navigation
fix(strategy): count futures unrealized PNL once in portfolio_value - #658
Open
jackiectl2 wants to merge 2 commits into
Open
jackiectl2 wants to merge 2 commits into
jackiectl2 wants to merge 2 commits into
Conversation
In futures mode Strategy.portfolio_value returned balance + leverage * sum(position PNL). A position's qty already includes the leverage (an order needs qty * price / leverage of margin), so Position.pnl is the whole profit, and closing the position adds exactly that amount to the balance: position_service passes estimate_PNL(qty, ...) to add_realized_pnl. Multiplying the PNL by the leverage again overstated open gains and losses (leverage - 1) times. With leverage 2, a long of 1 BTC from 10 to 20 on a 10,000 balance reported a portfolio value of 10,020. Closing it at 20 leaves a balance and portfolio value of 10,010, so the value fell on a close at the same price. save_daily_portfolio_balance, which records the equity curve the metrics use, already counts balance + PNL, so portfolio_value also disagreed with Jesse's own daily balances. Return balance + PNL. Spot is unchanged, as its leverage is always 1. TestPortfolioValue asserted the leveraged sum and now asserts the plain one. The new TestFuturesPortfolioValueWithLeverage checks the value with leverage 2 before and after closing a position. This is the calculation questioned in jesse-ai#515, which the stale bot closed without a fix. Signed-off-by: Tianlang (Jackie) Chen <305351710+jackiectl2@users.noreply.github.com>
Signed-off-by: Tianlang (Jackie) Chen <305351710+jackiectl2@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
In futures mode
Strategy.portfolio_valuereturnsbalance + leverage * sum(position PNL). A position's qty already includes the leverage (an order needsqty * price / leverageof margin), soPosition.pnlis the whole profit, and closing the position adds exactly that amount to the balance (position_servicepassesestimate_PNL(qty, ...)toadd_realized_pnl). Multiplying the PNL by the leverage again overstates open gains and losses by(leverage - 1)times.Example with leverage 2 and a 10,000 balance: a long of 1 BTC from 10 to 20 reports a portfolio value of 10,020. Closing it at 20 leaves a balance and portfolio value of 10,010, so the value drops on a close at the same price.
save_daily_portfolio_balance, which records the daily balances behind the equity curve and metrics, already countsbalance + PNL, soportfolio_valuealso disagrees with Jesse's own daily balances. A strategy that sizes positions or checks drawdown fromself.portfolio_valuewith leverage above 1 currently works from the inflated number.This is the calculation questioned in #515, which the stale bot closed without a fix.
Change
portfolio_valuereturnsbalance + PNLin futures mode. Spot is unchanged, since its leverage is always 1.TestPortfolioValueasserted the leveraged sum; it now asserts the plain one.TestFuturesPortfolioValueWithLeverage(run bytest_futures_portfolio_value_counts_unrealized_pnl_once_with_leverage) opens 1 BTC at 10 with leverage 2, checks the value at 20, closes there, and checks that the balance and value both equal 10,010 afterwards.Testing
master(portfolio_valueis 10020.0 with a balance of 10000.0 and a PNL of 10) and passes with the change.python -m pytest -n 4 --dist=loadfile -o faulthandler_timeout=120(Linux, Python 3.12,requirements.txt, pytest-xdist 3.5.0): 2605 passed, 101 skipped, 16 failed. The 16 failures are all intests/test_mcp_worker_port.py(the loopback MCP worker cannot be reached in my sandbox: connection resets and no IPv6) and fail identically onmaster, which gives 2604 passed, 101 skipped, 16 failed.pyrefly check jesse/strategies/Strategy.pyandpyrefly check 'jesse/strategies/*/__init__.py'(as in AGENTS.md): 0 errors.Not tested: live or paper trading (no exchange session was run), Windows and macOS, and Python versions other than 3.12.