"""
Technical Indicators
Collection of technical analysis indicators.
"""
import pandas as pd
import numpy as np
def calculate_rsi(prices: pd.Series, window: int = 14) -> pd.Series:
"""Calculate Relative Strength Index."""
delta = prices.diff()
gain = (delta.where(delta > 0, 0)).rolling(window=window).mean()
loss = (-delta.where(delta < 0, 0)).rolling(window=window).mean()
rs = gain / loss
return 100 - (100 / (1 + rs))
def calculate_macd(prices: pd.Series, fast: int = 12, slow: int = 26, signal: int = 9) -> Dict[str, pd.Series]:
"""Calculate MACD indicators."""
exp1 = prices.ewm(span=fast).mean()
exp2 = prices.ewm(span=slow).mean()
macd = exp1 - exp2
macd_signal = macd.ewm(span=signal).mean()
macd_histogram = macd - macd_signal
return {
'macd': macd,
'signal': macd_signal,
'histogram': macd_histogram
}
def calculate_bollinger_bands(prices: pd.Series, window: int = 20, num_std: float = 2) -> Dict[str, pd.Series]:
"""Calculate Bollinger Bands."""
sma = prices.rolling(window=window).mean()
std = prices.rolling(window=window).std()
return {
'upper': sma + (std * num_std),
'middle': sma,
'lower': sma - (std * num_std)
}