Spaces:
Running
Running
Commit
·
d2e4d0a
1
Parent(s):
c0666a4
add sma and all tickers
Browse files
app.py
CHANGED
|
@@ -1,19 +1,26 @@
|
|
| 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import yfinance as yf
|
| 4 |
import panel as pn
|
| 5 |
|
| 6 |
@pn.cache
|
| 7 |
-
def get_df(ticker, startdate , enddate , interval="1d"):
|
| 8 |
# interval="1d"
|
| 9 |
# get_df(ticker ="PG", startdate="2000-01-01" , enddate="2023-09-01" , interval="1d")
|
| 10 |
DF = yf.Ticker(ticker).history(start=startdate,end=enddate,interval=interval)
|
| 11 |
-
DF['
|
| 12 |
DF = DF.reset_index()
|
| 13 |
return DF
|
| 14 |
|
| 15 |
-
def get_hvplot(ticker , startdate , enddate , interval):
|
| 16 |
-
DF = get_df(ticker , startdate=startdate , enddate=enddate , interval=interval)
|
| 17 |
|
| 18 |
import hvplot.pandas # Ensure hvplot is installed (pip install hvplot)
|
| 19 |
from sklearn.linear_model import LinearRegression
|
|
@@ -36,23 +43,22 @@ def get_hvplot(ticker , startdate , enddate , interval):
|
|
| 36 |
|
| 37 |
# # Step 4: Create a line plot for linear regression
|
| 38 |
line_plot = DF.hvplot(x='Date', y='Predicted_Close', kind='line',line_dash='dashed', color='red')
|
|
|
|
| 39 |
|
| 40 |
# # Step 5: Overlay scatter plot and linear regression line
|
| 41 |
# return (scatter_plot * line_plot).opts(width=800, height=600, show_grid=True, gridstyle={ 'grid_line_color': 'gray'})
|
| 42 |
# 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]}
|
| 43 |
-
return (scatter_plot * line_plot).opts(width=800, height=600, show_grid=True)
|
| 44 |
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
pn.extension('bokeh', template='bootstrap')
|
| 50 |
-
import hvplot.pandas
|
| 51 |
-
|
| 52 |
-
tickers = ['AAPL', 'META', 'GOOG', 'IBM', 'MSFT','NKE','DLTR','DG']
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Create a DatePicker widget with a minimum date of 2000-01-01
|
| 58 |
date_start = pn.widgets.DatePicker(
|
|
@@ -64,7 +70,7 @@ date_start = pn.widgets.DatePicker(
|
|
| 64 |
date_end = pn.widgets.DatePicker(
|
| 65 |
name ="End Date",# value=datetime(2000, 1, 1),
|
| 66 |
description='Select a Date',
|
| 67 |
-
end= date(2023, 9, 1)
|
| 68 |
)
|
| 69 |
|
| 70 |
date_start.value = date(2010,1,1)
|
|
@@ -73,6 +79,7 @@ date_end.value = date.today()
|
|
| 73 |
pn.Row(
|
| 74 |
pn.Column( ticker, window , date_start , date_end),
|
| 75 |
# pn.panel(pn.bind(get_hvplot, ticker, "2010-01-01","2023-09-01","1d")) #, sizing_mode='stretch_width')
|
| 76 |
-
pn.panel(pn.bind(get_hvplot, ticker, date_start , date_end,"1d")) #, sizing_mode='stretch_width')
|
| 77 |
).servable(title="Under Valued Screener- Linear Regression")
|
| 78 |
|
|
|
|
|
|
| 1 |
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import panel as pn
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from datetime import date
|
| 6 |
+
pn.extension('bokeh', template='bootstrap')
|
| 7 |
+
import hvplot.pandas
|
| 8 |
+
|
| 9 |
import pandas as pd
|
| 10 |
import yfinance as yf
|
| 11 |
import panel as pn
|
| 12 |
|
| 13 |
@pn.cache
|
| 14 |
+
def get_df(ticker, startdate , enddate , interval="1d",window=50):
|
| 15 |
# interval="1d"
|
| 16 |
# get_df(ticker ="PG", startdate="2000-01-01" , enddate="2023-09-01" , interval="1d")
|
| 17 |
DF = yf.Ticker(ticker).history(start=startdate,end=enddate,interval=interval)
|
| 18 |
+
DF['SMA'] = DF.Close.rolling(window=window).mean()
|
| 19 |
DF = DF.reset_index()
|
| 20 |
return DF
|
| 21 |
|
| 22 |
+
def get_hvplot(ticker , startdate , enddate , interval,window):
|
| 23 |
+
DF = get_df(ticker , startdate=startdate , enddate=enddate , interval=interval,window=window)
|
| 24 |
|
| 25 |
import hvplot.pandas # Ensure hvplot is installed (pip install hvplot)
|
| 26 |
from sklearn.linear_model import LinearRegression
|
|
|
|
| 43 |
|
| 44 |
# # Step 4: Create a line plot for linear regression
|
| 45 |
line_plot = DF.hvplot(x='Date', y='Predicted_Close', kind='line',line_dash='dashed', color='red')
|
| 46 |
+
line_plot_SMA = DF.hvplot(x='Date', y='SMA', kind='line',line_dash='dashed', color='orange')
|
| 47 |
|
| 48 |
# # Step 5: Overlay scatter plot and linear regression line
|
| 49 |
# return (scatter_plot * line_plot).opts(width=800, height=600, show_grid=True, gridstyle={ 'grid_line_color': 'gray'})
|
| 50 |
# 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]}
|
| 51 |
+
return (scatter_plot * line_plot *line_plot_SMA).opts(width=800, height=600, show_grid=True)
|
| 52 |
|
| 53 |
|
| 54 |
+
|
| 55 |
+
# tickers = ['AAPL', 'META', 'GOOG', 'IBM', 'MSFT','NKE','DLTR','DG']
|
| 56 |
+
# ticker = pn.widgets.Select(name='Ticker', options=tickers)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
tickers = pd.read_csv('tickers.csv').Ticker.to_list()
|
| 59 |
+
ticker = pn.widgets.AutocompleteInput(name='Ticker', options=tickers , placeholder='Write Ticker here همین جا')
|
| 60 |
+
ticker.value = "AAPL"
|
| 61 |
+
window = pn.widgets.IntSlider(name='Window Size', value=200, start=5, end=1000, step=5)
|
| 62 |
|
| 63 |
# Create a DatePicker widget with a minimum date of 2000-01-01
|
| 64 |
date_start = pn.widgets.DatePicker(
|
|
|
|
| 70 |
date_end = pn.widgets.DatePicker(
|
| 71 |
name ="End Date",# value=datetime(2000, 1, 1),
|
| 72 |
description='Select a Date',
|
| 73 |
+
end= date.today() #date(2023, 9, 1)
|
| 74 |
)
|
| 75 |
|
| 76 |
date_start.value = date(2010,1,1)
|
|
|
|
| 79 |
pn.Row(
|
| 80 |
pn.Column( ticker, window , date_start , date_end),
|
| 81 |
# pn.panel(pn.bind(get_hvplot, ticker, "2010-01-01","2023-09-01","1d")) #, sizing_mode='stretch_width')
|
| 82 |
+
pn.panel(pn.bind(get_hvplot, ticker, date_start , date_end,"1d",window)) #, sizing_mode='stretch_width')
|
| 83 |
).servable(title="Under Valued Screener- Linear Regression")
|
| 84 |
|
| 85 |
+
|