AmirTrader commited on
Commit
51cdcf8
·
verified ·
1 Parent(s): 08966f1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -0
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
3
+
4
+ from datetime import datetime, date
5
+
6
+ import matplotlib.pyplot as plt
7
+ import seaborn
8
+
9
+ import yfinance as yf
10
+
11
+ # import holoviews as hvs
12
+ import panel as pn
13
+ # import hvplot as hv
14
+ # import hvplot.pandas
15
+
16
+ pn.extension("bokeh", template="bootstrap")
17
+
18
+ import os
19
+
20
+ from dotenv import load_dotenv
21
+ import holoviews as hv
22
+
23
+ # from datasets import load_dataset
24
+ from utils import load_hf_dataset # ,upload_to_hf_dataset, download_from_hf_dataset
25
+
26
+ # Load environment variables from .env file
27
+ load_dotenv()
28
+
29
+ # Get the Hugging Face API token from the environment; either set in .env file or in the environment directly in GitHub
30
+ HF_TOKEN = os.getenv("HF_TOKEN")
31
+
32
+ # Get the name of the GuruFocus dataset for TradingView, Finviz, MarketBeat and TipRanks to read from
33
+ dataset_name_TradingView_input = os.getenv("dataset_name_TradingView_input")
34
+
35
+
36
+ @pn.cache()
37
+ def get_tradingview(current_datetime):
38
+ # Load lastest TradingView DataSet from HuggingFace Dataset which is always america.csv
39
+ # download_from_hf_dataset("america.csv", dataset_name_TradingView_input, HF_TOKEN)
40
+ return load_hf_dataset("america.csv", HF_TOKEN, dataset_name_TradingView_input)
41
+
42
+
43
+
44
+ # Reading gurufocus,finviz from crawling pipelines with GitHub Action
45
+ current_datetime = datetime.now().strftime("%Y-%m-%d")
46
+ DF = get_tradingview(current_datetime)
47
+ # DF.Industry.unique()
48
+
49
+ def get_plot(tabtype, selected_Industry_or_Sector, startdate , enddate):
50
+ # selected_Industry_or_Sector= "Major Banks"
51
+ if tabtype == 'Industry':
52
+ ticker_list = list(DF.query('Industry==@selected_Industry_or_Sector & `Market Capitalization`>100e9').Ticker)
53
+ else:
54
+ ticker_list = list(DF.query('Sector==@selected_Industry_or_Sector & `Market Capitalization`>100e9').Ticker)
55
+ len(ticker_list)
56
+ # ticker_list = ['AAPL', 'AMZN', 'GOOGL','TSLA', 'NVDA' , 'AMD']
57
+
58
+ # start = datetime(2020, 1, 1)
59
+ # end = datetime.now()
60
+ # end = datetime(2020,1,1)
61
+ method = 'pearson'#, 'kendall', 'spearman'
62
+
63
+ tickers=[]
64
+ for ticker in ticker_list:
65
+ if not '/' in ticker:
66
+ df = yf.Ticker(ticker).history(start=startdate , end=enddate)
67
+ df['Ticker'] = ticker
68
+ tickers.append(df)
69
+ DFtotal = pd.concat(tickers)
70
+ DFtotal = DFtotal.reset_index()
71
+ DFtotal = DFtotal[['Date', 'Close', 'Ticker']]
72
+ df_pivot = DFtotal.pivot(index='Date', columns='Ticker', values='Close').reset_index()
73
+ df_pivot = df_pivot.drop('Date', axis=1)
74
+ corr_df = df_pivot.corr(method=method)
75
+
76
+ # Convert correlation matrix to format suitable for heatmap
77
+ corr_reset = corr_df.reset_index()
78
+ melted_corr = pd.melt(corr_reset, id_vars='Ticker', var_name='Variable', value_name='Correlation')
79
+
80
+ # Create heatmap using holoviews
81
+ hv.extension('bokeh')
82
+
83
+ heatmap = hv.HeatMap(melted_corr, kdims=['Ticker', 'Variable'], vdims='Correlation')
84
+ heatmap = heatmap.opts(
85
+ title=f'Heat Map correlation based on {method} method for {selected_Industry_or_Sector} sector from {startdate.year} - {enddate.year}',
86
+ width=800,
87
+ height=600,
88
+ tools=['hover'],
89
+ colorbar=True,
90
+ cmap='RdYlGn',
91
+ xrotation=45,
92
+ labelled=[],
93
+ fontsize={'title': 12, 'labels': 10},
94
+ )
95
+ return heatmap
96
+
97
+ # Create dropdown widget for industry selection
98
+ industry_selector = pn.widgets.Select(
99
+ name='Select Industry',
100
+ options=list(DF.Industry.unique()),
101
+ value='Major Banks'
102
+ )
103
+
104
+ # Create dropdown widget for industry selection
105
+ sector_selector = pn.widgets.Select(
106
+ name='Select Sector',
107
+ options=list(DF.Sector.unique()),
108
+ value='Finance'
109
+ )
110
+
111
+ date_start = pn.widgets.DatePicker(
112
+ name ="Start Date",
113
+ description='Select a Date',
114
+ start= date(2000, 1, 1)
115
+ )
116
+
117
+ date_end = pn.widgets.DatePicker(
118
+ name ="End Date",# value=datetime(2000, 1, 1),
119
+ description='Select a Date',
120
+ end= date.today() #date(2023, 9, 1)
121
+ )
122
+
123
+ date_start.value = date(2010,1,1)
124
+ date_end.value = date.today()
125
+
126
+
127
+ tab_Sector_content = pn.Row(pn.Column(date_start, date_end, sector_selector ), pn.bind(get_plot,'Sector', sector_selector, date_start, date_end))
128
+ tab_Industry_content = pn.Row(pn.Column(date_start, date_end, industry_selector ))#, pn.bind(get_plot,'Industry', industry_selector, date_start, date_end))
129
+ pn.Tabs(
130
+ ("Tab Sector", tab_Sector_content),
131
+ ("Tab Industry", tab_Industry_content)
132
+ ).servable('Stock Correlation Heatmap')
133
+
134
+ # pn.Row(pn.Column(date_start, date_end, industry_selector ), pn.bind(get_plot,industry_selector, date_start, date_end)).servable('US Stock Correlation Heatmap')
135
+
136
+
137
+
138
+