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!

Algorithmic Trading Basics

LiuAlgoTrader Optimization

amor71
amor71
LiuAlgoTrader Optimization

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.

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

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 machine learning, a hyperparameter is a parameter whose value is used to control the learning process. By contrast, the values of other parameters are derived via training.

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:

# This is a TOML configuration file.
bypass_market_schedule = false
portfolio_value = 25000

[strategies]

[strategies.TrendFollow]
debug = true
filename = "/Users/amichayoren/dev/trades/strats/trendfollow.py"
index = 'SP500'
portfolio_id = "2b94a5c6-4ccf-480c-91cd-71cdeacf158c"
rank_days = 90
rebalance_rate = "daily"
reinvest = true
stock_count = 10
Default 'tradeplan. toml' file

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.

[optimizer]
end_date = '2021-06-19'
start_date = '2021-01-01'

[optimizer.parameters.strategies.TrendFollow]
portfolio_id = {param_type = 'portfolio', size = 10000, credit = 1000}

[optimizer.hyperparameters.strategies.TrendFollow]
stock_count = {param_type = 'int', min = 5, max = 15}
Optimizer Configuration lines

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:

Sample optimizer run

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:

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

The Notebook loads all portfolio's transactions and analyzes them to produce the answer:

Portfolio Transactions

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.

LiuAlgoTrader/community
Liu Algo Trading Platform discussions
LiuAlgoTrader Community

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.




Algorithmic Trading BasicsCommunity Examples