Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,7 +24,7 @@ import holoviews as hv
|
|
| 24 |
from utils import load_hf_dataset # ,upload_to_hf_dataset, download_from_hf_dataset
|
| 25 |
|
| 26 |
# Load environment variables from .env file
|
| 27 |
-
|
| 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")
|
|
@@ -40,30 +40,34 @@ def get_tradingview(current_datetime):
|
|
| 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 |
-
|
|
|
|
| 50 |
# selected_Industry_or_Sector= "Major Banks"
|
| 51 |
-
|
| 52 |
-
|
| 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)
|
|
@@ -82,7 +86,7 @@ def get_plot(tabtype, selected_Industry_or_Sector, startdate , enddate):
|
|
| 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 {
|
| 86 |
width=800,
|
| 87 |
height=600,
|
| 88 |
tools=['hover'],
|
|
@@ -100,7 +104,19 @@ industry_selector = pn.widgets.Select(
|
|
| 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',
|
|
@@ -120,15 +136,15 @@ date_end = pn.widgets.DatePicker(
|
|
| 120 |
end= date.today() #date(2023, 9, 1)
|
| 121 |
)
|
| 122 |
|
| 123 |
-
date_start.value = date(
|
| 124 |
date_end.value = date.today()
|
| 125 |
|
| 126 |
|
| 127 |
-
tab_Sector_content = pn.Row(pn.Column(date_start, date_end, sector_selector ), pn.bind(
|
| 128 |
-
tab_Industry_content = pn.Row(pn.Column(date_start, date_end, industry_selector )
|
| 129 |
pn.Tabs(
|
| 130 |
-
("
|
| 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')
|
|
|
|
| 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")
|
|
|
|
| 40 |
return load_hf_dataset("america.csv", HF_TOKEN, dataset_name_TradingView_input)
|
| 41 |
|
| 42 |
|
| 43 |
+
@pn.cache()
|
| 44 |
+
def get_stock_history(ticker, startdate, enddate, current_datetime):
|
| 45 |
+
return yf.Ticker(ticker).history(start=startdate, end=enddate)
|
| 46 |
+
|
| 47 |
|
| 48 |
# Reading gurufocus,finviz from crawling pipelines with GitHub Action
|
| 49 |
current_datetime = datetime.now().strftime("%Y-%m-%d")
|
| 50 |
DF = get_tradingview(current_datetime)
|
| 51 |
# DF.Industry.unique()
|
| 52 |
|
| 53 |
+
|
| 54 |
+
def get_plot_Sector(selected_Sector, startdate , enddate, num_stocks, method_selector):
|
| 55 |
# selected_Industry_or_Sector= "Major Banks"
|
| 56 |
+
ticker_list = list(DF.query('Sector==@selected_Sector & `Market Capitalization`>100e9').Ticker)
|
| 57 |
+
ticker_list = list(DF.query('Sector==@selected_Sector').Ticker[0:num_stocks])
|
|
|
|
|
|
|
| 58 |
len(ticker_list)
|
| 59 |
# ticker_list = ['AAPL', 'AMZN', 'GOOGL','TSLA', 'NVDA' , 'AMD']
|
| 60 |
|
| 61 |
# start = datetime(2020, 1, 1)
|
| 62 |
# end = datetime.now()
|
| 63 |
# end = datetime(2020,1,1)
|
| 64 |
+
method = method_selector #'pearson'#, 'kendall', 'spearman'
|
| 65 |
|
| 66 |
tickers=[]
|
| 67 |
for ticker in ticker_list:
|
| 68 |
if not '/' in ticker:
|
| 69 |
+
# df = yf.Ticker(ticker).history(start=startdate , end=enddate)
|
| 70 |
+
df = get_stock_history(ticker, startdate, enddate, current_datetime)
|
| 71 |
df['Ticker'] = ticker
|
| 72 |
tickers.append(df)
|
| 73 |
DFtotal = pd.concat(tickers)
|
|
|
|
| 86 |
|
| 87 |
heatmap = hv.HeatMap(melted_corr, kdims=['Ticker', 'Variable'], vdims='Correlation')
|
| 88 |
heatmap = heatmap.opts(
|
| 89 |
+
title=f'Heat Map correlation based on {method} method for {selected_Sector} sector from {startdate.year} - {enddate.year}',
|
| 90 |
width=800,
|
| 91 |
height=600,
|
| 92 |
tools=['hover'],
|
|
|
|
| 104 |
options=list(DF.Industry.unique()),
|
| 105 |
value='Major Banks'
|
| 106 |
)
|
| 107 |
+
num_stocks = pn.widgets.IntSlider(
|
| 108 |
+
name='Number of Stocks',
|
| 109 |
+
start=5,
|
| 110 |
+
end=30,
|
| 111 |
+
value=10,
|
| 112 |
+
step=1
|
| 113 |
+
)
|
| 114 |
|
| 115 |
+
method_selector = pn.widgets.Select(
|
| 116 |
+
name='Correlation Method',
|
| 117 |
+
options=['pearson', 'kendall', 'spearman'],
|
| 118 |
+
value='pearson'
|
| 119 |
+
)
|
| 120 |
# Create dropdown widget for industry selection
|
| 121 |
sector_selector = pn.widgets.Select(
|
| 122 |
name='Select Sector',
|
|
|
|
| 136 |
end= date.today() #date(2023, 9, 1)
|
| 137 |
)
|
| 138 |
|
| 139 |
+
date_start.value = date(2020,1,1)
|
| 140 |
date_end.value = date.today()
|
| 141 |
|
| 142 |
|
| 143 |
+
tab_Sector_content = pn.Row(pn.Column(date_start, date_end, sector_selector, num_stocks, method_selector ), pn.bind(get_plot_Sector, sector_selector, date_start, date_end, num_stocks, method_selector))
|
| 144 |
+
# tab_Industry_content = pn.Row(pn.Column(date_start, date_end, industry_selector ), pn.bind(get_plot_Industry,industry_selector, date_start, date_end))
|
| 145 |
pn.Tabs(
|
| 146 |
+
("Sector", tab_Sector_content),
|
| 147 |
+
# ("Tab Industry", tab_Industry_content)
|
| 148 |
).servable('Stock Correlation Heatmap')
|
| 149 |
|
| 150 |
# 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')
|