Quick Review of Setting Up Alpaca API with Python

Matthew Tweed
Automation Generation
3 min readNov 29, 2018
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>')
# Daily OHLCV dataframe
aapl_daily = api.polygon.historic_agg('day', 'AAPL', limit=1000).df
# Dictionary of most recent earning stats for each company
list_earning = api.polygon.earnings(['MSFT','FB','AMZN'])
# Returns a list articles and their meta-data
tsla_news = api.polygon.news('TSLA')

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>')
# Lists currently open trades
positions = api.list_positions()
# Places a limit order
api.submit_order('AAPL',10,'buy','limit','gtc',170.50)
# Lists all open orders
orders = api.list_orders()

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.

--

--