"""US low-volatility defensive compounder -> moderate-leverage ETF strategy (FinLab).

A trend/momentum regime filter on QQQ decides between two books: in risk-on it
holds the stronger of two 2x leveraged index ETFs (QLD / SSO); in risk-off it
rotates into the strongest defensive ETF (IEF / GLD / SHY). Using 2x instead of
3x leverage lowers volatility drag and path dependency. Reproduces the backtest
in the article. Complete the AI-assisted setup flow at https://finlab.finance/en/setup before running this file.

    python strategy.py
"""
import finlab
from finlab import data
from finlab.backtest import sim
from finlab.dataframe import FinlabDataFrame

RISK_ASSETS = ["QLD", "SSO"]              # 2x leveraged index ETFs (held in risk-on)
DEFENSIVE_ASSETS = ["IEF", "GLD", "SHY"]  # treasuries / gold / cash-like (risk-off)


def build_position():
    data.set_market("us_fund")
    close = data.get("us_fund_price:adj_close")[["QQQ"] + RISK_ASSETS + DEFENSIVE_ASSETS]

    # 1. Risk-on regime: QQQ above its 200-day average AND positive 126-day return.
    qqq_trend = close["QQQ"] > close["QQQ"].rolling(200, min_periods=100).mean()
    qqq_momentum = close["QQQ"] / close["QQQ"].shift(126) - 1
    risk_on = qqq_trend & (qqq_momentum > 0)

    # 2. In risk-on: hold the stronger 2x ETF by 189-day-minus-21-day momentum.
    risk_score = FinlabDataFrame(
        close[RISK_ASSETS].pct_change(189) - close[RISK_ASSETS].pct_change(21)
    )
    risky = risk_score.is_largest(1) & risk_on.to_frame().reindex(risk_score.index).iloc[:, 0]

    # 3. In risk-off: hold the strongest defensive ETF by 63-day-minus-21-day momentum.
    defensive_score = FinlabDataFrame(
        close[DEFENSIVE_ASSETS].pct_change(63) - close[DEFENSIVE_ASSETS].pct_change(21)
    )
    defensive = (
        defensive_score.is_largest(1) & (~risk_on).to_frame().reindex(defensive_score.index).iloc[:, 0]
    )

    position = (
        risky.reindex(close.index).fillna(False).astype(float)
        .join(defensive.reindex(close.index).fillna(False).astype(float), how="outer")
        .fillna(0)
        .T.groupby(level=0).max().T
    )
    return position.loc["2016-01-01":]


def main():
    finlab.login()  # finlab guides you through login automatically
    position = build_position()
    report = sim(
        position,
        resample="M",           # rebalance monthly
        position_limit=1,       # max weight per asset; the strategy holds 1 ETF at a time
        stop_loss=0.06,         # 6% touched stop on the held ETF
        touched_exit=True,
        trade_at_price="close",
        upload=False,
    )
    print(report.get_stats())
    report.display()


if __name__ == "__main__":
    main()
