RSI 코드 구글에서 검색해서 코드 복붙했는데
값이 안 맞더라...
결국 외국꺼 찾아다가 맞게 계산한거 긁어옴
키움 RSI 랑 오차 거의 안남
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QAxContainer import *
import pyqtgraph as pg
import yfinance as yf
def computeRSI (data, time_window):
diff = data.diff(1).dropna() # diff in one field(one day)
#this preservers dimensions off diff values
up_chg = 0 * diff
down_chg = 0 * diff
# up change is equal to the positive difference, otherwise equal to zero
up_chg[diff > 0] = diff[ diff>0 ]
# down change is equal to negative deifference, otherwise equal to zero
down_chg[diff < 0] = diff[ diff < 0 ]
# check pandas documentation for ewm
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.ewm.html
# values are related to exponential decay
# we set com=time_window-1 so we get decay alpha=1/time_window
up_chg_avg = up_chg.ewm(com=time_window-1 , min_periods=time_window).mean()
down_chg_avg = down_chg.ewm(com=time_window-1 , min_periods=time_window).mean()
rs = abs(up_chg_avg/down_chg_avg)
rsi = 100 - 100/(1+rs)
return rsi
class MainWidget(QWidget):
def __init__(self):
super().__init__()
self.df = yf.download('TQQQ', start='2020-04-01', end='2022-03-18')
self.df['RSI'] = computeRSI(self.df['Close'], 14)
self.setLayout(mainLayout)
self.setGeometry(0, 0, 1000, 800)
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("한국투자증권-API")
mainWidget = MainWidget()
self.setCentralWidget(mainWidget)
self.setGeometry(50, 50, 1000, 800)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()
'미국 주식 프로그램 만들기(eFriend expert)' 카테고리의 다른 글
5. 주식 이동평균선 구하기 (0) | 2022.03.20 |
---|---|
4. iex cloud api 연동 (0) | 2022.03.19 |
2. 한국투자증권 API 설명 (0) | 2022.03.16 |
1. 미국주식자동매매 프로그램 매수매도 프로그램 (0) | 2022.03.16 |
0. 미국 주식 자동 매매 프로그램 만들기 준비물 (0) | 2022.03.16 |