Part I presented LiuAlgoTrader. Part II detailed an end-to-end example of backtesting a successful strategy with Liu.
This article focuses on Stock Portfolio management with Liu: How to create, trade, and analyze your portfolio results.
Let’s start with some terminology:
- Each execution of the
trader
application creates abatch
context. - The Liu platform saves
strategy
algorithmictrades
in a batch context. - A strategy may receive
portfolio
as a parameter. A link between a batch and a portfolio allows tracking trades over multiple trading sessions. - An
Account
is the equivalent of a bank account: it has initial cash and an allowed credit line. - Each account withdrawal or deposit is an
account transaction
. - Linking between an account and a portfolio allows tracking both cash and equity values over time.
- It is up to the strategy code to associate portfolio with batch and to track account withdraws in an account. Liu provides the building blocks and implementation patterns.
The below diagram visualizes the relationships between the different data objects:
Part II presented an end-to-end backtester
run, which is essentially a single batch. For the rest of this article, you will see how to run trader
sessions over multiple days and track your performance over time.
The article assumes you have a working setup of LiuAlgoTrader. If you do not yet, please go back to the previous articles and ensure you have a working environment and are sufficiently familiar with the platform.
We will be using a modified Trend-Following strategy from Andreas Clenow’s books (the books are highly recommended and include source code).
Here is a link to my version of the strategy; you will need to clone the repository (and remember to add a ⭐️).
Step #1 : Create a Portfolio with $100,000 and $10,000 credit line.
Type:
$ portfolio create 100000 10000
A response should be similar to this:
Step #2: include the Strategy definition in your tradeplan.toml file:
[strategies.TrendFollow]
debug = true
filename = "/home/me_me_me/dev/trades/strats/trendfollow.py"
index = 'SP500'
portfolio_id = "d3b8a521-2096-441e-a682-b9f1208b3c41"
rank_days = 90
rebalance_rate = "daily"
reinvest = true
stock_count = 15
volatility_threshold = 0.03
(filename should point to where you cloned the strategies repo, and portfolio_id should hold the portfolio id you just created)
Step #3: Assuming Alpaca rails (Free tier in Polygon.io may also work) and environment variables are correctly set. Type:
$ trader
And off we go
The trader application will end at the end of the trading day, and the day after, you will need to re-run the trader
application. A Later article will guide on how to set up a fully automated, production-ready trading platform.
To view the trades performed type:
$ portfolio trades d3b8a521-2096-441e-a682-b9f1208b3c41
To view your current equity holdings type:
$ portfolio equity d3b8a521-2096-441e-a682-b9f1208b3c41
Lastly, to see the cash withdrawals and deposit type:
$ portfolio account d3b8a521-2096-441e-a682-b9f1208b3c41
After several days of running on a paper account, use the Portfolio Performance Analysis Notebook. Similar to Part II, the backtester
application can run the strategy on past data. The optimizer
application, presented in a previous article, helps select the best initial values for the hyper-parameters in the toml file.
In the following article, we will explore how to monitor Liu performance during day-trading sessions.
Questions? Issues? Feel free to post a question on the LiuAlgoTrader Gitter page or open an issue on GitHub or email me directly.