How to Rebalance Your Stock Portfolio with Alpaca Trading API ~ Part II

Alpaca Paper Trading API

What is the Alpaca Trading API?

The Alpaca Trading API is an API interface that allows you to programmatically buy, sell, short stocks, and crypto — the best part being every trade executed is commission-free.

Why use Alpaca Trading API?

Between its ease of use and comprehensive features, there are a few main reasons to use Alpaca’s trading API. What makes it perfect for our walkthrough today are the following highlights:

  • No Fee Trading
  • Supports Fractional Shares
  • Simple API and SDK Offerings & Helpful Documentation
  • Paper Trading for Trading Practice & Model Integration

Getting Started with Alpaca Trading API

To follow along with this walkthrough, you will need to sign up for an Alpaca API account and retrieve your Alpaca API Key and Alpaca Secret API Key.

Alpaca API Keys View

How to Rebalance a Portfolio with Alpaca’s Trading API?

The steps we will take to initiate and rebalance a portfolio with Alpaca’s trading API are as follows:

  • Construct Initial Portfolio Weightings — See Part I;
  • Ensure Symbols are Eligible for Fractional Trading;
  • Close Unnecessary Portfolio Positions;
  • Retrieve and Adjust our Rebalance Equity;
  • Adjust Current Portfolio Allocations; and
  • Buy and Sell Equities Based on our Portfolio Allocations

Construct Initial Portfolio Weightings

Our final function will require a python dictionary of your desired portfolio weightings as an argument; therefore, please refer to Part I of this walkthrough on the methodologies and code to achieve this requirement.

Ensure Symbols are Eligible for Fractional Trading

While you can refactor the following code to only trade in whole share units, the following code requires fractional trading.

Close Unnecessary Portfolio Positions

When it comes to automating the rebalance of a portfolio, one of the most important steps is to close and sell positions before buying new ones. Selling must occur before purchasing because it allows us to replenish our account with the equity needed to buy shares to reach our allocation. If we reversed these steps, our automation would not be able to proceed because there would be no capital available to invest.

Retrieve and Adjust our Rebalance Equity

To properly trim and allocate towards our desired weightings & positions, we must first know how much equity we have available in the portfolio.

Adjust Current Portfolio Allocations

Now that we have closed out any unnecessary positions and retrieved our portfolio’s equity, we can proceed to trim and re-allocate our funds.

Rebalance Results

Where to access clean market data

At Intrinio, we’re recognized for being one of the top data providers that specializes in high-quality data, immediate customer service, and modern toolsfor a variety of fintech platforms and businesses. Our dedicated team wants to help you find the best data package that fits your unique needs. Reach out to one of our data specialists today to find out more about our equities market data packages. And to learn more about all our financial services and data please visit

Full Code Walkthrough



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


High-quality financial data. Tools built for developers. First-class, US-based support. Data doesn’t have to be hard or expensive.