The author | Lorenzo Ampil compile | source of vitamin k | forward Data Science

Since I began to learn investing, I have been exposed to different methods of stock analysis – technical analysis and fundamental analysis. I’ve even read many books and articles about these techniques.

In short, technical analysis suggests that you can determine the right time to buy or sell a stock based on its historical price and volume movements. Fundamental analysis, on the other hand, says you can measure the actual intrinsic value of a stock based on basic information in a company’s financial statements.

Both types of analysis make sense to me and I want to use them to inform my trading; However, I was always frustrated by one main thing:

There are many possible strategies that can be adopted, but there is no systematic way to choose one. In fact, most transactions still end up being “gut” decisions rather than data-driven ones.

So how do we evaluate these strategies? We can do this by comparing the expected return on investment (ROI) from each approach. The best way to evaluate a strategy is to use a method called backtesting, which evaluates its performance by simulating past use of it.

There are many backtesting frameworks out there, but most of them require advanced coding knowledge. A simple helloWorld implementation typically requires up to 30 lines of code.

To fill this gap, I decided to create FastQuant with the goal of introducing backtesting as easily as possible. With FastQuant, you can backtest your trading strategy with just 3 lines of code!

For the rest of this article, I’ll show you how to test a simple moving average crossover (SMAC) strategy using Jollibee Food Corp. (JFC) historical data back and forth.

Let’s get started!

Back test our first strategy

Install fastquant

It’s as simple as installing with PIP!

# Run this on your terminal
pip install fastquant

Or, you can run this from Jupyter like this! pip install fastquantCopy the code

Get stock data

Import the get_stock_data function from FastQuant to pull the Jollibee Food Corp. (JFC) inventory data from January 1, 2018 to January 1, 2019. Notice that we have a date (DT) column and a close (CLOSE) column.

from fastquant import get_stock_data
jfc = get_stock_data("JFC"."2018-01-01"."2019-01-01")
print(df.head())

# dt close
# 2019-01-01 293.0
# 2019-01-02 292.0
# 2019-01-03 309.0
# 2019-01-06 323.0
# 2019-01-07 321.0
Copy the code

Retest your trading strategy

A simple moving average crossover strategy (SMAC) was backtested using FastQuant’s backtest feature and Jollibee Food Corp. (JFC) historical stock data.

In the SMAC policy, fast_period refers to the time period for fast moving averages and slow_period refers to the time period for slow moving averages.

When the fast moving average crosses the slow moving average from below, it is considered a “buy” signal, and if it crosses from above to below, it is considered a “sell” signal.

First, let’s initialize the fast and slow periods to 15 and 40, respectively.

You should see the final portfolio value below at the bottom of the log. This value can be interpreted as the value of your portfolio at the end of the retest period (here, January 1, 2019).

The difference you get between the “final portfolio value” and the “initial portfolio value,” which will be the expected return based on the backtest (in this case PHP 411.83).

from fastquant import backtest
backtest('smac', jfc, fast_period=15, slow_period=40)

# Starting value: 100000.00
# Final value: 100411.83
Copy the code

Put it all together – back test with 3 lines of Python

The following code shows how to perform all of the above steps in three lines of Python:

from fastquant import backtest, get_stock_data
jfc = get_pse_data("JFC"."2018-01-01"."2019-01-01")
backtest('smac', jfc, fast_period=15, slow_period=40)

# Starting value: 100000.00
# Final value: 100411.83
Copy the code

Improved SMAC policies

Add fast period and slow period

This will illustrate how small changes can quickly turn a successful strategy into a failed one. After increasing to 30 in the fast growth phase and 50 in the slow growth phase, our final portfolio value dropped from PHP 100412 to PHP 83947 (down FROM PHP 16465)

backtest('smac', jfc, fast_period=30, slow_period=50)

# Starting value: 100000.00
# Final value: 83946.83
Copy the code

Reduce the slow period while keeping the fast period constant

In this case, the performance of our strategy actually improved! Our final portfolio value rose from PHP 100412 to PHP 102273 (adding PHP 1861), then reduced the slow cycle to 35 and kept the fast cycle at 15.

backtest('smac', jfc, fast_period=15, slow_period=35)

# Starting value: 100000.00
# Final value: 102272.90
Copy the code

The following table compares the performance of our three SMAC policies:

Overcome the limitations of testing

Now, does this mean we should trade with the best SMAC strategy? Maybe not yet.

Backtesting has quite a few limitations, and overcoming these often requires additional steps to increase our confidence in the reliability of backtesting results and recommendations.

Here are two limitations of backtesting and safeguards to overcome them:

A fitting

This is when the “best parameters” you export too closely match the pattern of the previous time period. This means that when you decide to use the strategy, your strategy’s expected profitability will not translate into actual profitability.

The best way to prevent this is to test your strategy from samples, which is similar to using “test sets” in machine learning. The idea is that when you want to assess the profitability of your trading strategy, you need to keep a test data. So, it’s hard to fit parameters because you’re not optimizing your strategy based on that data set.

Prospective bias

This is a deviation from using information not available during testing during backtesting. For example, you can test the effectiveness of a strategy on the JFC, assuming you know its financial performance (such as net income) a month before the JFC is actually made public. This will give you shaky confidence that your strategy could cost you a lot of money.

In this case, one of the best ways to avoid this bias is to thoroughly verify the assumptions you made when backtesting your strategy. It pays to critically evaluate your strategy and the information you need to execute it correctly.


These are just two of the many limitations that backtesting imposes. I do plan to write an article that discusses these in more detail in the future, so stay tuned!

Having addressed the above limitations, we should have more confidence in our chosen strategy; But remember, while we can be more confident in our strategy, its performance in the invisible real world will never be 100 percent certain.

I suggest that once you have a strategy in the real world, start with relatively little money and only add to it when it shows more consistent success; Otherwise, be prepared to kill it if it proves ineffective in the real world.

Provide more policies for FastQuant

Keep in mind that FastQuant has the same number of policies as the existing policy library. So far, there are eight strategies to choose from — including simple moving average crossover (SMAC), Relative Strength Index (RSI), and even strategies based on sentiment analysis!

As I mentioned in the introduction to this article, there are a number of different strategies that can be applied to trading. Thank you for reading this

The original link: towardsdatascience.com/backtest-yo…

Welcome to panchuangai blog: panchuang.net/

Sklearn123.com/

Welcome to docs.panchuang.net/