File size: 1,350 Bytes
f3d9e94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
import matplotlib.pyplot as plt
import numpy as np


#  HELPER FUNCTIONS:
def plot_column(dataframe, column_name):
    plt.figure(figsize=(6, 3))
    plt.plot(dataframe[column_name])
    plt.title(f"Plot of {column_name}")
    plt.xlabel("Index")
    plt.ylabel(column_name)
    plt.grid(True)
    return plt


# Calculate EMA
def ema(close, period=20):
    return close.ewm(span=period, adjust=False).mean()


# Calculate RSI
def rsi(close, period=14):
    delta = close.diff()
    gain, loss = delta.copy(), delta.copy()
    gain[gain < 0] = 0
    loss[loss > 0] = 0
    avg_gain = gain.rolling(period).mean()
    avg_loss = abs(loss.rolling(period).mean())
    rs = avg_gain / avg_loss
    rsi = 100.0 - (100.0 / (1.0 + rs))
    return rsi


# Calculate MACD
def macd(close, fast_period=12, slow_period=26, signal_period=9):
    fast_ema = close.ewm(span=fast_period, adjust=False).mean()
    slow_ema = close.ewm(span=slow_period, adjust=False).mean()
    macd_line = fast_ema - slow_ema
    signal_line = macd_line.ewm(span=signal_period, adjust=False).mean()
    histogram = macd_line - signal_line
    return macd_line


# Calculate OBV
def obv(close, volume):
    obv = np.where(
        close > close.shift(), volume, np.where(close < close.shift(), -volume, 0)
    ).cumsum()
    return obv