This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Imports | |
from pandas_datareader import data as pdr | |
from pandas import ExcelWriter | |
import yfinance as yf | |
import pandas as pd | |
import datetime | |
import time | |
import requests | |
yf.pdr_override() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Index Returns | |
index_df = pdr.get_data_yahoo(index_name, start_date, end_date) | |
index_df['Percent Change'] = index_df['Adj Close'].pct_change() | |
index_return = (index_df['Percent Change'] + 1).cumprod()[-1] | |
# Find top 30% performing stocks (relative to the S&P 500) | |
for ticker in tickers: | |
# Download historical data as CSV for each stock (makes the process faster) | |
df = pdr.get_data_yahoo(ticker, start_date, end_date) | |
df.to_csv(f'{ticker}.csv') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Checking Minervini conditions of top 30% of stocks in given list | |
rs_stocks = rs_df['Ticker'] | |
for stock in rs_stocks: | |
try: | |
df = pd.read_csv(f'{stock}.csv', index_col=0) | |
sma = [50, 150, 200] | |
for x in sma: | |
df["SMA_"+str(x)] = round(df['Adj Close'].rolling(window=x).mean(), 2) | |
# Storing required values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print(exportList) | |
writer = ExcelWriter("ScreenOutput.xlsx") | |
exportList.to_excel(writer, "Sheet1") | |
writer.save() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import pandas as pd | |
from yahoo_fin import stock_info as si | |
from pandas_datareader import DataReader | |
import numpy as np | |
tickers = si.tickers_sp500() | |
recommendations = [] | |
for ticker in tickers: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Imports | |
from pandas_datareader import data as pdr | |
from pandas import ExcelWriter | |
import yfinance as yf | |
import pandas as pd | |
import datetime | |
import time | |
import requests | |
yf.pdr_override() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import libraries | |
import pandas as pd | |
from bs4 import BeautifulSoup | |
import matplotlib.pyplot as plt | |
from urllib.request import urlopen | |
from urllib.request import Request | |
from nltk.sentiment.vader import SentimentIntensityAnalyzer | |
# Parameters | |
n = 3 #the # of article headlines displayed per ticker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get Data | |
finviz_url = 'https://finviz.com/quote.ashx?t=' | |
news_tables = {} | |
for ticker in tickers: | |
url = finviz_url + ticker | |
req = Request(url=url,headers={'user-agent': 'my-app/0.0.1'}) | |
resp = urlopen(req) | |
html = BeautifulSoup(resp, features="lxml") | |
news_table = html.find(id='news-table') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Iterate through the news | |
parsed_news = [] | |
for file_name, news_table in news_tables.items(): | |
for x in news_table.findAll('tr'): | |
text = x.a.get_text() | |
date_scrape = x.td.text.split() | |
if len(date_scrape) == 1: | |
time = date_scrape[0] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sentiment Analysis | |
analyzer = SentimentIntensityAnalyzer() | |
columns = ['Ticker', 'Date', 'Time', 'Headline'] | |
news = pd.DataFrame(parsed_news, columns=columns) | |
scores = news['Headline'].apply(analyzer.polarity_scores).tolist() | |
df_scores = pd.DataFrame(scores) | |
news = news.join(df_scores, rsuffix='_right') |
OlderNewer