Find_Similarity / app.py
AmirTrader's picture
Update app.py
dde9f52 verified
import os
import numpy as np
import pandas as pd
from datetime import datetime, date
import yfinance as yf
from dotenv import load_dotenv
import holoviews as hv
import hvplot.pandas # noqa
import panel as pn
from holoviews.plotting.links import RangeToolLink
pn.extension("bokeh", template="fast")
from datasets import load_dataset
from utils import load_hf_dataset, upload_to_hf_dataset, download_from_hf_dataset
import stumpy
# import matplotlib.pyplot as plt
### Preparting the data
# 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)
# Reading gurufocus,finviz from crawling pipelines with GitHub Action
current_datetime = datetime.now().strftime("%Y-%m-%d")
DF = get_tradingview(current_datetime)
ticker_list = list(DF.query(" `Market Capitalization`>10e9").Ticker)
# Widgets
ticker1 = "CRM"
ticker2 = "MSFT"
ticker1_widget = pn.widgets.AutocompleteInput(
name="Ticker A",
options=ticker_list,
placeholder="Write Ticker here همین جا",
value=f"{ticker1}",
restrict=False,
)
ticker2_widget = pn.widgets.AutocompleteInput(
name="Ticker B",
options=ticker_list,
placeholder="Write Ticker here همین جا",
value=f"{ticker2}",
restrict=False,
)
m_widget = pn.widgets.IntSlider(
name="Window Size (m)", value=250, start=5, end=400, step=10
)
similarity_rank_widget = pn.widgets.IntSlider(
name="Similarity Rank", value=0, start=0, end=50, step=1
)
# Create a DatePicker widget with a minimum date of 2000-01-01
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(2000, 1, 1)
date_end.value = date.today()
@pn.cache()
def get_DF(ticker1, date_start, date_end):
DF = yf.Ticker(ticker1).history(start=date_start, end=date_end)
return DF
def get_plot(ticker1, ticker2, m_widget, similarity_rank_widget, date_start, date_end):
DF1 = get_DF(ticker1, date_start, date_end)
DF1 = DF1.resample("5D").mean()
DF1["Date"] = DF1.index
DF2 = get_DF(ticker2, date_start, date_end)
DF2 = DF2.resample("5D").mean()
DF2["Date"] = DF2.index
m = m_widget # m = 200
varcol = "Close"
mp = stumpy.stump(T_A=DF1[varcol], m=m, T_B=DF2[varcol], ignore_trivial=True)
# ticker1_motif_index = mp[:, 0].argmin()
ticker1_motif_index = np.argpartition(mp[:, 0],similarity_rank_widget)[similarity_rank_widget]
print(f'The motif is located at index {ticker1_motif_index} of "{ticker1}"')
ticker2_motif_index = mp[ticker1_motif_index, 1]
print(f'The motif is located at index {ticker2_motif_index} of "{ticker2}"')
plt1 = hv.Curve(
DF1.iloc[ticker1_motif_index : ticker1_motif_index + m][varcol].values,
label=f"{ticker1}",
)
plt2 = hv.Curve(
DF2.iloc[ticker2_motif_index : ticker2_motif_index + m][varcol].values,
label=f"{ticker2}",
)
# Plot for DF1
plot1 = DF1.hvplot.line(x="Date", y=varcol, title=ticker1).opts(
width=500,
height=400,
show_grid=True,
ylim=(DF1[varcol].min(), DF1[varcol].max()),
)
motif1 = DF1.iloc[ticker1_motif_index : ticker1_motif_index + m]
motif1_plot = motif1.hvplot.line(y=varcol, color="red")
combined_plot1 = plot1 * motif1_plot # Overlay the motif on the main plot
# Plot for DF2
plot2 = DF2.hvplot.line(x="Date", y=varcol, title=ticker2).opts(
width=500,
height=400,
show_grid=True,
ylim=(DF2[varcol].min(), DF2[varcol].max()),
)
motif2 = DF2.iloc[ticker2_motif_index : ticker2_motif_index + m]
motif2_plot = motif2.hvplot.line(y=varcol, color="red")
combined_plot2 = plot2 * motif2_plot # Overlay the motif on the main plot
return pn.Row(
plt1 * plt2.opts(width=500, height=400),
pn.Column(
combined_plot1.opts(width=800, height=400),
combined_plot2.opts(width=800, height=400),
),
)
pn.Row(
pn.Column(ticker1_widget, ticker2_widget, m_widget, similarity_rank_widget, date_start, date_end),
pn.bind(get_plot, ticker1_widget, ticker2_widget, m_widget, similarity_rank_widget, date_start, date_end),
).servable(title="Find Similarity in Stock Price Patterns")