The provided Python script utilizes the Alpaca API, a financial data and trading API, to fetch news data for a given stock symbol. It then applies a sentiment analysis on the fetched news content using FinBERT, a language model developed by ProsusAI specifically for the financial domain, and stores the results in a pandas DataFrame.
How It Works
-
Dependencies: The script requires several Python libraries including
aiohttp
for asynchronous HTTP requests,pandas
andpandas_market_calendars
for data manipulation and market calendars,beautifulsoup4
for HTML parsing, andtransformers
andtorch
for using the FinBERT model. -
Configuration: Alpaca API keys are fetched from the environment variables. It checks for GPU availability and sets the device accordingly. It initializes a tokenizer and model using FinBERT.
-
Fetching News: It defines several helper functions to fetch the news related to a particular stock symbol within a specified time period. The
fetch_news
function uses the aiohttp library to make an asynchronous request to the Alpaca API and fetches the news data. -
Cleaning Data: The
clean_text
function is used to remove HTML tags and extra whitespaces from the news data. -
Sentiment Analysis: The
get_finbert_sentiment
function uses the FinBERT model to generate sentiment scores for the cleaned text. It returns one of three possible sentiments: ‘positive’, ‘negative’, or ‘neutral’. -
Main Function: The script gets the stock symbol and the number of trading days from the command-line arguments, fetches the news for the stock symbol, cleans the news content, performs sentiment analysis on the news content, and stores the results (the creation time, headline, content, and sentiment of each news item) in a pandas DataFrame.
The script is intended to be run from the command line, and it takes two arguments: the stock symbol and the number of trading days. The resulting DataFrame contains the timestamp, headline, content, and sentiment for each news item.
python fetch_alpaca_news_with_sentiment.py --symbol aapl --ndays 1
GPU is available. GPU: NVIDIA ...
created_at headline content sentiment
0 2023-06-07T17:01:33-04:00 36 Analysts Have This to Say About Apple Within the last quarter, Apple (NASDAQ:AAPL) h... positive
1 2023-06-07T16:34:00-04:00 Apple Acquiring AR Company That Works On Mario... Technology giant Apple Inc (NASDAQ:AAPL) has b... neutral
2 2023-06-07T14:59:42-04:00 Lionel Messi Joins MLS In Surprise Move: Here'... One of the greatest football (soccer) players ... positive
3 2023-06-07T13:35:19-04:00 10 Information Technology Stocks Whale Activit... This whale alert can help traders discover the... positive
4 2023-06-07T13:28:46-04:00 EXCLUSIVE: Top 10 Most-Searched Tickers On Ben... Each trading day features hundreds of headline... positive
5 2023-06-07T07:34:28-04:00 US Stock Market Dips After Double Top Signal, ... The U.S. stock market witnessed a pullback on ... neutral
6 2023-06-07T12:31:16-04:00 Apple vs. Meta: Who Will Win The VR Arms Race ... Just 4 days before Apple (NASDAQ:AAPL) unveile... positive
7 2023-06-07T12:22:01-04:00 How To Ride The Growing Autonomous Vehicles Ma... Tesla Inc (NASDAQ:TSLA) and Apple Inc (NASDAQ:... negative
8 2023-06-07T11:05:17-04:00 California Rep. Nancy Pelosi Sold Up to $1.00M... Representative Nancy Pelosi from California re... neutral
9 2023-06-07T10:30:18-04:00 Nancy Pelosi Unloads 2,900 Apple Shares Right ... One of the most prolific members of Congress h... positive
10 2023-06-07T10:02:29-04:00 The S&P Index Has Clearly Broken Through 4200 ... Not to toot my own horn, but I first wrote abo... positive
11 2023-06-07T09:27:15-04:00 Market Clubhouse Morning Memo - June 7th, 2023... Good Morning Traders! In today's Market Clubho... positive
12 2023-06-07T06:50:24-04:00 Bullish On iPhone 15 Cycle, Wedbush's Ives Fur... Wedbush analyst Daniel Ives believes that Appl... negative
13 2023-06-07T06:19:39-04:00 Market Volatility Drops Ahead Of Inflation Dat... The S&P 500 settled higher on Monday, as inves... neutral
14 2023-06-07T05:50:00-04:00 Salesforce Tries To Lure Workers Back To Offic... Bringing employees back to the office after th... neutral
15 2023-06-06T08:45:12-04:00 Is Apple's Vision Pro Headset Worth It? Top Te... On Monday, during its annual Worldwide Develop... positive
16 2023-06-07T03:13:57-04:00 Tim Cook Admits ChatGPT Excites Him But Says A... Apple Inc. (NASDAQ:AAPL) CEO Tim Cook recently... positive
17 2023-06-07T02:41:02-04:00 A Boxed Original iPhone Or S&P 500: Which Inve... Apple, Inc. (NASDAQ:AAPL) on Monday unveiled t... neutral
18 2023-06-06T23:27:53-04:00 Mark Cuban Dishes Out Entrepreneurial Advice C... Apple Inc. (NASDAQ:AAPL) has unveiled its high... positive
19 2023-06-06T22:47:09-04:00 Bitcoin Regains $27K, Ethereum, Dogecoin Soar ... Cryptocurrency investors saw a much-needed res... negative
The sample code is located here: fetch_alpaca_news_with_sentiment.py
If you’re interested in how news sentiment analysis can be combined with technical analysis to predict close prices, using an LSTM RNN model, please clone the following repository and execute the example code. Please see the README for additional information.
git clone https://github.com/JOravetz/IntelliStockAI.git