File size: 4,979 Bytes
51cdcf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9ebd6a
51cdcf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9ebd6a
 
 
 
51cdcf8
 
 
 
 
 
a9ebd6a
 
51cdcf8
0784634
 
51cdcf8
 
 
 
 
 
a9ebd6a
51cdcf8
 
 
 
a9ebd6a
 
51cdcf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9ebd6a
51cdcf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9ebd6a
 
 
 
 
 
 
51cdcf8
a9ebd6a
 
 
 
 
51cdcf8
 
 
 
134310c
51cdcf8
 
 
 
 
 
 
 
 
 
 
 
 
 
a9ebd6a
51cdcf8
 
 
a9ebd6a
 
51cdcf8
a9ebd6a
 
51cdcf8
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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')