Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to test back trading strategy

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Python back test trading strategy", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use Python back test trading strategy" bar!

Test back our first policy installation fastquant

It's as simple as using pip to install!

# run this pip install fastquant# on your terminal or, you can run this! pip install fastquant from jupyter to get stock data

Import the get_stock_data function from fastquant to pull Jollibee Food Corp. (JFC) inventory data from January 1, 2018 to January 1, 2019. Note that we have a date (dt) column and a closing price (close) column.

From fastquant import get_stock_datajfc = get_stock_data ("JFC", "2018-01-01", "2019-01-01") print (df.head ()) # dt close# 2019-01-01 293.00 2019-01-02 292.311 2019-01-03 309.4 2019-01-06 323.00 2019-01-07 321.0 retest your trading strategy

A simple moving average crossover strategy (SMAC) is tested back by using the backtest function of fastquant and the historical stock data of Jollibee Food Corp. (JFC).

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

When the fast moving average crosses the slow moving average from below, this is considered a "buy" signal, while if it crosses from top to bottom, it is considered a "sell" signal.

First, let's initialize the fast and slow cycles 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 (this is January 1, 2019).

The difference between the "final portfolio value" and the "initial portfolio value" you get will be based on the expected return of the return test (in this case, PHP 411.83).

From fastquant import backtestbacktest ('smac', jfc, fast_period=15, slow_period=40) # starting value: 100000.0 final value: 100411.83 put all the code together-retest 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_datajfc = get_pse_data ("JFC", "2018-01-01", "2019-01-01") backtest ('smac', jfc, fast_period=15, slow_period=40) # starting value: 100000.0 final value: 100411.83

Improve SMAC strategy to increase fast cycle and slow cycle

This will show that small changes can quickly turn a successful strategy into a failed strategy. After increasing to 30 and 50 in the fast growth period and slow growth period, respectively, our final portfolio value decreased from 100412 PHP to 83947 PHP (a decrease of 16465 PHP).

Backtest ('smac', jfc, fast_period=30, slow_period=50) # starting value: 100000.0 final value: 83946.83 reduces slow cycles while keeping fast cycles unchanged

In this case, the performance of our strategy has actually improved! Our final portfolio value rose from 100412 PHP to 102273 PHP (an increase of 1861 PHP), 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.0 final value: 102272.90

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

Overcome the limitations of testing

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

Retesting has considerable limitations, and overcoming these limitations usually requires additional steps to increase our confidence in the reliability of the retest results and recommendations.

The following are the two limitations of the return test and the safeguards to overcome them:

Over-fitting

This refers to the situation where the "best parameters" you derived are too consistent with the pattern of the previous time period. This means that when you decide to use this strategy, your strategy does not expect profitability to translate into actual profitability.

To prevent this, it is best to test your strategy from a sample, which is similar to using a "test suite" in machine learning. The purpose of this is that when you want to evaluate the profitability of your trading strategy, you need to retain a test data. In this way, it is difficult to fit the parameters because you do not optimize the strategy based on the dataset.

Forward-looking bias

This is due to the deviation caused by the use of information that is not available during the test during the back test. For example, you can test the effectiveness of a strategy on JFC, assuming you already know its financial performance (such as net income) a month before JFC is actually made public. This will give you unreliable confidence, and your strategy may lose you a lot of money.

In this case, one of the best ways to avoid this bias is to thoroughly verify the assumptions made during the backtest strategy. It is worthwhile to critically evaluate your strategy and the information you need to implement it correctly.

These are just two of the many limitations imposed by backtesting. I do intend to write an article to discuss these in more detail in the future, so please stay tuned!

After addressing the above limitations, we should have more confidence in the strategy we have chosen; but remember that while we can have more confidence in our strategy, but its performance in the invisible real world will never be 100% certain.

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

Provide more strategies for fastquant

Keep in mind that fastquant has the same number of policies as in the existing policy base. So far, there are eight strategies to choose from-including simple moving average crossover (SMAC), relative intensity index (RSI), and even strategies based on emotional analysis!

Thank you for your reading, the above is the content of "how to use Python back test trading strategy". After the study of this article, I believe you have a deeper understanding of how to use Python back test trading strategy, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report