Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import yfinance as yf
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import requests
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
import io
|
| 10 |
+
url_stocks = "https://financialmodelingprep.com/api/v3/stock/list?apikey="
|
| 11 |
+
url_sentiment = "https://yaakovy-fin-proj-docker.hf.space/ticker/"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def get_max_sentiment(row):
|
| 16 |
+
max_value = max(row['neg'], row['neu'], row['pos'])
|
| 17 |
+
if max_value == row['neg']:
|
| 18 |
+
return 'neg'
|
| 19 |
+
elif max_value == row['neu']:
|
| 20 |
+
return 'neu'
|
| 21 |
+
else:
|
| 22 |
+
return 'pos'
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def get_sentiment_data(stock_info):
|
| 27 |
+
symbol = stock_info.info['symbol']
|
| 28 |
+
url_sentiment_with_ticker = f"{url_sentiment}{symbol}"
|
| 29 |
+
response = requests.get(url_sentiment_with_ticker)
|
| 30 |
+
if response.status_code == 200:
|
| 31 |
+
json_data = json.loads(response.json())
|
| 32 |
+
df = pd.DataFrame(json_data)
|
| 33 |
+
df['sentiment'] = df.apply(get_max_sentiment, axis=1)
|
| 34 |
+
df = df.drop(['neg', 'neu', 'pos', 'sentiment_score'], axis=1)
|
| 35 |
+
return df
|
| 36 |
+
else:
|
| 37 |
+
return
|
| 38 |
+
|
| 39 |
+
def print_sentiment(stock_info):
|
| 40 |
+
df = get_sentiment_data(stock_info)
|
| 41 |
+
st.write("Market Sentiment")
|
| 42 |
+
st.dataframe(df, hide_index =True )
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def print_stock_info(stock_info):
|
| 47 |
+
stock_info_html = get_stock_info_from_html(stock_info.info)
|
| 48 |
+
st.write(stock_info_html, unsafe_allow_html=True)
|
| 49 |
+
plot_graph(stock_info)
|
| 50 |
+
|
| 51 |
+
col1, col2 = st.columns([0.8, 0.2])
|
| 52 |
+
with col1:
|
| 53 |
+
st.pyplot(plt)
|
| 54 |
+
with col2:
|
| 55 |
+
tf = st.radio(
|
| 56 |
+
"Select Time Frame",
|
| 57 |
+
["1Y", "3Y", "5Y"], index=2,
|
| 58 |
+
key="chart_time_frame",
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
def get_stock_info_from_html(stock_info):
|
| 62 |
+
si = stock_info
|
| 63 |
+
text = (f"<b>Comp. Name: </b> {si['longName']}, {si['city']}, {si['state']} {si['country']} <br>"
|
| 64 |
+
f"<b>Web site: </b> <a href=\"{si['website']}\">{si['website']}</a> <br>"
|
| 65 |
+
f"<b>Stock Price: </b> {si['currentPrice']} {str(si['financialCurrency'])}")
|
| 66 |
+
return text
|
| 67 |
+
|
| 68 |
+
def plot_graph(stock_info):
|
| 69 |
+
period = st.session_state.chart_time_frame or "5Y"
|
| 70 |
+
history = stock_info.history(period=period)
|
| 71 |
+
name = stock_info.info['longName']
|
| 72 |
+
plt.plot(history['Close'])
|
| 73 |
+
plt.xlabel('Date')
|
| 74 |
+
plt.ylabel('Price')
|
| 75 |
+
plt.title(f"{name} Stock Price")
|
| 76 |
+
return plt
|
| 77 |
+
|
| 78 |
+
st.title('_My Crystal Ball_')
|
| 79 |
+
par1 = "It allows users to stay informed about the latest market trends and make informed decisions about their investments."
|
| 80 |
+
par2 = "To get started, simply select whether you want information on currencies or stocks:<br> "\
|
| 81 |
+
"<ul><li><strong>Currencies</strong><br>Choose from a wide range of currencies to view the latest exchange rates, historical data, and charts.</li>"\
|
| 82 |
+
"<li><strong>Stocks</strong><br>Search for specific stocks to view real-time prices, historical data, news, and more.</li></ul>"
|
| 83 |
+
st.write(par1, unsafe_allow_html=True)
|
| 84 |
+
st.write(par2, unsafe_allow_html=True)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
if 'chart_time_frame' not in st.session_state:
|
| 88 |
+
st.session_state['chart_time_frame'] = '5Y'
|
| 89 |
+
|
| 90 |
+
if 'data_available' not in st.session_state:
|
| 91 |
+
st.session_state['data_available'] = False
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
option = st.selectbox("select", ["", "Currencies", "Stocks"], placeholder="Choose an option", label_visibility = "hidden")
|
| 95 |
+
|
| 96 |
+
if option == "Currencies":
|
| 97 |
+
input_text = "Enter currency pair"
|
| 98 |
+
else:
|
| 99 |
+
input_text = "Enter stock symbol"
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
text_box: str = None
|
| 103 |
+
btn_get_data = None
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
if option:
|
| 107 |
+
text_box = st.text_input(input_text)
|
| 108 |
+
|
| 109 |
+
with st.spinner('Wait for it...'):
|
| 110 |
+
if text_box:
|
| 111 |
+
ticker = text_box.upper()
|
| 112 |
+
try:
|
| 113 |
+
stock_info = yf.Ticker(ticker)
|
| 114 |
+
stock_info.info['longName']
|
| 115 |
+
except:
|
| 116 |
+
st.error('Ticker not found', icon="🚨")
|
| 117 |
+
st.session_state['data_available'] = False
|
| 118 |
+
else:
|
| 119 |
+
|
| 120 |
+
st.session_state['data_available'] = True
|
| 121 |
+
print_stock_info(stock_info)
|
| 122 |
+
print_sentiment(stock_info)
|
| 123 |
+
|