Align options realtime PnL with bid-net and show totals in stats.
Header float PnL now uses bid recycle minus premium like position cards. Stats adds realized/open/total net PnL. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -14,7 +14,13 @@ class OptionsHubLibTests(TestCase):
|
||||
@patch("lib.options.options_positions_lib.build_display_option_positions")
|
||||
def test_build_options_hub_snapshot_positions(self, mock_positions, _mock_stats):
|
||||
mock_positions.return_value = [
|
||||
{"inst_id": "ETH-USD_UM-260703-1800-C", "pos": 2, "upl": 1.5, "mark_px": 0.1}
|
||||
{
|
||||
"inst_id": "ETH-USD_UM-260703-1800-C",
|
||||
"pos": 2,
|
||||
"upl": 9.9,
|
||||
"mark_px": 0.1,
|
||||
"close_preview": {"estimated_pnl": 1.5},
|
||||
}
|
||||
]
|
||||
conn = MagicMock()
|
||||
conn.__enter__ = MagicMock(return_value=conn)
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
"""期权净盈亏汇总与持仓卡口径一致."""
|
||||
from unittest import TestCase
|
||||
from unittest.mock import patch
|
||||
|
||||
from lib.options.options_positions_lib import net_pnl_from_display_row, sum_options_net_pnl_usdc
|
||||
|
||||
|
||||
class OptionsNetPnlSumTests(TestCase):
|
||||
def test_net_pnl_from_display_row(self):
|
||||
self.assertEqual(
|
||||
net_pnl_from_display_row({"close_preview": {"estimated_pnl": -2.8}, "premium_paid": 4.95}),
|
||||
-2.8,
|
||||
)
|
||||
self.assertIsNone(
|
||||
net_pnl_from_display_row({"close_preview": {"bid_invalid": True, "estimated_pnl": -1}})
|
||||
)
|
||||
self.assertEqual(
|
||||
net_pnl_from_display_row(
|
||||
{"close_preview": {"total_received": 2.15}, "premium_paid": 4.95}
|
||||
),
|
||||
round(2.15 - 4.95, 4),
|
||||
)
|
||||
|
||||
@patch("lib.options.options_positions_lib.build_display_option_positions")
|
||||
def test_sum_options_net_pnl_usdc(self, mock_build):
|
||||
mock_build.return_value = [
|
||||
{"close_preview": {"estimated_pnl": -2.8}},
|
||||
{"close_preview": {"estimated_pnl": 1.0}},
|
||||
{"close_preview": {"bid_invalid": True, "estimated_pnl": 9}},
|
||||
]
|
||||
cfg = {"fetch_option_positions": lambda ex: [{"instId": "X"}]}
|
||||
self.assertEqual(sum_options_net_pnl_usdc(cfg, object()), -1.8)
|
||||
@@ -83,3 +83,4 @@ class OptionsStatsLibTests(TestCase):
|
||||
self.assertAlmostEqual(out["avg_loss"], 2.66, places=2)
|
||||
self.assertAlmostEqual(out["profit_loss_ratio"], 0.33, places=2)
|
||||
self.assertEqual(out["open_count"], 1)
|
||||
self.assertAlmostEqual(out["net_realized_pnl"], round(0.87 - 3.99 - 1.33, 4), places=4)
|
||||
|
||||
Reference in New Issue
Block a user