How to Create Auto-Updating Excel Spreadsheets of Stock Market Data with Python, AWS, and IEX Cloud

Auto-updating stock spreadsheets

As a full-stack developer working in the finance industry, I often need to provide user-friendly, up-to-date stock market data to business stakeholders and analysts. Manually updating Excel spreadsheets with the latest stock prices and metrics is tedious and error-prone.

In this guide, I‘ll show you how to automate the process of fetching stock data, generating Excel reports, and distributing them via the cloud using Python and AWS. By leveraging APIs, cloud services, and scheduled scripts, you can save hours of time each week while providing your team with the data they need to make informed decisions.

Overview of the Solution

Here‘s a high-level look at the components we‘ll be using:

  • Python: A powerful, versatile programming language well-suited for data analysis and automation tasks. Its extensive ecosystem of libraries makes it easy to work with APIs, spreadsheets, and cloud services.

  • IEX Cloud: A financial data provider offering real-time and historical market data through a flexible, affordable API. We‘ll use their API to fetch the latest stock quotes and financial metrics.

  • openpyxl: A Python library for reading and writing Excel files. It allows us to create richly-formatted spreadsheets programmatically.

  • AWS EC2: A web service that provides secure, resizable compute capacity in the cloud. We‘ll use an EC2 instance to run our data fetching and spreadsheet generation script on a set schedule.

  • AWS S3: An object storage service offering industry-leading scalability, security, and performance. We‘ll store our generated Excel files in an S3 bucket and share them via a public URL.

The basic workflow will look like this:

  1. A Python script running on an EC2 instance fetches the latest stock data from IEX Cloud
  2. The script generates an Excel file containing the stock data using openpyxl
  3. The Excel file is uploaded to an S3 bucket
  4. The S3 URL for the file is shared with end users, who can access it from anywhere
  5. The script is scheduled to run daily using cron, keeping the spreadsheet automatically updated

By automating each step in the pipeline, we can ensure that our stock reports are always accurate and available on-demand. Let‘s walk through how to set this up from scratch.

Step 1: Fetching Stock Data with Python and IEX Cloud

The first step is to write a Python script that retrieves the latest stock data from the IEX Cloud API. To follow along, you‘ll need to sign up for a free IEX Cloud account and get an API token.

We‘ll use the requests library to make HTTP requests to the IEX Cloud API. Here‘s a function that fetches the latest quote data for a given list of stock symbols:

import requests

def fetch_stock_data(symbols, token):
    url = ‘https://cloud.iexapis.com/stable/stock/market/batch‘
    params = {
        ‘symbols‘: ‘,‘.join(symbols), 
        ‘types‘: ‘quote‘,
        ‘token‘: token
    }
    response = requests.get(url, params=params)
    response.raise_for_status()
    return response.json()

This function takes a list of stock symbols and an IEX Cloud API token, constructs the appropriate API URL and parameters, makes a GET request, and returns the JSON response data.

We can call this function with a list of stock symbols we‘re interested in:

symbols = [‘AAPL‘, ‘GOOGL‘, ‘MSFT‘, ‘AMZN‘]
token = ‘YOUR_IEX_CLOUD_API_TOKEN‘
stock_data = fetch_stock_data(symbols, token)

The stock_data variable will now contain a dictionary of the latest market data for each requested symbol, including the current price, market cap, volume, and more.

Step 2: Generating Excel Files with Python and openpyxl

Now that we have our stock data, we need to create an Excel spreadsheet to present it in a user-friendly format. The openpyxl library makes it easy to create and style Excel files using Python.

First, install openpyxl using pip:

pip install openpyxl

Then, we can write a function to generate an Excel file from our stock data:

from openpyxl import Workbook
from openpyxl.styles import Font, Alignment, PatternFill
from io import BytesIO

def generate_excel_report(stock_data):
    wb = Workbook()
    ws = wb.active
    ws.title = ‘Stock Report‘

    headings = [‘Symbol‘, ‘Company‘, ‘Price‘, ‘Market Cap‘, ‘P/E Ratio‘]
    ws.append(headings)

    for symbol, quote in stock_data.items():
        row = [
            quote[‘symbol‘],
            quote[‘companyName‘],
            quote[‘latestPrice‘],
            quote[‘marketCap‘],
            quote[‘peRatio‘]
        ]
        ws.append(row)

    # Add some styling to the header row
    header_font = Font(bold=True)
    header_fill = PatternFill(start_color=‘4286f4‘, fill_type=‘solid‘)
    header_alignment = Alignment(horizontal=‘center‘)

    for cell in ws[1]:
        cell.font = header_font
        cell.fill = header_fill
        cell.alignment = header_alignment

    # Adjust column widths
    column_widths = [10, 40, 15, 20, 15]
    for i, width in enumerate(column_widths):
        ws.column_dimensions[ws.cell(1, i+1).column_letter].width = width

    excel_file = BytesIO()
    wb.save(excel_file)

    return excel_file

This function does the following:

  1. Creates a new workbook and selects the active sheet
  2. Writes the data headings to the first row
  3. Iterates over the stock quotes and writes the relevant data to each row
  4. Applies some custom formatting to the header row to make it stand out
  5. Adjusts the column widths for better readability
  6. Saves the workbook to an in-memory bytes buffer
  7. Returns the Excel file as a BytesIO object

We can call this function with our stock_data from step 1:

excel_file = generate_excel_report(stock_data)

The excel_file variable now contains our stock report spreadsheet, ready to be distributed.

Step 3: Distributing Excel Files via AWS S3

To make our generated Excel files easily accessible to end users, we‘ll upload them to an Amazon S3 bucket. S3 is a highly scalable object storage service that allows us to store and retrieve files from anywhere on the web.

First, make sure you have the AWS CLI installed and configured with your account credentials. Then, create a new S3 bucket to store the Excel files:

aws s3 mb s3://my-stock-reports

Next, we‘ll modify our Python script to upload the generated Excel file to S3 after creating it. We can use the boto3 library, the official AWS SDK for Python, to interact with S3.

Install boto3 using pip:

pip install boto3

Then, add the following code to upload the file to S3:

import boto3

def upload_to_s3(excel_file, bucket_name, object_name):
    s3 = boto3.resource(‘s3‘)
    bucket = s3.Bucket(bucket_name)
    excel_file.seek(0)
    bucket.put_object(Key=object_name, Body=excel_file, ACL=‘public-read‘)
    object_url = f‘https://{bucket_name}.s3.amazonaws.com/{object_name}‘
    return object_url

bucket_name = ‘my-stock-reports‘
object_name = ‘stock_report.xlsx‘
excel_url = upload_to_s3(excel_file, bucket_name, object_name)
print(f‘Excel file uploaded to: {excel_url}‘)

This code does the following:

  1. Creates an S3 resource using boto3
  2. Gets a reference to the target S3 bucket
  3. Seeks to the beginning of the BytesIO Excel file to ensure we read from the start
  4. Uploads the file to S3 with public read permissions
  5. Generates and prints the public URL where the file can be accessed

After running this code, the Excel file will be available at the printed URL, which you can share with your team.

Step 4: Scheduling the Script with cron

To make our stock report generation process fully automated, we‘ll schedule our Python script to run daily using cron, a time-based job scheduler found on Unix-like operating systems.

We‘ll run our script on an AWS EC2 instance for reliability and scalability. First, launch a new EC2 instance (a t2.micro instance will suffice for this use case), and connect to it via SSH.

Once connected, use a text editor like nano to open the crontab file:

crontab -e

Add the following line to the file, replacing /path/to/script.py with the actual path to your Python script:

0 9 * * * /usr/bin/python3 /path/to/script.py

This line instructs cron to run the specified Python script every day at 9:00 AM UTC. Save and close the file.

With this setup, our stock report will be automatically updated each morning without any manual intervention.

Extensions and Further Reading

Here are some ideas for extending this project and resources for learning more:

  • Add error handling and logging to the Python script to catch and diagnose issues
  • Include additional data points and metrics in the stock report, such as earnings, dividends, and analyst ratings
  • Generate visualizations and charts of the stock data using libraries like matplotlib or plotly
  • Send an email notification to stakeholders when a new report is generated using AWS Simple Email Service (SES)
  • Explore serverless options like AWS Lambda for running the script, rather than a long-running EC2 instance
  • Implement user authentication and access controls for the S3 bucket using AWS IAM

Resources:

Conclusion

In this guide, we‘ve seen how to automate the generation and distribution of stock market reports using Python, AWS, and the IEX Cloud API. By leveraging these powerful tools and services, we can save significant time and effort compared to manually updating spreadsheets.

Some key takeaways:

  • Python is a versatile language for data-driven automation tasks, with libraries like openpyxl for creating Excel files and boto3 for interacting with AWS.
  • Cloud services like AWS EC2 and S3 allow us to run scripts on a schedule and make their outputs easily accessible from anywhere.
  • Financial data APIs such as IEX Cloud provide easy access to a wealth of market data that we can incorporate into our reports and analyses.
  • Automated reporting pipelines free up time for higher-value activities and ensure that stakeholders always have access to up-to-date information.

As a full-stack developer working in finance, building systems like this is an important part of my role. By continuously looking for opportunities to automate repetitive tasks and leveraging the right tools and services, I‘m able to deliver more value to my team and organization.

I encourage you to try out this solution for yourself and see how you can adapt it for your own use case. With a little Python knowledge and an understanding of AWS fundamentals, you‘ll be well on your way to building your own automated reporting pipelines.

Similar Posts