You've successfully subscribed to Alpaca Resources
Great! Next, complete checkout for full access to Alpaca Resources
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.
  • Home
  • We're hiring
  • Support
  • Forum
  • Docs
  • Log in
  • Sign up

Commission-free Crypto Trading API is here!

Community Examples

LiuAlgoTrader — Part I — Power up!

amor71
amor71
Sample Tear Sheet with LiuAlgoTrader

Please note that this article is for educational purposes only. Alpaca does not recommend any specific securities or investment strategies.

I am a retail trader/investor. In other words, I’m an amateur — I was never trained as a “quant,” never worked for a hedge fund, spent time on the trading floor, or even studied economics. However, I am a trained software developer with a decent background in math and a disruptive approach to post-postmodernism. If you are like-minded, you might want to read the rest of this post, and if you’re a pro, you might be interested to see how we amateurs roll.

This post is a high-level brief explaining the background of my endeavor, LiuAlgoTrader, the trading platform setup, and some basic concepts to get you going.

amor71/LiuAlgoTrader
Framework for algorithmic trading. Contribute to amor71/LiuAlgoTrader development by creating an account on GitHub.
LiuAlgoTrader

Part II will include concepts of developing strategies with the framework and provide a line-by-line sample strategy.


Until a year and a half ago, I didn’t trade a single asset on any market, didn’t know the jargon or market dynamics. Today I think of it as a self-employed gig economy. It’s far from easy money; in fact, it’s pretty hard, but it’s up to you—total independence. The markets are fascinating phenomena: purely manufactured chaos (yes, it’s more Fractal than Stochastic), psychology induced yet dominated by machine-based trading.

About two years ago, I run into an audiobook on day-trading that my daughter put into our joint library. The first thing that I noticed is the manual approach pattern identification and the complexities of day-trader psychology. I’m old enough to realize this won’t work for me.

Shortly thereafter, I came across Alpaca Markets and a Python example of day-trading based on MACD.

Alpaca - Commission-Free API First Stock Brokerage
Alpaca is a modern platform for trading. Alpaca’s API is the interface for your trading algorithms, bots, or applications to communicate with Alpaca’s brokerage and other services.
Alpaca Markets

They are an awesome company they have just what I needed: APIs for trading, data feeds, account management, and a vibrant community. A big shout-out to the Alpaca team; thank you guys for making this available free of charge and help me take my baby steps into this fascinating world.

Alpaca Algorithmic Trading - Commission-Free API First Stock Brokerage
Alpaca is a modern platform for trading. Alpaca’s API is the interface for your trading algorithms, bots, or applications to communicate with Alpaca’s brokerage and other services.
Trading API
Unlimited access to real-time US stock market data | Alpaca Data API
Alpaca is a modern platform for trading. Alpaca’s API is the interface for your trading algorithms, bots, or applications to communicate with Alpaca’s brokerage and other services.
Market Data API

I noticed several things:

  1. Back-testing is super important, but it’s not the end-game.
  2. There are no complete out-of-the-box back-testing platforms for day trading. For successful day-trading, the platform has to scan thousands of signals concurrently and act near-real-time. The platform also must provide tools to review, improve and benchmark model performance.
  3. Trading services are dynamic eco-systems with brokers and data providers coming and going and constantly changing APIS. The platform has to have an abstraction layer on top data providers and brokers, letting developers focus on strategies rather than infrastructure.
  4. I need an “ML friendly” platform where I can start tinkering with machine learning (I am aware this point is a bit vague but super important, we’ll dwell on it in a later post).

Oh, and I want it to be completely free. Forever.

I decided to take on the challenge and write LiuAlgoTrader.

amor71/LiuAlgoTrader
Framework for algorithmic trading. Contribute to amor71/LiuAlgoTrader development by creating an account on GitHub.
LiuAlgoTrader

An Overview of LiuAlgoTrader

The rest of the post will walk you thru the setup of the framework and some basic concepts. The follow-up post will focus on how to develop and execute strategies using the platform. While I try to make it all sounds nifty swiftly, a word of warning before you go down the rabbit hole: this is not for everyone. LiuAlgoTrader is for developers, by developers. It does not have fancy-schmancy UI, it is not a “no-code” environment, and you need to be a solid Python developer to make the most of it. There is no money for the grab here, and unless you’re truly committed and willing to invests the many hours (months! ,years!) required to create algo trading strategies that will float, you better look somewhere else.

With that said, let’s fire up the engines!

The platform executes trades on Alpaca: you will need an account (=API keys) to get started. The second step is to figure what kind of data you need for your trades. Currently, the platform supports both Polygon.io and Alpaca Market Data APIs. Polygon.io is pricier than Alpaca Data API but has been around longer. A “free” version exists for both, but it’s quite limited.

LiuAlgoTrader is developed in Python 3.9 and tested on Python 3.8 (though switching to 3.9 is highly recommended).

LiuAlgoTrader requires a database to store trading results and details of the executed strategies for future analysis. The platform uses PostgreSQL.

PostgreSQL
The world’s most advanced open source database.
PostgreSQL

There is no heavy-lifting here: liu the installation wizard will download a Docker container and pre-populate the database with the required schema and sample data. You do need to make sure you have Docker Engine & Docker Compose installed.

Install Docker Compose
How to install Docker Compose
Docker Compose

I do my development and testing locally on macOS. My “production” environment is running on the Google Cloud Platform (GCP). It is made of a 4-CPU “Virtual Compute Instance” running Debian Linux and a GCP hosted PostgreSQL. The instance is automatically fired up at 9 AM EST on trading days and automatically shut down shortly after the trading day. Logs are always viewable on the GCP logs viewer, as well as the data in the database. The total cost of the environment is ~$120/month.

The platform is made of several executable components, all written in Python: trader for executing trades on live or paper-account, backtester replaying trading sessions, or testing strategies on past data, and market_miner intended for off-hours calculations for speeding up trading sessions. The platform comes equipped with an installation & configuration wizard called. liu

LiuAlgoTrader is installed by

pip install liualgotrader

While I do my development on macOS and my production setup is Linux-based, the platform runs just fine on Windows.

Before running the installation Wizard, you need to make sure you correctly set up the environment variables required by Alpaca SDK. You could find further reading on the configuration here (Note you really need just the first four to start with).

alpacahq/alpaca-trade-api-python
Python client for Alpaca’s trade API. Contribute to alpacahq/alpaca-trade-api-python development by creating an account on GitHub.
Python SDK for Alpaca API

Once done, all you have to do is type:

liu quickstart

And follow the instructions of the installation wizard (which would normally mean just pressing the return/enter a key a couple of times and the setup unavailing).

Once installed, the wizard will provide instruction on how to view a pre-recorded day-trading session.

Further reading on setups and configuration could be found in the full platform documentation.

Once the framework is fully installed, read the Getting Started page to understand the platform's basic concept.

In the next post, I will focus on end-to-end examples of creating and running strategies using the Platform. For now, if you run into issues during your installation or setup, feel free to post a question on the LiuAlgoTrader Gitter page or open an issue on GitHub or email me directly.

LiuAlgoTrader/community
Liu Algo Trading Platform discussions
LiuAlgoTrader Community
amor71/LiuAlgoTrader
Framework for algorithmic trading. Contribute to amor71/LiuAlgoTrader development by creating an account on GitHub.
GitHub

Brokerage services are provided by Alpaca Securities LLC ("Alpaca"), memberFINRA/SIPC, a wholly-owned subsidiary of AlpacaDB, Inc. Technology and services are offered by AlpacaDB, Inc.

Community ExamplesHedge Fund Basics