Quick Review of Setting Up Alpaca API with Python

Matthew Tweed
Nov 29, 2018 · 3 min read
Alpaca API Documentation

What is Alpaca?

Alpaca offers a simple REST API to easily build algorithms and applications around the US stock market with real-time market data. Also, they offer zero-commission stock brokerage service through their subsidiary, Alpaca Securities LLC (a FINRA member). Obviously, I think that avoiding excessive trading fees should help level the playing field, should enhance profits and make a wide range of new strategies viable.

Getting Started

The first step to any project is installing your SDKs, a process made simple by python pip.

Simply run: pip3 install alpaca-trade-api in your console, wait for the install and you’re ready to go.

Next, login to your Alpaca account and find or generate your API keys. These allow you to interact with the platform. Make sure to store them in a safe place, since they hold complete control of your account.

Once you have your keys, simply import the trading API into python and set up your instance.

import alpaca_trade_api as tradeapi

api = tradeapi.REST('<key_id>', '<secret_key>')

The API instance provides a simple interface to all the trading and data functionality you’ll need, without having to worry about any of the back-end calls.

Market data

Data is the backbone of any viable strategy, and the Alpaca API makes it quick and concise to access, via a wrapper around the Polygon API.

Everything from historical OHLCV data to current fundamental data and news, all in one place.

import alpaca_trade_api as tradeapi

api = tradeapi.REST('<key_id>', '<secret_key>')

Using Polygon’s API, you can quickly implement a wide range of strategies. From the simple technical analysis, using libraries such as ta-lib, to more complex strategies using fundamental data or alternative data such as news sentiment.

Trading and Order Management

As with market data, all your standard order functions can be accessed from the REST API, making the implementation of advanced trading strategies simpler than ever. Orders can be quickly placed, checked and canceled with a single command each.

import alpaca_trade_api as tradeapi

api = tradeapi.REST('<key_id>', '<secret_key>')

While this example only shows a simple limit order, Alpaca supports several options for more advanced orders, such as stop limits, market orders, and fill-or-kill orders.

Bringing it all Together

While learning how to build a successful investment strategy might not be quick, there’s no reason why learning the APIs shouldn’t be. With the right tools for the job, you can now start making your first trading algorithm.

Full API docs can be found here in Github and here in Alpaca’s site:


Technology and services are offered by AlpacaDB, Inc. Brokerage services are provided by Alpaca Securities LLC (alpaca.markets), member FINRA/SIPC. Alpaca Securities LLC is a wholly-owned subsidiary of AlpacaDB, Inc.

You can find us @AlpacaHQ, if you use twitter.

Automation Generation

News and thought leadership on the changing landscape of…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store