forked from y7iia/Financials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyst_recommendations.py
92 lines (71 loc) · 3.3 KB
/
analyst_recommendations.py
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
import streamlit as st
import yfinance as yf
import pandas as pd
def fetch_data(ticker_code):
# Fetch data
ticker = yf.Ticker(ticker_code)
info = ticker.info
# Convert the data into a DataFrame
df = pd.DataFrame.from_dict(info, orient='index').T
# Define the columns to be selected
col = ['shortName','recommendationKey','currentPrice','targetHighPrice','targetMeanPrice','targetLowPrice','numberOfAnalystOpinions']
# Check and select only existing columns
col = [c for c in col if c in df.columns]
# Continue if there are any columns to select
if not col:
raise ValueError('No financial information for this ticker, select another one')
# Define the dictionary for renaming the columns
col_dict = {
'shortName': 'Company الشركة',
'recommendationKey': 'Recommendation توصيات المحللين',
'currentPrice': 'Current Price السعر الحالي',
'targetHighPrice': 'Target High أعلى سعر مستهدف',
'targetMeanPrice': 'Target Mean متوسط السعر المستهدف',
'targetLowPrice': 'Target Low أدني سعر مستهدف',
'numberOfAnalystOpinions': 'Analyst Opinions عدد آراء المحلين'
}
# Select the columns from the DataFrame
selected_df = df[col]
# Rename the columns
renamed_df = selected_df.rename(columns=col_dict)
# Transpose the data
table = renamed_df.T
table['%'] = '-'
for i , n in enumerate(table[0]):
try:
if round((table[0][i] / table[0][2]-1)*100,2) > 0:
table['%'][i] = '+' + str(round((table[0][i] / table[0][2]-1)*100,1)) + '%'
else:
table['%'][i] = str(round((table[0][i] / table[0][2]-1)*100,1)) + '%'
except:
continue
table['%'][2] = '-'
table['%'][6] = '-'
table.rename(columns={0: 'المستهدف'}, inplace=True)
return table
st.title('اراء المحللين - Analyst Recommendations')
st.markdown(' @telmisany - برمجة يحيى التلمساني')
# Define the ticker code
code = st.text_input('Enter Ticker ادخل رمز السهم')
# Add a button for the user to click when they are ready to submit
if st.button('Submit'):
if code:
# Check if the ticker is numeric and append '.SR' if it is
if code.isdigit():
code = code + '.SR'
try:
table = fetch_data(code)
# Convert DataFrame to HTML and use inline style to control width
html_table = table.to_html(classes='table table-striped', header="true", table_id="html_table").replace('<table ','<table style="width:100% !important;" ')
# Display the HTML table in Streamlit
st.markdown(html_table, unsafe_allow_html=True)
except Exception as e:
st.error(f'Error fetching data: {e}')
st.write('\n')
st.markdown('[أنظر ايضا: حاسبة الدعوم والمقاومات](https://twitter.com/telmisany/status/1700897237096640791)')
st.write('\n')
st.markdown('[أنظر ايضا: الأرباح المبقاة](https://twitter.com/telmisany/status/1700128870349811959)')
# Add three empty lines for spacing
st.write('\n\n\n')
# Add a hyperlink to your Twitter account
st.markdown('[X تابعني في منصة](https://twitter.com/telmisany)')