Add hub strategy calculator page with trend and roll risk-based sizing.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-23 17:35:06 +08:00
parent 1ba0014fff
commit 253d353206
8 changed files with 887 additions and 2 deletions
+60
View File
@@ -0,0 +1,60 @@
"""hub_calculator_lib 测算逻辑。"""
from hub_calculator_lib import calc_roll_calculator, calc_trend_calculator
def test_trend_calculator_long_basic():
data, err = calc_trend_calculator(
direction="long",
capital_usdt=1000,
risk_percent=5,
leverage=5,
entry_price=100,
stop_loss=95,
add_upper=110,
take_profit=120,
dca_legs=3,
contract_size=1,
)
assert err is None
assert data is not None
assert data["risk_budget_u"] == 50.0
assert len(data["rows"]) >= 2
assert data["rows"][0]["label"] == "首仓"
assert data["first_profit_u"] is not None
assert data["first_profit_u"] > 0
def test_trend_calculator_short_rejects_bad_bounds():
data, err = calc_trend_calculator(
direction="short",
capital_usdt=1000,
risk_percent=5,
leverage=5,
entry_price=100,
stop_loss=90,
add_upper=110,
take_profit=80,
dca_legs=3,
)
assert data is None
assert err is not None
def test_roll_calculator_long():
data, err = calc_roll_calculator(
direction="long",
capital_usdt=1000,
risk_percent=5,
qty_existing=10,
entry_existing=100,
take_profit=120,
add_price=105,
new_stop_loss=98,
legs_done=0,
)
assert err is None
assert data is not None
assert data["add_contracts"] > 0
assert data["qty_after"] > 10
assert data["profit_at_tp_u"] is not None