24 Lessons in Quantitative Investing: Syllabus
§ 24 Lessons in Quantitative Investing
Course Syllabus
This section introduces the essential securities knowledge required for quantitative trading, such as price adjustment (adjusted prices). Adjusted prices are an unavoidable issue in quantitative trading and will run throughout our course. However, many answers found online are incorrect. This section also reveals an unexpected issue: Python has problems with rounding.
1. Introduction
1.1. Development of Securities Investment and Quantitative Trading
1.2. Knowledge Framework for Quantitative Trading
1.3. Who is Suitable for Quantitative Trading?
1.4. Overview of Quantitative Strategies
1.4.1. Alpha Strategies
1.4.2. Market-Neutral Strategies
1.4.3. High-Frequency Arbitrage Strategies
1.4.4. Technical Analysis-Based Strategies
1.4.5. Strategy Research Methods
1.5. Course Content Overview
1.6. How to Study This Course
1.6.1. Prerequisites
1.6.2. Introduction to Online Quantitative Environments
2. Securities Fundamentals and Data Sources: Akshare
2.1. Exchanges and Security Codes
2.2. Knowledge of Price Adjustment (Adjusted Prices)
2.3. Akshare
2.3.1. Installing akshare in the course environment
2.3.2. Real-time stock data
2.3.3. Historical stock data
2.3.4. Security lists
2.3.5. Trading calendars
2.3.6. Wrapping and improvement suggestions
2.3.7. Exercises
3. Data Sources: Tushare, JqDataSdk
3.1. TUSHARE
3.1.1. Installing and setting tokens in the course environment
3.1.2. Historical stock data
3.1.3. Security lists
3.1.4. Trading calendars
3.2. JQData Local Data
3.2.1. Installing and setting up accounts in the course environment
3.2.2. Historical stock data
3.2.3. Security lists
3.2.4. Trading calendars
3.3. BAOSTOCK
3.3.1. Historical stock data
3.3.2. Security lists
3.4. YFINANCE
The exercises in this section will prompt the linear transformation relationship between forward and backward adjusted prices.
4. Using Zillionare to Obtain Data
4.1. Omicron
4.1.1. Initializing omicron
4.1.2. Real-time stock data
4.1.3. Historical stock data
4.1.4. Security lists
4.1.5. Trading calendars
4.1.6. Sector data
4.2. Interpreting A-Share Data: Relationship Between Investor Numbers and Market Trends
5. Exercise Explanation (Video Only)
6. Small-Cap Strategy
6.1. Introduction to Small-Cap Strategies
6.2. Strategy Implementation - Manually Implementing the Simplest Backtest
6.2.1. Initialization
6.2.2. Plotting
6.2.3. Main Strategy Code
6.3. Strategy Optimization
6.3.1. Timing Optimization
6.3.2. Rule Optimization
6.3.3. Parameter Optimization
7. Bollinger Bands Strategy
7.1. Initializing with coursea
7.2. Bollinger Bands Strategy Based on Base Class - Backtesting Framework
7.3. Discussion on Strategy Optimization Directions
7.3.1. Parameter Optimization
7.3.2. Trend Judgment
8. Grid Trading
8.1. Grid Trading - A Strategy Without Timing
8.2. Code Implementation
8.2.1. Initialization
8.2.2. Evaluation Function
8.2.3. Strategy Behavior Analysis
8.3. Technical Implementation Issues
8.3.1. Order Price and Trading Timing
8.3.2. Stock Split and Dividend Issues
8.3.3. Trading Units
8.4. Strategy Optimization: From 1.37% to 79.8%!
8.4.1. Selecting Underlying Assets with "Bounds"
8.4.1.1. Large-Cap Stocks
8.4.1.2. Oversold Stocks
8.4.1.3. Convertible Bonds
8.4.2. Determining Grid Parameters Based on Historical Data
8.4.3. Improving Capital Utilization Efficiency
8.5. Trend-Following Grid
9. Numpy and Pandas
9.1. Numpy
9.1.1. Creating Arrays
9.1.1.1. Vanilla Version
9.1.1.2. Pre-set Special Arrays
9.1.1.3. Converting from Existing Arrays
9.1.2. Inspecting Array Properties
9.1.3. Array Operations
9.1.3.1. Dimensionality Increase
9.1.3.2. Dimensionality Reduction
9.1.3.3. Transposition
9.1.3.4. Adding/Removing Elements
9.1.4. Logical Operations and Comparisons
9.1.5. Set Operations
9.1.6. Mathematical Operations
9.1.6.1. Dot Product
9.1.6.2. Aggregation Operations and Statistical Functions
9.1.7. Reading, Searching, and Lookup
9.1.7.1. Indexing and Slicing
9.1.7.2. Lookup, Replacement, and Filtering
9.1.8. Type Conversion and typing Module
9.1.9. Structured Arrays
9.1.10. IO
9.1.11. Common Functions in Quantitative Trading
9.1.11.1. REF(close, n)
9.1.11.2. EVERY(cond, n)
9.1.11.3. LAST(cond_list, n, m)
9.1.11.4. BARSLAST
9.1.11.5. CROSS
9.2. Pandas
9.2.1. Creation
9.2.2. Data Access
9.2.3. Iterating Over DataFrames
9.3. Pandas vs. Numpy
10. Ta-Lib
10.1. Installing Ta-Lib
10.1.1. Installing the Native Library
10.1.1.1. MacOS
10.1.1.2. Linux
10.1.1.3. Windows
10.1.1.4. Using Conda
10.1.1.5. Third-Party Built Wheel Packages
10.1.2. Installing the Python Wrapper
10.2. Ta-Lib Overview
10.2.1. About the Documentation
10.2.2. Two Types of Interfaces
10.2.3. Method Overview
10.3. Common Indicator Functions
10.3.1. ATR
10.3.2. Moving Averages
10.3.2.1. SMA
10.3.2.2. EMA
10.3.2.3. WMA
10.3.3. Bollinger Bands
10.3.4. MACD
10.3.5. RSI
10.3.6. OBV (On-Balance Volume)
10.4. Pattern Recognition Functions
10.4.1. CDL3LINESTRIKE
10.4.2. CDL3WHITESOLDIERS
Statistics and probability play an important role in quantitative analysis. Lessons 11–12 review the most commonly used statistical and probabilistic knowledge in quantitative trading, including moments from first to fourth, PDF/CDF, covariance, etc. In the examples of this chapter, we solve problems such as "Can we buy the dip when the Shanghai Composite Index drops by 4%?"
11. Data Analysis and Python Implementation (1)
11.1. Examining Data Distribution
11.1.1. Finding the Center of Data
11.1.1.1. Mean and Centroid
11.1.1.2. Median
11.1.1.3. Mode
11.1.2. Measuring the Dispersion of Quantitative Data
11.1.2.1. Quantiles
11.1.2.2. Variance and Standard Deviation
11.1.2.3. Frequency, PMF, PDF, CDF, PPF, and Histograms
11.1.2.4. Probability Density and Probability Density Function
11.1.2.5. Cumulative Probability and Cumulative Distribution Function (CDF)
11.1.2.6. CDF Estimation and Its Applications
11.1.2.7. Relationships Between Concepts
11.1.3. Distribution Shape of Data
11.1.4. Concept of Central Moments
11.1.5. Interpretation and Application of Skewness and Kurtosis in Investment
12. Data Analysis and Python Implementation (2)
12.1. Statistical Inference Methods
12.1.1. Quantile Plots
12.1.2. Hypothesis Testing Methods
12.2. Fitting, Regression, and Residuals
12.2.1. Residuals and Their Measurement
12.2.1.1. max_error
12.2.1.2. mean_absolute_error
12.2.1.3. mean_absolute_percentage_error
12.2.1.4. mean_squared_error
12.2.1.5. Rooted Mean Squared Error
12.2.2. Regression Analysis
12.3. Correlation
12.3.1. Covariance and Correlation Coefficients
12.3.2. Pearson Correlation and Spearman Correlation
12.3.3. Correlation Analysis Examples
12.4. Distance and Similarity
12.4.1. Listing Common Distance Definitions
12.4.2. How to Calculate Distance
12.5. Normalization
13. Practical Technical Analysis
Traditional technical analysis lacks strong theoretical support, but it is based on traders' experience and naturally has its merits. After learning Lessons 11–12, we apply the knowledge to technical analysis and find that it leads to new insights. With the support of statistical theory, traditional technical analysis significantly improves the robustness of algorithms and timing capabilities through adaptive parameters.
13.1. Box Detection
13.1.1. Statistics-Based Methods
13.1.2. Clustering-Based Algorithms
13.2. Finding Peaks and Valleys
13.2.1. Implementation in SciPy
13.2.2. Third-Party Library: Zigzag
13.2.3. How to Smooth Curves
13.2.4. Double-Top Pattern Detection
13.2.5. Rounding Bottom Detection
13.3. Convexity/Concavity Detection
14. Factor Analysis
Factors are features with predictive power. Factor analysis and testing are rapid screening methods for their characteristics and are mandatory knowledge for entering quantitative institutions. We manually implement each step of factor analysis step-by-step, and then introduce Alphalens, a common factor testing framework.
14.1. Factor Classification
14.2. Factor Analysis
14.2.1. Preprocessing
14.2.1.1. Outlier Clipping
14.2.1.2. Missing Values Handling
14.2.1.3. Distribution Adjustment
14.2.1.4. Standardization
14.2.1.5. Neutralization
14.3. Single-Factor Testing
14.3.1. Regression Method
14.3.1.1. Factor Evaluation Using Regression
14.3.2. IC Analysis Method
14.3.2.1. Factor Evaluation Using IC Analysis
14.3.3. Layered Backtest Method
14.3.4. Differences and Connections Between the Three Methods
14.4. Factor Evaluation System
15. Alphalens and Others
15.1. Alphalens
15.1.1. Alphalens Calling Process
15.1.2. Data Preprocessing
15.1.3. Factor Analysis
15.1.4. Common Errors and Warnings in Alphalens
15.1.4.1. Timezone Issues
15.1.4.2. MaxLossExceedError
15.1.4.3. FutureWarning
15.2. JQFactor and jqfactor-analyzer
15.3. Sympy
15.4. Statistics
15.5. Statsmodels
15.5.1. OLS (Ordinary Least Squares) Estimation
15.5.2. Comparing OLS with RLM
15.5.3. ARIMA Models and Time Series Forecasting
15.6. Zipline
15.7. Pyfolio
15.8. TA
Plotting is not just for creating beautiful visualizations but also for unleashing the full potential of data and revealing hidden insights. This is no different in quantitative trading. We need the ability to plot candlestick charts, overlay backtest signals for strategy tuning, and generate backtest reports.
16. Matplotlib Plotting
16.1. Introduction to Matplotlib
16.2. How Plots Are Constructed
16.2.1. Top-Level Concepts
16.2.2. Relationships Between pyplot, Figure, and Axes
16.2.3. Layout
16.2.4. Figure Anatomy
16.3. High-Frequency Usage Objects
16.3.1. Axis
16.3.1.1. Spine Positioning and Hiding
16.3.1.2. Sharing X-Axis
16.3.1.3. Ticks
16.3.2. Text and Chinese Characters
16.3.3. Styles and Colors
16.3.3.1. Colormaps
17. Plotly Plotting
17.1. Basic Concepts in Plotly
17.2. Plotly Module Structure
17.2.1. Plotly Express
17.2.2. Graph Objects
17.2.3. Others
17.3. Comparing Plotly Express and go.Figure
17.4. Plotting Stock Analysis Charts in Plotly
17.4.1. Candlestick Chart Plotting
17.4.2. Overlaying Technical Indicators
17.4.3. Subplots
17.4.4. Display Area
17.4.5. Interactive Tooltips
17.5. Colors
17.5.1. Discrete Color Sequences
17.5.2. Continuous Color Scales
17.6. Themes and Templates
17.7. Introduction to Dash
17.7.1. Hello World
17.7.2. Connecting to Data
17.7.3. Adding Interactive Controls
17.7.4. Beautifying Applications
17.7.5. Deep Dive into Dash
18. Seaborn and PyEcharts Plotting
18.1. Seaborn
18.1.1. Seaborn Plotting Overview
18.1.1.1. Visualizing Statistical Relationships
18.1.1.2. Visualizing Data Distributions
18.1.1.3. Visualizing Bivariate Distributions
18.1.1.4. Joint and Marginal Distributions
18.1.1.5. Regression Fitting
18.1.2. Themes
18.1.3. Using Palettes
18.1.3.1. Qualitative Palettes
18.1.3.2. Continuous Palettes
18.1.3.3. Diverging Palettes
18.2. PyEcharts
18.2.1. Running in Notebook/JupyterLab
18.2.2. Calling Conventions
18.2.3. Using Options
18.2.4. Subplots and Layouts
18.2.4.1. Grid Layout
18.2.4.2. Page Layout
18.2.4.3. Tab Layout
18.2.4.4. Timeline
18.3. On Colors and Aesthetics
Backtrader is the most famous open-source backtesting framework. Many institutions lack the R&D capability for internal quantitative investment research systems and often use backtrader for backtesting internally. We dedicate two lessons to explaining the usage of backtrader in depth.
19. Backtrader Backtesting Framework (1)
19.1. Quick Start
19.2. Backtrader Syntax Sugar
19.2.1. Time Series (Lines)
19.2.2. Operator Overloading
19.3. Data Feeds
19.3.1. GenericCSVData
19.3.2. Pandas Feed
19.3.3. Creating a Custom Feed
19.3.4. Adding New Data Columns
19.4. Multi-Timeframe Data
19.4.1. Comparing Multi-Timeframe Technical Indicators
19.5. Indicators
19.5.1. Built-In Indicator Library
19.5.2. Custom Indicators
19.5.2.1. Minimum Period
20. Backtrader Backtesting Framework (2)
20.1. Cerebro
20.1.1. Adding Loggers
20.1.2. Adding Observers
20.1.3. Execution and Plotting
20.2. Order
20.2.1. notify_order
20.3. Trading Agents
20.3.1. Querying Assets and Positions
20.3.2. Volume Limits
20.3.2.1. FixedSize
20.3.2.2. FixedBarPerc
20.3.2.3. BarPointPerc
20.3.3. Trading Timing - Cheat-On-Open
20.3.4. Trading Timing - Cheat-On-Close
20.3.5. Trading Functions
20.3.5.1. Standard Trading Functions
20.3.5.2. order_target Series
20.3.6. Portfolio Trading
20.3.7. OCO Orders
20.3.8. Slippage and Transaction Costs
20.3.8.1. Fixed Slippage
20.3.8.2. Percentage Slippage
20.3.9. Transaction Fees
20.4. Visualization
20.4.1. Observers
20.4.1.1. Broker Observer
20.4.1.2. BuySell Observer
20.4.1.3. Trade Observer
20.4.1.4. TimeReturn Observer
20.4.1.5. DrawDown Observer
20.4.1.6. Benchmark Observer
20.4.2. Custom Plotting
20.4.3. Collecting Backtest Data
20.5. Optimization
20.6. Summary
21. Strategy Backtest Evaluation
How to interpret backtest results? This is a strategy evaluation issue. Here we also answer a question: If your strategy goes live but performs below expectations, under what circumstances should you abort the strategy? This is a common interview question.
21.1. Return Rates
21.1.1. Simple Return Rates
21.1.2. Logarithmic Return Rates
21.1.3. Cumulative Returns
21.1.4. Aggregate Returns
21.1.5. Annual Return
21.2. Risk-Adjusted Return Rates
21.2.1. Sharpe Ratio
21.2.2. Relationship Between Sharpe Ratio and Asset Curve
21.2.3. Sortino Ratio
21.2.4. Max Drawdown
21.2.5. Relationship Between Sharpe and Max Drawdown
21.2.6. Annualized Volatility
21.2.7. Calmar Ratio
21.2.8. Omega Ratio
21.3. Benchmark-Related Metrics
21.3.1. Information Ratio
21.3.2. Alpha/Beta
21.4. Visualization of Strategy Evaluation
21.4.1. Metrics
21.4.2. Plots
21.4.3. Basic and Full
21.4.4. HTML
22. Backtesting Pitfalls
Have you heard of the phenomenon where backtests yield profits but live trading results in losses? How is it caused? In this section, we introduce backtesting pitfalls. With rich practical experience, we help you quickly compensate for the lack of live trading experience.
22.1. Survivorship Bias
22.2. Look-Ahead Bias
22.2.1. Reference Errors
22.2.2. Look-Ahead Pricing (Stealing Prices)
22.2.3. Look-Ahead Bias Caused by Price Adjustment
22.2.4. PIT (Point-in-Time) Data
22.3. Issues Caused by Price Adjustment
22.3.1. Necessity of Using Adjusted Price Data
22.3.2. Issues with Backward-Adjusted Prices
22.3.3. Other Issues Related to Price Adjustment
22.4. Trading Rules
22.4.1. T+1 Trading
22.4.2. Price Limits (Up/Down Limits)
22.5. Overfitting
22.6. Backtest Duration
22.7. Differences Between Backtesting and Live Trading
22.7.1. Signal Flickering
22.7.2. Impact Costs
22.7.3. Impossible Execution Prices
22.7.4. Matching Issues
22.8. Monopoly Backtesting Framework
22.8.1. Introduction to Backtesting Features
22.8.1.1. Architecture and Style
22.8.2. Strategy Framework
22.8.2.1. Data and Data Formats
22.8.2.2. Multi-Timeframe Data
22.8.2.3. Driving Mode and Performance
22.8.2.4. Backtest Reports
22.8.3. A Complete Strategy Example
22.8.4. Parameter Optimization
22.9. References
All preparations are ultimately for integrating with live trading. The last two lessons will introduce various integration solutions.