technical-patterns-lab/tests/test_renaming.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

143 lines
4.6 KiB
Python

"""
测试重命名后的API是否正常工作
"""
import numpy as np
from converging_triangle import (
calc_geometry_score,
calc_activity_score,
calc_breakout_strength,
)
def test_geometry_score():
"""测试形态规则度计算"""
pivot_indices = np.array([0, 10, 20, 30])
pivot_values = np.array([100.0, 95.0, 90.0, 85.0])
slope = -0.5
intercept = 100.0
score = calc_geometry_score(pivot_indices, pivot_values, slope, intercept)
print(f"✅ 形态规则度计算成功: {score:.4f}")
assert 0 <= score <= 1, "分数应在 0~1 之间"
return score
def test_activity_score():
"""测试价格活跃度计算"""
high = np.array([105, 102, 100, 98, 96, 94, 92, 90, 88, 86])
low = np.array([95, 92, 90, 88, 86, 84, 82, 80, 78, 76])
upper_slope = -1.0
upper_intercept = 105.0
lower_slope = -1.0
lower_intercept = 95.0
score = calc_activity_score(high, low, upper_slope, upper_intercept,
lower_slope, lower_intercept, 0, 9)
print(f"✅ 价格活跃度计算成功: {score:.4f}")
assert 0 <= score <= 1, "分数应在 0~1 之间"
return score
def test_breakout_strength():
"""测试突破强度计算(使用新的参数名)"""
geometry_score = 0.8
activity_score = 0.7
strength_up, strength_down, price_up, price_down, conv, vol, geom, act = \
calc_breakout_strength(
close=105.0,
upper_line=100.0,
lower_line=90.0,
volume_ratio=1.5,
width_ratio=0.3,
geometry_score=geometry_score,
activity_score=activity_score,
)
print(f"✅ 突破强度计算成功:")
print(f" - 向上强度: {strength_up:.4f}")
print(f" - 形态规则度: {geom:.4f}")
print(f" - 价格活跃度: {act:.4f}")
assert 0 <= strength_up <= 1, "强度应在 0~1 之间"
assert abs(geom - geometry_score) < 0.01, "形态规则度应正确传递"
assert abs(act - activity_score) < 0.01, "价格活跃度应正确传递"
return strength_up, strength_down
def test_imports():
"""测试新命名的导入"""
try:
from converging_triangle import calc_geometry_score, calc_activity_score
from converging_triangle_optimized import (
calc_geometry_score_optimized,
calc_activity_score_optimized,
)
print("✅ 所有函数导入成功")
return True
except ImportError as e:
print(f"❌ 导入失败: {e}")
return False
def test_dataclass_fields():
"""测试数据类字段重命名"""
from converging_triangle import ConvergingTriangleResult
from triangle_detector_api import StrengthComponents
# 检查 ConvergingTriangleResult 字段
result = ConvergingTriangleResult()
assert hasattr(result, 'geometry_score'), "应有 geometry_score 字段"
assert hasattr(result, 'activity_score'), "应有 activity_score 字段"
assert not hasattr(result, 'fitting_score'), "不应有旧的 fitting_score 字段"
assert not hasattr(result, 'boundary_utilization'), "不应有旧的 boundary_utilization 字段"
print("✅ ConvergingTriangleResult 字段正确")
# 检查 StrengthComponents 字段
comp = StrengthComponents(
price_score=0.5,
convergence_score=0.6,
volume_score=0.4,
geometry_score=0.7,
activity_score=0.8,
)
assert comp.geometry_score == 0.7, "geometry_score 应正确设置"
assert comp.activity_score == 0.8, "activity_score 应正确设置"
print("✅ StrengthComponents 字段正确")
return True
if __name__ == "__main__":
print("=" * 60)
print("测试重命名后的API")
print("=" * 60)
try:
# 1. 测试导入
print("\n[1/5] 测试函数导入...")
test_imports()
# 2. 测试数据类字段
print("\n[2/5] 测试数据类字段...")
test_dataclass_fields()
# 3. 测试形态规则度
print("\n[3/5] 测试形态规则度计算...")
test_geometry_score()
# 4. 测试价格活跃度
print("\n[4/5] 测试价格活跃度计算...")
test_activity_score()
# 5. 测试突破强度
print("\n[5/5] 测试突破强度计算...")
test_breakout_strength()
print("\n" + "=" * 60)
print("✅ 所有测试通过!重命名成功!")
print("=" * 60)
except Exception as e:
print("\n" + "=" * 60)
print(f"❌ 测试失败: {e}")
print("=" * 60)
import traceback
traceback.print_exc()