File size: 6,741 Bytes
2e82b81 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pandas_datareader import data
from keras.models import load_model
import streamlit as st
import plotly.graph_objects as go
import datetime as dt
import yfinance as yf
import pandas_ta as ta
from plotly.subplots import make_subplots
from datetime import timedelta
from sklearn.preprocessing import MinMaxScaler
st.set_page_config(page_title='CryptoPredict 2.0', page_icon=':chart_with_upwards_trend:')
st.title('CryptoCurrency Price Prediction')
stocks = [ 'BTC-USD', 'ETH-USD', 'BNB-USD', 'SOL-USD', 'ADA-USD', 'XRP-USD', 'DOT-USD', 'DOGE-USD',
'AVAX-USD', 'LTC-USD', 'MATIC-USD', 'SHIB-USD']
st.markdown('#')
with st.expander(""):
col1, col2, col3 = st.columns([1, 1, 1])
col2.markdown("###### CRYPTO CURRENCIES")
col2.markdown("""
| Cryptocurrency | Ticker Symbol |
| --- | --- |
| Bitcoin | BTC-USD |
| Ethereum | ETH-USD |
| Binance Coin | BNB-USD |
| Solana | SOL-USD |
| Cardano | ADA-USD |
| XRP | XRP-USD |
| Polkadot | DOT-USD |
| Dogecoin | DOGE-USD |
| Avalanche | AVAX-USD |
| Litecoin | LTC-USD |
| Polygon | MATIC-USD |
| Shiba Inu | SHIB-USD |
""")
user_input = st.selectbox('Enter Stock Ticker', stocks)
st.markdown('# ')
st.markdown('##### Select The Date Range For Technical Analysis')
START = st.date_input('START:', value=pd.to_datetime("2017-01-01"))
TODAY = st.date_input('END (Today):', value=pd.to_datetime("today"))
stock_info = yf.Ticker(user_input).fast_info
# stock_info.keys() for other properties you can explore
st.subheader(user_input)
def load_data(user_input):
yf.pdr_override()
daata = data.get_data_yahoo(user_input, start=START, end=TODAY)
daata.reset_index(inplace=True)
return daata
df = load_data(user_input)
# describing data
st.subheader('Data Range 2017-Today')
# df= df.reset_index()
st.write(df.tail(10))
st.write(df.describe())
# Force lowercase (optional)
df.columns = [x.lower() for x in df.columns]
st.subheader("Prediction of Stock Price")
# train test split
data_training = pd.DataFrame(df['close'][0:int(len(df) * 0.70)])
data_testing = pd.DataFrame(df['close'][int(len(df) * 0.70): int(len(df))])
st.write("training data: ", data_training.shape)
st.write("testing data: ", data_testing.shape)
# scaling of data using min max scaler (0,1)
scaler = MinMaxScaler(feature_range=(0, 1))
data_training_array = scaler.fit_transform(data_training)
# Load model
model = load_model("lstm_model_2.h5")
# testing part
past_100_days = data_training.tail(30)
final_df = past_100_days.append(data_testing, ignore_index=True)
input_data = scaler.fit_transform(final_df)
x_test = []
y_test = []
for i in range(100, input_data.shape[0]):
x_test.append(input_data[i - 100: i])
y_test.append(input_data[i, 0])
x_test, y_test = np.array(x_test), np.array(y_test)
y_predicted = model.predict(x_test)
scaler = scaler.scale_
scale_factor = 1 / scaler[0]
y_predicted = y_predicted * scale_factor
y_test = y_test * scale_factor
st.subheader('Stock Price Prediction by Date')
df1 = df.reset_index()['close']
scaler = MinMaxScaler(feature_range=(0, 1))
df1 = scaler.fit_transform(np.array(df1).reshape(-1, 1))
# datemax="24/06/2022"
datemax = dt.datetime.strftime(dt.datetime.now() - timedelta(1), "%d/%m/%Y")
datemax = dt.datetime.strptime(datemax, "%d/%m/%Y")
x_input = df1[:].reshape(1, -1)
temp_input = list(x_input)
temp_input = temp_input[0].tolist()
date1 = st.date_input("Enter Date in this format yyyy-mm-dd")
result = st.button("Predict")
# st.write(result)
if result:
from datetime import datetime
my_time = datetime.min.time()
date1 = datetime.combine(date1, my_time)
# date1=str(date1)
# date1=dt.datetime.pastime(time_str,"%Y-%m-%d")
nDay = date1 - datemax
nDay = nDay.days
date_rng = pd.date_range(start=datemax, end=date1, freq='D')
date_rng = date_rng[1:date_rng.size]
lst_output = []
n_steps = x_input.shape[1]
i = 0
while i <= nDay:
if len(temp_input) > n_steps:
# print(temp_input)
x_input = np.array(temp_input[1:])
print("{} day input {}".format(i, x_input))
x_input = x_input.reshape(1, -1)
x_input = x_input.reshape((1, n_steps, 1))
# print(x_input)
yhat = model.predict(x_input, verbose=0)
print("{} day output {}".format(i, yhat))
temp_input.extend(yhat[0].tolist())
temp_input = temp_input[1:]
# print(temp_input)
lst_output.extend(yhat.tolist())
i = i + 1
else:
x_input = x_input.reshape((1, n_steps, 1))
yhat = model.predict(x_input, verbose=0)
print(yhat[0])
temp_input.extend(yhat[0].tolist())
print(len(temp_input))
lst_output.extend(yhat.tolist())
i = i + 1
res = scaler.inverse_transform(lst_output)
# output = res[nDay-1]
output = res[nDay]
st.write("*Predicted Price for Date :*", date1, "*is*", np.round(output[0], 2))
st.success('The Price is {}'.format(np.round(output[0], 2)))
# st.write("predicted price : ",output)
predictions = res[res.size - nDay:res.size]
print(predictions.shape)
predictions = predictions.ravel()
print(type(predictions))
print(date_rng)
print(predictions)
print(date_rng.shape)
@st.cache_data
def convert_df(df):
return df.to_csv().encode('utf-8')
df = pd.DataFrame(data=date_rng)
df['Predictions'] = predictions.tolist()
df.columns = ['Date', 'Price']
st.write(df)
csv = convert_df(df)
st.download_button(
"Press to Download",
csv,
"file.csv",
"text/csv",
key='download-csv'
)
# visualization
fig = plt.figure(figsize=(10, 6))
xpoints = date_rng
ypoints = predictions
plt.plot(xpoints, ypoints, color='blue', marker='o', linestyle='-', linewidth=2,
markersize=5) # Customize line style and marker
plt.xticks(rotation=45, fontsize=10) # Rotate x-axis labels and adjust fontsize
plt.yticks(fontsize=10) # Adjust fontsize of y-axis labels
plt.xlabel('Date', fontsize=12) # Set x-axis label and adjust fontsize
plt.ylabel('Price', fontsize=12) # Set y-axis label and adjust fontsize
plt.title('Cryptocurrency Price Prediction', fontsize=14) # Set plot title and adjust fontsize
plt.grid(True, linestyle='--', alpha=0.5) # Add grid lines with linestyle and transparency
plt.tight_layout() # Adjust layout to prevent clipping of labels
# Display the plot in Streamlit
st.pyplot(fig)
|