"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
# Adapted from microsoft/qlib@d5379c520f66a39953bad76234a7019a72796fd0:qlib/contrib/data/handler.py
# (Apache-2.0). Copyright (c) Microsoft Corporation.
# ============================================================
# 中文名称: 上涨天数计数 20日
# 简要说明: rolling_mean(1[close>close_prev], 20),20日内上涨天数占比。
# 典型用途: 衡量20日内上涨频率,值高表示持续上涨行情。
# ============================================================
"""qlib158 CNTP20: formula = \\mathrm{rolling\\_mean}(\\mathrm{1}[\\mathrm{close}>\\mathrm{close}_{{-1}}], 20)."""
from __future__ import annotations
import pandas as pd
__alpha_meta__ = {
'id': 'qlib158_cntp20',
'theme': ['reversal'],
'formula_latex': '\\\\mathrm{rolling\\\\_mean}(\\\\mathrm{1}[\\\\mathrm{close}>\\\\mathrm{close}_{{-1}}], 20)',
'columns_required': ['close'],
'universe': ['equity_us', 'equity_cn', 'equity_hk', 'equity_in', 'equity_kr'],
'frequency': ['1d'],
'decay_horizon': 20,
'min_warmup_bars': 20,
'notes': 'A missing close leaves window+1 rolling rows NaN; on sparse panels this can trigger the >95% NaN registry guard (from ~15% missing bars).',
}
def compute(panel: dict[str, pd.DataFrame]) -> pd.DataFrame:
"""Return qlib158 CNTP20 on the supplied OHLCV panel."""
c = panel['close']
prev = c.shift(1)
up = (c > prev).astype('float64').where(c.notna() & prev.notna()) # a missing close is not an up day
return up.rolling(window=20, min_periods=20).mean()