reab5555 commited on
Commit
bc8d3fc
·
verified ·
1 Parent(s): 956f484

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -0
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import yfinance as yf
4
+ import plotly.graph_objects as go
5
+ from plotly.colors import n_colors
6
+ from datetime import datetime, timedelta
7
+
8
+
9
+ # Load stock data
10
+ def load_stocks():
11
+ df = pd.read_csv('TASE_stock_list_2023.csv')
12
+ df['Symbol'] = df['Symbol'] + '.TA'
13
+ return df
14
+
15
+
16
+ # Get stock data and calculate percentage change
17
+ def get_stock_data(start_date, end_date, stocks):
18
+ data = {}
19
+ for _, row in stocks.iterrows():
20
+ symbol = row['Symbol']
21
+ name = row['Name']
22
+ stock = yf.Ticker(symbol)
23
+ hist = stock.history(start=start_date, end=end_date)
24
+ if not hist.empty:
25
+ initial_price = hist['Close'].iloc[0]
26
+ final_price = hist['Close'].iloc[-1]
27
+ pct_change = ((final_price - initial_price) / initial_price) * 100
28
+ if pct_change < 100: # Only include stocks with pct change below 100
29
+ data[symbol] = {
30
+ 'name': name,
31
+ 'initial_price': initial_price,
32
+ 'final_price': final_price,
33
+ 'pct_change': pct_change
34
+ }
35
+ return data
36
+
37
+
38
+ # Create bar chart
39
+ def create_chart(data):
40
+ sorted_data = dict(sorted(data.items(), key=lambda x: x[1]['pct_change'], reverse=True)[:100]) # Top 100 stocks
41
+
42
+ fig = go.Figure()
43
+
44
+ # Generate a color scale from light green to dark green
45
+ colors = n_colors('rgb(144,238,144)', 'rgb(0,100,0)', len(sorted_data), colortype='rgb')
46
+
47
+ # Reverse the color order so that higher values get darker colors
48
+ colors = colors[::-1]
49
+
50
+ fig.add_trace(go.Bar(
51
+ x=list(sorted_data.keys()),
52
+ y=[v['pct_change'] for v in sorted_data.values()],
53
+ text=[f"{v['pct_change']:.2f}%" for v in sorted_data.values()],
54
+ textposition='inside',
55
+ textfont=dict(color='white'), # Set text color to white for all bars
56
+ hovertext=[
57
+ f"{v['name']}<br>Symbol: {k}<br>Initial: {v['initial_price']:.2f}<br>Final: {v['final_price']:.2f}<br>Change: {v['pct_change']:.2f}%"
58
+ for k, v in sorted_data.items()],
59
+ hoverinfo='text',
60
+ marker_color=colors
61
+ ))
62
+ title = 'Stocks Performance'
63
+ fig.update_layout(
64
+ title=title,
65
+ xaxis_title='Stocks',
66
+ yaxis_title='Percentage Change',
67
+ hoverlabel=dict(
68
+ bgcolor="black",
69
+ font_size=12,
70
+ font_family="Rockwell",
71
+ font_color="white" # This ensures all hover text is white
72
+ )
73
+ )
74
+ return fig
75
+
76
+
77
+ # Main function
78
+ def analyze_stocks(year, month, day, num_days):
79
+ stocks = load_stocks()
80
+ start_date = datetime(year, month, day)
81
+ end_date = start_date + timedelta(days=num_days)
82
+ data = get_stock_data(start_date, end_date, stocks)
83
+
84
+ chart = create_chart(data)
85
+
86
+ return chart
87
+
88
+
89
+ # Set up Gradio interface
90
+ current_date = datetime.now()
91
+
92
+ with gr.Blocks() as demo:
93
+ gr.Markdown("# TASE Stock Performance Analyzer")
94
+ gr.Markdown("Analyze stock performances based on date range.")
95
+
96
+ with gr.Row():
97
+ year = gr.Dropdown(choices=list(range(2000, 2025)), label="Year", value=current_date.year)
98
+ month = gr.Dropdown(choices=list(range(1, 13)), label="Month", value=current_date.month)
99
+ day = gr.Dropdown(choices=list(range(1, 32)), label="Day", value=1)
100
+
101
+ num_days = gr.Slider(minimum=1, maximum=365, step=1, label="Number of Days", value=14)
102
+
103
+ submit_btn = gr.Button("Analyze")
104
+
105
+ output = gr.Plot()
106
+
107
+ submit_btn.click(
108
+ fn=analyze_stocks,
109
+ inputs=[year, month, day, num_days],
110
+ outputs=output
111
+ )
112
+
113
+ demo.launch()