-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsiTest.py
143 lines (121 loc) · 4.13 KB
/
rsiTest.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import yfinance as yf
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os
def compute_rsi_list(price_list, period):
size = len(price_list)
up_move = [np.nan] * size
down_move = [np.nan] * size
avg_up = [np.nan] * size
avg_down = [np.nan] * size
rs = [np.nan] * size
rsi = [np.nan] * size
# Calculate Up Move & Down Move
prev_price = price_list[0]
for i in range(1, size):
up_move[i]= 0
down_move[i] = 0
if price_list[i] > prev_price:
up_move[i] = price_list[i] - prev_price
if price_list[i] < prev_price:
down_move[i] = abs(price_list[i] - prev_price)
prev_price = price_list[i]
# Calculate initial Average Up & Down, RS and RSI
avg_up[period] = sum(up_move[1:period + 1]) / len(up_move[1:period + 1])
avg_down[period] = sum(down_move[1:period + 1]) / len(down_move[1:period + 1])
rs[period] = avg_up[period] / avg_down[period]
rsi[period] = 100 - (100 / (1 + rs[period]))
# Calculate rest of Average Up, Average Down, RS, RSI
prev_avg_up = avg_up[period]
prev_avg_down = avg_down[period]
for i in range(period + 1, size):
avg_up[i] = (prev_avg_up * (period - 1) + up_move[i]) / period
avg_down[i] = (prev_avg_down * (period - 1) + down_move[i]) / period
prev_avg_down = avg_down[i]
prev_avg_up = avg_up[i]
rs[i] = avg_up[i] / avg_down[i]
rsi[i] = 100 - (100 / (1 + rs[i]))
return rsi
def compute_value(price_list, rsi):
size = len(price_list)
buy_amount = 1
sell_amount = 1
money = 10000
n_shares = money / price_list[0]
money_list = []
prev_buy = False
prev_sell = False
buy_x = []
buy_y = []
sell_x = []
sell_y = []
for i in range(1,size):
if(rsi[i] < 30):
prev_buy = True
prev_sell = False
money -= price_list[i] * buy_amount
buy_x.append(price_list[i])
buy_y.append(i)
elif(rsi[i] > 70):
prev_sell = True
prev_buy = False
n_shares -= 1
money += price_list[i] * sell_amount
sell_x.append(price_list[i])
sell_y.append(i)
money_list.append(money)
return money_list #, buy_x, buy_y, sell_x, sell_y
def main():
stock_name = "TSLA"
stock = yf.Ticker(stock_name)
data=pd.read_csv("AAPL_stock_sample/AAPL_1hour_sample.txt", sep=",", header=None, names=["DateTime", "Open", "High", "Low", "Close", "Volume"])
close = data['Close'].to_list()
close = stock.history(period = '1y')['Close'].to_list()
plt.title("{} stock and RSI usage".format(stock_name))
for period in [5, 6, 7, 8, 10, 14]:
rsi = compute_rsi_list(close, period)
plt.subplot(3, 1, 2)
plt.xlabel("t")
plt.ylabel("RSI")
plt.plot(rsi, label = period)
money_list = compute_value(close, rsi)
plt.subplot(3, 1, 3)
plt.xlabel("t")
plt.ylabel("cash")
plt.plot(money_list, label = period)
plt.legend()
plt.subplot(3, 1, 2)
plt.axhline(y=30, color='red', linestyle='--')
plt.axhline(y=70, color='green', linestyle='--')
plt.legend()
plt.subplot(3, 1, 1)
plt.xlabel("t")
plt.ylabel("stock price")
plt.plot(close)
plt.show()
# plt.title("{} stock and RSI usage".format(stock_name))
# rsi = compute_rsi_list(close, 7)
# plt.subplot(3, 1, 2)
# plt.xlabel("t")
# plt.ylabel("RSI")
# plt.plot(rsi, label = 7)
# money_list, buy_x, buy_y, sell_x, sell_y = compute_value(close, rsi)
# plt.subplot(3, 1, 3)
# plt.xlabel("t")
# plt.ylabel("cash")
# plt.plot(money_list, label = 7)
# plt.legend()
# plt.subplot(3, 1, 2)
# plt.axhline(y=30, color='green', linestyle='--')
# plt.axhline(y=70, color='red', linestyle='--')
# plt.legend()
# plt.subplot(3, 1, 1)
# plt.scatter(buy_y, buy_x, color="green")
# plt.scatter(sell_y, sell_x, color="red")
# plt.xlabel("t")
# plt.ylabel("stock price")
# plt.plot(close)
# plt.show()
if __name__=="__main__":
main()