Please note that this article is for educational and informational purposes only. All screenshots are for illustrative purposes only. The views and opinions expressed are those of the author and do not reflect or represent the views and opinions of Alpaca. Alpaca does not recommend any specific securities or investment strategies.
This article starts from a boilerplate, a fully automated AI-based trading system. However, it’s not the second part — think of it as a spin-off.
OK, so you have followed the previous tutorial and now you have your Awesome Fully Automated AI-Based Trading System, or AFAABTS for short. Now what? Now it’s time to enhance it, adding more sources, more complexity, that kind of stuff. Today we’re going to add more flavor to Alpaca, one of the two brokers that were already included.
So first of all let’s recap a little bit: what is Alpaca? According to their main page, it is an API for stock trading:
Trade with algorithms, connect with apps, build services — all with commission-free stock trading API
Alpaca is a registered securities broker, and a member of FINRA and SIPC. Among their perks, they list:
- Advanced trading: Many advanced investing and trading strategies are supported, such as margins or short selling.
- Testing: The Alpaca paper account, the one we’re going to use here, is a testing environment with real-time data but no real money. There are some tiny differences with the real deal, but it’s a great tool anyway.
- Multi-language: SDKs are available in Python, .NET/C# or Go, among others.
- Commission-free: Alpaca does not apply commissions for trading. However, there’s a disclosure:
No minimums. Commission-Free trading means that there are no commission charges for Alpaca self-directed individual cash brokerage accounts that trade U.S. listed securities through an API. Relevant SEC and FINRA fees may apply.
We will use the alpaca-trade-api library, the Python SDK, which has over 700 stars in GitHub. To install:
$ pip install alpaca-trade-api
Let’s take a quick tour and check what Alpaca offers.
Everything that’s done through the API requires an API key ID and a secret key that can be found in the dashboard:
From that point, all actions will have the same structure:
The api object is the one we’ll use to access all the methods.
Getting account info
Using the api object from before, we can access all the account info:
There’s more! Take a look at all the attributes of the account that can be accessed using the API.
Getting the data and placing an order
“Ok Ruben, this is all great and whatever but can you please cut to the chase and tell me how to buy and sell stuff using this or what?”
OK, fine. Let’s get to the good part.
The first thing we need to know is whether or not the market is open:
Easy, right? Then we can go ahead and check the data of all assets. Alpaca works with five exchanges: ARCA, NYSE, AMEX, NASDAQ and BATS, offering the following information on each asset:
The tradable attribute indicates if the asset is tradable in Alpaca, and the symbol attribute is, well, the symbol!
Knowing that, we can check the pricing information for a particular asset (note that the following code has been taken almost literally from the Alpaca docs!):
With that info, we can decide if we want to buy, or hold, or sell. That’s also very simple — once again, these examples come straight from Alpaca docs. A market order to buy one share of Google at the market price:
A limit order to sell one share of Google at a particular price, here 2000$, when the market opens:
Checking the portfolio
To get the info of the assets we possess, we need to call the list_positions method. For a specific method we can use get_position, but bear in mind that it will throw an APIError exception if the position does not exist in our portfolio.
Alpaca is a broker for developers — or, even better, for folks who code. It provides a simple yet powerful API — enough to do almost anything we want to do. Besides that, the paper account is a handy testing environment to try out our strategies without risk. Remember to test everything thoroughly, or even seek professional advice, before using real money. Trading is not easy — if it were, everyone would be rich!
As always, you can check the code in GitHub.
- Alpaca Team, API Documentation, AlpacaDOCS.
- R. Romero, How To Create A Fully Automated AI Based Trading System With Python (2020), Medium
Alpaca does not prepare, edit, or endorse Third Party Content. Alpaca does not guarantee the accuracy, timeliness, completeness or usefulness of Third Party Content, and is not responsible or liable for any content, advertising, products, or other materials on or available from third party sites.