Use daily open as perp-hedge entry and toggle buy-straddle vs perp overlays.

Amp-stats now prices premium from each day's open, and the form switches mutually between straddle and perpetual-options对照.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-28 14:31:17 +08:00
parent d049c5d317
commit 2ce67da8e8
6 changed files with 191 additions and 97 deletions
+17 -5
View File
@@ -95,7 +95,7 @@ class AmpStatsLibTests(unittest.TestCase):
def test_perp_hedge_hit_and_pnl(self):
from lib.hub.amp_stats_lib import perp_hedge_day_pnl
# spot=1800 optLev=100 → prem/coin=18; 1:2 → premium=36
# 开盘=1800 optLev=100 → prem/coin=18; 1:2 → premium=36
# A move ≈ 52.83; B portfolio move = 51
rows = [
{"open": 1800, "close": 1860, "change": 60, "up_points": 60, "down_points": 0, "amplitude": 60, "settlement_day": "2026-07-01"},
@@ -103,7 +103,6 @@ class AmpStatsLibTests(unittest.TestCase):
{"open": 1800, "close": 1820, "change": 20, "up_points": 20, "down_points": 0, "amplitude": 20, "settlement_day": "2026-07-03"},
]
hedge = {
"spot": 1800,
"target_profit_u": 15,
"perp_leverage": 10,
"option_leverage": 100,
@@ -114,19 +113,31 @@ class AmpStatsLibTests(unittest.TestCase):
s = summarize_rows(rows, perp_hedge=hedge)
ph = s["perp_hedge"]
self.assertIsNotNone(ph)
self.assertEqual(ph["entry"], "open")
self.assertEqual(ph["spot"], 1800.0)
self.assertEqual(ph["opt_coins"], 2.0)
self.assertEqual(ph["premium_total"], 36.0)
self.assertAlmostEqual(ph["move_b"], 51.0, places=4)
self.assertEqual(ph["hit_a_days"], 1) # only +60
self.assertEqual(ph["hit_b_days"], 1) # only -60
# up day pnl = 60 - 36 - fee
up_pnl = perp_hedge_day_pnl(change=60, open_px=1800, close_px=1860, premium_total=36, opt_coins=2)
down_pnl = perp_hedge_day_pnl(change=-60, open_px=1800, close_px=1740, premium_total=36, opt_coins=2)
up_pnl = perp_hedge_day_pnl(
change=60, open_px=1800, close_px=1860, option_leverage=100, opt_coins=2
)
down_pnl = perp_hedge_day_pnl(
change=-60, open_px=1800, close_px=1740, option_leverage=100, opt_coins=2
)
self.assertAlmostEqual(down_pnl, 60 * (2 - 1) - 36, places=4) # 24
self.assertAlmostEqual(ph["down_pnl_total"], down_pnl, places=4)
self.assertGreater(up_pnl, 0)
self.assertEqual(ph["up_days"], 2)
self.assertEqual(ph["down_days"], 1)
# 不同开盘 → 不同权利金
hi_open_pnl = perp_hedge_day_pnl(
change=-60, open_px=2000, close_px=1940, option_leverage=100, opt_coins=2
)
self.assertAlmostEqual(hi_open_pnl, 60 - 40, places=4) # prem=40
csv_text = build_export_csv(
{
"exchange": "okx",
@@ -139,6 +150,7 @@ class AmpStatsLibTests(unittest.TestCase):
)
self.assertIn("永期对冲对照", csv_text)
self.assertIn("永期盈亏", csv_text)
self.assertIn("按日开盘", csv_text)
def test_long_straddle_stats(self):
rows = [