forex-scanner / data /indicators.py
SKaush's picture
Upload 30 files
aa79b15 verified
Raw History Blame Contribute Delete
3.71 kB
"""data/indicators.py — technical indicators added to OHLCV dataframes."""
import numpy as np
import pandas as pd
from config import (EMA_FAST, EMA_SLOW, SMA_TREND, RSI_PERIOD, RSI_MA_PERIOD,
MACD_FAST, MACD_SLOW, MACD_SIGNAL, ADX_PERIOD, VOL_MA)
def add_mas(df, include_trend=False):
df = df.copy()
df["ma_fast"] = df["close"].ewm(span=EMA_FAST, adjust=False).mean()
df["ma_slow"] = df["close"].ewm(span=EMA_SLOW, adjust=False).mean()
if include_trend:
df["ma_trend"] = df["close"].rolling(SMA_TREND).mean()
df["dist_slow"] = df["close"] - df["ma_slow"]
conds = [
(df["close"] > df["ma_fast"]) & (df["ma_fast"] > df["ma_slow"]),
(df["close"] < df["ma_fast"]) & (df["ma_fast"] < df["ma_slow"]),
(df["close"] > df["ma_slow"]) & (df["close"] < df["ma_fast"]),
(df["close"] < df["ma_slow"]) & (df["close"] > df["ma_fast"]),
]
df["price_zone"] = np.select(
conds, ["above_both","below_both","between_pb_up","between_pb_dn"], default="mixed")
df["cross"] = 0
df.loc[(df["ma_fast"] > df["ma_slow"]) & (df["ma_fast"].shift(1) <= df["ma_slow"].shift(1)), "cross"] = 1
df.loc[(df["ma_fast"] < df["ma_slow"]) & (df["ma_fast"].shift(1) >= df["ma_slow"].shift(1)), "cross"] = -1
if include_trend and "ma_trend" in df.columns:
df["ema_stack_bull"] = (df["ma_fast"] > df["ma_slow"]) & (df["ma_slow"] > df["ma_trend"])
df["ema_stack_bear"] = (df["ma_fast"] < df["ma_slow"]) & (df["ma_slow"] < df["ma_trend"])
return df
def add_rsi(df):
df = df.copy(); delta = df["close"].diff()
ag = delta.clip(lower=0).ewm(alpha=1/RSI_PERIOD, min_periods=RSI_PERIOD, adjust=False).mean()
al = (-delta.clip(upper=0)).ewm(alpha=1/RSI_PERIOD, min_periods=RSI_PERIOD, adjust=False).mean()
df["rsi"] = 100 - (100 / (1 + ag / al.replace(0, np.nan)))
df["rsi_ma"] = df["rsi"].rolling(RSI_MA_PERIOD).mean()
df["rsi_above_ma"] = df["rsi"] > df["rsi_ma"]
return df
def add_macd(df):
df = df.copy()
ef = df["close"].ewm(span=MACD_FAST, adjust=False).mean()
es = df["close"].ewm(span=MACD_SLOW, adjust=False).mean()
df["macd"] = ef - es
df["macd_sig"] = df["macd"].ewm(span=MACD_SIGNAL, adjust=False).mean()
df["macd_hist"] = df["macd"] - df["macd_sig"]
return df
def add_adx(df):
df = df.copy(); hi, lo, cl = df["high"], df["low"], df["close"]
pdm = hi.diff().clip(lower=0); ndm = (-lo.diff()).clip(lower=0)
pdm[pdm < ndm] = 0; ndm[ndm < pdm] = 0
tr = pd.concat([hi-lo, (hi-cl.shift()).abs(), (lo-cl.shift()).abs()], axis=1).max(axis=1)
a = 1 / ADX_PERIOD
atr = tr.ewm(alpha=a, min_periods=ADX_PERIOD, adjust=False).mean()
pdi = 100 * pdm.ewm(alpha=a, min_periods=ADX_PERIOD, adjust=False).mean() / atr
ndi = 100 * ndm.ewm(alpha=a, min_periods=ADX_PERIOD, adjust=False).mean() / atr
dx = 100 * (pdi - ndi).abs() / (pdi + ndi).replace(0, np.nan)
df["adx"] = dx.ewm(alpha=a, min_periods=ADX_PERIOD, adjust=False).mean()
df["plus_di"] = pdi; df["minus_di"] = ndi
return df
def add_vol(df):
df = df.copy()
df["vol_ma"] = df["tick_volume"].rolling(VOL_MA).mean()
df["vol_ratio"] = df["tick_volume"] / df["vol_ma"]
return df
def add_atr(df, period=14):
df = df.copy(); hi = df["high"]; lo = df["low"]; cl = df["close"]
tr = pd.concat([hi-lo, (hi-cl.shift()).abs(), (lo-cl.shift()).abs()], axis=1).max(axis=1)
df["atr"] = tr.ewm(span=period, adjust=False).mean()
return df
def add_all(df, include_trend=False):
for fn in [lambda d: add_mas(d, include_trend), add_rsi, add_macd, add_adx, add_vol, add_atr]:
df = fn(df)
return df