-
Notifications
You must be signed in to change notification settings - Fork 37
/
ema_macd_rsi.py
126 lines (96 loc) · 4.77 KB
/
ema_macd_rsi.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import pandas as pd
import ta
import trading_strategies.visualise as v
# @author: vita
'''
This strategy combines ema_crossover_rsi_alternative and a modified ema_crossover_macd to determine buy and sell
signals. Ema_crossover_macd was modified such that 9EMA only needs to be below/above 21EMA to fulfill sell/buy
signals respectively rather than a crossover below or above.
'''
class EMAMACDRSI:
def __init__(self, file_path):
self.df = pd.read_csv(file_path)
# self.df = pd.DataFrame(inputs)
self.close = self.df['close']
def calculate_21_ema(self):
self.df['21ema'] = self.df['close'].ewm(span=21, adjust=False).mean()
def calculate_9_ema(self):
self.df['9ema'] = self.df['close'].ewm(span=9, adjust=False).mean()
def add_macd_diff(self):
self.df['macd_histogram'] = ta.trend.MACD(close = self.close).macd_diff()
def add_distance_between_21ema_and_close(self):
self.df['distance'] = self.df['21ema'] - self.close
def add_rsi(self):
rsi_ind = ta.momentum.RSIIndicator(close=self.close, n=14)
self.df['rsi'] = rsi_ind.rsi()
def calculate_12_ema(self):
self.df['12ema'] = self.df['close'].ewm(span=12, adjust=False).mean()
def calculate_6_ema(self):
self.df['6ema'] = self.df['close'].ewm(span=6, adjust=False).mean()
def determine_signal(self, dframe):
ema9 = dframe['9ema']
ema21 = dframe['21ema']
histogram = dframe['macd_histogram']
close = dframe['close']
ema6 = dframe['6ema']
ema12 = dframe['12ema']
rsi = dframe['rsi']
action = 0
# SELL CRITERIA: 9EMA crosses below 21EMA followed by a MACD histogram crossover into negatives
if ((ema9.iloc[-2] < ema21.iloc[-2]) and (
(histogram.iloc[-1] < 0 and histogram.iloc[-2] > 0) or (histogram.iloc[-1] > 0 and histogram.iloc[-2] < 0))) \
or ((ema6.iloc[-1] < ema12.iloc[-1]) and (rsi.iloc[-1] < 50)):
action = -1
# BUY CRITERIA: 9EMA crosses above 21EMA followed by a MACD histogram crossover ito positives
if ((ema9.iloc[-2] > ema21.iloc[-2]) and (
(histogram.iloc[-1] > 0 and histogram.iloc[-2] < 0) or (histogram.iloc[-1] < 0 and histogram.iloc[-2] > 0))) \
or ((ema6.iloc[-1] > ema12.iloc[-1]) and (rsi.iloc[-1] > 50)):
action = 1
return action, close.iloc[-1]-ema9.iloc[-1]
def run_ema_macd_rsi(self):
self.calculate_21_ema()
self.calculate_9_ema()
self.add_macd_diff()
self.calculate_12_ema()
self.calculate_6_ema()
self.add_rsi()
return self.determine_signal(self.df), self.df
''' The following methods are for plotting '''
def find_all_signals(self, plot_df):
# assign initial value of hold
plot_df['signal'] = 0
start = -1 * len(plot_df)
end = start + 36
# loop through data to determine all signals
while end < 0:
curr_window = plot_df[start:end]
action = self.determine_signal(curr_window)[0]
plot_df.loc[plot_df.index[end - 1], 'signal'] = action
end += 1
start += 1
action = self.determine_signal(plot_df[-15:])[0]
plot_df.loc[plot_df.index[-1], 'signal'] = action
def plot_graph(self):
# create shallow copy for plotting so as to not accidentally impact original df
plot_df = self.df.copy(deep=False)
self.find_all_signals(plot_df)
plot_df = plot_df[120:220]
# preparing values for horizontal line at rsi values 80 and 20 for visual purposes
plot_df['50_line'] = 50
# initialise visualisation object for plotting
visualisation = v.Visualise(plot_df)
# determining one buy signal example for plotting
visualisation.determine_buy_marker()
# determining one sell signal example for plotting
visualisation.determine_sell_marker()
# add subplots of RSI, 80 line and 20 line
visualisation.add_subplot(plot_df['6ema'], panel=0, color='pink', width=0.75)
visualisation.add_subplot(plot_df['12ema'], panel=0, color='b', width=0.75)
visualisation.add_subplot(plot_df['rsi'], panel=1, color='m', width=0.75, ylabel='RSI')
visualisation.add_subplot(plot_df['50_line'], panel=1, color='k', secondary_y=False, width=0.75,
linestyle='solid')
visualisation.add_subplot(plot_df['macd_histogram'], panel=2, color='b', type='bar')
visualisation.add_subplot(plot_df['9ema'], panel=0, color='orange', width=0.75)
visualisation.add_subplot(plot_df['21ema'], panel=0, color='turquoise', width=0.75)
# create final plot with title
visualisation.plot_graph("EMA with MACD and RSI strategy")