Spaces:
Runtime error
Runtime error
| import pandas as pd | |
| import yfinance as yf | |
| import panel as pn | |
| def get_df(ticker, startdate , enddate , interval="1d"): | |
| # interval="1d" | |
| # get_df(ticker ="PG", startdate="2000-01-01" , enddate="2023-09-01" , interval="1d") | |
| DF = yf.Ticker(ticker).history(start=startdate,end=enddate,interval=interval) | |
| DF['SMA50'] = DF.Close.rolling(window=50).mean() | |
| DF = DF.reset_index() | |
| return DF | |
| def get_hvplot(ticker , startdate , enddate , interval): | |
| DF = get_df(ticker , startdate=startdate , enddate=enddate , interval=interval) | |
| import hvplot.pandas # Ensure hvplot is installed (pip install hvplot) | |
| from sklearn.linear_model import LinearRegression | |
| import holoviews as hv | |
| hv.extension('bokeh') | |
| # Assuming your dataframe is named 'df' with columns 'Date' and 'Close' | |
| # If not, replace 'Date' and 'Close' with your actual column names. | |
| # Step 1: Create a scatter plot using hvplot | |
| scatter_plot = DF.hvplot(x='Date', y='Close', kind='scatter',title=f'{ticker} Close vs. Date') | |
| # Step 2: Fit a linear regression model | |
| DF['Date2'] = pd.to_numeric(DF['Date']) | |
| X = DF[['Date2']] | |
| y = DF[['Close']] #.values | |
| model = LinearRegression().fit(X, y) | |
| # # Step 3: Predict using the linear regression model | |
| DF['Predicted_Close'] = model.predict(X) | |
| # # Step 4: Create a line plot for linear regression | |
| line_plot = DF.hvplot(x='Date', y='Predicted_Close', kind='line',line_dash='dashed', color='red') | |
| # # Step 5: Overlay scatter plot and linear regression line | |
| # return (scatter_plot * line_plot).opts(width=800, height=600, show_grid=True, gridstyle={ 'grid_line_color': 'gray'}) | |
| # 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]} | |
| return (scatter_plot * line_plot).opts(width=800, height=600, show_grid=True) | |
| import panel as pn | |
| from datetime import datetime | |
| from datetime import date | |
| pn.extension('bokeh', template='bootstrap') | |
| import hvplot.pandas | |
| tickers = ['AAPL', 'META', 'GOOG', 'IBM', 'MSFT','NKE','DLTR','DG'] | |
| ticker = pn.widgets.Select(name='Ticker', options=tickers) | |
| window = pn.widgets.IntSlider(name='Window Size', value=50, start=1, end=200, step=5) | |
| # 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(2023, 9, 1) | |
| ) | |
| date_start.value = date(2010,1,1) | |
| date_end.value = date.today() | |
| pn.Row( | |
| pn.Column( ticker, window , date_start , date_end), | |
| # pn.panel(pn.bind(get_hvplot, ticker, "2010-01-01","2023-09-01","1d")) #, sizing_mode='stretch_width') | |
| pn.panel(pn.bind(get_hvplot, ticker, date_start , date_end,"1d")) #, sizing_mode='stretch_width') | |
| ).servable(title="Under Valued Screener- Linear Regression") | |