How to get Historic news C#

I have been struggling with getting historic news requests to work so I thought I would post what I had to do. Fair warning its ugly but it seems to get the info. If anyone has a better solution please let me know.

            String KEY_ID = "enteryourID";
            String SECRET_KEY = "EnteryourKey";

List<string> symbols = new List<string>(); // eventually I would pull my full list of symbols

            DateTime nowTime = DateTime.Now.ToUniversalTime();
            DateTime pastTime = DateTime.Now.AddDays(-4).ToUniversalTime();
            TimeSpan newsTime = new TimeSpan();

            Alpaca.Markets.Interval<DateTime> newsInt = new Alpaca.Markets.Interval<DateTime>(pastTime, nowTime);

            var newsArticleRequest = new NewsArticlesRequest(symbols);
            newsArticleRequest.ExcludeItemsWithoutContent = false;
            newsArticleRequest.SendFullContentForItems = true;
            newsArticleRequest.TimeInterval = newsInt;

            var newsClient = Alpaca.Markets.Environments.Live.GetAlpacaDataClient(new SecretKey(KEY_ID, SECRET_KEY)).ListNewsArticlesAsync(newsArticleRequest);

 var cancellationTokenSource = new CancellationTokenSource();

await foreach (var article in newsClient.ToAsyncEnumerable())
                 foreach(var item in article.Items) 
                        Console.WriteLine(item.Source);  //Etc. etc. untill you have all data
                catch(Exception ex) 
                    Console.WriteLine("prior news errror: " + ex);


If you get the “Bad Request” exception executing your code it’s not your problem but instead, the .NET SDK minor issue that will be fixed in the next service release. I’ve tested your scenario on my side using the next script and it works fine:

using Alpaca.Markets;
using Alpaca.Markets.Extensions;

    var key = new SecretKey("...", "...");
    using var client = Environments.Paper.GetAlpacaDataClient(key);

    var request = new NewsArticlesRequest
        TimeInterval = new Interval<DateTime>().WithFrom(DateTime.Today.AddDays(-4))

    await foreach (var article in client.GetNewsArticlesAsAsyncEnumerable(request))
catch (Exception e)

There are two important differences between my code and your original snippet:

  • The WithPageSize method sets the news request page size to a value of 50 instead of the old hard-coded default value of 100 used by the SDK package.
  • I use the Alpaca.Markets.Extensions NuGet package - it provides a set of useful methods for handling paginated responses in a truly asynchronous manner.

Appreciate the feedback. I just wanted to post something that works as people keep asking for examples thought I would contribute.

1 Like