-
Notifications
You must be signed in to change notification settings - Fork 15
/
FROEC-PB-CAP-HL.py
277 lines (245 loc) · 11.9 KB
/
FROEC-PB-CAP-HL.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#导入函数库
from jqdata import *
from jqlib.technical_analysis import *
from jqfactor import get_factor_values
import numpy as np
import pandas as pd
import statsmodels.api as sm
import datetime as dt
#初始化函数
def initialize(context):
# 设定基准
set_benchmark('000905.XSHG')
# 用真实价格交易
set_option('use_real_price', True)
# 打开防未来函数
set_option("avoid_future_data", True)
# 将滑点设置为0
set_slippage(FixedSlippage(0))
# 设置交易成本万分之三,不同滑点影响可在归因分析中查看
set_order_cost(OrderCost(open_tax=0, close_tax=0.001, open_commission=0.0003, close_commission=0.0003, close_today_commission=0, min_commission=5),type='fund')
# 过滤order中低于error级别的日志
log.set_level('order', 'error')
#初始化全局变量
g.stock_num = 10 #最大持仓数
g.limit_up_list = [] #记录持仓中涨停的股票
g.hold_list = [] #当前持仓的全部股票
g.history_hold_list = [] #过去一段时间内持仓过的股票
g.not_buy_again_list = [] #最近买过且涨停过的股票一段时间内不再买入
g.limit_days = 20 #不再买入的时间段天数
g.target_list = [] #开盘前预操作股票池
g.industry_control = True #过滤掉不看好的行业
g.industry_filter_list = ['钢铁I','煤炭I','石油石化I','采掘I', #重资产
'银行I','非银金融I','金融服务I', #高负债
'交运设备I','交通运输I','传媒I','环保I'] #盈利差
#列表中的行业选择为主观判断结果,如果g.industry_control为False,则上述列表不影响选股
# 设置交易运行时间
run_daily(prepare_stock_list, time='9:05', reference_security='000300.XSHG') #准备预操作股票池
run_weekly(weekly_adjustment, weekday=1, time='9:30', reference_security='000300.XSHG') #默认周一开盘调仓,收益最高
run_daily(check_limit_up, time='14:00', reference_security='000300.XSHG') #检查持仓中的涨停股是否需要卖出
run_daily(print_position_info, time='15:10', reference_security='000300.XSHG') #打印复盘信息
#1-1 选股模块
def get_stock_list(context):
yesterday = str(context.previous_date)
initial_list = get_all_securities().index.tolist()
initial_list = filter_new_stock(context,initial_list)
initial_list = filter_kcb_stock(context, initial_list)
initial_list = filter_st_stock(initial_list)
#PB过滤
q = query(valuation.code, valuation.pb_ratio, indicator.eps).filter(valuation.code.in_(initial_list)).order_by(valuation.pb_ratio.asc())
df = get_fundamentals(q)
df = df[df['eps']>0]
df = df[df['pb_ratio']>0]
pb_list = list(df.code)[:int(0.5*len(df.code))]
#ROEC过滤
#因为get_history_fundamentals有返回数据限制最多5000行,需要把pb_list拆分后查询再组合
interval = 1000 #count=5时,一组最多1000个,组数向下取整
pb_len = len(pb_list)
if pb_len <= interval:
df = get_history_fundamentals(pb_list, fields=[indicator.code, indicator.roe], watch_date=yesterday, count=5, interval='1q')
else:
df_num = pb_len // interval
df = get_history_fundamentals(pb_list[:interval], fields=[indicator.code, indicator.roe], watch_date=yesterday, count=5, interval='1q')
for i in range(df_num):
dfi = get_history_fundamentals(pb_list[interval*(i+1):min(pb_len,interval*(i+2))], fields=[indicator.code, indicator.roe], watch_date=yesterday, count=5, interval='1q')
df = df.append(dfi)
df = df.groupby('code').apply(lambda x:x.reset_index()).roe.unstack()
df['increase'] = 4*df.iloc[:,4] - df.iloc[:,0] - df.iloc[:,1] - df.iloc[:,2] - df.iloc[:,3]
df.dropna(inplace=True)
df.sort_values(by='increase',ascending=False, inplace=True)
temp_list = list(df.index)
temp_len = len(temp_list)
roe_list = temp_list[:int(0.1*temp_len)]
#行业过滤
if g.industry_control == True:
industry_df = get_stock_industry(roe_list, yesterday)
ROE_list = filter_industry(industry_df, g.industry_filter_list)
else:
ROE_list = roe_list
#市值排序
q = query(valuation.code,valuation.circulating_market_cap).filter(valuation.code.in_(ROE_list)).order_by(valuation.circulating_market_cap.asc())
df = get_fundamentals(q)
ROEC_list = list(df.code)
return ROEC_list
#1-2 行业过滤函数
def get_stock_industry(securities, watch_date, level='sw_l1', method='industry_name'):
industry_dict = get_industry(securities, watch_date)
industry_ser = pd.Series({k: v.get(level, {method: np.nan})[method] for k, v in industry_dict.items()})
industry_df = industry_ser.to_frame('industry')
return industry_df
def filter_industry(industry_df, select_industry, level='sw_l1', method='industry_name'):
filter_df = industry_df.query('industry != @select_industry')
filter_list = filter_df.index.tolist()
return filter_list
#1-3 准备股票池
def prepare_stock_list(context):
#1...2
#获取已持有列表
g.hold_list= []
for position in list(context.portfolio.positions.values()):
stock = position.security
g.hold_list.append(stock)
#获取最近一段时间持有过的股票列表
g.history_hold_list.append(g.hold_list)
if len(g.history_hold_list) >= g.limit_days:
g.history_hold_list = g.history_hold_list[-g.limit_days:]
temp_set = set()
for hold_list in g.history_hold_list:
for stock in hold_list:
temp_set.add(stock)
g.not_buy_again_list = list(temp_set)
#获取昨日涨停列表
if g.hold_list != []:
df = get_price(g.hold_list, end_date=context.previous_date, frequency='daily', fields=['close','high_limit'], count=1, panel=False, fill_paused=False)
df = df[df['close'] == df['high_limit']]
g.high_limit_list = list(df.code)
else:
g.high_limit_list = []
#1-4 整体调整持仓
def weekly_adjustment(context):
#1 #获取应买入列表
g.target_list = get_stock_list(context)[:10] #2
g.target_list = filter_paused_stock(g.target_list)
g.target_list = filter_limitup_stock(context, g.target_list)
g.target_list = filter_limitdown_stock(context, g.target_list)
#过滤最近买过且涨停过的股票
recent_limit_up_list = get_recent_limit_up_stock(context, g.target_list, g.limit_days)
black_list = list(set(g.not_buy_again_list).intersection(set(recent_limit_up_list)))
g.target_list = [stock for stock in g.target_list if stock not in black_list]
#截取不超过最大持仓数的股票量
g.target_list = g.target_list[:min(g.stock_num, len(g.target_list))]
#调仓卖出
for stock in g.hold_list:
if (stock not in g.target_list) and (stock not in g.high_limit_list):
log.info("卖出[%s]" % (stock))
position = context.portfolio.positions[stock]
close_position(position)
else:
log.info("已持有[%s]" % (stock))
#调仓买入
position_count = len(context.portfolio.positions)
target_num = len(g.target_list)
if target_num > position_count:
value = context.portfolio.cash / (target_num - position_count)
for stock in g.target_list:
if context.portfolio.positions[stock].total_amount == 0:
if open_position(stock, value):
if len(context.portfolio.positions) == target_num:
break
#1-5 调整昨日涨停股票
def check_limit_up(context):
now_time = context.current_dt
if g.high_limit_list != []:
#对昨日涨停股票观察到尾盘如不涨停则提前卖出,如果涨停即使不在应买入列表仍暂时持有
for stock in g.high_limit_list:
current_data = get_price(stock, end_date=now_time, frequency='1m', fields=['close','high_limit'], skip_paused=False, fq='pre', count=1, panel=False, fill_paused=True)
if current_data.iloc[0,0] < current_data.iloc[0,1]:
log.info("[%s]涨停打开,卖出" % (stock))
position = context.portfolio.positions[stock]
close_position(position)
else:
log.info("[%s]涨停,继续持有" % (stock))
#2-1 过滤停牌股票
def filter_paused_stock(stock_list):
current_data = get_current_data()
return [stock for stock in stock_list if not current_data[stock].paused]
#2-2 过滤ST及其他具有退市标签的股票
def filter_st_stock(stock_list):
current_data = get_current_data()
return [stock for stock in stock_list
if not current_data[stock].is_st
and 'ST' not in current_data[stock].name
and '*' not in current_data[stock].name
and '退' not in current_data[stock].name]
#2-3 获取最近N个交易日内有涨停的股票
def get_recent_limit_up_stock(context, stock_list, recent_days):
stat_date = context.previous_date
new_list = []
for stock in stock_list:
df = get_price(stock, end_date=stat_date, frequency='daily', fields=['close','high_limit'], count=recent_days, panel=False, fill_paused=False)
df = df[df['close'] == df['high_limit']]
if len(df) > 0:
new_list.append(stock)
return new_list
#2-4 过滤涨停的股票
def filter_limitup_stock(context, stock_list):
last_prices = history(1, unit='1m', field='close', security_list=stock_list)
current_data = get_current_data()
return [stock for stock in stock_list if stock in context.portfolio.positions.keys()
or last_prices[stock][-1] < current_data[stock].high_limit]
#2-5 过滤跌停的股票
def filter_limitdown_stock(context, stock_list):
last_prices = history(1, unit='1m', field='close', security_list=stock_list)
current_data = get_current_data()
return [stock for stock in stock_list if stock in context.portfolio.positions.keys()
or last_prices[stock][-1] > current_data[stock].low_limit]
#2-6 过滤科创板
def filter_kcb_stock(context, stock_list):
return [stock for stock in stock_list if stock[0:3] != '688']
#2-7 过滤次新股
def filter_new_stock(context,stock_list):
yesterday = context.previous_date
return [stock for stock in stock_list if not yesterday - get_security_info(stock).start_date < datetime.timedelta(days=250)]
#3-1 交易模块-自定义下单
def order_target_value_(security, value):
if value == 0:
log.debug("Selling out %s" % (security))
else:
log.debug("Order %s to value %f" % (security, value))
return order_target_value(security, value)
#3-2 交易模块-开仓
def open_position(security, value):
order = order_target_value_(security, value)
if order != None and order.filled > 0:
return True
return False
#3-3 交易模块-平仓
def close_position(position):
security = position.security
order = order_target_value_(security, 0) # 可能会因停牌失败
if order != None:
if order.status == OrderStatus.held and order.filled == order.amount:
return True
return False
#4-1 打印每日持仓信息
def print_position_info(context):
#打印当天成交记录
trades = get_trades()
for _trade in trades.values():
print('成交记录:'+str(_trade))
#打印账户信息
for position in list(context.portfolio.positions.values()):
securities=position.security
cost=position.avg_cost
price=position.price
ret=100*(price/cost-1)
value=position.value
amount=position.total_amount
print('代码:{}'.format(securities))
print('成本价:{}'.format(format(cost,'.2f')))
print('现价:{}'.format(price))
print('收益率:{}%'.format(format(ret,'.2f')))
print('持仓(股):{}'.format(amount))
print('市值:{}'.format(format(value,'.2f')))
print('———————————————————————————————————')
print('———————————————————————————————————————分割线————————————————————————————————————————')