LiuAlgoTrader Optimization
This tutorial goes through how to use the Optimizer Function in the open-source framework LiuAlgoTrader when backtesting a trading strategy.
Please note that this article is for educational purposes only. Alpaca does not recommend any specific securities or investment strategies.
I wrote LiuAlgoTrader so I can trade using my long-earned development skill-set. Consistent winning is not easy.
That’s when back-testing enters: a tool that lets you test-drive your strategies, compressing months of potential trading into minutes of calculations.
Almost every strategy that I wrote has some configuration parameters. These parameters are the number of equities in your portfolio, EMA thresholds, length of the rolling window for volatility calculations, etc.
These parameters need to be optimized to tune your strategy.
Enter Hyperparameters
In other words, hyper-parameters span the configuration space of strategies, selecting the optimal values can drastically improve strategies performance.
With release 0.3.11, LiuAlgoTrader has a new tool: optimizer
. This unique application allows running, parallel back-testing of strategies while generating all possible combinations for hyper-parameters and storing these results to the database.
Once the optimization session ends, use a Jupyter Notebook to load all the back-test results and compare how the different configurations affect total returns, Sharpe ratio, and volatility.
It is now up to you to select your desired parameters by taking these three data points (or add a few of your own).
Let us see an example: you’re running a ‘Trend Following’ strategy with an account with $10,000 and wish to select the number of different equities that should make up your algo traded.
Your tradeplan. toml
file looks something like this:
But is 10 really the best selection for stock_count
? Enter optimizer :
Adding these lines to the configuration files instructs the optimizer application to run back-test sessions in the first half of 2021.
Now your tradeplan. toml
looks like this sample trade-plan
The hyper-parameter is stock_count
with values ranging from 5 to 15. The optimizer can receive several hyper-parameters and create and iterate through the entire configuration space.
Contrary to the hyper-parameter, the ”parameters” depict how to calculate and over-ride the strategy configuration parameter for portfolio_id
For each execution of the back-test iteration, a new portfolio is generated with $10,000 and a credit line of $1,000.
Running the optimizer is as simple as typing:
optimizer
The optimizer may run for a while, producing an output:
Make sure to copy and optimizer session-id -> you will need it later for your analysis. Once completed, use the dedicated Notebook to view the collection of back-test sessions:
The Notebook loads all portfolio's transactions and analyzes them to produce the answer:
So it’s not 10 but rather 11 that yields the best returns and best Sharpe ratio for the strategy!
For further reading check LiuAlgoTrader optimizer documentation.
Questions? Issues? Feel free to post a question on the LiuAlgoTrader Gitter page or open an issue on GitHub or email me directly.
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.