-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindicators.py
More file actions
38 lines (32 loc) · 971 Bytes
/
Copy pathindicators.py
File metadata and controls
38 lines (32 loc) · 971 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import pandas as pd
import numpy as np
from ta.momentum import RSIIndicator
from ta.volatility import AverageTrueRange
def add_rsi(df: pd.DataFrame, period: int) -> pd.DataFrame:
df = df.copy()
rsi = RSIIndicator(close=df["close"], window=period)
df["rsi"] = rsi.rsi()
return df
from ta.volatility import AverageTrueRange
def add_atr(df, window=14):
# Not enough candles to compute ATR
if len(df) < window:
df["atr"] = 0
return df
try:
atr = AverageTrueRange(
high=df["high"],
low=df["low"],
close=df["close"],
window=window,
fillna=True
).average_true_range()
df["atr"] = atr
except Exception:
df["atr"] = 0
return df
def add_volatility_features(df: pd.DataFrame) -> pd.DataFrame:
df = df.copy()
df["returns"] = df["close"].pct_change()
df["volatility"] = df["returns"].rolling(20).std() * 100
return df