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.
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.
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.
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:
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.