import yfinance as yf
import time
import pandas as pd
import matplotlib.pyplot as plt
from fbprophet import Prophet
from datetime import datetime,timedelta
import telegram
telgm_token = '텔레그램 토큰'
bot = telegram.Bot(token = telgm_token)
#오늘 날짜 -365일 부터 데이터 가져오기
starttime = datetime.now() - timedelta(days=365)
endtime =datetime.now()
#주식 코드 input문 이용 하기
stockcode = input('한국주식 코드를 입력해주세요')
stockinfo = yf.download(stockcode+'.KS',start = starttime, end=endtime)
print(stockinfo)
#현재 가격 불러오기
stock_now_price = stockinfo['Close'][-1]
stockinfo['y'] = stockinfo['Close']
stockinfo['ds'] = stockinfo.index
#x,y 설정
stockinfo = stockinfo[['ds','y']]
# PROPHET
df_pb = Prophet(
daily_seasonality=True,
changepoint_prior_scale=0.15,
)
#df_pb.add_country_holidays(country_name='KR')
df_pb.fit(stockinfo)
#20일 미래 예측
periodsdate= 20
df_future = df_pb.make_future_dataframe(periods=periodsdate, freq='D',include_history = True)
# ##모델 확인
print(df_future.tail(10))
data_forecast = df_pb.predict(df_future)
# # #예측 결과 확인
print(data_forecast[['ds','yhat','yhat_lower','yhat_upper']].tail(20))
# Plot the forecast with the actuals
#'yhat' 예측 가격 데이터 변수저장
closeDf_ = data_forecast[data_forecast['ds'] == data_forecast.iloc[-1]['ds']]
predictprice = closeDf_['yhat'].values[0]
f, ax = plt.subplots(1)
f.set_figheight(5)
f.set_figwidth(15)
ax.scatter(stockinfo.ds, stockinfo['y'], color='r')
fig = df_pb.plot(data_forecast, ax=ax)
plt.title('Forecast AND Actuals')
plt.xlabel('DATE')
plt.ylabel('Price')
#소수점 없애기
int_nowprice = int(stock_now_price)
int_price = int(predictprice)
print("현재 가격 :" ,str(int_nowprice) + '원 입니다')
print('20일 뒤 삼성SDI 주가 예측: '+str(int_price) + '원 입니다')
plt.savefig('./predict.png')
filepath = "./predict.png"
bot.send_photo(chat_id = '@텔레그램채널명', photo=open(filepath, 'rb'))
time.sleep(1)
bot.sendMessage(chat_id = '@텔레그램채널명', text="삼성SDI 주가예측:" +str(int_price))
plt.show()
다음 시간은 리눅스 Crontab을 이용해서 텔레그램 봇을 연동후, 일정 주기마다 예측한 결과 전송
'프로젝트' 카테고리의 다른 글
Python - Prophet 주식 예측- 3 하이퍼 파라미터 튜닝 (0) | 2022.10.22 |
---|---|
Python - Prophet 주가예측 -2 그래프 검증 (0) | 2022.10.21 |
Python - fbprophet을 이용한 한국주식 예측 하기 (0) | 2022.10.04 |