Historical News on Python 3.11

I’m trying to get Historical News data. It appears this only available via alpaca-trade-api.

Is the news API migrating to Alpaca-py?

I’m unable to install alpaca-trade-api on Python 3.11.1

aiohttp/_websocket.c(198): fatal error C1083: Cannot open include file: ‘longintrepr.h’: No such file or directory

How should I access the News API on Python 3.11.1?

@nye I personally feel the alpaca-py SDK is missing things (eg ‘news’), cumbersome and certainly not documented very well. That said, one can actually make any endpoint call. The key is to create a generic RESTClient (the various built in clients such as StockHistoricalDataClient and TradingClient are based on this class). The problem with the built in StockHistoricalDataClient client is that it has “v2” as the endpoint version but the news API uses “v1beta1”. There isn’t a way to override that.

So, to fetch news with the alpaca-py SDK 1) create a “news_client” then 2) use the get method to execute the news endpoint call. Something like this

from alpaca.common.rest import RESTClient

news_client = RESTClient(base_url='https://data.alpaca.markets',
                         api_version='v1beta1',
                         api_key=ALPACA_API_KEY_ID_PAPER, 
                         secret_key=ALPACA_API_SECRET_KEY_PAPER,)

news_endpoint = '/news'
parameters = {'start':'2021-12-28T00:00:00Z',
              'end':'2021-12-31T11:59:59Z',
              'symbols':'AAPL'
}

news = news_client.get(news_endpoint, parameters,)

One could execute any API call like this (there are similar methods for post, patch, put, and delete). The get method ‘parameters’ is just a dictionary of the endpoint parameters you wish to specify. Ensure to put both the parameter name and value in quotes.

Give that a try.

2 Likes

Here is the final method for getting news:

class DataManager(object):
    historicaldata_client: None
    trading_client:None
    rest_client:None

    def __init__(self):
        self.historicaldata_client = StockHistoricalDataClient(config.alpaca_api_key_id, config.alpaca_api_secret_key)
        self.trading_client = TradingClient(config.alpaca_api_key_id, config.alpaca_api_secret_key)
        self.rest_client =RESTClient(base_url='https://data.alpaca.markets',api_version='v1beta1',api_key=config.alpaca_api_key_id, secret_key=config.alpaca_api_secret_key,)

    def get_news(self, symbols:pd.Series, start_datetime:datetime, end_datetime:datetime):
        utc_start_datetime = pd.to_datetime(start_datetime, utc=True)
        utc_end_datetime = pd.to_datetime(end_datetime, utc=True)
        
        news_list = []
        page_token = None
        while True:
            news_endpoint = '/news'
            parameters = {'start':utc_start_datetime.isoformat(),
                        'end':utc_end_datetime.isoformat(),
                        'page_token':page_token,
                        'symbols':symbols.to_list()
            }

            resp = self.rest_client.get(news_endpoint, parameters,)
            page_token = resp.get('next_page_token')
            temp_list = resp.get('news')
 
            news_list.extend(temp_list)
            if not page_token:
                break
            
        return news_list
2 Likes

This only worked for me when I called it with 1 symbol. I did something like this:

Define the symbols, start_datetime, and end_datetime parameters

symbols = pd.Series([“AAPL”])
start_datetime = datetime(2023, 4, 16)
end_datetime = datetime(2023, 4, 17)
news_list = dm.get_news(symbols, start_datetime, end_datetime)
but if I tried:
symbols = pd.Series([“AAPL”,“MSFT”])

I get an error:
APIError: {“message”:“Invalid format for parameter symbols: multiple values for single value parameter ‘symbols’”}