Download qrypto/indicators.py from Qempireautomation/qrypto-engine: direct link, hf CLI and curl.
- Browser
- Download file 1.76 kB
-
https://huggingface.co/Qempireautomation/qrypto-engine/resolve/main/qrypto/indicators.py
- Command line
-
hf download hf://Qempireautomation/qrypto-engine/qrypto/indicators.py
-
curl -L -o indicators.py https://huggingface.co/Qempireautomation/qrypto-engine/resolve/main/qrypto/indicators.py
1.76 kB
| """Technical indicators on pandas Series. Pure, vectorised, no lookahead.""" | |
| from __future__ import annotations | |
| import numpy as np | |
| import pandas as pd | |
| def sma(s: pd.Series, n: int) -> pd.Series: | |
| return s.rolling(n, min_periods=n).mean() | |
| def ema(s: pd.Series, n: int) -> pd.Series: | |
| return s.ewm(span=n, adjust=False, min_periods=n).mean() | |
| def rsi(s: pd.Series, n: int = 14) -> pd.Series: | |
| delta = s.diff() | |
| up = delta.clip(lower=0) | |
| down = -delta.clip(upper=0) | |
| avg_up = up.ewm(alpha=1 / n, adjust=False, min_periods=n).mean() | |
| avg_down = down.ewm(alpha=1 / n, adjust=False, min_periods=n).mean() | |
| rs = avg_up / avg_down.replace(0, np.nan) | |
| out = 100 - 100 / (1 + rs) | |
| out = out.mask((avg_down == 0) & (avg_up > 0), 100.0) | |
| out = out.mask((avg_down == 0) & (avg_up == 0), 50.0) | |
| return out | |
| def atr(df: pd.DataFrame, n: int = 14) -> pd.Series: | |
| prev_close = df["close"].shift(1) | |
| tr = pd.concat( | |
| [df["high"] - df["low"], (df["high"] - prev_close).abs(), (df["low"] - prev_close).abs()], axis=1 | |
| ).max(axis=1) | |
| return tr.ewm(alpha=1 / n, adjust=False, min_periods=n).mean() | |
| def bollinger(s: pd.Series, n: int = 20, k: float = 2.0) -> tuple[pd.Series, pd.Series, pd.Series]: | |
| mid = sma(s, n) | |
| sd = s.rolling(n, min_periods=n).std(ddof=0) | |
| return mid - k * sd, mid, mid + k * sd | |
| def zscore(s: pd.Series, n: int = 20) -> pd.Series: | |
| mid = sma(s, n) | |
| sd = s.rolling(n, min_periods=n).std(ddof=0) | |
| return (s - mid) / sd.replace(0, np.nan) | |
| def donchian(df: pd.DataFrame, n: int = 20) -> tuple[pd.Series, pd.Series]: | |
| # shifted by one so today's breakout is measured against *prior* n bars | |
| return df["low"].rolling(n).min().shift(1), df["high"].rolling(n).max().shift(1) | |