Files
crypto_monitor/tests/test_trend_preview_tp.py

59 lines
2.1 KiB
Python

"""趋势回调预览:止盈盈利 U、止损金额 U、金额盈亏比。"""
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_money_reward_risk_ratio,
calc_risk_budget_usdt,
calc_tp_profit_usdt,
)
class TestTrendPreviewTp(unittest.TestCase):
def test_risk_budget_from_snapshot(self):
self.assertAlmostEqual(calc_risk_budget_usdt(110.73, 5), 5.5365, places=2)
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.53,
"stop_loss": 75.5,
"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.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.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__":
unittest.main()