Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
from sklearn.model_selection import train_test_split
|
5 |
+
from sklearn.linear_model import LinearRegression
|
6 |
+
import streamlit as st
|
7 |
+
from datetime import date
|
8 |
+
import yfinance as yf
|
9 |
+
from plotly import graph_objs as go
|
10 |
+
|
11 |
+
START = "2019-01-01"
|
12 |
+
TODAY = date.today().strftime("%Y-%m-%d")
|
13 |
+
|
14 |
+
st.title("Cryptocurrency Price Forecaster")
|
15 |
+
html_temp = """
|
16 |
+
<marquee behavior="scroll" direction="left">ALL INVESTMENTS ARE SUBJECT TO PRICE FLUCTUATIONS AND OTHER MARKET RISKS..... </marquee>
|
17 |
+
"""
|
18 |
+
st.markdown(html_temp,unsafe_allow_html=True)
|
19 |
+
|
20 |
+
currency=("ETH-USD","BTC-USD","BNB-USD","MATIC-USD","TRX-USD","DOGE-USD","SOL-USD","ATOM-USD")
|
21 |
+
selected_currency=st.selectbox("select coin",currency)
|
22 |
+
|
23 |
+
n_days = st.slider("Days of prediction",1,30)
|
24 |
+
|
25 |
+
# period =n_years*365
|
26 |
+
|
27 |
+
@st.cache_data(allow_output_mutation=True)
|
28 |
+
|
29 |
+
# @st.cache
|
30 |
+
def load_data(ticker):
|
31 |
+
data=yf.download(ticker, START, TODAY)
|
32 |
+
data.reset_index(inplace=True)
|
33 |
+
return data
|
34 |
+
|
35 |
+
data_load_state=st.text("load data...")
|
36 |
+
data=load_data(selected_currency)
|
37 |
+
data_load_state.text("loading data....done")
|
38 |
+
|
39 |
+
# st.subheader('Raw data')
|
40 |
+
# st.write(data.head(7))
|
41 |
+
# st.write(data.tail(7))
|
42 |
+
|
43 |
+
def plot_raw_data():
|
44 |
+
fig=go.Figure()
|
45 |
+
fig.add_trace(go.Scatter(x=data['Date'], y=data['Open'], name='open'))
|
46 |
+
fig.add_trace(go.Scatter(x=data['Date'], y=data['Close'], name='close'))
|
47 |
+
fig.layout.update(title_text="Time Series Graph", xaxis_rangeslider_visible=True)
|
48 |
+
st.plotly_chart(fig)
|
49 |
+
|
50 |
+
# plot_raw_data()
|
51 |
+
|
52 |
+
def get_data(ticker):
|
53 |
+
data=load_data(ticker)
|
54 |
+
data[str(n_days)+'_Day_Price_Forecast'] = data[['Close']].shift(-n_days)
|
55 |
+
X= np.array(data[['Close']])
|
56 |
+
X= X[:data.shape[0]-n_days]
|
57 |
+
y= np.array(data[str(n_days)+'_Day_Price_Forecast'])
|
58 |
+
y= y[:-n_days]
|
59 |
+
return X,y
|
60 |
+
|
61 |
+
X, y= get_data(selected_currency)
|
62 |
+
|
63 |
+
#linear regression
|
64 |
+
def result(X,y):
|
65 |
+
X_train, X_test, y_train,y_test = train_test_split(X,y,test_size=0.2)
|
66 |
+
linReg = LinearRegression()
|
67 |
+
linReg.fit(X_train,y_train)
|
68 |
+
x_projection = np.array(data[['Close']])[-n_days:]
|
69 |
+
linReg_prediction = linReg.predict(x_projection)
|
70 |
+
lr_acc = linReg.score(X_test,y_test)#r^2 test
|
71 |
+
return x_projection,linReg_prediction,lr_acc
|
72 |
+
n, m, p= result(X,y)
|
73 |
+
|
74 |
+
# st.write(n)
|
75 |
+
# st.subheader('Predicted prices ')
|
76 |
+
# st.write(m)
|
77 |
+
|
78 |
+
r_list=list(range(1,n_days))
|
79 |
+
def plot_result_data():
|
80 |
+
fig=go.Figure()
|
81 |
+
fig.add_trace(go.Scatter(x=r_list, y=m, name='predicted'))
|
82 |
+
fig.layout.update(title_text="Time Series Data", xaxis_rangeslider_visible=True)
|
83 |
+
st.plotly_chart(fig)
|
84 |
+
# plot_result_data()
|
85 |
+
if st.button("View Past data"):
|
86 |
+
|
87 |
+
#st.subheader('Raw data')
|
88 |
+
st.write(data.head(7))
|
89 |
+
st.write(data.tail(7))
|
90 |
+
plot_raw_data()
|
91 |
+
|
92 |
+
if st.button("Predict future Prices"):
|
93 |
+
st.subheader('Predicted prices ')
|
94 |
+
st.write(m)
|
95 |
+
plot_result_data()
|
96 |
+
if st.button("Accuracy check"):
|
97 |
+
st.write(p*100)
|
98 |
+
if st.button('INR CONVERTER'):
|
99 |
+
st.write(m*82.56)
|
100 |
+
st.write(f'''
|
101 |
+
<a target="_blank" href="https://www.coinbase.com/learn/crypto-basics">
|
102 |
+
<button style = "background-color:#16767B; border-radius:7px;">
|
103 |
+
LEARN MORE
|
104 |
+
</button>
|
105 |
+
</a>
|
106 |
+
''',
|
107 |
+
unsafe_allow_html=True
|
108 |
+
)
|
109 |
+
|