Python Parameter Optimisation for TA-Lib Trading Strategy

AAA Quants
9 min readAug 28, 2020

How to test a trading strategy and optimise its parameters


A vital step in quantitative research is finding the best parameter sets for a trading strategy. Having a set of (near) optimised parameters might distinguish a winning strategy from a mediocre one. This is often unappreciated and results in underperforming trading strategies.

In this Python tutorial, we will build some codes and automate the process. I’ve chosen a simple Moving Average Crossover strategy as an example. You will see that there is more than meets the eye. During this process you will learn some facts that may surprise you.

I have often been asked the question: why not use the tools conventionally available in machine learning and AI? My answer is twofold: first the danger of overfitting is indeed so high for financial time series, that these tools simply do not apply. Second, for someone familiar with the dynamics of trading, most of the computer generated models do not make sense even by the most unconventional, open-minded standards and their performance is expectedly poor.

Quantitative Trading Like a Pro: Essential Python Course — Learn the foundations of algorithmic trading and build your own trading systems. Join us:

Be aware that the Moving Average Crossover is not the world’s most profitable strategy and merely an example. However, with some small changes you can apply this methodology to your own strategies on a large scale.

So, today we will discuss the following:

  • Download free market data
  • Build a fast and efficient backtest
  • What type of parameter sweeps should be used
  • Strategy verification

Download Market Data

We begin by downloading the necessary Python packages for our analysis, such as ‘TA-Lib’ for the technical analysis and ‘yfinance’ for the market data.

Then we define a list of stocks, which are some large-cap US equities for simplicity. Next, we use the python package yfinance to download market data for these stocks.

AAA Quants

AI, Quantitative Analysis and Data Science Solutions for Finance