Introducing

Learn How to Download Historical Stock Market Data

May 14, 2024

In this tutorial, we'll guide you through the steps of accessing and downloading historical stock market data from Polygon.io. This will be particularly helpful if you're looking to analyze historical data across stocks, options, indices, forex, or cryptocurrencies.

Historical market data is an invaluable resource. Whether you are backtesting trading strategies, conducting academic research, or developing algorithms for automated trading systems, having access to detailed historical data can significantly enhance your chances of developing a well-rounded idea. It's also crucial for risk management, allowing you to test how your strategies would have performed during past market crashes or volatile conditions.

Now, let's get started by setting up access and exploring different ways to retrieve this data.

Prerequisites

Before diving in, ensure you signup, and have an active paid subscription with Polygon as the Flat Files feature is included in all paid plans. You'll need your login credentials and possibly API keys if you plan to use S3 access.

Web-based File Browser

Our Web-based File Browser is by far the easiest way to access historical market data. You can start browsing without logging in, and no additional tools or downloads are required.

  • Browse: There is no login necessary to navigate our extensive archives, including data for Stocks, Options, Indices, Forex, and Crypto.
  • Details: Immediately see essential details like the number of files, their total size, and the last update dates.
  • Download: Once logged in, you can easily select and download the data files directly to your device with just a few clicks.

This tool is designed to streamline your access to data, offering a straightforward and efficient browsing experience, with secure downloading for logged-in users.

S3 Client Access

S3 access offers flexibility for interactive use, scripting automated jobs, and embedding S3 logic into your software for easy data management. It's compatible with a variety of clients, including the AWS S3 CLI, MinIO, Rclone, and the Python boto3 SDK.

For detailed configuration instructions, please visit our knowledge base article.

For the following examples, we will use the MinIO client. Before configuring your S3 client, you will need to obtain the S3 endpoint, bucket name, and your Access and Secret keys from the Polygon Dashboard. Once you have installed the client and have your credentials, run the following command to create your

.mc/config.json
file configuration.

mc alias set s3polygon https://files.polygon.io Your-AccessKey-Here Your-SecretKey-Here

Now that you have the client configured, let’s explore the top-level buckets or directories to see what is available. Listing operations work for anyone, even without specific subscription access to the Flat Files products, but to actually download a dataset you will need an active subscription.

$ mc ls s3polygon/flatfiles/
[2024-03-05 08:58:06 PST]     0B global_crypto/
[2024-03-05 08:58:06 PST]     0B global_forex/
[2024-03-05 08:58:06 PST]     0B us_indices/
[2024-03-05 08:58:06 PST]     0B us_options_opra/
[2024-03-05 08:58:06 PST]     0B us_stocks_sip/

For instance, if you want to look at the stock aggregates directory, you can list the files in that specific path as shown below:

$ mc ls s3polygon/flatfiles/us_stocks_sip/day_aggs_v1/2024/03/
[2024-03-01 19:00:19 PST] 200KiB STANDARD 2024-03-01.csv.gz
[2024-03-04 19:00:21 PST] 200KiB STANDARD 2024-03-04.csv.gz

You can preview the contents of a file without downloading it which can be very useful to learn the format of these files and what to expect. This command displays the beginning of a file, providing a quick look at the data structure and content, which is especially useful for very large files:

$ mc cat s3polygon/flatfiles/us_stocks_sip/day_aggs_v1/2024/03/2024-03-04.csv.gz | gzcat | head -4
ticker,volume,open,close,high,low,window_start,transactions
A,2169240,139.15,142.86,143.49,138.81,1709528400000000000,36438
AA,3954744,27.5,27.48,27.55,27.04,1709528400000000000,36282
AAA,37833,25.16,25.06,25.16,25.0101,1709528400000000000,66

If you decide to use a file for your analysis, simply download it directly to your local machine using the following command:

$ mc cp s3polygon/flatfiles/us_stocks_sip/day_aggs_v1/2024/03/2024-03-04.csv.gz .

With just a few easy commands shown in these examples, you are all set to leverage these extensive datasets to their full potential.

Next Steps

We're excited to share Flat Files for downloading a wide variety of high-quality market data at a reasonable price. Beyond just historical data, we offer rich APIs and options for real-time data streaming, accommodating a diverse range of needs from backtesting strategies to live trading applications. Whether you're a researcher, trader, or developer, our tools are designed to provide you with the robust data solutions you need to succeed in the fast-paced world of finance.

You already have access to , so we encourage you to start leveraging them right away. Happy exploring!

From the blog

See what's happening at polygon.io

hunting anomalies in the stock market Feature Image
tutorial

Hunting Anomalies in the Stock Market

This tutorial demonstrates how to detect short-lived statistical anomalies in historical US stock market data by building tools to identify unusual trading patterns and visualize them through a user-friendly web interface.