import numpy as np import pandas as pd from datetime import datetime, date import matplotlib.pyplot as plt import seaborn import yfinance as yf # import holoviews as hvs import panel as pn # import hvplot as hv # import hvplot.pandas pn.extension("bokeh", template="bootstrap") import os from dotenv import load_dotenv import holoviews as hv # from datasets import load_dataset from utils import load_hf_dataset # ,upload_to_hf_dataset, download_from_hf_dataset # Load environment variables from .env file load_dotenv() # Get the Hugging Face API token from the environment; either set in .env file or in the environment directly in GitHub HF_TOKEN = os.getenv("HF_TOKEN") # Get the name of the GuruFocus dataset for TradingView, Finviz, MarketBeat and TipRanks to read from dataset_name_TradingView_input = os.getenv("dataset_name_TradingView_input") @pn.cache() def get_tradingview(current_datetime): # Load lastest TradingView DataSet from HuggingFace Dataset which is always america.csv # download_from_hf_dataset("america.csv", dataset_name_TradingView_input, HF_TOKEN) return load_hf_dataset("america.csv", HF_TOKEN, dataset_name_TradingView_input) @pn.cache() def get_stock_history(ticker, startdate, enddate, current_datetime): return yf.Ticker(ticker).history(start=startdate, end=enddate) # Reading gurufocus,finviz from crawling pipelines with GitHub Action current_datetime = datetime.now().strftime("%Y-%m-%d") DF = get_tradingview(current_datetime) # DF.Industry.unique() def get_plot_Sector(selected_Sector, startdate , enddate, num_stocks, method_selector): # selected_Industry_or_Sector= "Major Banks" # ticker_list = list(DF.query('Sector==@selected_Sector & `Market Capitalization`>100e9').Ticker) ticker_list = list(DF.query('Sector==@selected_Sector').sort_values('Market Capitalization', ascending=False).Ticker[0:num_stocks]) len(ticker_list) # ticker_list = ['AAPL', 'AMZN', 'GOOGL','TSLA', 'NVDA' , 'AMD'] # start = datetime(2020, 1, 1) # end = datetime.now() # end = datetime(2020,1,1) method = method_selector #'pearson'#, 'kendall', 'spearman' tickers=[] for ticker in ticker_list: if not '/' in ticker: # df = yf.Ticker(ticker).history(start=startdate , end=enddate) df = get_stock_history(ticker, startdate, enddate, current_datetime) df['Ticker'] = ticker tickers.append(df) DFtotal = pd.concat(tickers) DFtotal = DFtotal.reset_index() DFtotal = DFtotal[['Date', 'Close', 'Ticker']] df_pivot = DFtotal.pivot(index='Date', columns='Ticker', values='Close').reset_index() df_pivot = df_pivot.drop('Date', axis=1) corr_df = df_pivot.corr(method=method) # Convert correlation matrix to format suitable for heatmap corr_reset = corr_df.reset_index() melted_corr = pd.melt(corr_reset, id_vars='Ticker', var_name='Variable', value_name='Correlation') # Create heatmap using holoviews hv.extension('bokeh') heatmap = hv.HeatMap(melted_corr, kdims=['Ticker', 'Variable'], vdims='Correlation') heatmap = heatmap.opts( title=f'Heat Map correlation based on {method} method for {selected_Sector} sector from {startdate.year} - {enddate.year}', width=800, height=600, tools=['hover'], colorbar=True, cmap='RdYlGn', xrotation=45, labelled=[], fontsize={'title': 12, 'labels': 10}, ) return heatmap # Create dropdown widget for industry selection industry_selector = pn.widgets.Select( name='Select Industry', options=list(DF.Industry.unique()), value='Major Banks' ) num_stocks = pn.widgets.IntSlider( name='Number of Stocks', start=5, end=30, value=10, step=1 ) method_selector = pn.widgets.Select( name='Correlation Method', options=['pearson', 'kendall', 'spearman'], value='pearson' ) # Create dropdown widget for industry selection sector_selector = pn.widgets.Select( name='Select Sector', options=list(DF.Sector.unique()), value='Health Technology' ) date_start = pn.widgets.DatePicker( name ="Start Date", description='Select a Date', start= date(2000, 1, 1) ) date_end = pn.widgets.DatePicker( name ="End Date",# value=datetime(2000, 1, 1), description='Select a Date', end= date.today() #date(2023, 9, 1) ) date_start.value = date(2020,1,1) date_end.value = date.today() 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)) # tab_Industry_content = pn.Row(pn.Column(date_start, date_end, industry_selector ), pn.bind(get_plot_Industry,industry_selector, date_start, date_end)) pn.Tabs( ("Sector", tab_Sector_content), # ("Tab Industry", tab_Industry_content) ).servable('Stock Correlation Heatmap') # 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')