Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,34 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
from datetime import datetime, date
|
| 5 |
|
| 6 |
import yfinance as yf
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
import os
|
| 11 |
-
|
| 12 |
from dotenv import load_dotenv
|
| 13 |
|
| 14 |
import holoviews as hv
|
| 15 |
import hvplot.pandas # noqa
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
import hvplot.pandas # Provides hvplot method for DataFrames
|
| 19 |
-
import holoviews as hv
|
| 20 |
-
from holoviews import opts
|
| 21 |
import panel as pn
|
| 22 |
-
|
| 23 |
from holoviews.plotting.links import RangeToolLink
|
| 24 |
|
| 25 |
-
pn.extension("bokeh", template="
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
| 29 |
-
from utils import load_hf_dataset
|
| 30 |
|
| 31 |
import stumpy
|
| 32 |
# import matplotlib.pyplot as plt
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
# Load environment variables from .env file
|
| 37 |
load_dotenv()
|
| 38 |
|
| 39 |
# Get the Hugging Face API token from the environment; either set in .env file or in the environment directly in GitHub
|
| 40 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
| 41 |
# Get the name of the GuruFocus dataset for TradingView, Finviz, MarketBeat and TipRanks to read from
|
| 42 |
dataset_name_TradingView_input = os.getenv("dataset_name_TradingView_input")
|
| 43 |
|
|
@@ -49,15 +40,15 @@ def get_tradingview(current_datetime):
|
|
| 49 |
return load_hf_dataset("america.csv", HF_TOKEN, dataset_name_TradingView_input)
|
| 50 |
|
| 51 |
|
| 52 |
-
|
| 53 |
# Reading gurufocus,finviz from crawling pipelines with GitHub Action
|
| 54 |
current_datetime = datetime.now().strftime("%Y-%m-%d")
|
| 55 |
|
| 56 |
DF = get_tradingview(current_datetime)
|
| 57 |
-
ticker_list = list(DF.query(
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
ticker1_widget = pn.widgets.AutocompleteInput(
|
| 63 |
name="Ticker A",
|
|
@@ -76,77 +67,91 @@ ticker2_widget = pn.widgets.AutocompleteInput(
|
|
| 76 |
)
|
| 77 |
|
| 78 |
m_widget = pn.widgets.IntSlider(
|
| 79 |
-
name=
|
| 80 |
-
value=200,
|
| 81 |
-
start=50,
|
| 82 |
-
end=400,
|
| 83 |
-
step=10
|
| 84 |
)
|
| 85 |
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# Create a DatePicker widget with a minimum date of 2000-01-01
|
| 88 |
date_start = pn.widgets.DatePicker(
|
| 89 |
-
name
|
| 90 |
-
description='Select a Date',
|
| 91 |
-
start= date(2000, 1, 1)
|
| 92 |
)
|
| 93 |
|
| 94 |
date_end = pn.widgets.DatePicker(
|
| 95 |
-
name
|
| 96 |
-
description=
|
| 97 |
-
end=
|
| 98 |
)
|
| 99 |
|
| 100 |
-
date_start.value = date(2000,1,1)
|
| 101 |
date_end.value = date.today()
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
print(f'The motif is located at index {ticker1_motif_index} of "{ticker1}"')
|
| 118 |
|
| 119 |
ticker2_motif_index = mp[ticker1_motif_index, 1]
|
| 120 |
print(f'The motif is located at index {ticker2_motif_index} of "{ticker2}"')
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
plt4_1 = hv.Curve(DF2[varcol])
|
| 131 |
-
# plt4_2=hv.Curve(DF2.iloc[ticker2_motif_index:ticker2_motif_index+m ,-1].values , DF2.iloc[ticker2_motif_index:ticker2_motif_index+m][varcol].values)
|
| 132 |
|
| 133 |
# Plot for DF1
|
| 134 |
-
plot1 = DF1.hvplot.line(x=
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
combined_plot1 = plot1 * motif1_plot # Overlay the motif on the main plot
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
plot2 = DF2.hvplot.line(x=
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
combined_plot2 = plot2 * motif2_plot # Overlay the motif on the main plot
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
|
| 6 |
from datetime import datetime, date
|
| 7 |
|
| 8 |
import yfinance as yf
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
|
| 12 |
import holoviews as hv
|
| 13 |
import hvplot.pandas # noqa
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
import panel as pn
|
|
|
|
| 15 |
from holoviews.plotting.links import RangeToolLink
|
| 16 |
|
| 17 |
+
pn.extension("bokeh", template="fast")
|
|
|
|
| 18 |
|
| 19 |
+
from datasets import load_dataset
|
| 20 |
+
from utils import load_hf_dataset, upload_to_hf_dataset, download_from_hf_dataset
|
| 21 |
|
| 22 |
import stumpy
|
| 23 |
# import matplotlib.pyplot as plt
|
| 24 |
|
| 25 |
+
### Preparting the data
|
|
|
|
| 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 |
|
|
|
|
| 40 |
return load_hf_dataset("america.csv", HF_TOKEN, dataset_name_TradingView_input)
|
| 41 |
|
| 42 |
|
|
|
|
| 43 |
# Reading gurufocus,finviz from crawling pipelines with GitHub Action
|
| 44 |
current_datetime = datetime.now().strftime("%Y-%m-%d")
|
| 45 |
|
| 46 |
DF = get_tradingview(current_datetime)
|
| 47 |
+
ticker_list = list(DF.query(" `Market Capitalization`>10e9").Ticker)
|
| 48 |
|
| 49 |
+
# Widgets
|
| 50 |
+
ticker1 = "CRM"
|
| 51 |
+
ticker2 = "MSFT"
|
| 52 |
|
| 53 |
ticker1_widget = pn.widgets.AutocompleteInput(
|
| 54 |
name="Ticker A",
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
m_widget = pn.widgets.IntSlider(
|
| 70 |
+
name="Window Size (m)", value=250, start=5, end=400, step=10
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
)
|
| 72 |
|
| 73 |
+
similarity_rank_widget = pn.widgets.IntSlider(
|
| 74 |
+
name="Similarity Rank", value=0, start=0, end=50, step=1
|
| 75 |
+
)
|
| 76 |
|
| 77 |
# Create a DatePicker widget with a minimum date of 2000-01-01
|
| 78 |
date_start = pn.widgets.DatePicker(
|
| 79 |
+
name="Start Date", description="Select a Date", start=date(2000, 1, 1)
|
|
|
|
|
|
|
| 80 |
)
|
| 81 |
|
| 82 |
date_end = pn.widgets.DatePicker(
|
| 83 |
+
name="End Date", # value=datetime(2000, 1, 1),
|
| 84 |
+
description="Select a Date",
|
| 85 |
+
end=date.today(), # date(2023, 9, 1)
|
| 86 |
)
|
| 87 |
|
| 88 |
+
date_start.value = date(2000, 1, 1)
|
| 89 |
date_end.value = date.today()
|
| 90 |
|
| 91 |
+
@pn.cache()
|
| 92 |
+
def get_DF(ticker1, date_start, date_end):
|
| 93 |
+
DF = yf.Ticker(ticker1).history(start=date_start, end=date_end)
|
| 94 |
+
return DF
|
| 95 |
+
|
| 96 |
+
def get_plot(ticker1, ticker2, m_widget, similarity_rank_widget, date_start, date_end):
|
| 97 |
+
DF1 = get_DF(ticker1, date_start, date_end)
|
| 98 |
+
DF1 = DF1.resample("5D").mean()
|
| 99 |
+
DF1["Date"] = DF1.index
|
| 100 |
+
DF2 = get_DF(ticker2, date_start, date_end)
|
| 101 |
+
DF2 = DF2.resample("5D").mean()
|
| 102 |
+
DF2["Date"] = DF2.index
|
| 103 |
+
|
| 104 |
+
m = m_widget # m = 200
|
| 105 |
+
varcol = "Close"
|
| 106 |
+
mp = stumpy.stump(T_A=DF1[varcol], m=m, T_B=DF2[varcol], ignore_trivial=True)
|
| 107 |
+
|
| 108 |
+
# ticker1_motif_index = mp[:, 0].argmin()
|
| 109 |
+
ticker1_motif_index = np.argpartition(mp[:, 0],similarity_rank_widget)[similarity_rank_widget]
|
| 110 |
print(f'The motif is located at index {ticker1_motif_index} of "{ticker1}"')
|
| 111 |
|
| 112 |
ticker2_motif_index = mp[ticker1_motif_index, 1]
|
| 113 |
print(f'The motif is located at index {ticker2_motif_index} of "{ticker2}"')
|
| 114 |
|
| 115 |
+
plt1 = hv.Curve(
|
| 116 |
+
DF1.iloc[ticker1_motif_index : ticker1_motif_index + m][varcol].values,
|
| 117 |
+
label=f"{ticker1}",
|
| 118 |
+
)
|
| 119 |
+
plt2 = hv.Curve(
|
| 120 |
+
DF2.iloc[ticker2_motif_index : ticker2_motif_index + m][varcol].values,
|
| 121 |
+
label=f"{ticker2}",
|
| 122 |
+
)
|
|
|
|
|
|
|
| 123 |
|
| 124 |
# Plot for DF1
|
| 125 |
+
plot1 = DF1.hvplot.line(x="Date", y=varcol, title=ticker1).opts(
|
| 126 |
+
width=500,
|
| 127 |
+
height=400,
|
| 128 |
+
show_grid=True,
|
| 129 |
+
ylim=(DF1[varcol].min(), DF1[varcol].max()),
|
| 130 |
+
)
|
| 131 |
+
motif1 = DF1.iloc[ticker1_motif_index : ticker1_motif_index + m]
|
| 132 |
+
motif1_plot = motif1.hvplot.line(y=varcol, color="red")
|
| 133 |
combined_plot1 = plot1 * motif1_plot # Overlay the motif on the main plot
|
| 134 |
|
| 135 |
+
# Plot for DF2
|
| 136 |
+
plot2 = DF2.hvplot.line(x="Date", y=varcol, title=ticker2).opts(
|
| 137 |
+
width=500,
|
| 138 |
+
height=400,
|
| 139 |
+
show_grid=True,
|
| 140 |
+
ylim=(DF1[varcol].min(), DF1[varcol].max()),
|
| 141 |
+
)
|
| 142 |
+
motif2 = DF2.iloc[ticker2_motif_index : ticker2_motif_index + m]
|
| 143 |
+
motif2_plot = motif2.hvplot.line(y=varcol, color="red")
|
| 144 |
combined_plot2 = plot2 * motif2_plot # Overlay the motif on the main plot
|
| 145 |
|
| 146 |
+
return pn.Row(
|
| 147 |
+
plt1 * plt2.opts(width=500, height=400),
|
| 148 |
+
pn.Column(
|
| 149 |
+
combined_plot1.opts(width=800, height=400),
|
| 150 |
+
combined_plot2.opts(width=800, height=400),
|
| 151 |
+
),
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
pn.Row(
|
| 155 |
+
pn.Column(ticker1_widget, ticker2_widget, m_widget, similarity_rank_widget, date_start, date_end),
|
| 156 |
+
pn.bind(get_plot, ticker1_widget, ticker2_widget, m_widget, similarity_rank_widget, date_start, date_end),
|
| 157 |
+
).servable(title="Find Similarity in Stock Price Patterns")
|