This article thoroughly introduces fundamental concepts, advantages, and limitations of quantitative investment, along with practical guidance on strategies and technical tools. It covers basic concepts such as data sources, preparation, and common analytical indicators. Additionally, it discusses considerations and risk management strategies for live trading. The tutorial provides a comprehensive guide from theory to practice, suitable for those looking to deepen their understanding of quantitative investment.
Introduction to Quantitative InvestmentQuantitative investment is a method of making investment decisions based on mathematical models and algorithms. The core of quantitative investment lies in the analysis of historical data to uncover patterns that can be applied in future markets. It involves extensive data processing, statistical analysis, and machine learning techniques to enhance the objectivity and scientific nature of decision-making.
Data sources for quantitative investment typically include:
Data preparation is a crucial step in quantitative investment, including the following steps:
Variables and Types
int
, float float
, string str
, list list
, dictionary dict
, etc.# Integer a = 10 # Float b = 3.14 # String c = "Hello, world!" # List d = [1, 2, 3, 4] # Dictionary e = {"name": "Alice", "age": 25}
Control Structures
if
, elif
, else
for
, while
Example Code
# Conditional Statements if a > 5: print("a > 5") elif a == 5: print("a == 5") else: print("a < 5") # Loop Statements for i in range(5): print(i) while a > 0: a -= 1 print(a)
Functions
def
Example Code
def add(a, b): return a + b result = add(3, 5) print(result) # Output: 8
Modules and Libraries
import
, from ... import
numpy
, pandas
, matplotlib
, scikit-learn
Example Code
import numpy as np import pandas as pd import matplotlib.pyplot as plt # Example Data data = np.random.rand(10) df = pd.DataFrame(data, columns=["values"]) # Data Visualization plt.plot(df["values"]) plt.show()
NumPy: Numerical computing library.
import numpy as np # Create array arr = np.array([1, 2, 3, 4, 5]) print(arr)
Pandas: Data analysis library.
import pandas as pd # Create DataFrame data = {'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35]} df = pd.DataFrame(data) print(df)
Matplotlib: Data visualization library.
import matplotlib.pyplot as plt # Plot line chart plt.plot([1, 2, 3, 4], [1, 4, 9, 16]) plt.xlabel('x label') plt.ylabel('y label') plt.title('Sample Plot') plt.show()
Scikit-learn: Machine learning library.
from sklearn.linear_model import LinearRegression import numpy as np # Train linear regression model X = np.array([[1], [2], [3], [4]]).reshape(-1, 1) y = np.array([2, 4, 6, 8]) model = LinearRegression() model.fit(X, y) print("Coefficients:", model.coef_)
Matplotlib: Basic plotting library in Python.
import matplotlib.pyplot as plt # Plot line chart plt.plot([1, 2, 3, 4], [1, 4, 9, 16]) plt.xlabel('x label') plt.ylabel('y label') plt.title('Sample Plot') plt.show()
Seaborn: Advanced plotting library based on Matplotlib, more user-friendly and aesthetically pleasing.
import seaborn as sns # Plot box plot tips = sns.load_dataset("tips") sns.boxplot(x="day", y="total_bill", data=tips) plt.show()
Plotly: Interactive plotting library.
import plotly.express as px # Plot scatter plot df = px.data.iris() fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species") fig.show()
Bokeh: Interactive plotting library, suitable for real-time data visualization.
from bokeh.plotting import figure, show # Plot line chart p = figure(title="Simple line plot", x_axis_label='x', y_axis_label='y') p.line([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], legend_label="Temp.", line_width=2) show(p)
import pandas as pd import numpy as np import matplotlib.pyplot as plt from backtesting import Backtest, Strategy # Load data def load_data(file_path): return pd.read_csv(file_path) data = load_data('data.csv') # Define strategy class SimpleStrategy(Strategy): def init(self): self.sma50 = self.I(lambda: self.data.Close.rolling(window=50).mean()) def next(self): if self.sma50[-1] < self.data.Close[-1] and self.sma50[-2] >= self.data.Close[-2]: self.buy() elif self.sma50[-1] > self.data.Close[-1] and self.sma50[-2] <= self.data.Close[-2]: self.sell() # Initialize backtest bt = Backtest(data, SimpleStrategy) # Execute backtest stats = bt.run() # Output backtest results print(stats)
import pandas as pd import numpy as np from backtesting import Backtest, Strategy # Load data data = pd.read_csv('stock_data.csv') # Define strategy class MovingAverageStrategy(Strategy): ma_window = 50 def init(self): self.ma = self.I(lambda: self.data.Close.rolling(window=self.ma_window).mean()) def next(self): if self.ma[-1] > self.ma[-2] and self.data.Close[-1] < self.ma[-1]: self.buy() elif self.ma[-1] < self.ma[-2] and self.data.Close[-1] > self.ma[-1]: self.sell() # Initialize backtest bt = Backtest(data, MovingAverageStrategy) # Execute backtest stats = bt.run() # Output backtest results print(stats)
Example Code
import pandas as pd from backtesting import Backtest, Strategy from backtesting.lib import resample_apply # Load data data = pd.read_csv('stock_data.csv') # Define strategy class SimpleStrategy(Strategy): ma_window = 50 def init(self): self.ma = self.I(lambda: self.data.Close.rolling(window=self.ma_window).mean()) def next(self): if self.data.Close[-1] > self.ma[-1] and self.portfolio.positions[self.data.symbol] == 0: self.buy() elif self.data.Close[-1] < self.ma[-1] and self.portfolio.positions[self.data.symbol] > 0: self.sell() # Initialize backtest bt = Backtest(data, SimpleStrategy) bt.run() # Output backtest results print(bt._stats)
Example Code
import pandas as pd from backtesting import Backtest, Strategy # Load data data = pd.read_csv('stock_data.csv') # Define strategy class RiskManagementStrategy(Strategy): ma_window = 50 stop_loss = 0.05 # 5% stop-loss take_profit = 0.1 # 10% take-profit def init(self): self.ma = self.I(lambda: self.data.Close.rolling(window=self.ma_window).mean()) def next(self): if self.data.Close[-1] > self.ma[-1] and self.portfolio.positions[self.data.symbol] == 0: self.buy(size=self.account.balance * 0.01) # Maximum 1% of funds elif self.data.Close[-1] < self.ma[-1] and self.portfolio.positions[self.data.symbol] > 0: self.sell() # Set stop-loss and take-profit for trade in self.trades: if trade.is_long: if trade.price * (1 + self.take_profit) < self.data.Close[-1]: self.sell(size=trade.size) elif trade.price * (1 - self.stop_loss) > self.data.Close[-1]: self.sell(size=trade.size) # Initialize backtest bt = Backtest(data, RiskManagementStrategy) bt.run() # Output backtest results print(bt._stats)
Account Setup
import pandas as pd from backtesting import Backtest, Strategy # Load data data = pd.read_csv('stock_data.csv') # Define strategy class SimpleStrategy(Strategy): ma_window = 50 def init(self): self.ma = self.I(lambda: self.data.Close.rolling(window=self.ma_window).mean()) def next(self): if self.data.Close[-1] > self.ma[-1] and self.portfolio.positions[self.data.symbol] == 0: self.buy(size=self.account.balance * 0.01) # 1% funding proportion elif self.data.Close[-1] < self.ma[-1] and self.portfolio.positions[self.data.symbol] > 0: self.sell() # Initialize backtest bt = Backtest(data, SimpleStrategy) bt.run() # Output backtest results print(bt._stats)
import pandas as pd from backtesting import Backtest, Strategy # Load data data = pd.read_csv('stock_data.csv') # Define strategy class SimpleStrategy(Strategy): ma_window = 50 def init(self): self.ma = self.I(lambda: self.data.Close.rolling(window=self.ma_window).mean()) def next(self): if self.data.Close[-1] > self.ma[-1] and self.portfolio.positions[self.data.symbol] == 0: self.buy(size=self.account.balance * 0.01) # 1% funding proportion elif self.data.Close[-1] < self.ma[-1] and self.portfolio.positions[self.data.symbol] > 0: self.sell() # Initialize backtest bt = Backtest(data, SimpleStrategy) bt.run() # Output backtest results print(bt._stats)Quantitative Investment Community and Learning Resources
import pandas as pd # Sample data data = pd.DataFrame({ 'date': pd.date_range(start='2020-01-01', periods=100), 'close': np.random.rand(100) * 100 }) # Data visualization plt.plot(data['date'], data['close']) plt.xlabel('Date') plt.ylabel('Close Price') plt.title('Stock Price Movement') plt.show()
This guide aims to provide a comprehensive understanding of quantitative investment, enabling practical application of related knowledge. We hope this guide will help you better understand and implement quantitative investment strategies.