PyQuant News 🐍
PyQuant News 🐍

@pyquantnews

17 Tweets 82 reads Jan 29, 2023
Nobody taught me how to backtest a trading strategy.
So I read all the books, documentation, and blogs.
Then, I distilled what I learned into a simple step-by-step guide.
But unlike a 300-page book, this won't take you a month to read.
Here it is in 2 minutes:
But first, what’s a backtest?
A backtest:
• Tests trading ideas against historic market data
• Is used to check the robustness of trading strategies
• Is a simulation of how a strategy might have performed in the market
And most beginners get it wrong...
Here's how:
• Expect backtest results to be the same in real life
• Build their own backtesting framework
• Introduce bias into their backtest
So, how do you avoid these problems?
Use the Backtrader backtest library.
Backtrader is an event-driven backtesting framework designed to remove bias.
It’s easy to build and test trading strategies in a reusable way.
But it’s hard to get started.
Fortunately, I lay it out step-by-step here.
By reading this thread, you will be able to
- Get data from OpenBB
- Build a backtest using Backtrader
- Assess the results using QuantStats
Here's how to do it in Python, step by step.
Start by importing pandas, the OpenBB SDK, QuantStats, and Backtrader.
There’s an unsolved issue with Backtrader that prevents it from downloading data. That’s why you need the OpenBB SDK.
Here’s a simple workaround.
This function downloads the data from the OpenBB SDK, converts it to a CSV, and reads it in the Backtrader’s `YahooFinanceCSVData`.
Fund managers report their holdings monthly.
They don’t want to tell investors they lost money on meme stocks.
So they sell them and buy higher-quality assets, like bonds.
Can you take advantage of this?
Start with a simple helper function that gets the last day of the month.
Next, setup the BackTrader strategy.
This code tests if there’s a position in the market.
If not, it checks if the current day is within the first week of the month and creates a short position.
Otherwise, if the current day is within the last week, it creates a long position.
Now, run the backtest.
The first step is to create a backtesting engine (Backtrader calls it Cerebro).
Then add the data, initial cash, and strategy logic.
The last step is to convert the results into a pandas DataFrame.
Trading takes time, money, and effort.
To make sure you're better off not being long TLT, compare the strategy results to a long-only strategy.
QuantStats makes it easy.
Here’s how to use it.
Running this code prints 70 different performance and risk metrics.
The strategy underperforms the long-only strategy on an absolute basis.
But, it has better risk-adjusted returns, lower drawdowns, and lower volatility.
It also has a better profit factor—which is important for active strategies.
By reading the thread, you can backtest a real trading strategy with Backtrader.
Now you can get data, backtest the strategy, and analyze the results to test the performance of your strategies.
This thread is packed with information.
If you can't get to it all now, click the link to hop to the top tweet.
Then retweet it (with a comment!) so you can come back to it later.
The FREE 7-day masterclass that will get you up and running with Python for quant finance.
Here's what you get:
• Working code to trade with Python
• Frameworks to get you started TODAY
• Trading strategy formation framework
7 days. Big results.
pythonforquantfinancemasterclass.com

Loading suggestions...