Skip to content

fix(strategy): count futures unrealized PNL once in portfolio_value - #658

Open
jackiectl2 wants to merge 2 commits into
jesse-ai:masterfrom
jackiectl2:portfolio-value-leverage
Open

jackiectl2 wants to merge 2 commits into
jesse-ai:masterfrom
jackiectl2:portfolio-value-leverage

Conversation

@jackiectl2

Copy link
Copy Markdown

In futures mode Strategy.portfolio_value returns 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 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 counts balance + PNL, so portfolio_value also disagrees with Jesse's own daily balances. A strategy that sizes positions or checks drawdown from self.portfolio_value with 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_value returns balance + PNL in futures mode. Spot is unchanged, since its leverage is always 1.
  • TestPortfolioValue asserted the leveraged sum; it now asserts the plain one.
  • New TestFuturesPortfolioValueWithLeverage (run by test_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

  • The new test fails on master (portfolio_value is 10020.0 with a balance of 10000.0 and a PNL of 10) and passes with the change.
  • Full suite, 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 in tests/test_mcp_worker_port.py (the loopback MCP worker cannot be reached in my sandbox: connection resets and no IPv6) and fail identically on master, which gives 2604 passed, 101 skipped, 16 failed.
  • pyrefly check jesse/strategies/Strategy.py and pyrefly 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.

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>
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.

1 participant