# Alpaca Markets news data for price prediction

**URL:** <https://forum.alpaca.markets/t/alpaca-markets-news-data-for-price-prediction/12510>\
**Category:** Alpaca Market Data\
**Created:** [June 8, 2023, 11:00pm UTC](https://forum.alpaca.markets/t/alpaca-markets-news-data-for-price-prediction/12510 "2023-06-08T23:00:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_O1](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@Joe\_O1](https://forum.alpaca.markets/u/Joe_O1)\
**Post date:** [June 8, 2023, 11:00pm UTC](https://forum.alpaca.markets/t/alpaca-markets-news-data-for-price-prediction/12510/1 "2023-06-08T23:00:51Z")

</div>

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

1. **Dependencies** : The script requires several Python libraries including `aiohttp` for asynchronous HTTP requests, `pandas` and `pandas_market_calendars` for data manipulation and market calendars, `beautifulsoup4` for HTML parsing, and `transformers` and `torch` for using the FinBERT model.

2. **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.

3. **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.

4. **Cleaning Data** : The `clean_text` function is used to remove HTML tags and extra whitespaces from the news data.

5. **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’.

6. **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.

```auto
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](https://github.com/JOravetz/IntelliStockAI/blob/main/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.

```auto
git clone https://github.com/JOravetz/IntelliStockAI.git

```

 ![Figure_1](https://us1.discourse-cdn.com/flex020/uploads/alp/original/2X/5/56d728a1f599cca79c2deff3a14a3737db7959bc.png)  
 ![Figure_2](https://us1.discourse-cdn.com/flex020/uploads/alp/original/2X/c/cf58bd5ddd6fc6a08c1729a62537dd0464a86db4.png)
