INNER CODE UNIT · Python

_levels

renee-jia/trading-bot · ai_sell_put_plan.py:103

def _levels(hist):
    """SMA20 / SMA50 / SMA200 / 21-day low / tape fallbacks from 1y history."""
    closes = hist["Close"].dropna()
    out = {"sma_20": None, "sma_50": None, "sma_200": None, "low_21d": None,
           "change_1d": None, "from_high": None, "rsi": None}
    if len(closes) >= 2:
        out["change_1d"] = float((closes.iloc[-1] / closes.iloc[-2] - 1) * 100)
        out["from_high"] = float((closes.iloc[-1] / closes.max() - 1) * 100)
    if len(closes) >= 15:
        diff = closes.diff().dropna().tail(14)
        gain, loss = diff.clip(lower=0).mean(), (-diff.clip(upper=0)).mean()
        out["rsi"] = 100.0 if loss == 0 else float(100 - 100 / (1 + gain / loss))
    if len(closes) >= 20:
        out["sma_20"] = float(closes.tail(20).mean())
    if len(closes) >= 50:
        out["sma_50"] = float(closes.tail(50).mean())
    if len(closes) >= 200:
        out["sma_200"] = float(closes.tail(200).mean())

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…