Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import numpy as np
|
| 3 |
-
|
| 4 |
-
import io
|
|
|
|
| 5 |
from datetime import datetime
|
| 6 |
from datetime import timedelta
|
| 7 |
|
|
@@ -12,6 +13,11 @@ import hvplot.pandas
|
|
| 12 |
|
| 13 |
import yfinance as yf
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
pn.extension('bokeh', template='bootstrap')
|
| 16 |
|
| 17 |
def _extract_raw_data(ticker):
|
|
@@ -60,31 +66,69 @@ def get_text(df):
|
|
| 60 |
sio.seek(0)
|
| 61 |
return sio
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
current_datetime = datetime.now().strftime("%Y-%m-%d")
|
| 65 |
-
# daily_gurufocus_csvfile = f"https://raw.githubusercontent.com/alirezax2/GurusFocusCrawl/main/gurufocusuruFocus_merged_{current_datetime}.csv"
|
| 66 |
-
daily_gurufocus_csvfile = f"https://raw.githubusercontent.com/alirezax2/GurusFocusCrawl/main/gurufocus/GuruFocus_merged_{current_datetime}.csv"
|
| 67 |
-
daily_gurufocus_DF = pd.read_csv(daily_gurufocus_csvfile)
|
| 68 |
|
| 69 |
-
#Reading tiprank from local crawling pipeline; Need to update github action to merge gurufocus daily with weekly tiprank
|
| 70 |
-
monthly_tiprank_csvfile = f"https://raw.githubusercontent.com/alirezax2/GurusFocusCrawl/main/tipranks/tipranks_2024-06-01.csv"
|
| 71 |
-
monthly_tiprank_DF = pd.read_csv(monthly_tiprank_csvfile)[['Ticker','SmartScore','Market Capitalization','Sector','Industry']]
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
daily_finviz_DF['FinVizPrice'] = pd.to_numeric(daily_finviz_DF['Price'], errors='coerce').fillna(0).astype(float)
|
| 77 |
daily_finviz_DF['FinVizTarget'] = pd.to_numeric(daily_finviz_DF['Target Price'], errors='coerce').fillna(0).astype(float)
|
| 78 |
daily_finviz_DF['FinVizTargetpercent'] = (100*(daily_finviz_DF['FinVizTarget']-daily_finviz_DF['FinVizPrice'])/daily_finviz_DF['FinVizPrice']).round(2)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
|
| 82 |
#Merging tipranks with Gurufocus
|
| 83 |
DFgurufocus = daily_gurufocus_DF[['Ticker' , 'GFValue']] # , 'GFValuediff']]
|
| 84 |
DFmerge_tipranks_gurufocus = DFgurufocus.merge(monthly_tiprank_DF)
|
| 85 |
|
| 86 |
#Merging Finviz with Merged last one
|
| 87 |
-
DFmerge_tipranks_gurufocus = DFmerge_tipranks_gurufocus.merge(
|
| 88 |
|
| 89 |
if 'Price' in DFmerge_tipranks_gurufocus.columns and 'GFValue' in DFmerge_tipranks_gurufocus.columns:
|
| 90 |
DFmerge_tipranks_gurufocus['GFValuepercent'] = 100* ( DFmerge_tipranks_gurufocus['GFValue'] - DFmerge_tipranks_gurufocus['Price']) / DFmerge_tipranks_gurufocus['Price']
|
|
@@ -94,13 +138,14 @@ if 'Price' in DFmerge_tipranks_gurufocus.columns and 'GFValue' in DFmerge_tipran
|
|
| 94 |
DFmerge_tipranks_gurufocus = DFmerge_tipranks_gurufocus.drop(columns=['Market Capitalization'])
|
| 95 |
DFmerge_tipranks_gurufocus = DFmerge_tipranks_gurufocus[['Ticker', 'Sector', 'Industry' , 'MarketCap' , 'SmartScore', 'Price' , 'GFValue' , 'GFValuepercent' , 'FinVizTarget','FinVizTargetpercent']]
|
| 96 |
|
|
|
|
| 97 |
#widget
|
| 98 |
ticker = pn.widgets.AutocompleteInput(name='Ticker', options=list(DFmerge_tipranks_gurufocus.Ticker) , placeholder='Write Ticker here همین جا',value='ALL', restrict=False)
|
| 99 |
SmartScore = pn.widgets.EditableRangeSlider(name='Smart Score', start=0, end=10, value=(9, 10), step=1)
|
| 100 |
Industry = pn.widgets.CheckBoxGroup( name='Select Industry', value=list(set(DFmerge_tipranks_gurufocus.Industry)), options=list(set(DFmerge_tipranks_gurufocus.Industry)), inline=True)
|
| 101 |
Sector = pn.widgets.CheckBoxGroup( name='Select Sector', value=list(set(DFmerge_tipranks_gurufocus.Sector)), options=list(set(DFmerge_tipranks_gurufocus.Sector)), inline=False)
|
| 102 |
-
GFValuepercent = pn.widgets.FloatSlider(name='GF Value %', start=-100, end=
|
| 103 |
-
FinVizTargetpercent = pn.widgets.FloatSlider(name='FinViz Target %', start=-100, end=
|
| 104 |
MarketCap = pn.widgets.FloatSlider(name='Market Capital (B$)', start=0, end=1000, step=1, value=10)
|
| 105 |
# Create a file download link
|
| 106 |
download_button_GuruFocus = pn.widgets.FileDownload(
|
|
@@ -146,4 +191,4 @@ download_button_watchlist = pn.widgets.FileDownload(
|
|
| 146 |
pn.extension('tabulator')
|
| 147 |
bound_plot = pn.bind(get_DF, DF=DFmerge_tipranks_gurufocus,ticker=ticker,SmartScore=SmartScore,GFValuepercent=GFValuepercent, FinVizTargetpercent=FinVizTargetpercent, Sector=Sector ,MarketCap=MarketCap)
|
| 148 |
|
| 149 |
-
pn.Column(pn.Row(pn.Column(ticker,SmartScore,GFValuepercent, FinVizTargetpercent, MarketCap, Sector, download_button_GuruFocus,download_button_FinViz ,download_button_Filter, download_button_watchlist, png_pane),bound_plot)).servable(title="Fair Value Ranking - Merged Gurufocus & Tipranks & FinViz & MorningStars")
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import numpy as np
|
| 3 |
+
|
| 4 |
+
import os,io
|
| 5 |
+
|
| 6 |
from datetime import datetime
|
| 7 |
from datetime import timedelta
|
| 8 |
|
|
|
|
| 13 |
|
| 14 |
import yfinance as yf
|
| 15 |
|
| 16 |
+
from dotenv import load_dotenv
|
| 17 |
+
# from datasets import load_dataset
|
| 18 |
+
|
| 19 |
+
from utils import upload_to_hf_dataset, download_from_hf_dataset, load_hf_dataset
|
| 20 |
+
|
| 21 |
pn.extension('bokeh', template='bootstrap')
|
| 22 |
|
| 23 |
def _extract_raw_data(ticker):
|
|
|
|
| 66 |
sio.seek(0)
|
| 67 |
return sio
|
| 68 |
|
| 69 |
+
|
| 70 |
+
# Load environment variables from .env file
|
| 71 |
+
load_dotenv()
|
| 72 |
+
|
| 73 |
+
# Get the Hugging Face API token from the environment; either set in .env file or in the environment directly in GitHub
|
| 74 |
+
HF_TOKEN = os.getenv('HF_TOKEN')
|
| 75 |
+
|
| 76 |
+
# Get the name of the GuruFocus dataset for TradingView, Finviz, MarketBeat and TipRanks to read from
|
| 77 |
+
dataset_name_BeatingMarket_input = os.getenv('dataset_name_BeatingMarket_input')
|
| 78 |
+
dataset_name_TradingView_input = os.getenv('dataset_name_TradingView_input')
|
| 79 |
+
dataset_name_GuruFocus_input = os.getenv('dataset_name_GuruFocus_input')
|
| 80 |
+
dataset_name_FinViz_input = os.getenv('dataset_name_FinViz_input')
|
| 81 |
+
dataset_name_TipRanks_input = os.getenv('dataset_name_TipRanks_input')
|
| 82 |
+
|
| 83 |
+
|
| 84 |
current_datetime = datetime.now().strftime("%Y-%m-%d")
|
|
|
|
|
|
|
|
|
|
| 85 |
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
@pn.cache()
|
| 88 |
+
def get_tradingview(current_datetime):
|
| 89 |
+
#Load lastest TradingView DataSet from HuggingFace Dataset which is always america.csv
|
| 90 |
+
# download_from_hf_dataset("america.csv", dataset_name_TradingView_input, HF_TOKEN)
|
| 91 |
+
return load_hf_dataset("america.csv", HF_TOKEN, dataset_name_TradingView_input)
|
| 92 |
+
|
| 93 |
+
@pn.cache()
|
| 94 |
+
def get_marketbeat(current_datetime):
|
| 95 |
+
return load_hf_dataset("beatingmarket.csv", HF_TOKEN, dataset_name_BeatingMarket_input)
|
| 96 |
+
|
| 97 |
+
@pn.cache()
|
| 98 |
+
def get_gurufocus(current_datetime):
|
| 99 |
+
#Rading gurufocus from github action pipeline
|
| 100 |
+
# daily_gurufocus_csvfile = f"https://raw.githubusercontent.com/alirezax2/GurusFocusCrawl/main/gurufocusuruFocus_merged_{current_datetime}.csv"
|
| 101 |
+
# daily_gurufocus_DF = pd.read_csv(daily_gurufocus_csvfile)
|
| 102 |
+
# daily_gurufocus_DF = load_dataset(dataset_name_GuruFocus_output , data_files="GuruFocus_merged.csv", split="train" , token=HF_TOKEN ).to_pandas()
|
| 103 |
+
return load_hf_dataset("GuruFocus_merged.csv", HF_TOKEN, dataset_name_GuruFocus_input)
|
| 104 |
+
|
| 105 |
+
@pn.cache()
|
| 106 |
+
def get_finviz(current_datetime):
|
| 107 |
+
return load_hf_dataset("FinViz.csv", HF_TOKEN, dataset_name_FinViz_input)
|
| 108 |
+
|
| 109 |
+
@pn.cache()
|
| 110 |
+
def get_tipranks(current_datetime):
|
| 111 |
+
return load_hf_dataset("tipranks.csv", HF_TOKEN, dataset_name_TipRanks_input)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
#Reading gurufocus,finviz from local crawling pipeline; Need to update github action to merge gurufocus daily with monthly tiprank
|
| 115 |
+
daily_gurufocus_DF = get_gurufocus(current_datetime)
|
| 116 |
+
daily_finviz_DF = get_finviz(current_datetime)
|
| 117 |
+
monthly_tiprank_DF = get_tipranks(current_datetime)
|
| 118 |
+
monthly_tiprank_DF = monthly_tiprank_DF[['Ticker','SmartScore','Market Capitalization','Sector','Industry']]
|
| 119 |
+
|
| 120 |
+
|
| 121 |
daily_finviz_DF['FinVizPrice'] = pd.to_numeric(daily_finviz_DF['Price'], errors='coerce').fillna(0).astype(float)
|
| 122 |
daily_finviz_DF['FinVizTarget'] = pd.to_numeric(daily_finviz_DF['Target Price'], errors='coerce').fillna(0).astype(float)
|
| 123 |
daily_finviz_DF['FinVizTargetpercent'] = (100*(daily_finviz_DF['FinVizTarget']-daily_finviz_DF['FinVizPrice'])/daily_finviz_DF['FinVizPrice']).round(2)
|
| 124 |
+
daily_finviz_DF_filtered = daily_finviz_DF[['Ticker','Price','FinVizTarget','FinVizTargetpercent']]
|
|
|
|
| 125 |
|
| 126 |
#Merging tipranks with Gurufocus
|
| 127 |
DFgurufocus = daily_gurufocus_DF[['Ticker' , 'GFValue']] # , 'GFValuediff']]
|
| 128 |
DFmerge_tipranks_gurufocus = DFgurufocus.merge(monthly_tiprank_DF)
|
| 129 |
|
| 130 |
#Merging Finviz with Merged last one
|
| 131 |
+
DFmerge_tipranks_gurufocus = DFmerge_tipranks_gurufocus.merge(daily_finviz_DF_filtered)
|
| 132 |
|
| 133 |
if 'Price' in DFmerge_tipranks_gurufocus.columns and 'GFValue' in DFmerge_tipranks_gurufocus.columns:
|
| 134 |
DFmerge_tipranks_gurufocus['GFValuepercent'] = 100* ( DFmerge_tipranks_gurufocus['GFValue'] - DFmerge_tipranks_gurufocus['Price']) / DFmerge_tipranks_gurufocus['Price']
|
|
|
|
| 138 |
DFmerge_tipranks_gurufocus = DFmerge_tipranks_gurufocus.drop(columns=['Market Capitalization'])
|
| 139 |
DFmerge_tipranks_gurufocus = DFmerge_tipranks_gurufocus[['Ticker', 'Sector', 'Industry' , 'MarketCap' , 'SmartScore', 'Price' , 'GFValue' , 'GFValuepercent' , 'FinVizTarget','FinVizTargetpercent']]
|
| 140 |
|
| 141 |
+
|
| 142 |
#widget
|
| 143 |
ticker = pn.widgets.AutocompleteInput(name='Ticker', options=list(DFmerge_tipranks_gurufocus.Ticker) , placeholder='Write Ticker here همین جا',value='ALL', restrict=False)
|
| 144 |
SmartScore = pn.widgets.EditableRangeSlider(name='Smart Score', start=0, end=10, value=(9, 10), step=1)
|
| 145 |
Industry = pn.widgets.CheckBoxGroup( name='Select Industry', value=list(set(DFmerge_tipranks_gurufocus.Industry)), options=list(set(DFmerge_tipranks_gurufocus.Industry)), inline=True)
|
| 146 |
Sector = pn.widgets.CheckBoxGroup( name='Select Sector', value=list(set(DFmerge_tipranks_gurufocus.Sector)), options=list(set(DFmerge_tipranks_gurufocus.Sector)), inline=False)
|
| 147 |
+
GFValuepercent = pn.widgets.FloatSlider(name='GF Value %', start=-100, end=100, step=1, value=12.0)
|
| 148 |
+
FinVizTargetpercent = pn.widgets.FloatSlider(name='FinViz Target %', start=-100, end=100, step=1, value=12.0)
|
| 149 |
MarketCap = pn.widgets.FloatSlider(name='Market Capital (B$)', start=0, end=1000, step=1, value=10)
|
| 150 |
# Create a file download link
|
| 151 |
download_button_GuruFocus = pn.widgets.FileDownload(
|
|
|
|
| 191 |
pn.extension('tabulator')
|
| 192 |
bound_plot = pn.bind(get_DF, DF=DFmerge_tipranks_gurufocus,ticker=ticker,SmartScore=SmartScore,GFValuepercent=GFValuepercent, FinVizTargetpercent=FinVizTargetpercent, Sector=Sector ,MarketCap=MarketCap)
|
| 193 |
|
| 194 |
+
pn.Column(pn.Row(pn.Column(ticker,SmartScore,GFValuepercent, FinVizTargetpercent, MarketCap, Sector, download_button_GuruFocus,download_button_FinViz ,download_button_Filter, download_button_watchlist, png_pane),bound_plot)).servable(title="Fair Value Ranking - Merged Gurufocus & Tipranks & FinViz & MorningStars")
|