Skip to content

Instantly share code, notes, and snippets.

@marcosan93
Created December 2, 2021 00:50
Show Gist options
  • Select an option

  • Save marcosan93/e0e66cb9483e1027252d0b08533cce19 to your computer and use it in GitHub Desktop.

Select an option

Save marcosan93/e0e66cb9483e1027252d0b08533cce19 to your computer and use it in GitHub Desktop.
def getNewsAndPrices(ticker, days):
"""
Retrieves financial news and price history over the course of a
specified number of days fora given stock ticker.
"""
# List of news
news = []
# How many days back to retrieve
ago = datetime.now() - timedelta(days=days)
# Getting news over the course of a year
for i in tqdm(range(10, days, 10)):
# Grabbing the news
resp = client.get_financial_news(
s=ticker,
from_=(ago+timedelta(days=i-10)).strftime("%Y-%m-%d"),
to=(ago+timedelta(days=i)).strftime("%Y-%m-%d"),
limit=100
)
# Adding to the news list
news.extend(resp)
# Filtering out irrelevant news
lst = [i for i in news if sum(x in {ticker.lower()} for x in nltk.word_tokenize(i['title'].lower()))>=1]
# Formatting the date
news = pd.DataFrame(lst)
news['date'] = news['date'].apply(lambda x: x[:10])
# Getting prices
prices = pd.DataFrame(client.get_prices_eod(ticker, from_=ago.strftime("%Y-%m-%d")))
# Set index
prices = prices.set_index('date', drop=True)
return news, prices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment