Build Your Own Stock Trading Bot with Alpaca Markets: Historical Price Data Retrieval

Build Your Own Stock Trading Bot with Alpaca Markets: Historical Price Data Retrieval
Title Image for the TradeOxy Blog Post: Build Your Own Stock Trading Bot with Alpaca Markets: Historical Price Data Retrieval

Did you know that the combination of all US stock markets has over 10,000 different stocks you can trade?

As amazing as that is, if you're building your own stock trading bots, it may present quite a challenge for you! You might be thinking:

  1. How on earth do I get access to all that historical market data for my trading algorithms?
  2. How can I analyze market data at that kind of scale?
  3. How hard will it be to get access to that historical price data?
Image of an anime character thinking about the three keys for retrieving historical data. From the TradeOxy Blog Post: Build Your Own Stock Trading Bot with Alpaca Markets.

When I first started stock trading at the age of 10, the answer to those questions was "very hard, expensive, and time consuming". Today we would call it manual trading, and my process was:

  1. Purchase a national news paper
  2. Go to the business section
  3. Find the listing of the previous days opening, high, low and close data
  4. Note it down in my notebook
  5. Manually analyze my data

Clearly that's not a scalable process!

I'm glad to say that in todays computer driven world, things are much easier, and I'm going to show you how!

What You'll Learn Through My Blog

I'm going to show you everything you need to build your own trading bot from scratch, including:

  • Your own completely customizable development environment, including the worlds most popular technical indicators, and cutting-edge AI from ChatGPT
  • The ability to retrieve historical stock data on over 10,000 US based stocks
  • The power to calculate stock technical indicators on any stock you retrieve
  • The insight to manage your risk

The premium episodes expand this amazing knowledge base to include:

  • A whole host of technical indicators
  • Advanced risk management
  • Advanced strategy building
P.S. All trading is at your own risk. My goal is to provide you with the tools you need to make your own decisions, not give you a perfect solution. Always do your own research!

What You'll Have by the End of this Episode

By the end of this episode, you'll be able to retrieve historical market data for your stock trading bot for over 10,000 different stocks. You'll have learned how to:

  • Connect to the Alpaca.Markets API
  • Query a stock of your choice
  • Format the data for use in your trading strategies
  • Use Python code to retrieve the above

By the way, if you want to check out the completed code for this episode, here's a link to my something-cool repo on GitHub. Don't forget to drop me a star to say hi ❤️

What You Need to Have Completed

The best and easiest way to complete this episode is to have a development environment set up in the same way as my "Build Your Own Trading Bot Dev Environment that works from Anywhere and includes TA Lib, AI". You can check out that free episode using the button below.

Helpful Help from TradeOxy

TradeOxy's mission is to democratize algorithmic trading, and that mission extends to my blog.

If at any point you get stuck in this series, then reach out to us on our Discord chat. I and my team will be more than happy to help out (just make sure it's about our content).

What is Alpaca Markets?

In their own words, Alpaca Markets is an API for Stock and Crypto Trading. They provide a full range of features for everything from retrieving historical data, making trades, and keeping an eye on your portfolio.

Alpaca Markets Web Page view. Part of the TradeOxy Blog: Build Your Own Stock Trading Bot with Alpaca Markets

Why do I use Alpaca Markets for my Automated Trading Bots?

Compared so some of the other platforms I've used and developed for, I've found Alpaca Markets extremely easy to use, and really easy to integrate into my automated trading bots. They're also very active in adding new features and asset classes to their API, covering:

  • Stocks
  • Crypto
  • Forex

Specifically for our purposes, Alpaca Markets offers some great advantages:

  • API is pretty well documented
  • API is easy to use
  • Provides a generous free tier

Note. These words are my own, I do NOT receive a commission for this!

How Do I Sign Up for Alpaca Markets?

To sign up for Alpaca Markets (required for the rest of this episode), follow the link in the button below.

Connect to Alpaca Markets

Now that you've signed up to Alpaca, lets get your trading bot connected.

Alpaca Markets Interaction File

Assuming you're using the development environment we created in this episode, head to your file system, and create a file called

Here's what your full file structure should look like:

File structure to include your Alpaca Markets Library

This file is going to contain everything you need to interact with the Alpaca Markets API.

Alpaca Markets Authentication: Get Your Keys

Authenticating to the Alpaca Markets API is actually pretty simple - especially when you're using the dev environment we set up previously. There's two components we're going to work through - getting your keys then integrating them into your trading bot.

First, we need to create our API Key and Secret Key in Alpaca Markets. This allows us to use their key-based authentication to access their data (and in future episodes our trading history). To do this:

  1. Log in to your Alpaca account and choose the "Paper" option
  2. In the Paper section, find the button "View API Keys" and click it
  3. Generate your keys
P.S. If you're wondering what 'Paper Trading' is, I cover this in another episode.

Alpaca Markets Authentication: Add the Keys to Your Trading Bot

Next, we're going to add these keys to your GitHub Codespace using the GitHub Encrypted Secrets. To do this:

  1. Navigate to your GitHub
  2. Click on your profile picture, then settings
  3. Navigate down to the Codespaces section, then click on "New secret"
  4. Add your API Key, calling it "ALPACA_API" (CAPS included)
  5. Add your API Secret Key, calling it "ALPACA_SECRET_API" (caps included)

At this point, if your Codespace is open, you'll get a pop up notifying you that a new secret has been added, and that to use it you need to reload the environment.

Once both secrets are added, reload the environment.

P.S. While no security measure is 100% effective at all times, GitHub Encrypted Secrets are an amazing way to manage secret information for your trading bots. In future episodes, we'll be able to leverage this functionality to manage the entire trading process, including the ability to execute trades on your behalf.

Update Your Trading Bot Codespace

Let's update your Codespace so that it's ready to develop your trade ideas.

We'll start by adding a virtual environment or venv to your code. This will allow you to lock your environment so that it works as expected.

To do this in your Codespace:

  1. Navigate to the top menu
  2. Select 'Show and Run Commands'
  3. Search for 'Python: Create Environment'
  4. Create the environment

Next, update your requirements.txt to include the following:


The Python requests library gives you the ability to make HTTP requests to HTTP APIs like Alpaca.

Now, run the command pip install -r requirements.txt in your terminal to install the library.

Update Your Trading Bot Code

Let's connect your trading bot to Alpaca Markets so you can begin bringing your trading ideas to life!

First, we'll add in the environment variables to make your Alpaca API Key and API Secret Key available. Do this by adding the following to your file:

Next, we'll add a Python function to the code that:

  1. Receives a url that relates to an Alpaca Markets HTTP endpoint
  2. Adds your authentication
  3. Retrieves the data
  4. Handles any errors
  5. Converts the response to json
  6. Returns the result

Here's the code:

In a little bit, we'll be using this code to get the information we need for your algorithmic trading bots.

Get Historic Data for Your Stock Trading Bots

In order to retrieve historical pricing data for your trading bots, we need to update your Codespace a little further. This will allow us to view the data in a much easier format, which should allow you to make more effective and profitable trades.

Python Pandas: Bringing Trading Strategies to Life Since 2008

To do this, we'll be leveraging a famous Python library called Pandas. Pandas is one of the most popular and widely used tools for data analysis in Python, with over 20 million downloads per month. It was created by a person called Wes McKinney in 2008 while he was working at a quantitative investment firm. He needed a high performance, flexible tool to perform analysis on financial data, and Panda's was the result.

Panda's is truly unique in the data analysis world. It is extremely flexible, fast, and powerful. Its wide usage means that there are integrations with pretty much any trading platform you can imagine.

Images for the Python Pandas Library. Part of the TradeOxy Blog Post: Build Your Stock Trading Bot with Alpaca Markets

How do I add Python Pandas to my Algorithmic Trading Bot?

Start by updating your requirements.txt to include pandas. Here's what it should look like:


Now, run the same command in your terminal to install it in your Codespace virtual environment: pip install -r requirements.txt

How Do I Retrieve Historical Market Data from Alpaca Markets?

We'll be retrieving Klines, or candlesticks, from Alpaca Markets for our trading bots stocks.

Start by adding the following line to line three of your file:

import pandas
import datetime

Now add the following function to the bottom of your file:

This is a pretty powerful function, and there's a lot going on, so I'll quickly outline it for you.

  1. Receives a list of the stocks and timeframe you wish to retrieve, along with information about the number of candlesticks you want to retrieve, and the start and end date of your function.
  2. Checks your inputs to make sure they are in the right format, and no silly errors like the start_date being ahead of the end_date have occurred.
  3. Converts your python list object into a comma separated string (required for Alpaca API)
  4. Sets your start and end date to the right format
  5. Converts all of your data into a dictionary
  6. Queries the API
  7. Converts the data into a pandas DataFrame and then updates the fields so that they're nice and easy to read for you and for your algo trading bots.
  8. Returns the DataFrame

Retrieve Data from the Financial Markets

So far, you've done a lot of work setting up your algo trading bot to automatically retrieve market data for your trading algorithms. Now we're going to see it in action!

Set Your Trading Bot to Auto Run

When we set up the dev environment in a previous episode, I asked you to create a file called So far it's been hanging out doing absolutely nothing. Let's change that.

Go to and delete the code print("Hello world")

Now, add the following code:

Here's what happened when I ran this code:

Retrieving data for AAPL using Trading Bot code

Scaling Up Your Automated Trading Systems

With this code in hand, feel free to have a little play with the input parameters, located on lines 5-7.

How to Retrieve Market Data for Multiple Stocks

For instance, many of the best stock trading bots use multiple stocks. To do this, you can alter line 5 so that it includes any active stocks you want to try. For instance, you could get the original FAANG by altering this line to:

symbols = ["AAPL", "GOOGL", "META", "NFLX", "AMZN"]

How to Retrieve Price Data for Different Timeframes

Alternatively, many experienced traders like trade different, smaller timeframes. To do this, you could alter line 7 to retrieve the 30-minute timeframe by change it to this:

timeframe = "30min"

How to Retrieve More Candlesticks for Technical Analysis

If you're into technical analysis, you might want to expand the number of candlesticks you want to retrieve. This will allow your investment strategies to consider a greater range of variables and is also quite useful for machine learning algorithms.

To do this, you could change line 6 to retrieve more candles, for instance:

max_number_of_candles = 1000
Note. For this to work, you must also make sure that your start_time and end_time are sufficiently large to retrieve the data. For instance, if you wanted 1000 daily candlesticks, you would need to make sure you collect over 3 years of data.

Why not all?

Ultimately, most successful traders I've observed use a combination of the variables I've mentioned above to predict future trends.

What's the Next Step for Your Algo Trading Bot?

With the conclusion of this episode, you've got everything you need to take your stock trading bot software to the next level.

Check out my blog to see how you can:

  • Add a variety of technical indicators to your trading bot
  • Use data to make informed trading decisions and identify trading opportunities
  • Take your analysis and turn it into a trading strategy
  • Create your own AI stock trading bot
  • Integrate automated trading so that you can capitalize on market trends
  • Use TradeOxy to take everything we've learned and turbocharge it

About Me and About Us

Hi! My name is James and I'm passionate about helping others explore the wonderful world of code, trading bots, and all things tech. You can find me on LinkedIn, YouTube.

TradeOxy is a trading assistance website I've cofounded with Mitsi, and we're on a mission to democratize algorithmic trading. Sign up today if that mission resonates with you!

List of Resources

  1. Dev Environment Setup
  2. Alpaca Markets
  3. GitHub Secrets

Complete Code for