Files
2026FIFAWorldCup/platform/backend/app/analytics/hedging_calculator.py

40 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""串關動態對沖Dynamic Hedging計算器。"""
from __future__ import annotations
def calculate_hedge_amount(
original_stake: float,
parlay_total_odds: float,
final_leg_hedge_odds: float,
) -> dict[str, float]:
"""
在 1 場或 2/3 場連贏快到最終局,計算對沖下注金額。
將原始串關保本化:
目標是「串關全過的淨利」與「對沖走向中的淨利」在最後同值。
設原始串關到位後保底淨利 = S * (O_parlay - 1)
對沖選項淨利 = H * (O_hedge - 1)
求 H * (O_hedge - 1) = S * (O_parlay - 1) - H
=> H = (S * (O_parlay - 1)) / O_hedge
"""
if original_stake <= 0:
raise ValueError('original_stake 必須大於 0')
if parlay_total_odds <= 1:
raise ValueError('parlay_total_odds 必須大於 1')
if final_leg_hedge_odds <= 1:
raise ValueError('final_leg_hedge_odds 必須大於 1')
expected_parlay_net = original_stake * (parlay_total_odds - 1)
hedge_stake = expected_parlay_net / final_leg_hedge_odds
profit_after_hedge = hedge_stake * (final_leg_hedge_odds - 1)
return {
'hedge_stake': round(hedge_stake, 4),
'locked_profit': round(profit_after_hedge, 4),
'parlay_net_after_hedge_if_win': round(expected_parlay_net - hedge_stake, 4),
'hedge_net_if_win': round(profit_after_hedge, 4),
}