Backtesting.py - Full course in python

Chad-Thackray

Backtesting.py - Full course in python by Chad-Thackray

The "Backtesting.py" course in Python is an introduction to backtesting and focuses on the essentials of backtesting. Unlike other backtesting libraries, backtesting.py is lightweight, easy to understand, and has a built-in parameter optimizer and heatmap plotter. It has some limitations, such as not being able to trade multiple assets or allowing fractional shares. The instructor demonstrates how to define a strategy as a class and pre-calculate values for technical indicators. The course includes backtesting with a dataset and how to plot and interact with the data using the browser. It explores the backtesting.py dashboard, strategy optimization, and how to apply constraints while using lambda functions. The speaker also demonstrates how to implement different order types, such as stop loss and take profit orders and initiate short positions. Finally, the video covers how to use the "resample apply" function to enable multi-time frame strategies. The course aims to provide beginners with a strong foundation in backtesting and is a great option for those wanting to get started with backtesting.

00:00:00

In this section, the instructor introduces backtesting.py and explains its pros and cons. He emphasizes that unlike other backtesting libraries like vector bt or backtrader, backtesting.py is lightweight and focuses on the essentials of backtesting, making it easy to understand and use. He also mentions that it's very quick, thanks to its event-driven backtester, and has a built-in parameter optimizer and heat map plotter. However, it has some limitations like not being able to trade multiple assets and not allowing fractional shares. The instructor also shows how small the documentation is and how easy it is to install, making it a great option for beginners who want to get started with backtesting.

00:05:00

In this section, the instructor sets up a Python file and imports the necessary libraries to perform a backtest in backtesting.py. The library comes with some built-in Google price data, which is stored in a pandas dataframe. The instructor then goes on to explain how to define a strategy as a class that extends the strategy class, with two functions: init and next. The init function runs once at initialization and should contain calculations that can be done in one big batch. The rsi is calculated inside this function. The next function runs through each data point individually and evaluates whether to buy on the next candle. The instructor defines an RSI indicator using the talib library and the close price.

00:10:00

In this section, the course instructor explains how to pre-calculate values for a trading strategy by using technical indicators, specifically the RSI and crossover libraries. To implement a simple crossover strategy, the instructor imports the crossover library from backtesting.py and uses it to determine when to buy or sell. If the RSI is above a certain value, it will sell everything, and if the RSI goes below a lower bound, it will buy. The section also covers defining variables for a class and provides an example of backtesting with a dataset. The backtesting analysis shows that the strategy performed poorly, with massive drawdowns, indicating it may not be the best strategy to use. The course also covers how to plot and interact easily with the data in the web browser using bt.plot.

00:15:00

In this section, the video tutorial is demonstrating the backtesting.py dashboard and how it can be used to evaluate a strategy. The dashboard enables traders to visualize and analyze trades, see different indicators, and track the performance of their portfolio over time. The tutorial then moves on to discussing strategy optimization, which allows traders to fine-tune their parameters, such as the upper and lower bounds of the RSI, and the RSI window, for maximum profit. The optimizer can simulate different combinations of these parameters and find the optimal ones, based on a chosen statistic, such as the sharp ratio. Constraints can also be applied to the optimizer to ensure that certain criteria are met.

00:20:00

In this section of the video, the instructor shows how to apply a constraint to select only a subset of values in backtesting.py. The lambda function allows users to create a function that returns true or false based on any parameters and pick the upper and lower bounds, RSI window or anything else that is suitable. This optimizer is well multi-threaded, which means it is running separate back tests on each of the cores of the computer, efficiently using the full power, and selecting the best options. Users can also choose their optimization function to optimize a different parameter according to their preference. Finally, the optim function tries to figure out how to make the most money while being in the market for the least amount of time.

00:25:00

In this section, the video presenter demonstrates how to make the backtesting.py function more flexible by adding a filter that excludes trading strategies with less than 10 trades. This is done by adding a new parameter that checks the number of trades, and if it is less than 10, the function returns -1. This ensures that the function only considers trading strategies that have a minimum of 10 trades. The presenter also explains how to organize backtest results using HTML files and separate folders to avoid clogging up the directory. Finally, the function is run on example.py, resulting in a more reasonable output with 17 trades.

00:30:00

In this section, the video explains how to optimize functions with a wide range of different values and introduces the "max tries" argument to perform randomized grid searches. Using the "max tries" argument saves time, reduces the chance of overfitting, and randomly selects values to test rather than an exhaustive grid search. The video also discusses how to use the "return_heatmap" argument to create heat maps that compare how different changes to the upper and lower bounds of a function affect the outcome. The data can be transformed using a simple Pandas group-by statement to view the results.

00:35:00

In this section, the speaker explains how group by works in Pandas and how to unstack a 2 by 2 matrix to plot a heatmap using Seaborn. They demonstrate how to optimize for different statistics such as the Sharp ratio, and how to change the color scheme of the heatmap. The speaker then introduces a function called "plot_heat_maps" that can be used to plot multiple heatmaps when dealing with more than two parameters. This function is built into the backtesting.py library and can be used to visualize the various combinations of parameters that are used in the optimization process.

00:40:00

In this section, the video creator introduces the concept of heat maps and parameter optimization. Heat maps show how different variables relate to each other, and the optimization process involves taking different simulations and aggregating them using an aggregation function. However, optimizing multiple variables can be difficult, especially for ten or more variables. The video creator then moves on to multi-time frame strategies using the resample apply function, which helps to find other time frames. The function resamples and aggregates data for different time periods, enabling us to obtain weekly data from daily data. The creator explains how to use the resample apply function and how to implement two different time frames in a strategy.

00:45:00

In this section, the speaker discusses how to use different time frames in a trading strategy. They demonstrate how to create a daily and weekly RSI using Pandas and Backtesting.py, and then use them in a simple trading strategy that buys when the weekly RSI is below 30 and there is a crossover on the daily RSI, and sells when the weekly RSI is above 70. They also show how to use other time frames and conditions to create different trading strategies. The speaker also briefly discusses how to implement different order types in Backtesting.py, such as stop loss and take profit orders.

00:50:00

In this section, the video creator demonstrates how to initiate short positions using the Backtesting.py library in Python. The code is modified to buy and close shorts with the help of the position class and self-dot position method. The creator then demonstrates how to identify and fix the multiple position problem using the "is long" and "is short" method. Finally, to initiate trades only if the position exists, an "or not self dot position" statement is included. The creator prints the position value to check the logic and runs the backtest to demonstrate the effect on trading.

00:55:00

In this section, the speaker discusses the implementation of take profit and stop loss in backtesting.py. They show how easy it is to add a stop loss by setting the "sl" parameter as a percentage of the current price, and how a take profit can be added by using the "tp" parameter as a multiple of the price or any indicator of choice. The speaker also demonstrates using the "size" parameter to determine position sizing when buying or selling, either as a percentage of available cash or a dollar value. They note that while backtesting.py does not handle fractional shares by default, it can be a useful tool in building and testing various trading strategies.

01:00:00

In this section of the tutorial, the instructor talks about implementing a strategy where instead of giving a percentage of available cash, the code tells how many of an asset to buy. This approach could be useful for strategies that involve laddering into a position or dollar cost averaging. The code is modified to buy any time that the value of the RSI is smaller than the lower band. This results in more buying action and can be thought of as a funky dollar cost averaging strategy where we buy during dips and sell when the value goes up. The instructor also talks about how to extract trades out of the final backtest using the pandas data frame, and how this can be used to export data to other visualization tools.

01:05:00

In this section of the video, the speaker discusses the "bars since" function as a way to conditionally trigger trades in backtesting using the backtesting.py library in Python. The function allows for a condition to be set that returns a value of true or false, indicating how many bars since the condition was last true. This can be used to track the duration of trades and make further analysis on raw trade data. The speaker also mentions the open-source nature of the library, which allows for modification of the code, or using it for historical record keeping, and encourages viewers to explore the library's documentation.

01:10:00

In this section, the speaker ends the video by providing his contact details for those interested in using testing py or any other backtesting library. He encourages viewers to check out his description for more information and wishes them luck in their backtesting adventures.

More from
Chad-Thackray

No videos found.

Related Videos

No related videos found.

Trending
AI Music

No music found.