fix: trend preview uses USDT profit, snapshot risk budget, and money RR across four exchanges
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""趋势回调预览:参考价首仓止盈与补仓后止盈。"""
|
||||
"""趋势回调预览:止盈盈利 U、止损金额 U、金额盈亏比。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
@@ -11,41 +11,47 @@ sys.path.insert(0, str(ROOT))
|
||||
|
||||
from strategy_trend_lib import ( # noqa: E402
|
||||
build_trend_preview_level_rows,
|
||||
calc_planned_reward_risk_ratio,
|
||||
calc_take_profit_for_rr,
|
||||
calc_money_reward_risk_ratio,
|
||||
calc_risk_budget_usdt,
|
||||
calc_tp_profit_usdt,
|
||||
)
|
||||
|
||||
|
||||
class TestTrendPreviewTp(unittest.TestCase):
|
||||
def test_short_ref_price_first_tp_matches_form_tp(self):
|
||||
ref, sl, tp = 72.6, 75.5, 65.0
|
||||
rr = calc_planned_reward_risk_ratio("short", ref, sl, tp)
|
||||
self.assertIsNotNone(rr)
|
||||
first_tp = calc_take_profit_for_rr("short", ref, sl, rr)
|
||||
self.assertAlmostEqual(first_tp, tp, places=2)
|
||||
def test_risk_budget_from_snapshot(self):
|
||||
self.assertAlmostEqual(calc_risk_budget_usdt(110.73, 5), 5.5365, places=2)
|
||||
|
||||
def test_preview_levels_include_first_and_dca_tp(self):
|
||||
def test_short_profit_at_form_take_profit(self):
|
||||
profit = calc_tp_profit_usdt("short", 72.53, 66.0, 1114, 0.00167)
|
||||
self.assertIsNotNone(profit)
|
||||
self.assertGreater(profit, 0)
|
||||
rr = calc_money_reward_risk_ratio(profit, 5.5365)
|
||||
self.assertIsNotNone(rr)
|
||||
self.assertGreater(rr, 1.5)
|
||||
|
||||
def test_preview_levels_use_money_rr(self):
|
||||
preview = {
|
||||
"direction": "short",
|
||||
"live_price_ref": 72.6,
|
||||
"live_price_ref": 72.53,
|
||||
"stop_loss": 75.5,
|
||||
"take_profit": 65.0,
|
||||
"first_order_amount": 1113,
|
||||
"take_profit": 66.0,
|
||||
"first_order_amount": 1114,
|
||||
"snapshot_available_usdt": 110.73,
|
||||
"risk_percent": 5,
|
||||
"contract_size": 0.00167,
|
||||
"grid_prices_json": json.dumps([73.42, 73.83]),
|
||||
"leg_amounts_json": json.dumps([222, 222]),
|
||||
}
|
||||
enriched, rows = build_trend_preview_level_rows(preview)
|
||||
self.assertEqual(enriched["preview_unified_stop_loss"], 75.5)
|
||||
self.assertAlmostEqual(enriched["preview_first_take_profit"], 65.0, places=1)
|
||||
self.assertAlmostEqual(enriched["preview_risk_amount_u"], 5.5365, places=2)
|
||||
self.assertEqual(enriched["preview_take_profit_price"], 66.0)
|
||||
self.assertEqual(len(rows), 3)
|
||||
self.assertEqual(rows[0]["label"], "首仓")
|
||||
self.assertAlmostEqual(rows[0]["take_profit"], 65.0, places=1)
|
||||
self.assertEqual(rows[0]["stop_loss"], 75.5)
|
||||
self.assertIsNotNone(rows[1]["avg_entry"])
|
||||
self.assertIsNotNone(rows[1]["take_profit"])
|
||||
self.assertEqual(rows[1]["stop_loss"], 75.5)
|
||||
# 做空:补仓价上移 → 均价上移 → 同等 RR 下止盈价上移
|
||||
self.assertGreater(rows[2]["take_profit"], rows[1]["take_profit"])
|
||||
self.assertEqual(rows[0]["risk_u"], enriched["preview_risk_amount_u"])
|
||||
self.assertIsNotNone(rows[0]["profit_u"])
|
||||
self.assertAlmostEqual(rows[0]["rr"], rows[0]["profit_u"] / 5.5365, places=2)
|
||||
self.assertEqual(rows[1]["risk_u"], enriched["preview_risk_amount_u"])
|
||||
self.assertGreater(rows[2]["profit_u"], rows[1]["profit_u"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user