technical-patterns-lab/tests/test_tilt_score.py
褚宏光 0f8b9d836b Refactor strength scoring system with new parameters and renaming
- Introduced a new "tilt" parameter to the strength scoring system, allowing for the assessment of triangle slope directionality.
- Renamed existing parameters: "拟合贴合度" to "形态规则度" and "边界利用率" to "价格活跃度" for improved clarity.
- Updated normalization methods for all strength components to ensure they remain within the [0, 1] range, facilitating LLM tuning.
- Enhanced documentation to reflect changes in parameter names and scoring logic, including detailed explanations of the new tilt parameter.
- Modified multiple source files and scripts to accommodate the new scoring structure and ensure backward compatibility.

Files modified:
- `src/converging_triangle.py`, `src/converging_triangle_optimized.py`, `src/triangle_detector_api.py`: Updated parameter names and scoring logic.
- `scripts/plot_converging_triangles.py`, `scripts/generate_stock_viewer.py`: Adjusted for new scoring parameters in output.
- New documentation files created to explain the renaming and new scoring system in detail.
2026-01-29 15:55:50 +08:00

126 lines
4.2 KiB
Python

"""
测试倾斜度分数计算功能
测试 calc_tilt_score() 函数的正确性
"""
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
from converging_triangle import calc_tilt_score
def test_basic_cases():
"""测试基本情况"""
print("\n=== 测试基本情况 ===")
# 1. 对称三角形 + 向上突破(中轴水平)
score = calc_tilt_score(-0.03, 0.03, "up")
print(f"对称三角形 + 向上突破: {score:.3f} (期望 = 0.5)")
assert abs(score - 0.5) < 0.01, f"对称三角形应该得0.5,实际: {score:.3f}"
# 2. 对称三角形 + 向下突破
score = calc_tilt_score(-0.03, 0.03, "down")
print(f"对称三角形 + 向下突破: {score:.3f} (期望 = 0.5)")
assert abs(score - 0.5) < 0.01, f"对称三角形应该得0.5,实际: {score:.3f}"
# 3. 任意形态 + 未突破
score = calc_tilt_score(0.01, -0.01, "none")
print(f"任意形态 + 未突破: {score:.3f} (期望 = 0.5)")
assert score == 0.5, f"未突破应该得0.5,实际: {score:.3f}"
def test_upward_tilt():
"""测试向上倾斜的情况"""
print("\n=== 测试向上倾斜 ===")
# 轻微向上倾斜 + 向上突破(顺势)
score = calc_tilt_score(0.0, 0.05, "up")
print(f"向上倾斜 + 向上突破: {score:.3f} (期望 > 0.5)")
assert score > 0.5, f"向上倾斜向上突破应该 > 0.5,实际: {score:.3f}"
# 轻微向上倾斜 + 向下突破(逆势)
score = calc_tilt_score(0.0, 0.05, "down")
print(f"向上倾斜 + 向下突破: {score:.3f} (期望 < 0.5)")
assert score < 0.5, f"向上倾斜向下突破应该 < 0.5,实际: {score:.3f}"
# 强烈向上倾斜 + 向上突破
score = calc_tilt_score(0.0, 0.20, "up")
print(f"强向上倾斜 + 向上突破: {score:.3f} (期望明显 > 0.5)")
assert score > 0.55, f"强向上倾斜向上突破应该 > 0.55,实际: {score:.3f}"
def test_downward_tilt():
"""测试向下倾斜的情况"""
print("\n=== 测试向下倾斜 ===")
# 轻微向下倾斜 + 向下突破(顺势)
score = calc_tilt_score(-0.05, 0.0, "down")
print(f"向下倾斜 + 向下突破: {score:.3f} (期望 > 0.5)")
assert score > 0.5, f"向下倾斜向下突破应该 > 0.5,实际: {score:.3f}"
# 轻微向下倾斜 + 向上突破(逆势)
score = calc_tilt_score(-0.05, 0.0, "up")
print(f"向下倾斜 + 向上突破: {score:.3f} (期望 < 0.5)")
assert score < 0.5, f"向下倾斜向上突破应该 < 0.5,实际: {score:.3f}"
# 强烈向下倾斜 + 向下突破
score = calc_tilt_score(-0.20, 0.0, "down")
print(f"强向下倾斜 + 向下突破: {score:.3f} (期望明显 > 0.5)")
assert score > 0.55, f"强向下倾斜向下突破应该 > 0.55,实际: {score:.3f}"
def test_bounds():
"""测试边界"""
print("\n=== 测试边界 ===")
test_cases = [
(0.1, 0.1, "up"),
(-0.1, -0.1, "down"),
(0.0, 0.0, "none"),
(0.05, -0.05, "up"),
(-0.05, 0.05, "down"),
(0.5, 0.5, "up"), # 极端情况
(-0.5, -0.5, "down"), # 极端情况
]
for upper, lower, direction in test_cases:
score = calc_tilt_score(upper, lower, direction)
print(f" 斜率({upper:+.2f}, {lower:+.2f}) + {direction:5s}: {score:.3f}")
assert 0.0 <= score <= 1.0, f"分数超出 [0, 1] 范围: {score}"
print("所有分数都在 [0, 1] 范围内")
def run_all_tests():
"""运行所有测试"""
print("=" * 60)
print("倾斜度分数计算 - 单元测试")
print("=" * 60)
try:
test_basic_cases()
test_upward_tilt()
test_downward_tilt()
test_bounds()
print("\n" + "=" * 60)
print("[PASS] 所有测试通过!")
print("=" * 60)
return True
except AssertionError as e:
print("\n" + "=" * 60)
print(f"[FAIL] 测试失败: {e}")
print("=" * 60)
return False
except Exception as e:
print("\n" + "=" * 60)
print(f"[ERROR] 运行错误: {e}")
print("=" * 60)
return False
if __name__ == "__main__":
success = run_all_tests()
sys.exit(0 if success else 1)