Tortoise/tests/test_strategy_a500_hs300.py
2026-06-24 16:50:59 +08:00

79 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""策略2中证 A500 与沪深 300 双轴图测试。"""
from datetime import date
from strategy.a500_close_chart import ClosePoint
from strategy.a500_hs300_close_chart import DEFAULT_OUTPUT_PATH, DualAxisSeries, _scale_points_by_ratio, align_points_by_date, build_dual_axis_svg_chart
def test_5_1_align_points_by_common_date() -> None:
"""
功能说明验证策略2按共同交易日对齐两组收盘价。
参数说明:无。
返回值说明:无返回值。
注意事项:用例编号 5-1。
"""
left = [ClosePoint(date(2024, 9, 23), 100), ClosePoint(date(2024, 9, 24), 101)]
right = [ClosePoint(date(2024, 9, 24), 201), ClosePoint(date(2024, 9, 25), 202)]
aligned_left, aligned_right = align_points_by_date(left, right)
assert aligned_left == [ClosePoint(date(2024, 9, 24), 101)]
assert aligned_right == [ClosePoint(date(2024, 9, 24), 201)]
def test_5_2_build_dual_axis_svg_chart() -> None:
"""
功能说明验证策略2可生成包含双轴和两条折线的 SVG。
参数说明:无。
返回值说明:无返回值。
注意事项:用例编号 5-2。
"""
left = DualAxisSeries(
"中证 A500",
"sh000510",
"#2563eb",
[ClosePoint(date(2024, 9, 23), 3700), ClosePoint(date(2024, 9, 24), 3710)],
)
right = DualAxisSeries(
"沪深 300",
"sh000300",
"#dc2626",
[ClosePoint(date(2024, 9, 23), 3300), ClosePoint(date(2024, 9, 24), 3310)],
)
svg = build_dual_axis_svg_chart(left, right, "中证 A500 与沪深 300 收盘价对比")
assert svg.startswith("<svg")
assert "中证 A500 与沪深 300 收盘价对比" in svg
assert "中证 A500左轴" in svg
assert "沪深 300右轴" in svg
assert svg.count("<polyline") == 2
def test_5_3_same_return_ratio_maps_to_same_y() -> None:
"""
功能说明验证策略2相同涨跌比例会映射到相同 Y 坐标。
参数说明:无。
返回值说明:无返回值。
注意事项:用例编号 5-3两个指数起点不同但第二天都上涨 10%
"""
left = [ClosePoint(date(2024, 9, 23), 1000), ClosePoint(date(2024, 9, 24), 1100)]
right = [ClosePoint(date(2024, 9, 23), 3000), ClosePoint(date(2024, 9, 24), 3300)]
left_coordinates = _scale_points_by_ratio(left, 1000, 0, 0, 100, 100, 0, 0.1)
right_coordinates = _scale_points_by_ratio(right, 3000, 0, 0, 100, 100, 0, 0.1)
assert left_coordinates[0][1] == right_coordinates[0][1]
assert left_coordinates[1][1] == right_coordinates[1][1]
def test_5_4_default_output_path_uses_strategy_directory() -> None:
"""
功能说明验证策略2默认输出到同名子目录。
参数说明:无。
返回值说明:无返回值。
注意事项:用例编号 5-4。
"""
assert DEFAULT_OUTPUT_PATH.as_posix() == "strategy/output/a500_hs300_close_chart/a500_hs300_close.svg"