
Backtesting simulates a trading strategy on historical market data to quantify performance using metrics like PNL. It helps identify profitability, refine rules, and guide both automated and manual trading.
Guard against backtesting errors by using training and validation splits, avoiding overfitting in moving-average strategies, noting regime changes, fees, and data granularity, since past performance isn't a predictor of results.
Organize the backtesting workflow by collecting historical crypto market data via exchange APIs, storing it in HDF5 for speed, then backtesting Python strategies and optimizing with C++ and NSGA-II.
Install Python 3.9.5 on Windows or Mac, add Python 3 to path, and use the appropriate installer (Windows 64 bit or Mac OS X) before verifying with Python --version.
Install and configure the PyCharm community edition to write and run Python code. Create a project with a virtual environment and install libraries like requests to interact with exchange APIs.
Learn how crypto exchanges provide data via rest APIs over http, returning json for public endpoints like market data and order books, with attention to rate limits and api documentation.
Set up the entry point in the module, create a two-logger system with stream and file handlers, configure format and levels, and select mode via user input under __name__ == '__main__'.
Create the BinanceClient class to encapsulate Binance REST API calls, initialize base urls for spot and futures, and implement a make_request method using requests with error handling and logging.
Call the get_symbols method to fetch available symbols from either spot or futures endpoints, parse the response, and store a list of symbol strings for Binance futures.
Implement get_historical_data to fetch Binance historical candles using symbol, start_time, end_time and limit; then parse raw_candles into timestamp, open, high, low, close, and volume tuples for database storage.
Add new exchange modules (Binance and FTX), fetch the markets list, and request historical data using a unified client that handles endpoint variants, response validation, and timestamp formatting.
Develop a data_collector module that gathers the full price history from Binance or FTX, using get_historical_data with start and end times, and handles initial, most recent, and older data.
Collect the full price history of a symbol by repeatedly calling client.get_historical_data on Binance with start_time and end_time, batching 1000 candles, handling rate limits with pauses, and updating timestamps.
Explore hdf5 as a hierarchical file format for crypto backtesting, using groups and datasets in .h5 files. Interact with data via python h5py and visualize with hdfview.
Learn to create an hdf5 dataset with h5py by defining a symbol-specific dataset, checking existing keys, and configuring 6 columns (open, high, low, close, timestamp, volume) with float64 data.
Create a write_data() method that takes a symbol and data as tuples, converts to a numpy array, resizes the dataspace, and inserts the data.
Compute the dataset's min and max timestamps using a numpy array and a lambda key on the first column. Integrate get_first_last_timestamp into write_data to filter data and prevent duplicates.
Fetch data from a dataset with the get_data() function, returning a pandas DataFrame of candlesticks (timestamp, open, high, low, close, volume) filtered by from_time to_time and indexed by datetime.
Convert 1-minute crypto candles to 5, 15, or other timeframes using a pandas resample function, with proper open, high, low, close, and volume aggregation to prepare data for backtesting.
Learn backtesting in Python using pandas for vectorized operations, create high_low_average and signals to go long or short, and discuss look-ahead bias and flexible strategies.
Learn how the On-Balance Volume (OBV) indicator uses volume and price direction to predict moves, including divergences, and how to apply a 5-period moving average cross for backtesting crypto strategies.
Create a backtesting interface with a backtester.run() that takes exchange, symbol, timeframe, from_time, and to_time, supports selecting strategies like obv, and resamples data for backtests.
Implement obv backtesting in Python, compute obv and its moving average with a ma_period, generate signals, and compute pnl using percentage change on 15-minute Binance BTCUSDT data.
Calculate rolling highs and lows to build the Ichimoku cloud and related lines, then implement a long/short trading strategy based on Tenkan Sen crossovers, cloud position, and Chikou Span.
Develop and backtest the ichimoku indicator in python by coding tenkan and kijun, senkou spans, and chikou span, generate crossover signals, and compute pnl.
Identify supports and resistances by multi-hit price levels, guard against look-ahead bias, and code Python signals to break levels for long or short trades with take profit and stop loss.
Code and backtest a support and resistance strategy by rounding highs and lows, grouping price levels, and validating breakouts with minimum points and candle separation.
Identify and validate supports and resistances from price groups to detect breakouts in crypto backtesting with python and c++. Plot levels with mplfinance and avoid look-ahead bias by using candles.
Backtest crypto trading strategies by coding support and resistance breakouts in Python and C++, tracking open long and short positions, entry prices, pnl, take profit or stop loss logic.
Develop a dynamic parameter input system for backtesting crypto strategies, using a STRAT_PARAMS dictionary to collect strategy-specific inputs in the terminal, with type handling and validation.
Speed up backtests by replacing pandas iterrows loops with numpy arrays for high, low, and close data, aligning with times, to achieve faster execution times.
Compare strategies by stability and PnL, using maximum drawdown as a backtest indicator; in pandas, compute cumulated PnL, maximum cumulated PnL, and drawdown to derive the maximum drawdown for optimization.
Set up a Windows C++ development environment for backtesting crypto strategies using Visual Studio Code, CMake, gcc via MSYS2, and the HDF5 library, then configure a backtestingCpp project.
Set up the Mac OS C++ development environment for backtesting crypto strategies by installing Visual Studio Code, C++ extensions, CMake, Homebrew, and the hdf5 library, then configure gcc or clang.
Set up and run your first C++ program with CMake, link the HDF5 C library, include the hdf5.h header, and verify a hello world output on build.
Open and close an HDF5 file using the C library by implementing a database class with header and cpp files, using H5Fopen, H5Fclose, and libver bounds for safe, fast access.
Implement a fast get_data() in C++ using HDF5 to read a 2d six-column candles dataset, manage dynamic memory, and sort timestamps with qsort.
Resample one-minute candles into longer timeframes with a new utils function. Return six vectors for timestamp, open, high, low, close, volume, and handle missing candles.
Develop a moving average crossover backtest in C++, using a Sma class to detect fast and slow moving average crossovers and generate signals, with code organized in a strategies folder.
Execute the sma cross-over backtest by initializing pnl, updating slow and fast moving average vectors from close prices, and generating long or short signals to compute pnl and maximum drawdown.
Explore the parabolic SAR trading strategy, detailing the acceleration factor from 0.02 to 0.2, extreme points, and how the indicator signals long and short trends with stop-loss reversals.
Code the parabolic SAR indicator for backtesting crypto strategies in a C++/Python framework, using acceleration factor parameters, extreme points, and trend calculations to generate trading signals and evaluate PNL.
Learn how to load a C++ backtesting library into Python, expose C interfaces via a shared library, and run backtests for SMA and parabolic SAR strategies, retrieving pnl and drawdown.
Define optimization for backtesting and maximize or minimize the objective function. Explore NSGA 2, Pareto frontier, and genetic algorithm concepts for multi objective parameter optimization.
Prepare the optimizer module to run backtests and optimizations using NSGA-II, configuring population size and generations, and integrating the Python backtester with a C++ library.
Generate the initial NSGA-II population for backtesting crypto strategies by creating a BacktestResult model, tracking domination count, rank, and crowding distance while using bounded random parameters.
Evaluate the population by running backtests on each individual to assign pnl and max drawdown, update NSGA II class, and return the population for Python and C++ strategies.
Sort the population with non-dominated sorting to assign fronts (F1 onward) based on PNL and maximum drawdown, building a dictionary of individuals and applying domination and dominated_by rules per NSGA-2.
Calculate crowding distance for a population by sorting on PNL and maximum drawdown, assign infinite distance to extreme points, and update each individual's distance to promote diversity in NSGA-II backtesting.
Generate the offspring population for NSGA-2 by selecting two parents via tournament, performing crossover and mutation on their parameters, and ensuring the offspring matches the initial population size.
Generate the next generation of the population by processing fronts with non dominated sorting and crowding distance to assemble a new population of backtest results.
Enforce random parameter constraints for backtest trading strategies, ensuring ichimoku Tenkan and Kijun order and slow vs fast moving averages, then adjust parameters for offspring in population generation.
Implement the NSGA-2 process by building and evaluating populations, generating offspring, and computing fronts and crowding distances to form the next generation for backtests.
Backtest your trading ideas before implementing them in real conditions!
Backtesting is an essential step when elaborating a trading strategy. This course will explain how you can use programming to estimate the potential performance of your strategy and avoid unpleasant surprises in live trading.
By the end of the course, you will be able to build your own backtesting framework and comfortably use all its features.
Collect and store large amounts of market data
Before starting to backtest, you need to have a reliable system that collects, stores and organizes the data. You will learn how to fetch data from any cryptocurrency exchange (Binance, FTX...) and store candlestick data efficiently in a powerful file format: HDF5. Many developers do not yet know about this file format, so you will have the upper hand by learning it!
Get your coding skills to the next level with Python AND C++
Python serves as the ideal programming language for building the main features of your backtesting system. You will also use the Pandas library to calculate technical indicators from scratch and control the output of this calculation with precision.
But that's not all: Do you want to perform backtesting on a large amount of data with many complex operations? This requires a lot of computing power, and this is where C++ coding can be incredibly useful. You will be surprised to discover that C++ is not as scary as it may seem.
Have a scientific approach to your backtesting: use an optimization algorithm!
This course is ambitious, and it addresses real-world problems: you'll want to find parameters for your strategy that will maximize its performance. To help you with this task, you will learn how to write an optimization algorithm from the Genetic Algorithm family: NSGA-2. When it comes to backtesting, this approach is unique, and you won't find it anywhere else.
Most of the content of this course can be applied to traditional markets like the stock market.
Disclaimer: This course is not investment advice. The trading strategies are presented as examples.