Commit ·
62effec
1
Parent(s): af42d4f
primeiro commit
Browse files- Bitcoin_historical_data.ipynb +0 -0
- app.py +113 -0
- archive/dowload_csv.txt +1 -0
- controller.py +88 -0
- models/LSTM_bitcoin_predict.pkl +0 -0
- models/close_model.pkl +0 -0
- models/high_model.pkl +0 -0
- models/low_model.pkl +0 -0
- requirements.txt +121 -0
Bitcoin_historical_data.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import plotly.express as px
|
| 6 |
+
import plotly.graph_objects as go
|
| 7 |
+
import numpy as np
|
| 8 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 9 |
+
|
| 10 |
+
from controller import predict_bitcoin_prices, today_open_price, predict_next_thirty_days, get_formated_data
|
| 11 |
+
|
| 12 |
+
today_price = today_open_price()
|
| 13 |
+
df = predict_bitcoin_prices(today_price)
|
| 14 |
+
df = df.applymap(lambda x: f"{x:.2f}" if isinstance(x, (float, np.float64)) else x)
|
| 15 |
+
|
| 16 |
+
# Estilo CSS para aumentar a fonte
|
| 17 |
+
st.markdown(
|
| 18 |
+
"""
|
| 19 |
+
<style>
|
| 20 |
+
.dataframe tbody tr td {
|
| 21 |
+
font-size: 16px !important;
|
| 22 |
+
}
|
| 23 |
+
</style>
|
| 24 |
+
""",
|
| 25 |
+
unsafe_allow_html=True,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Exibindo tabela
|
| 29 |
+
st.title("Previsão do Bitcoin para Hoje")
|
| 30 |
+
st.dataframe(df)
|
| 31 |
+
|
| 32 |
+
#=================================================================
|
| 33 |
+
st.subheader("Preveja com um valor de abertura especifico")
|
| 34 |
+
|
| 35 |
+
today_price = st.number_input("Digite o valor de abertura do BTC:", min_value=1, step=1)
|
| 36 |
+
today_price = [[today_price]]
|
| 37 |
+
|
| 38 |
+
df = predict_bitcoin_prices(today_price)
|
| 39 |
+
df = df.applymap(lambda x: f"{x:.2f}" if isinstance(x, (float, np.float64)) else x)
|
| 40 |
+
df = df.rename(columns={"Abertura(Hoje)": "Abertura"})
|
| 41 |
+
|
| 42 |
+
st.markdown(
|
| 43 |
+
"""
|
| 44 |
+
<style>
|
| 45 |
+
.dataframe tbody tr td {
|
| 46 |
+
font-size: 16px !important;
|
| 47 |
+
}
|
| 48 |
+
</style>
|
| 49 |
+
""",
|
| 50 |
+
unsafe_allow_html=True,
|
| 51 |
+
)
|
| 52 |
+
st.dataframe(df)
|
| 53 |
+
|
| 54 |
+
#=================================================================
|
| 55 |
+
# Simulando os dados (substitua por seus dados reais)
|
| 56 |
+
|
| 57 |
+
#OBS: Caso queira utilizar os dados kaggle, descomente abaxio e comente "df_BTC_daily = pd.read_csv("archive/yfinance_BTC.csv")"
|
| 58 |
+
|
| 59 |
+
#df_BTC_daily = pd.read_csv("archive/BTC-Daily.csv")
|
| 60 |
+
df_BTC_daily = pd.read_csv("archive/yfinance_BTC.csv")
|
| 61 |
+
|
| 62 |
+
scaler = MinMaxScaler(feature_range=(0, 1))
|
| 63 |
+
closed_date_df = get_formated_data(df_BTC_daily, scaler)
|
| 64 |
+
lst_output = predict_next_thirty_days(df_BTC_daily, scaler)
|
| 65 |
+
|
| 66 |
+
# Processando os dados
|
| 67 |
+
lstmdf = closed_date_df.tolist()
|
| 68 |
+
lstmdf.extend((np.array(lst_output).reshape(-1, 1)).tolist())
|
| 69 |
+
lstmdf = scaler.inverse_transform(lstmdf).reshape(1, -1).tolist()[0]
|
| 70 |
+
|
| 71 |
+
historical_data = lstmdf[:-30]
|
| 72 |
+
forecast_data = lstmdf[-30:]
|
| 73 |
+
|
| 74 |
+
# Criando o gráfico com Plotly
|
| 75 |
+
fig = go.Figure()
|
| 76 |
+
|
| 77 |
+
# Dados históricos
|
| 78 |
+
fig.add_trace(go.Scatter(
|
| 79 |
+
x=list(range(len(historical_data))),
|
| 80 |
+
y=historical_data,
|
| 81 |
+
mode='lines',
|
| 82 |
+
name='Dados Históricos',
|
| 83 |
+
line=dict(color='blue')
|
| 84 |
+
))
|
| 85 |
+
|
| 86 |
+
# Previsão
|
| 87 |
+
fig.add_trace(go.Scatter(
|
| 88 |
+
x=list(range(len(historical_data), len(lstmdf))),
|
| 89 |
+
y=forecast_data,
|
| 90 |
+
mode='lines',
|
| 91 |
+
name='Previsão (30 dias)',
|
| 92 |
+
line=dict(color='orange')
|
| 93 |
+
))
|
| 94 |
+
|
| 95 |
+
# Atualizando layout
|
| 96 |
+
fig.update_layout(
|
| 97 |
+
title_text="Gráfico com a Previsão",
|
| 98 |
+
plot_bgcolor="white",
|
| 99 |
+
font=dict(size=15, color="black"),
|
| 100 |
+
legend_title_text="Legenda",
|
| 101 |
+
xaxis=dict(showgrid=False, title="Timestamp"),
|
| 102 |
+
yaxis=dict(showgrid=False, title="Stock"),
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
st.title("Gráfico com Dados Históricos e Previsão Para os Proximos 30 Dias")
|
| 106 |
+
st.plotly_chart(fig)
|
| 107 |
+
|
| 108 |
+
#========================================================================
|
| 109 |
+
prediction_df = pd.DataFrame({
|
| 110 |
+
'Predicted Close Price': forecast_data
|
| 111 |
+
})
|
| 112 |
+
st.table(prediction_df)
|
| 113 |
+
|
archive/dowload_csv.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
link dos arquivos: https://www.kaggle.com/datasets/prasoonkottarathil/btcinusd/data
|
controller.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import yfinance as yf
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 7 |
+
|
| 8 |
+
def predict_bitcoin_prices(open_price):
|
| 9 |
+
high_model = joblib.load("models/high_model.pkl")
|
| 10 |
+
low_model = joblib.load("models/low_model.pkl")
|
| 11 |
+
close_model = joblib.load("models/close_model.pkl")
|
| 12 |
+
|
| 13 |
+
high_pred = high_model.predict(open_price)
|
| 14 |
+
low_pred = low_model.predict(open_price)
|
| 15 |
+
close_pred = close_model.predict(open_price)
|
| 16 |
+
|
| 17 |
+
data = {
|
| 18 |
+
"Abertura(Hoje)": [open_price],
|
| 19 |
+
"Maior Valor": [high_pred[0]],
|
| 20 |
+
"Menor Valor": [low_pred[0]],
|
| 21 |
+
"Fechamento": [close_pred[0]],
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
return pd.DataFrame(data)
|
| 25 |
+
|
| 26 |
+
def today_open_price():
|
| 27 |
+
bitcoin = yf.Ticker("BTC-USD")
|
| 28 |
+
historical_data = bitcoin.history(period="1d")
|
| 29 |
+
|
| 30 |
+
price_open = historical_data['Open'].iloc[0] if not historical_data.empty else None
|
| 31 |
+
return [[price_open]]
|
| 32 |
+
|
| 33 |
+
def predict_next_thirty_days(df, scaler):
|
| 34 |
+
time_stpe = 15
|
| 35 |
+
model = joblib.load("models/LSTM_bitcoin_predict.pkl")
|
| 36 |
+
test_data = get_data(df, scaler)
|
| 37 |
+
|
| 38 |
+
# Pegando os últimos `time_step` dados para o input inicial
|
| 39 |
+
x_input = test_data[len(test_data) - time_stpe:].reshape(1, -1)
|
| 40 |
+
|
| 41 |
+
#converte para lista para podermos adicinar novos valores a medida que o loop avança
|
| 42 |
+
temp_imput = list(x_input)
|
| 43 |
+
temp_imput = temp_imput[0].tolist()
|
| 44 |
+
|
| 45 |
+
#Aramazena os valores preveistos
|
| 46 |
+
lst_output = []
|
| 47 |
+
n_steps = time_stpe
|
| 48 |
+
i = 0
|
| 49 |
+
pred_days = 30
|
| 50 |
+
|
| 51 |
+
while(i < pred_days):
|
| 52 |
+
if(len(temp_imput) > time_stpe):
|
| 53 |
+
x_input = np.array(temp_imput[1:])
|
| 54 |
+
x_input = x_input.reshape(1, -1)
|
| 55 |
+
x_input = x_input.reshape((1, n_steps, 1))
|
| 56 |
+
|
| 57 |
+
yhat = model.predict(x_input, verbose=0)
|
| 58 |
+
|
| 59 |
+
temp_imput.extend(yhat[0].tolist())
|
| 60 |
+
temp_imput = temp_imput[1:]
|
| 61 |
+
|
| 62 |
+
lst_output.extend(yhat.tolist())
|
| 63 |
+
i=i+1
|
| 64 |
+
else:
|
| 65 |
+
x_input = x_input.reshape((1, n_steps, 1))
|
| 66 |
+
yhat = model.predict(x_input, verbose=0)
|
| 67 |
+
temp_imput.extend(yhat[0].tolist())
|
| 68 |
+
|
| 69 |
+
lst_output.extend(yhat.tolist())
|
| 70 |
+
i=i+1
|
| 71 |
+
|
| 72 |
+
return lst_output
|
| 73 |
+
|
| 74 |
+
def get_data(df, scaler):
|
| 75 |
+
df = get_formated_data(df, scaler)
|
| 76 |
+
# Seleciona apenas os dados de teste (40% finais dos dados)
|
| 77 |
+
test_data = df[int(len(df) * 0.60):]
|
| 78 |
+
return test_data
|
| 79 |
+
|
| 80 |
+
def get_formated_data(df, scaler):
|
| 81 |
+
# Filtra colunas e datas
|
| 82 |
+
df = df[["date", "close"]]
|
| 83 |
+
df = df[df["date"] > "2020-12-31"]
|
| 84 |
+
df.drop(columns=["date"], inplace=True)
|
| 85 |
+
|
| 86 |
+
# Normaliza os dados
|
| 87 |
+
df = scaler.fit_transform(np.array(df).reshape(-1, 1))
|
| 88 |
+
return df
|
models/LSTM_bitcoin_predict.pkl
ADDED
|
Binary file (28.2 kB). View file
|
|
|
models/close_model.pkl
ADDED
|
Binary file (544 Bytes). View file
|
|
|
models/high_model.pkl
ADDED
|
Binary file (544 Bytes). View file
|
|
|
models/low_model.pkl
ADDED
|
Binary file (544 Bytes). View file
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
absl-py==2.1.0
|
| 2 |
+
altair==5.4.1
|
| 3 |
+
asttokens==2.4.1
|
| 4 |
+
astunparse==1.6.3
|
| 5 |
+
attrs==24.2.0
|
| 6 |
+
backcall==0.2.0
|
| 7 |
+
beautifulsoup4==4.12.3
|
| 8 |
+
blinker==1.8.2
|
| 9 |
+
cachetools==5.5.0
|
| 10 |
+
certifi==2024.8.30
|
| 11 |
+
charset-normalizer==3.4.0
|
| 12 |
+
click==8.1.7
|
| 13 |
+
comm==0.2.2
|
| 14 |
+
contourpy==1.1.1
|
| 15 |
+
cycler==0.12.1
|
| 16 |
+
debugpy==1.8.7
|
| 17 |
+
decorator==5.1.1
|
| 18 |
+
executing==2.1.0
|
| 19 |
+
fastjsonschema==2.20.0
|
| 20 |
+
flatbuffers==24.3.25
|
| 21 |
+
fonttools==4.54.1
|
| 22 |
+
frozendict==2.4.6
|
| 23 |
+
gast==0.4.0
|
| 24 |
+
gitdb==4.0.11
|
| 25 |
+
GitPython==3.1.43
|
| 26 |
+
google-auth==2.35.0
|
| 27 |
+
google-auth-oauthlib==1.0.0
|
| 28 |
+
google-pasta==0.2.0
|
| 29 |
+
grpcio==1.67.1
|
| 30 |
+
h5py==3.11.0
|
| 31 |
+
html5lib==1.1
|
| 32 |
+
idna==3.10
|
| 33 |
+
importlib-metadata==8.5.0
|
| 34 |
+
importlib-resources==6.4.5
|
| 35 |
+
ipykernel==6.29.5
|
| 36 |
+
ipython==8.12.3
|
| 37 |
+
ipython-genutils==0.2.0
|
| 38 |
+
jedi==0.19.1
|
| 39 |
+
jinja2==3.1.4
|
| 40 |
+
joblib==1.4.2
|
| 41 |
+
jsonschema==4.23.0
|
| 42 |
+
jsonschema-specifications==2023.12.1
|
| 43 |
+
jupyter-client==8.6.3
|
| 44 |
+
jupyter-core==5.7.2
|
| 45 |
+
keras==2.13.1
|
| 46 |
+
kiwisolver==1.4.7
|
| 47 |
+
libclang==18.1.1
|
| 48 |
+
lxml==5.3.0
|
| 49 |
+
Markdown==3.7
|
| 50 |
+
markdown-it-py==3.0.0
|
| 51 |
+
MarkupSafe==2.1.5
|
| 52 |
+
matplotlib==3.7.5
|
| 53 |
+
matplotlib-inline==0.1.7
|
| 54 |
+
mdurl==0.1.2
|
| 55 |
+
multitasking==0.0.11
|
| 56 |
+
narwhals==1.13.3
|
| 57 |
+
nbformat==4.2.0
|
| 58 |
+
nest-asyncio==1.6.0
|
| 59 |
+
numpy==1.24.3
|
| 60 |
+
oauthlib==3.2.2
|
| 61 |
+
opt-einsum==3.4.0
|
| 62 |
+
packaging==24.1
|
| 63 |
+
pandas==2.0.3
|
| 64 |
+
parso==0.8.4
|
| 65 |
+
peewee==3.17.7
|
| 66 |
+
pexpect==4.9.0
|
| 67 |
+
pickleshare==0.7.5
|
| 68 |
+
pillow==10.4.0
|
| 69 |
+
pkgutil-resolve-name==1.3.10
|
| 70 |
+
platformdirs==4.3.6
|
| 71 |
+
plotly==5.24.1
|
| 72 |
+
prompt-toolkit==3.0.48
|
| 73 |
+
protobuf==4.25.5
|
| 74 |
+
psutil==6.0.0
|
| 75 |
+
ptyprocess==0.7.0
|
| 76 |
+
pure-eval==0.2.3
|
| 77 |
+
pyarrow==17.0.0
|
| 78 |
+
pyasn1==0.6.1
|
| 79 |
+
pyasn1-modules==0.4.1
|
| 80 |
+
pydeck==0.9.1
|
| 81 |
+
pygments==2.18.0
|
| 82 |
+
pyparsing==3.1.4
|
| 83 |
+
python-dateutil==2.9.0.post0
|
| 84 |
+
pytz==2024.2
|
| 85 |
+
pyzmq==26.2.0
|
| 86 |
+
referencing==0.35.1
|
| 87 |
+
requests==2.32.3
|
| 88 |
+
requests-oauthlib==2.0.0
|
| 89 |
+
rich==13.9.4
|
| 90 |
+
rpds-py==0.20.1
|
| 91 |
+
rsa==4.9
|
| 92 |
+
scikit-learn==1.3.2
|
| 93 |
+
scipy==1.10.1
|
| 94 |
+
seaborn==0.13.2
|
| 95 |
+
six==1.16.0
|
| 96 |
+
sklearn==0.0
|
| 97 |
+
smmap==5.0.1
|
| 98 |
+
soupsieve==2.6
|
| 99 |
+
stack-data==0.6.3
|
| 100 |
+
streamlit==1.40.0
|
| 101 |
+
tenacity==9.0.0
|
| 102 |
+
tensorboard==2.13.0
|
| 103 |
+
tensorboard-data-server==0.7.2
|
| 104 |
+
tensorflow==2.13.1
|
| 105 |
+
tensorflow-estimator==2.13.0
|
| 106 |
+
tensorflow-io-gcs-filesystem==0.34.0
|
| 107 |
+
termcolor==2.4.0
|
| 108 |
+
threadpoolctl==3.5.0
|
| 109 |
+
toml==0.10.2
|
| 110 |
+
tornado==6.4.1
|
| 111 |
+
traitlets==5.14.3
|
| 112 |
+
typing-extensions==4.12.2
|
| 113 |
+
tzdata==2024.2
|
| 114 |
+
urllib3==2.2.3
|
| 115 |
+
watchdog==4.0.2
|
| 116 |
+
wcwidth==0.2.13
|
| 117 |
+
webencodings==0.5.1
|
| 118 |
+
werkzeug==3.0.6
|
| 119 |
+
wrapt==1.16.0
|
| 120 |
+
yfinance==0.2.48
|
| 121 |
+
zipp==3.20.2
|