import yfinance as yf

# get the SMA, needed for Bollinger
def get_sma(prices, rate):
 return prices.rolling(rate).mean()

# get the Bollinger bands
def get_bollinger_bands(prices, rate, dev=2):
 prices = prices['Close']
 sma = get_sma(prices, rate)
 std = prices.rolling(rate).std()
 bollinger_up = sma + std * dev
 bollinger_down = sma - std * dev
 return bollinger_up, bollinger_down

# RSI - get the RSI data
def RSI(data, window=14, adjust=True):
 data = data['Close']
 delta = data.diff()
 up = delta.clip(lower=0)
 down = -1 * delta.clip(upper=0)
 ema_up = up.ewm(com=window, adjust=adjust).mean()
 ema_down = down.ewm(com=window, adjust=adjust).mean()
 RS = ema_up / ema_down
 RSI = 100 - 100 / (1 + RS)
 return RSI

# get stochastic data
# returns 2 values:
# %K - percentage of the difference between the highest and the lowest price
# over the period - the current percentage of the price
# %D - the average of %K
# %K - "fast" %D - "slow"
def get_stochastic(data, window=14):
# high = data['High'].rolling(window).max()
 high = data['Close'].rolling(window).max()
# low = data['Low'].rolling(window).min()
 low = data['Close'].rolling(window).min()
 percentK = (data['Close'] - low) * 100 / (high - low)
 percentD = percentK.rolling(3).mean()
 return percentK, percentD
