Files
Trademind_OpenCode/trademind/indicators.py
T

39 lines
1.2 KiB
Python

"""Technische Indikatoren (reine pandas/numpy, ohne TA-Lib)."""
from __future__ import annotations
import numpy as np
import pandas as pd
def ema(series: pd.Series, period: int) -> pd.Series:
return series.ewm(span=period, adjust=False).mean()
def rsi(close: pd.Series, period: int = 14) -> pd.Series:
delta = close.diff()
gain = delta.clip(lower=0.0)
loss = -delta.clip(upper=0.0)
avg_gain = gain.ewm(alpha=1 / period, adjust=False).mean()
avg_loss = loss.ewm(alpha=1 / period, adjust=False).mean()
rs = avg_gain / avg_loss.replace(0.0, np.nan)
out = 100 - (100 / (1 + rs))
return out.fillna(50.0)
def atr(df: pd.DataFrame, period: int = 14) -> pd.Series:
high, low, close = df["high"], df["low"], df["close"]
prev_close = close.shift(1)
tr = pd.concat(
[(high - low), (high - prev_close).abs(), (low - prev_close).abs()], axis=1
).max(axis=1)
return tr.ewm(alpha=1 / period, adjust=False).mean()
def crossover(a: pd.Series, b: pd.Series) -> pd.Series:
return (a > b) & (a.shift(1) <= b.shift(1))
def crossunder(a: pd.Series, b: pd.Series) -> pd.Series:
return (a < b) & (a.shift(1) >= b.shift(1))