AmirTrader commited on
Commit
8c62d52
·
1 Parent(s): 25e554f
Files changed (1) hide show
  1. app.py +109 -0
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import panel as pn
3
+ from datetime import datetime
4
+ from datetime import date
5
+ pn.extension('bokeh', template='bootstrap')
6
+ import hvplot.pandas
7
+
8
+ import yfinance as yf
9
+
10
+ @pn.cache
11
+ def getPolygonDF(ticker , startdate , enddate , window, window2):
12
+ import time
13
+ #My Polygon API
14
+ mypolgonAPI = "wYB9CMZDYB6wL0ycZN33yjQ20Q0xVFlY"
15
+ headers = {"Authorization": f"Bearer {mypolgonAPI}"}
16
+
17
+ import pandas as pd
18
+ import requests
19
+ import json
20
+
21
+ interval = "minute"
22
+ period = "5"
23
+ limit = 50000
24
+
25
+
26
+ dflst = []
27
+ nexturl = f"https://api.polygon.io/v2/aggs/ticker/{ticker}/range/{period}/{interval}/{startdate}/{enddate}?adjusted=true&sort=asc&limit={limit}"
28
+ while True:
29
+ r1 = requests.get(nexturl , headers=headers)
30
+ df = pd.DataFrame(json.loads(r1.text)["results"])
31
+ df['UNIXTIME'] = pd.to_datetime(df['t'], unit='ms', utc=True).map(lambda x: x.tz_convert('America/New_York'))
32
+ dflst.append(df)
33
+ print(df.shape)
34
+ time.sleep(15)
35
+ try:
36
+ nexturl = json.loads(r1.text)["next_url"]
37
+ except:
38
+ break
39
+ DF = pd.concat(dflst)
40
+ DF['SMA'] = DF.Close.rolling(window=window).mean()
41
+ DF['SMA2'] = DF.Close.rolling(window=window2).mean()
42
+ DF = DF.reset_index()
43
+
44
+ def get_hvplot(ticker , startdate , enddate , interval,window,window2):
45
+ DF = get_df(ticker , startdate=startdate , enddate=enddate , interval=interval,window=window,window2=window2)
46
+
47
+ import hvplot.pandas # Ensure hvplot is installed (pip install hvplot)
48
+ from sklearn.linear_model import LinearRegression
49
+ import holoviews as hv
50
+ hv.extension('bokeh')
51
+ # Assuming your dataframe is named 'df' with columns 'Date' and 'Close'
52
+ # If not, replace 'Date' and 'Close' with your actual column names.
53
+
54
+ # Step 1: Create a scatter plot using hvplot
55
+ scatter_plot = DF.hvplot(x='Date', y='Close', kind='scatter',title=f'{ticker} Close vs. Date')
56
+
57
+ # Step 2: Fit a linear regression model
58
+ DF['Date2'] = pd.to_numeric(DF['Date'])
59
+ X = DF[['Date2']]
60
+ y = DF[['Close']] #.values
61
+ model = LinearRegression().fit(X, y)
62
+
63
+ # # Step 3: Predict using the linear regression model
64
+ DF['Predicted_Close'] = model.predict(X)
65
+
66
+ # # Step 4: Create a line plot for linear regression
67
+ line_plot = DF.hvplot(x='Date', y='Predicted_Close', kind='line',line_dash='dashed', color='red')
68
+ line_plot_SMA = DF.hvplot(x='Date', y='SMA', kind='line',line_dash='dashed', color='orange')
69
+ line_plot_SMA2 = DF.hvplot(x='Date', y='SMA2', kind='line',line_dash='dashed', color='orange')
70
+
71
+ # # Step 5: Overlay scatter plot and linear regression line
72
+ # return (scatter_plot * line_plot).opts(width=800, height=600, show_grid=True, gridstyle={ 'grid_line_color': 'gray'})
73
+ # grid_style = {'grid_line_color': 'black'}#, 'grid_line_width': 1.5, 'ygrid_bounds': (0.3, 0.7),'minor_xgrid_line_color': 'lightgray', 'xgrid_line_dash': [4, 4]}
74
+ return (scatter_plot * line_plot *line_plot_SMA *line_plot_SMA2).opts(width=800, height=600, show_grid=True)
75
+
76
+
77
+ tickers = ['AAPL', 'META', 'GOOG', 'IBM', 'MSFT','NKE','DLTR','DG']
78
+ # ticker = pn.widgets.Select(name='Ticker', options=tickers)
79
+
80
+ # tickers = pd.read_csv('tickers.csv').Ticker.to_list()
81
+ ticker = pn.widgets.AutocompleteInput(name='Ticker', options=tickers , placeholder='Write Ticker here همین جا')
82
+ ticker.value = "AAPL"
83
+ window = pn.widgets.IntSlider(name='Window Size', value=50, start=5, end=1000, step=5)
84
+ window2 = pn.widgets.IntSlider(name='Window Size2', value=150, start=5, end=1000, step=5)
85
+
86
+ # Create a DatePicker widget with a minimum date of 2000-01-01
87
+ date_start = pn.widgets.DatePicker(
88
+ name ="Start Date",
89
+ description='Select a Date',
90
+ start= date(2023, 1, 1)
91
+ )
92
+
93
+ date_end = pn.widgets.DatePicker(
94
+ name ="End Date",# value=datetime(2000, 1, 1),
95
+ description='Select a Date',
96
+ end= date(2023, 1, 3) #date.today() #date(2023, 9, 1)
97
+ )
98
+
99
+ date_start.value = date(2023,1,1)
100
+ date_end.value = date(2023, 1, 3) # date.today()
101
+
102
+ pn.Row(
103
+ pn.Column( ticker, window , window2, date_start , date_end),
104
+ # pn.bind(calc_fairprice_CDF,ticker),
105
+ # pn.bind(calc_fairprice_DnetP,ticker)),
106
+ # pn.panel(pn.bind(get_hvplot, ticker, "2010-01-01","2023-09-01","1d")) #, sizing_mode='stretch_width')
107
+ pn.panel(pn.bind(get_hvplot, ticker, date_start , date_end,"1d",window,window2)), #, sizing_mode='stretch_width')
108
+ # pn.panel(pn.bind(get_income_hvplot, ticker)) #, sizing_mode='stretch_width')
109
+ ).servable(title="Under Valued Screener- Linear Regression")