Files
crypto_monitor/tests/test_trend_preview_tp.py
T

53 lines
1.9 KiB
Python

"""趋势回调预览:参考价首仓止盈与补仓后止盈。"""
from __future__ import annotations
import json
import sys
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
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,
)
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_preview_levels_include_first_and_dca_tp(self):
preview = {
"direction": "short",
"live_price_ref": 72.6,
"stop_loss": 75.5,
"take_profit": 65.0,
"first_order_amount": 1113,
"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.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"])
if __name__ == "__main__":
unittest.main()