-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
multi_factor_strategy.py
180 lines (150 loc) · 5.92 KB
/
multi_factor_strategy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'''
Author: Charmve [email protected]
Date: 2023-04-16 01:28:13
LastEditors: Charmve [email protected]
LastEditTime: 2023-04-16 01:57:58
FilePath: /Qbot/pytrader/strategies/multi_factor_strategy.py
Version: 1.0.1
Blogs: charmve.blog.csdn.net
GitHub: https://github.com/Charmve
Description:
Copyright (c) 2023 by Charmve, All Rights Reserved.
Licensed under the MIT License.
'''
import backtrader as bt
import tushare as ts
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
class PEFetcher:
def __init__(self, ts):
self.ts = ts
def get_factor_data(self, symbol, date):
df = self.ts.get_k_data(symbol, start=date.strftime('%Y-%m-%d'), end=date.strftime('%Y-%m-%d'))
if len(df) > 0:
return df.iloc[0]['close'] / df.iloc[0]['eps']
else:
return np.nan
class PBFetcher:
def __init__(self, ts):
self.ts = ts
def get_factor_data(self, symbol, date):
df = self.ts.get_k_data(symbol, start=date.strftime('%Y-%m-%d'), end=date.strftime('%Y-%m-%d'))
if len(df) > 0:
return df.iloc[0]['close'] / df.iloc[0]['bvps']
else:
return np.nan
class ROEFetcher:
def __init__(self, ts):
self.ts = ts
def get_factor_data(self, symbol, date):
df = self.ts.pro_bar(
ts_code=symbol,
asset='E',
start_date=date.strftime('%Y%m%d'),
end_date=date.strftime('%Y%m%d')
)
if len(df) > 0:
return df.iloc[0]['roe']
else:
return np.nan
def get_data(symbol, data_fetchers, start_date, end_date):
ts.set_token('e96f18882532434b7692388cb028eb267d3f0d56845dc92eef06ea4a')
data = {}
dates = ts.trade_cal()
trade_dates = dates[dates.isOpen == 1]['calendarDate']
for date in trade_dates:
hist_data = ts.get_hist_data(symbol, start=date, end=date)
factors_data = {}
for factor in data_fetchers:
factor_data = factor['data_fetcher'].get_factor_data(hist_data, factor['code'])
factors_data[factor['code']] = factor_data
data[date] = factors_data
return data
class MultiFactorModelStrategy(bt.Strategy):
params = (
('num_stocks', 10),
)
def __init__(self, factors):
self.factors = factors
self.coef_ = None
def calculate_signal(self, data):
features = []
for factor in self.factors:
feature = factor['function'](data)
if 'scaling' in factor:
feature = factor['scaling'](feature)
features.append(feature)
X = np.array(features).T
y = data.close[-1] / data.close[-2] - 1 # 当日涨跌幅度作为因变量
reg = LinearRegression().fit(X, y)
return reg.coef_
def rebalance_portfolio(self):
# 获取当前所有股票的因子数据
factors_data = {}
for factor in self.factors:
data = factor['data_fetcher'].get_factor_data(factor['code'], self.data.datetime.date(0))
factors_data[factor['code']] = data
# 合并所有因子数据
merged_data = pd.concat(factors_data, axis=1)
# 计算每个股票的综合因子得分
scores = {}
for symbol in merged_data.index.levels[1]:
stock_factors = merged_data.loc[:, symbol].dropna()
score = np.dot(stock_factors, self.coef_)
scores[symbol] = score
# 选择得分最高的股票作为持仓
top_stocks = sorted(scores.items(), key=lambda x: x[1], reverse=True)[:self.params.num_stocks]
for symbol, _ in top_stocks:
current_price = self.data.close[0]
available_balance = self.broker.get_cash() / current_price
max_buy_volume = int(available_balance)
if max_buy_volume > 0:
buy_volume = min(100, max_buy_volume)
cost = current_price * buy_volume
commission = cost * 0.001
total_cost = cost + commission
self.buy(size=buy_volume)
def next(self):
if not self.position:
# 如果当前没有持仓,计算因子得分并重新调整组合
signal = self.calculate_signal(self.data)
self.coef_ = signal / np.sum(np.abs(signal))
self.rebalance_portfolio()
else:
# 如果当前有持仓,检查是否需要清仓
positions = self.broker.get_positions()
for symbol, position in positions.items():
if not self.getdatabyname(symbol).close:
continue
current_price = self.getdatabyname(symbol).close[0]
revenue = current_price * position.size
commission = revenue * 0.001
total_revenue = revenue - commission
self.sell(data=self.getdatabyname(symbol), size=position.size)
if __name__ == '__main__':
# 加载数据
ts.set_token('e96f18882532434b7692388cb028eb267d3f0d56845dc92eef06ea4a')
pro = ts.pro_api()
data_fetchers = [
{'code': 'pe_ttm', 'data_fetcher': PEFetcher(pro)},
{'code': 'pb', 'data_fetcher': PBFetcher(pro)},
{'code': 'roe_ttm', 'data_fetcher': ROEFetcher(pro)},
]
data = bt.feeds.PandasData(dataname=get_data('600438.SH', data_fetchers, '2018-01-01', '2021-09-01'))
# 添加策略
factors = [
{'code': 'pe_ttm', 'function': lambda data: data.pe_ttm},
{'code': 'pb', 'function': lambda data: data.pb},
{'code': 'roe_ttm', 'function': lambda data: data.roe_ttm},
]
cerebro = bt.Cerebro()
cerebro.addstrategy(MultiFactorModelStrategy, factors=factors)
# 设置初始资金和手续费
cerebro.broker.setcash(1000000.0)
cerebro.broker.setcommission(commission=0.001)
# 运行回测
cerebro.adddata(data)
cerebro.run()
# 输出回测结果
final_value = cerebro.broker.getvalue()