# -*- coding: utf-8 -*-
"""生成 GridMaster EA 的 .set 参数文件 (UTF-16LE + BOM, CRLF)"""

BASE = {
    # 通用设置
    "InpComment":        "GridMaster",
    "InpMaxSpreadPoints": 0,
    "InpSlippagePoints": 50,
    "InpLogLevel":       0,
    "InpShowPanel":      True,
    # 基础设置
    "InpIsEnable":       True,
    "InpIsAutoLot":      False,
    "InpAutoLotMargin":  20000.0,
    "InpInitialLot":     0.01,
    "InpMaxLot":         0.4,
    "InpAllowBuy":       True,
    "InpAllowSell":      True,
    # 交易时段 (平台时间, 仅约束首单)
    "InpSessionMonday":    "02:00-19:00",
    "InpSessionTuesday":   "02:00-19:00",
    "InpSessionWednesday": "02:00-19:00",
    "InpSessionThursday":  "02:00-19:00",
    "InpSessionFriday":    "02:00-19:00",
    "InpSessionSaturday":  "",
    "InpSessionSunday":    "00:00-00:00",
    # 加仓参数
    "InpPipsNormalize":   True,
    "InpAddMode":         0,      # 0=逆势加仓(马丁格尔)
    "InpContinueAddAfterSL": False,
    # 止损参数
    "InpUseAtrSl":        True,
    "InpAtrPeriod":       14,
    "InpAtrThreshold":    5.0,
    "InpAtrMultHigh":     20.0,
    "InpAtrMultLow":      25.0,
    "InpUseFixedSl":      False,
    "InpFixedSlPoints":   0,
    "InpUseAtrTp":        False,
    "InpAtrTpMultiplier": 20.0,
    "InpUseFixedTp":      False,
    "InpFixedTpPoints":   0,
    # 移动止盈
    "InpUseTrailingTp":   False,
    "InpTrailStartPips":  2500,
    "InpTrailMarketPips": 800,
    "InpShowTrailLine":   True,
    # 整体止盈止损
    "InpMoneyUnit":       0,      # 0=美元 1=美分
    "InpOverallTpAmount": 800.0,
    "InpRetraceStart":    100.0,
    "InpRetraceDrawdown": 10.0,
    "InpEquityStopPct":   0.0,
    # 信号参数
    "InpFirstOpenMode":   0,      # 0=RSI反转
    "InpUseRsiFilter":    True,
    "InpRsiFilterAdd":    True,
    "InpRsiTF":           0,      # PERIOD_CURRENT
    "InpRsiPeriod":       14,
    "InpRsiUpper":        70.0,
    "InpRsiLower":        30.0,
    "InpAddRsiMidline":   50.0,
    "InpUseMaFilter":     False,
    "InpMaTF":            0,
    "InpMaPeriod":        200,
    "InpOneEntryPerBar":  True,
}

# 四套方案: (文件名后缀, 魔术号, 倍数, 最大单数, 加仓间距point, 整体止损)
PLANS = [
    ("原版复刻", 20260901, 1.50, 15, 2500, 0.0),
    ("保守",     20260902, 1.15, 15, 4500, 15000.0),
    ("稳健",     20260903, 1.20, 12, 4500, 15000.0),
    ("进取",     20260904, 1.25, 12, 4500, 15000.0),
]


def fmt(v):
    if isinstance(v, bool):
        return "true" if v else "false"
    if isinstance(v, float):
        s = repr(v)
        return s
    return str(v)


for name, magic, mult, nmax, dist, sl in PLANS:
    p = dict(BASE)
    p["InpMagicNum"] = magic
    p["InpAddMultiplier"] = mult
    p["InpMaxOrders"] = nmax
    p["InpAddDistancePips"] = dist
    p["InpOverallSlAmount"] = sl

    # 保持与 EA 声明一致的输出顺序
    order = [
        "InpMagicNum", "InpComment", "InpMaxSpreadPoints", "InpSlippagePoints",
        "InpLogLevel", "InpShowPanel",
        "InpIsEnable", "InpIsAutoLot", "InpAutoLotMargin", "InpInitialLot",
        "InpMaxLot", "InpAllowBuy", "InpAllowSell",
        "InpSessionMonday", "InpSessionTuesday", "InpSessionWednesday",
        "InpSessionThursday", "InpSessionFriday", "InpSessionSaturday",
        "InpSessionSunday",
        "InpAddMultiplier", "InpAddDistancePips", "InpPipsNormalize",
        "InpMaxOrders", "InpAddMode", "InpContinueAddAfterSL",
        "InpUseAtrSl", "InpAtrPeriod", "InpAtrThreshold", "InpAtrMultHigh",
        "InpAtrMultLow", "InpUseFixedSl", "InpFixedSlPoints", "InpUseAtrTp",
        "InpAtrTpMultiplier", "InpUseFixedTp", "InpFixedTpPoints",
        "InpUseTrailingTp", "InpTrailStartPips", "InpTrailMarketPips",
        "InpShowTrailLine",
        "InpMoneyUnit", "InpOverallTpAmount", "InpOverallSlAmount",
        "InpRetraceStart", "InpRetraceDrawdown", "InpEquityStopPct",
        "InpFirstOpenMode", "InpUseRsiFilter", "InpRsiFilterAdd", "InpRsiTF",
        "InpRsiPeriod", "InpRsiUpper", "InpRsiLower", "InpAddRsiMidline",
        "InpUseMaFilter", "InpMaTF", "InpMaPeriod", "InpOneEntryPerBar",
    ]
    missing = [k for k in order if k not in p]
    extra = [k for k in p if k not in order]
    assert not missing and not extra, (missing, extra)

    lines = ["%s=%s" % (k, fmt(p[k])) for k in order]
    text = "\r\n".join(lines) + "\r\n"
    fn = "GridMaster_%s.set" % name
    with open(fn, "wb") as f:
        f.write(b"\xff\xfe")
        f.write(text.encode("utf-16-le"))
    print("%-24s %d 行  %d 字节" % (fn, len(lines), 2 + len(text.encode("utf-16-le"))))
