Skip to main content
QUICK REVIEW

[Paper Review] Automatic Historical Stock Price Dataset Generation Using Python

Arunima Mandal, Yuanhang Shao|arXiv (Cornell University)|Aug 25, 2023
Stock Market Forecasting MethodsDecision Sciences3 citations
TL;DR

This paper presents a Python-based framework for automatically generating comprehensive historical stock price datasets from major indices like S&P 500 and Nasdaq using the yfinance library and direct URL queries. It automates data collection, handles corporate actions via adjusted prices, and saves individual company data in CSV format with company names, reducing manual effort for researchers.

ABSTRACT

With the dynamic political and economic environments, the ever-changing stock markets generate large amounts of data daily. Acquiring up-to-date data is crucial to enhancing predictive precision in stock price behavior studies. However, preparing the dataset manually can be challenging and time-demanding. The stock market analysis usually revolves around specific indices such as S&P500, Nasdaq, Dow Jones, the New York Stock Exchange (NYSE), etc. It is necessary to analyze all the companies of any particular index. While raw data are accessible from diverse financial websites, these resources are tailored for individual company data retrieval and there is a big gap between what is available and what is needed to generate large datasets. Python emerges as a valuable tool for comprehensively collecting all constituent stocks within a given index. While certain online sources offer code snippets for limited dataset generation, a comprehensive and unified script is yet to be developed and publicly available. Therefore, we present a comprehensive and consolidated code resource that facilitates the extraction of updated datasets for any particular time period and for any specific stock market index and closes the gap. The code is available at https://github.com/amp1590/automatic_stock_data_collection.

Motivation & Objective

  • To address the time-consuming and repetitive challenge of manually compiling large-scale, up-to-date historical stock price datasets from financial indices.
  • To eliminate reliance on fragmented or incomplete code snippets by providing a unified, reusable Python script for dataset generation.
  • To support researchers in stock price prediction by enabling automated, scalable, and accurate data collection for any desired time period and index.
  • To offer two implementation approaches—one using the yfinance library and another via direct URL queries—ensuring flexibility and compatibility across environments.

Proposed method

  • The method uses Python with the yfinance library to programmatically download historical stock data for all constituent stocks of a given index, such as S&P 500 or Nasdaq.
  • It extracts the list of tickers from Wikipedia for S&P 500 and alternative sources for other indices, ensuring accurate and up-to-date company symbol retrieval.
  • For each ticker, the script downloads daily, weekly, or monthly data using yfinance’s download() function with auto-adjustment for splits and dividends.
  • The script converts the Date index into a column, appends a 'Name' column with the ticker symbol, and saves each company’s data as a separate CSV file in a designated directory.
  • An alternative method bypasses yfinance by directly constructing query strings to Yahoo Finance’s API endpoint, using timestamps and URL parameters to fetch data via pandas read_csv.
  • The system includes error handling to skip companies with no data in the requested range and tracks the number of successfully retrieved stocks.

Experimental results

Research questions

  • RQ1How can historical stock price datasets for major indices be generated automatically and efficiently using Python?
  • RQ2What are the most reliable and scalable methods for collecting adjusted historical stock prices across hundreds or thousands of tickers?
  • RQ3How can the process of dataset generation be decoupled from manual data entry and fragmented code snippets to improve reproducibility?
  • RQ4What are the performance and reliability differences between using the yfinance library and direct URL-based data fetching for large-scale data collection?

Key findings

  • The proposed script successfully generates complete, individual CSV files for each stock in the S&P 500 and Nasdaq indices, with all financial metrics including adjusted closing prices.
  • The system correctly handles corporate actions such as stock splits and dividends by applying auto-adjustment in the yfinance library, ensuring data integrity.
  • The script identifies and excludes stocks that do not exist during the requested date range, preventing errors and improving robustness.
  • The alternative direct URL method achieves equivalent results without requiring the yfinance library, offering a lightweight alternative for restricted environments.
  • The framework reduces the time and effort required to build large historical datasets, enabling researchers to focus on analysis rather than data acquisition.
  • The code is publicly available on GitHub, supporting reuse, extension, and integration into research workflows.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.