"""测试转债评价指标计算 - 复现 daily_pnl_std None 问题"""

from ldnode.calculator.handlers.cb_evalution_handler import handle_cb_evalution

parameters = {
    "task_id": "016b337e-1007-44fe-aa72-9dbe9ae57194",
    "strategy_config": {
        "backtest_type": 1,
        "benchmark": "EQUALWEIGHT",
        "capital": 100000.0,
        "commission": 0.001,
        "computed_factors": [
            [{"expr": "ts_shift(close_stk, 20)", "expr_type": 2, "factor": "node_1", "is_intermediate": True}],
            [{"expr": "close_stk / node_1", "expr_type": 2, "factor": "node_2", "is_intermediate": True}],
            [{"expr": "node_2 - 1", "expr_type": 2, "factor": "node_3", "is_intermediate": True}],
            [
                {"expr": "ts_std(pct_chg_stk, 20)", "expr_type": 2, "factor": "custom_FaEEd", "is_intermediate": True},
                {"expr": "node_3 * 100", "expr_type": 2, "factor": "custom_kVozv", "is_intermediate": True},
            ],
            [{"expr": "custom_kVozv / custom_FaEEd", "expr_type": 2, "factor": "custom_NYHAc", "is_intermediate": False}],
        ],
        "date_range": {"end_date": "2026-01-21", "start_date": "2025-01-02"},
        "exclude_area": None,
        "exclude_call": ["已满足强赎条件", "公告提示强赎", "公告实施强赎", "公告到期赎回", "已公告强赎", "已公告强赎"],
        "exclude_first_industries": None,
        "exclude_ignore_list": None,
        "exclude_list_days": 20,
        "exclude_market": None,
        "exclude_orgform": None,
        "exclude_rating": ["BBB+", "BBB", "BBB-", "BB+", "CCC", "B", "B+", "C", "CC", "BB", "BB-", "B-"],
        "exclude_second_industries": ["银行"],
        "exclude_st": True,
        "exclude_third_industries": None,
        "exclude_yy_rating": [],
        "filter_factors": {
            "conditions": [
                {
                    "conditions": [
                        {"factor": "left_years", "operator": "le", "type": 0, "value": 1.0},
                        {"factor": "redeem_remain_days", "operator": "le", "type": 3, "value": 14.0},
                        {"factor": "close", "operator": "lt", "type": 0, "value": 120.0},
                        {"factor": "close", "operator": "gt", "type": 0, "value": 200.0},
                        {"factor": "conv_value", "operator": "le", "type": 0, "value": 115.0},
                    ],
                    "relation": "OR",
                },
                {"factor": "list_days", "operator": "le", "type": 0, "value": 20},
                {"factor": "redeem_remain_days", "operator": "le", "type": 0, "value": 10},
            ],
            "relation": "OR",
        },
        "hold_method": {
            "hold_range_type": 1,
            "hold_range_value": {"end": 10.0, "start": 1.0},
            "hold_threshold": 2.0,
            "hold_weight": 2,
            "max_hold_weight": 1.0,
        },
        "pools": [
            {"pool_builtin_scope": "sh_mainboard", "pool_customize_type": None, "pool_filter_factors": None, "pool_securities": None, "pool_type": 0},
            {"pool_builtin_scope": "sz_mainboard", "pool_customize_type": None, "pool_filter_factors": None, "pool_securities": None, "pool_type": 0},
        ],
        "rank_factors": [
            {"ascending": False, "factor": "conv_prem", "neutralization_factor": None, "process_missing_data_method": None, "type": 0, "weight": 4.0},
            {"ascending": False, "factor": "pe_ttm", "neutralization_factor": None, "process_missing_data_method": None, "type": 0, "weight": 8.0},
            {"ascending": True, "factor": "pre_close", "neutralization_factor": None, "process_missing_data_method": None, "type": 0, "weight": 3.0},
            {"ascending": True, "factor": "custom_NYHAc", "neutralization_factor": None, "process_missing_data_method": None, "type": 1, "weight": 3.0},
            {"ascending": True, "factor": "pure_value", "neutralization_factor": None, "process_missing_data_method": None, "type": 0, "weight": 5.0},
        ],
        "rebalance_method": {"rebalance_type": 0, "rebalance_value": 1},
        "rotation_type": 0,
        "stop_method": {"stop_type": 1, "stop_value": 0.07},
    },
    "test_begin_date": "2025-01-03",
}

if __name__ == "__main__":
    import json
    result = handle_cb_evalution(parameters)
    print(json.dumps(result, indent=2, default=str))
