AmirTrader commited on
Commit
b0d54d8
·
verified ·
1 Parent(s): 33b5c07

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -0
app.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
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="bootstrap")
26
+
27
+
28
+ # from datasets import load_dataset
29
+ from utils import load_hf_dataset # ,upload_to_hf_dataset, download_from_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
+
44
+
45
+ @pn.cache()
46
+ def get_tradingview(current_datetime):
47
+ # Load lastest TradingView DataSet from HuggingFace Dataset which is always america.csv
48
+ # download_from_hf_dataset("america.csv", dataset_name_TradingView_input, HF_TOKEN)
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(' `Market Capitalization`>10e9').Ticker)
58
+
59
+ ticker1 ="CRM"
60
+ ticker2= "MSFT"
61
+
62
+ ticker1_widget = pn.widgets.AutocompleteInput(
63
+ name="Ticker A",
64
+ options=ticker_list,
65
+ placeholder="Write Ticker here همین جا",
66
+ value=f"{ticker1}",
67
+ restrict=False,
68
+ )
69
+
70
+ ticker2_widget = pn.widgets.AutocompleteInput(
71
+ name="Ticker B",
72
+ options=ticker_list,
73
+ placeholder="Write Ticker here همین جا",
74
+ value=f"{ticker2}",
75
+ restrict=False,
76
+ )
77
+
78
+ m_widget = pn.widgets.IntSlider(
79
+ name='Window Size (m)',
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 ="Start Date",
90
+ description='Select a Date',
91
+ start= date(2000, 1, 1)
92
+ )
93
+
94
+ date_end = pn.widgets.DatePicker(
95
+ name ="End Date",# value=datetime(2000, 1, 1),
96
+ description='Select a Date',
97
+ end= date.today() #date(2023, 9, 1)
98
+ )
99
+
100
+ date_start.value = date(2000,1,1)
101
+ date_end.value = date.today()
102
+
103
+ def get_plot(ticker1,ticker2, m_widget, date_start , date_end):
104
+
105
+ DF1 = yf.Ticker(ticker1).history(start=date_start , end=date_end); DF1 = DF1.resample('5D').mean(); DF1['Date'] = DF1.index
106
+ DF2 = yf.Ticker(ticker2).history(start=date_start , end=date_end); DF2 = DF2.resample('5D').mean(); DF2['Date'] = DF2.index
107
+
108
+ m = 200
109
+ m = m_widget
110
+ varcol = 'Close'
111
+ mp = stumpy.stump(T_A = DF1[varcol],
112
+ m = m,
113
+ T_B = DF2[varcol],
114
+ ignore_trivial = False)
115
+
116
+ ticker1_motif_index = mp[:, 0].argmin()
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
+ plt1 = hv.Curve( DF1.iloc[ticker1_motif_index : ticker1_motif_index + m][varcol].values, label=f'{ticker1}')
124
+ plt2 = hv.Curve(DF2.iloc[ticker2_motif_index:ticker2_motif_index+m][varcol].values, label=f'{ticker2}')
125
+
126
+
127
+ plt3_1 = hv.Curve(DF1[varcol])
128
+ # plt3_2= hv.Curve(DF1.iloc[ticker1_motif_index : ticker1_motif_index + m ,-1].values , DF1.iloc[ticker1_motif_index : ticker1_motif_index + m][varcol].values)
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='Date', y=varcol, title=ticker1).opts(width=500, height=400)
135
+ motif1 = DF1.iloc[ticker1_motif_index: ticker1_motif_index + m]
136
+ motif1_plot = motif1.hvplot.line( y=varcol, color='red')
137
+ combined_plot1 = plot1 * motif1_plot # Overlay the motif on the main plot
138
+
139
+ # # Plot for DF2
140
+ plot2 = DF2.hvplot.line(x='Date', y=varcol, title=ticker2).opts(width=500, height=400)
141
+ motif2 = DF2.iloc[ticker2_motif_index: ticker2_motif_index + m]
142
+ motif2_plot = motif2.hvplot.line( y=varcol, color='red')
143
+ combined_plot2 = plot2 * motif2_plot # Overlay the motif on the main plot
144
+
145
+
146
+ # return pn.Row(plt1*plt2 , pn.Column(plt3_1.opts(width=800, height=400) ,plt4_1.opts(width=800, height=400) ))
147
+ return pn.Row(plt1*plt2 , pn.Column(combined_plot1.opts(width=800, height=400) ,combined_plot2.opts(width=800, height=400) ))
148
+
149
+
150
+ pn.Row( pn.Column(ticker1_widget, ticker2_widget, m_widget, date_start , date_end),
151
+ pn.bind(get_plot, ticker1_widget, ticker2_widget , m_widget, date_start , date_end)).servable(title="Find Similarity between two stocks")
152
+