!pip install pyupbit
import pyupbit
#BTC 최근 200시간의 데이터 불러옴
df = pyupbit.get_ohlcv("KRW-BTC", interval="minute60")
df
open | high | low | close | volume | value | |
---|---|---|---|---|---|---|
2021-05-08 20:00:00 | 70080000.0 | 70397000.0 | 70031000.0 | 70278000.0 | 282.481680 | 1.983373e+10 |
2021-05-08 21:00:00 | 70202000.0 | 70600000.0 | 69775000.0 | 70353000.0 | 512.864414 | 3.595321e+10 |
2021-05-08 22:00:00 | 70353000.0 | 71400000.0 | 70261000.0 | 70402000.0 | 1074.924200 | 7.618756e+10 |
2021-05-08 23:00:00 | 70467000.0 | 70586000.0 | 70101000.0 | 70264000.0 | 495.536391 | 3.485509e+10 |
2021-05-09 00:00:00 | 70245000.0 | 70732000.0 | 69377000.0 | 69561000.0 | 674.450592 | 4.724700e+10 |
... | ... | ... | ... | ... | ... | ... |
2021-05-16 23:00:00 | 59941000.0 | 60155000.0 | 59293000.0 | 59355000.0 | 417.846935 | 2.493730e+10 |
2021-05-17 00:00:00 | 59370000.0 | 59440000.0 | 58800000.0 | 58810000.0 | 417.550735 | 2.465069e+10 |
2021-05-17 01:00:00 | 58811000.0 | 59000000.0 | 58327000.0 | 58480000.0 | 494.023674 | 2.897628e+10 |
2021-05-17 02:00:00 | 58513000.0 | 58638000.0 | 57900000.0 | 57996000.0 | 675.896966 | 3.928135e+10 |
2021-05-17 03:00:00 | 57997000.0 | 58278000.0 | 57501000.0 | 57550000.0 | 231.009586 | 1.336449e+10 |
200 rows × 6 columns
#시간(ds)와 종가(y)값만 남김
df = df.reset_index()
df['ds'] = df['index']
df['y'] = df['close']
data = df[['ds','y']]
data
ds | y | |
---|---|---|
0 | 2021-05-08 20:00:00 | 70278000.0 |
1 | 2021-05-08 21:00:00 | 70353000.0 |
2 | 2021-05-08 22:00:00 | 70402000.0 |
3 | 2021-05-08 23:00:00 | 70264000.0 |
4 | 2021-05-09 00:00:00 | 69561000.0 |
... | ... | ... |
195 | 2021-05-16 23:00:00 | 59355000.0 |
196 | 2021-05-17 00:00:00 | 58810000.0 |
197 | 2021-05-17 01:00:00 | 58480000.0 |
198 | 2021-05-17 02:00:00 | 57996000.0 |
199 | 2021-05-17 03:00:00 | 57550000.0 |
200 rows × 2 columns
#prophet 불러옴
from fbprophet import Prophet
#학습
model = Prophet()
model.fit(data)
INFO:fbprophet:Disabling yearly seasonality. Run prophet with yearly_seasonality=True to override this. INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
<fbprophet.forecaster.Prophet at 0x7fc15ca2ee90>
#24시간 미래 예측
future = model.make_future_dataframe(periods=24, freq='H')
forecast = model.predict(future)
#그래프1
fig1 = model.plot(forecast)
#그래프2
fig2 = model.plot_components(forecast)
#매수 시점의 가격
nowValue = pyupbit.get_current_price("KRW-BTC")
nowValue
57098000.0
#종가의 가격을 구함
#현재 시간이 자정 이전
closeDf = forecast[forecast['ds'] == forecast.iloc[-1]['ds'].replace(hour=9)]
#현재 시간이 자정 이후
if len(closeDf) == 0:
closeDf = forecast[forecast['ds'] == data.iloc[-1]['ds'].replace(hour=9)]
#어쨋든 당일 종가
closeValue = closeDf['yhat'].values[0]
closeValue
58097882.673339084
#구체적인 가격
print("현재 시점 가격: ", nowValue)
print("종가의 가격: ", closeValue)
현재 시점 가격: 57098000.0 종가의 가격: 58097882.673339084
forecast
ds | trend | yhat_lower | yhat_upper | trend_lower | trend_upper | additive_terms | additive_terms_lower | additive_terms_upper | daily | daily_lower | daily_upper | multiplicative_terms | multiplicative_terms_lower | multiplicative_terms_upper | yhat | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2021-05-08 20:00:00 | 7.049319e+07 | 6.978264e+07 | 7.145666e+07 | 7.049319e+07 | 7.049319e+07 | 73763.938017 | 73763.938017 | 73763.938017 | 73763.938017 | 73763.938017 | 73763.938017 | 0.0 | 0.0 | 0.0 | 7.056695e+07 |
1 | 2021-05-08 21:00:00 | 7.054058e+07 | 6.981790e+07 | 7.151720e+07 | 7.054058e+07 | 7.054058e+07 | 155154.366642 | 155154.366642 | 155154.366642 | 155154.366642 | 155154.366642 | 155154.366642 | 0.0 | 0.0 | 0.0 | 7.069573e+07 |
2 | 2021-05-08 22:00:00 | 7.058797e+07 | 6.993552e+07 | 7.159752e+07 | 7.058797e+07 | 7.058797e+07 | 200297.460741 | 200297.460741 | 200297.460741 | 200297.460741 | 200297.460741 | 200297.460741 | 0.0 | 0.0 | 0.0 | 7.078827e+07 |
3 | 2021-05-08 23:00:00 | 7.063536e+07 | 6.995876e+07 | 7.160555e+07 | 7.063536e+07 | 7.063536e+07 | 127033.847973 | 127033.847973 | 127033.847973 | 127033.847973 | 127033.847973 | 127033.847973 | 0.0 | 0.0 | 0.0 | 7.076239e+07 |
4 | 2021-05-09 00:00:00 | 7.068275e+07 | 6.977696e+07 | 7.145736e+07 | 7.068275e+07 | 7.068275e+07 | -64004.026527 | -64004.026527 | -64004.026527 | -64004.026527 | -64004.026527 | -64004.026527 | 0.0 | 0.0 | 0.0 | 7.061875e+07 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
219 | 2021-05-17 23:00:00 | 5.741650e+07 | 5.620334e+07 | 5.893053e+07 | 5.640791e+07 | 5.859450e+07 | 127033.847977 | 127033.847977 | 127033.847977 | 127033.847977 | 127033.847977 | 127033.847977 | 0.0 | 0.0 | 0.0 | 5.754353e+07 |
220 | 2021-05-18 00:00:00 | 5.735429e+07 | 5.587900e+07 | 5.883842e+07 | 5.624516e+07 | 5.862328e+07 | -64004.026533 | -64004.026533 | -64004.026533 | -64004.026533 | -64004.026533 | -64004.026533 | 0.0 | 0.0 | 0.0 | 5.729029e+07 |
221 | 2021-05-18 01:00:00 | 5.729208e+07 | 5.553360e+07 | 5.871707e+07 | 5.610166e+07 | 5.864511e+07 | -264151.046571 | -264151.046571 | -264151.046571 | -264151.046571 | -264151.046571 | -264151.046571 | 0.0 | 0.0 | 0.0 | 5.702793e+07 |
222 | 2021-05-18 02:00:00 | 5.722988e+07 | 5.527597e+07 | 5.845177e+07 | 5.594232e+07 | 5.868056e+07 | -331244.053737 | -331244.053737 | -331244.053737 | -331244.053737 | -331244.053737 | -331244.053737 | 0.0 | 0.0 | 0.0 | 5.689863e+07 |
223 | 2021-05-18 03:00:00 | 5.716767e+07 | 5.526153e+07 | 5.871909e+07 | 5.577408e+07 | 5.873862e+07 | -207139.503841 | -207139.503841 | -207139.503841 | -207139.503841 | -207139.503841 | -207139.503841 | 0.0 | 0.0 | 0.0 | 5.696053e+07 |
224 rows × 16 columns