# 开始时间
start_date = '2016-10-16'
# 结束时间
end_date = '2017-07-18'
instruments = D.instruments(start_date, end_date)
df = D.history_data(instruments, start_date, end_date, fields=['in_sse50'])
instruments = list(set(df[df['in_sse50'] == 1].instrument))
# 初始化账户
def initialize(context):
context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5)) # 设置手续费,买入成本为万分之三,卖出为千分之1.3
context.short_period = 5 # 短期均线
context.long_period = 50 # 长期均线
context.ins = context.instruments
context.weights = 1/len(context.instruments)
def handle_data(context, data):
# 长期均线值要有意义,需要在50根k线之后
if context.trading_day_index < context.long_period:
return
for k in context.instruments:
sid = context.symbol(k) # 将标的转化为equity格式
price = data.current(sid, 'price') # 最新价格
short_mavg = data.history(sid, 'price',context.short_period, '1d').mean() # 短期均线值
long_mavg = data.history(sid, 'price',context.long_period, '1d').mean() # 长期均线值
cash = context.portfolio.portfolio_value # 现金
cur_position = context.portfolio.positions[sid].amount # 持仓
# 交易逻辑
# 如果短期均线大于长期均线形成金叉,并且没有持仓,并且该股票可以交易
if short_mavg > long_mavg and cur_position == 0 and data.can_trade(sid):
context.order_target_percent(sid, context.weights) # 买入
# 如果短期均线小于长期均线形成死叉,并且有持仓,并且该股票可以交易
elif short_mavg < long_mavg and cur_position > 0 and data.can_trade(sid):
context.order_target_percent(sid, 0) # 全部卖出
strategy_a = M.trade.v4(
instruments=instruments,
start_date=start_date,
end_date=end_date,
initialize=initialize,
handle_data=handle_data,
order_price_field_buy='open', # 以开盘价买入
order_price_field_sell='open', # 以开盘价卖出
capital_base=1000000, # 本金
)
[2020-05-06 15:50:51.803782] INFO: moduleinvoker: backtest.v8 开始运行..
[2020-05-06 15:50:51.809249] INFO: backtest: biglearning backtest:V8.3.4
[2020-05-06 15:50:51.810267] INFO: backtest: product_type:stock by specified
[2020-05-06 15:50:51.950698] INFO: moduleinvoker: cached.v2 开始运行..
[2020-05-06 15:50:58.910083] INFO: backtest: 读取股票行情完成:152762
[2020-05-06 15:51:07.880939] INFO: moduleinvoker: cached.v2 运行完成[15.930217s].
[2020-05-06 15:51:07.971134] INFO: algo: TradingAlgorithm V1.6.7
[2020-05-06 15:51:08.038843] INFO: algo: trading transform...
[2020-05-06 15:51:34.264259] INFO: algo: handle_splits get splits [dt:2017-04-28 00:00:00+00:00] [asset:Equity(277 [600893.SHA]), ratio:0.9959063529968262]
[2020-05-06 15:51:34.265987] INFO: Position: position stock handle split[sid:277, orig_amount:400, new_amount:401.0, orig_cost:37.799999237262384, new_cost:37.65, ratio:0.9959063529968262, last_sale_price:34.05999755859375]
[2020-05-06 15:51:34.267675] INFO: Position: after split: PositionStock(asset:Equity(277 [600893.SHA]), amount:401.0, cost_basis:37.65, last_sale_price:34.20000076293945)
[2020-05-06 15:51:34.269246] INFO: Position: returning cash: 21.94
[2020-05-06 15:51:42.909610] INFO: algo: handle_splits get splits [dt:2017-06-09 00:00:00+00:00] [asset:Equity(189 [600048.SHA]), ratio:0.9691542387008667]
[2020-05-06 15:51:42.911013] INFO: Position: position stock handle split[sid:189, orig_amount:1700, new_amount:1754.0, orig_cost:9.720000268438381, new_cost:9.42, ratio:0.9691542387008667, last_sale_price:9.74000072479248]
[2020-05-06 15:51:42.912582] INFO: Position: after split: PositionStock(asset:Equity(189 [600048.SHA]), amount:1754.0, cost_basis:9.42, last_sale_price:10.050000190734863)
[2020-05-06 15:51:42.914373] INFO: Position: returning cash: 1.04
[2020-05-06 15:51:43.736541] INFO: algo: handle_splits get splits [dt:2017-06-13 00:00:00+00:00] [asset:Equity(59 [600340.SHA]), ratio:0.9814606308937073]
[2020-05-06 15:51:43.738394] INFO: Position: position stock handle split[sid:59, orig_amount:400, new_amount:407.0, orig_cost:35.07999801653347, new_cost:34.43, ratio:0.9814606308937073, last_sale_price:34.939998626708984]
[2020-05-06 15:51:43.739762] INFO: Position: after split: PositionStock(asset:Equity(59 [600340.SHA]), amount:407.0, cost_basis:34.43, last_sale_price:35.599998474121094)
[2020-05-06 15:51:43.740771] INFO: Position: returning cash: 19.42
[2020-05-06 15:51:44.125189] INFO: algo: handle_splits get splits [dt:2017-06-14 00:00:00+00:00] [asset:Equity(17 [601166.SHA]), ratio:0.9631197452545166]
[2020-05-06 15:51:44.126471] INFO: algo: handle_splits get splits [dt:2017-06-14 00:00:00+00:00] [asset:Equity(254 [600036.SHA]), ratio:0.9670965671539307]
[2020-05-06 15:51:44.127395] INFO: Position: position stock handle split[sid:17, orig_amount:1000, new_amount:1038.0, orig_cost:16.619998931970223, new_cost:16.01, ratio:0.9631197452545166, last_sale_price:15.929999351501465]
[2020-05-06 15:51:44.128475] INFO: Position: after split: PositionStock(asset:Equity(17 [601166.SHA]), amount:1038.0, cost_basis:16.01, last_sale_price:16.53999900817871)
[2020-05-06 15:51:44.129517] INFO: Position: returning cash: 4.66
[2020-05-06 15:51:44.130480] INFO: Position: position stock handle split[sid:254, orig_amount:800, new_amount:827.0, orig_cost:20.449998855778272, new_cost:19.78, ratio:0.9670965671539307, last_sale_price:21.750001907348633]
[2020-05-06 15:51:44.131372] INFO: Position: after split: PositionStock(asset:Equity(254 [600036.SHA]), amount:827.0, cost_basis:19.78, last_sale_price:22.489999771118164)
[2020-05-06 15:51:44.132210] INFO: Position: returning cash: 4.75
[2020-05-06 15:51:44.790547] INFO: algo: handle_splits get splits [dt:2017-06-16 00:00:00+00:00] [asset:Equity(15 [600104.SHA]), ratio:0.948145866394043]
[2020-05-06 15:51:44.791966] INFO: algo: handle_splits get splits [dt:2017-06-16 00:00:00+00:00] [asset:Equity(208 [601628.SHA]), ratio:0.9911565184593201]
[2020-05-06 15:51:44.792850] INFO: algo: handle_splits get splits [dt:2017-06-16 00:00:00+00:00] [asset:Equity(229 [601668.SHA]), ratio:0.9779872298240662]
[2020-05-06 15:51:44.793676] INFO: Position: position stock handle split[sid:15, orig_amount:600, new_amount:632.0, orig_cost:25.69999885649991, new_cost:24.37, ratio:0.948145866394043, last_sale_price:30.170001983642578]
[2020-05-06 15:51:44.794515] INFO: Position: after split: PositionStock(asset:Equity(15 [600104.SHA]), amount:632.0, cost_basis:24.37, last_sale_price:31.81999969482422)
[2020-05-06 15:51:44.795277] INFO: Position: returning cash: 24.56
[2020-05-06 15:51:44.796116] INFO: Position: position stock handle split[sid:208, orig_amount:600, new_amount:605.0, orig_cost:25.96000101089554, new_cost:25.73, ratio:0.9911565184593201, last_sale_price:26.89999008178711]
[2020-05-06 15:51:44.796945] INFO: Position: after split: PositionStock(asset:Equity(208 [601628.SHA]), amount:605.0, cost_basis:25.73, last_sale_price:27.14000129699707)
[2020-05-06 15:51:44.797722] INFO: Position: returning cash: 9.51
[2020-05-06 15:51:44.798544] INFO: Position: position stock handle split[sid:229, orig_amount:1700, new_amount:1738.0, orig_cost:9.609999656744284, new_cost:9.4, ratio:0.9779872298240662, last_sale_price:9.329997062683105]
[2020-05-06 15:51:44.799459] INFO: Position: after split: PositionStock(asset:Equity(229 [601668.SHA]), amount:1738.0, cost_basis:9.4, last_sale_price:9.539999008178711)
[2020-05-06 15:51:44.800403] INFO: Position: returning cash: 2.46
[2020-05-06 15:51:46.221267] INFO: algo: handle_splits get splits [dt:2017-06-22 00:00:00+00:00] [asset:Equity(273 [600606.SHA]), ratio:0.96875]
[2020-05-06 15:51:46.222462] INFO: Position: position stock handle split[sid:273, orig_amount:2100, new_amount:2167.0, orig_cost:7.940000057472207, new_cost:7.69, ratio:0.96875, last_sale_price:7.75]
[2020-05-06 15:51:46.223356] INFO: Position: after split: PositionStock(asset:Equity(273 [600606.SHA]), amount:2167.0, cost_basis:7.69, last_sale_price:8.0)
[2020-05-06 15:51:46.224105] INFO: Position: returning cash: 5.75
[2020-05-06 15:51:47.778192] INFO: algo: handle_splits get splits [dt:2017-06-29 00:00:00+00:00] [asset:Equity(14 [601006.SHA]), ratio:0.9700958132743835]
[2020-05-06 15:51:47.779493] INFO: algo: handle_splits get splits [dt:2017-06-29 00:00:00+00:00] [asset:Equity(187 [600518.SHA]), ratio:0.9908759593963623]
[2020-05-06 15:51:47.780534] INFO: algo: handle_splits get splits [dt:2017-06-29 00:00:00+00:00] [asset:Equity(331 [601198.SHA]), ratio:0.9913645386695862]
[2020-05-06 15:51:47.781563] INFO: Position: position stock handle split[sid:14, orig_amount:2300, new_amount:2370.0, orig_cost:7.500000000149344, new_cost:7.28, ratio:0.9700958132743835, last_sale_price:8.110000610351562]
[2020-05-06 15:51:47.782515] INFO: Position: after split: PositionStock(asset:Equity(14 [601006.SHA]), amount:2370.0, cost_basis:7.28, last_sale_price:8.359999656677246)
[2020-05-06 15:51:47.783410] INFO: Position: returning cash: 7.3
[2020-05-06 15:51:47.784323] INFO: Position: position stock handle split[sid:187, orig_amount:900, new_amount:908.0, orig_cost:17.569999709886226, new_cost:17.41, ratio:0.9908759593963623, last_sale_price:21.719999313354492]
[2020-05-06 15:51:47.791738] INFO: Position: after split: PositionStock(asset:Equity(187 [600518.SHA]), amount:908.0, cost_basis:17.41, last_sale_price:21.919998168945312)
[2020-05-06 15:51:47.792774] INFO: Position: returning cash: 6.24
[2020-05-06 15:51:47.793729] INFO: Position: position stock handle split[sid:331, orig_amount:900, new_amount:907.0, orig_cost:17.440000558880442, new_cost:17.29, ratio:0.9913645386695862, last_sale_price:17.220003128051758]
[2020-05-06 15:51:47.794674] INFO: Position: after split: PositionStock(asset:Equity(331 [601198.SHA]), amount:907.0, cost_basis:17.29, last_sale_price:17.3700008392334)
[2020-05-06 15:51:47.795531] INFO: Position: returning cash: 14.46
[2020-05-06 15:51:48.782944] INFO: algo: handle_splits get splits [dt:2017-07-04 00:00:00+00:00] [asset:Equity(267 [600795.SHA]), ratio:0.9694443941116333]
[2020-05-06 15:51:48.784112] INFO: algo: handle_splits get splits [dt:2017-07-04 00:00:00+00:00] [asset:Equity(20 [601881.SHA]), ratio:0.9874271154403687]
[2020-05-06 15:51:48.785160] INFO: Position: position stock handle split[sid:267, orig_amount:4800, new_amount:4951.0, orig_cost:3.390000105742071, new_cost:3.29, ratio:0.9694443941116333, last_sale_price:3.490000009536743]
[2020-05-06 15:51:48.786156] INFO: Position: after split: PositionStock(asset:Equity(267 [600795.SHA]), amount:4951.0, cost_basis:3.29, last_sale_price:3.6000001430511475)
[2020-05-06 15:51:48.792040] INFO: Position: returning cash: 1.01
[2020-05-06 15:51:48.793393] INFO: Position: position stock handle split[sid:20, orig_amount:1300, new_amount:1316.0, orig_cost:12.319999697064077, new_cost:12.17, ratio:0.9874271154403687, last_sale_price:11.78000545501709]
[2020-05-06 15:51:48.794590] INFO: Position: after split: PositionStock(asset:Equity(20 [601881.SHA]), amount:1316.0, cost_basis:12.17, last_sale_price:11.930000305175781)
[2020-05-06 15:51:48.795717] INFO: Position: returning cash: 6.51
[2020-05-06 15:51:49.090625] INFO: algo: handle_splits get splits [dt:2017-07-05 00:00:00+00:00] [asset:Equity(299 [601818.SHA]), ratio:0.9751861691474915]
[2020-05-06 15:51:49.091971] INFO: Position: position stock handle split[sid:299, orig_amount:4100, new_amount:4204.0, orig_cost:4.010000229193093, new_cost:3.91, ratio:0.9751861691474915, last_sale_price:3.9300005435943604]
[2020-05-06 15:51:49.093107] INFO: Position: after split: PositionStock(asset:Equity(299 [601818.SHA]), amount:4204.0, cost_basis:3.91, last_sale_price:4.03000020980835)
[2020-05-06 15:51:49.094110] INFO: Position: returning cash: 1.28
[2020-05-06 15:51:49.376176] INFO: algo: handle_splits get splits [dt:2017-07-06 00:00:00+00:00] [asset:Equity(259 [600100.SHA]), ratio:0.9822945594787598]
[2020-05-06 15:51:49.377311] INFO: algo: handle_splits get splits [dt:2017-07-06 00:00:00+00:00] [asset:Equity(34 [600016.SHA]), ratio:0.9804877638816833]
[2020-05-06 15:51:49.378350] INFO: Position: position stock handle split[sid:259, orig_amount:1200, new_amount:1221.0, orig_cost:13.830000878140584, new_cost:13.59, ratio:0.9822945594787598, last_sale_price:13.869999885559082]
[2020-05-06 15:51:49.379322] INFO: Position: after split: PositionStock(asset:Equity(259 [600100.SHA]), amount:1221.0, cost_basis:13.59, last_sale_price:14.120000839233398)
[2020-05-06 15:51:49.380134] INFO: Position: returning cash: 8.73
[2020-05-06 15:51:49.380946] INFO: Position: position stock handle split[sid:34, orig_amount:2100, new_amount:2141.0, orig_cost:8.03999996275537, new_cost:7.88, ratio:0.9804877638816833, last_sale_price:8.039999008178711]
[2020-05-06 15:51:49.381750] INFO: Position: after split: PositionStock(asset:Equity(34 [600016.SHA]), amount:2141.0, cost_basis:7.88, last_sale_price:8.199999809265137)
[2020-05-06 15:51:49.382531] INFO: Position: returning cash: 6.36
[2020-05-06 15:51:49.656836] INFO: algo: handle_splits get splits [dt:2017-07-07 00:00:00+00:00] [asset:Equity(64 [601328.SHA]), ratio:0.9563814401626587]
[2020-05-06 15:51:49.658074] INFO: Position: position stock handle split[sid:64, orig_amount:2700, new_amount:2823.0, orig_cost:6.190000057547187, new_cost:5.92, ratio:0.9563814401626587, last_sale_price:5.920001029968262]
[2020-05-06 15:51:49.660028] INFO: Position: after split: PositionStock(asset:Equity(64 [601328.SHA]), amount:2823.0, cost_basis:5.92, last_sale_price:6.190000057220459)
[2020-05-06 15:51:49.660842] INFO: Position: returning cash: 0.84
[2020-05-06 15:51:50.028065] INFO: algo: handle_splits get splits [dt:2017-07-10 00:00:00+00:00] [asset:Equity(49 [601088.SHA]), ratio:0.8667565584182739]
[2020-05-06 15:51:50.029668] INFO: Position: position stock handle split[sid:49, orig_amount:900, new_amount:1038.0, orig_cost:19.500000000056676, new_cost:16.9, ratio:0.8667565584182739, last_sale_price:19.320003509521484]
[2020-05-06 15:51:50.030897] INFO: Position: after split: PositionStock(asset:Equity(49 [601088.SHA]), amount:1038.0, cost_basis:16.9, last_sale_price:22.28999900817871)
[2020-05-06 15:51:50.032020] INFO: Position: returning cash: 6.84
[2020-05-06 15:51:50.348337] INFO: algo: handle_splits get splits [dt:2017-07-11 00:00:00+00:00] [asset:Equity(134 [601318.SHA]), ratio:0.98924320936203]
[2020-05-06 15:51:50.349559] INFO: algo: handle_splits get splits [dt:2017-07-11 00:00:00+00:00] [asset:Equity(177 [601398.SHA]), ratio:0.9560229778289795]
[2020-05-06 15:51:50.350627] INFO: Position: position stock handle split[sid:134, orig_amount:400, new_amount:404.0, orig_cost:37.489997863807915, new_cost:37.09, ratio:0.98924320936203, last_sale_price:50.58000564575195]
[2020-05-06 15:51:50.351678] INFO: Position: after split: PositionStock(asset:Equity(134 [601318.SHA]), amount:404.0, cost_basis:37.09, last_sale_price:51.130001068115234)
[2020-05-06 15:51:50.352707] INFO: Position: returning cash: 17.68
[2020-05-06 15:51:50.353648] INFO: Position: position stock handle split[sid:177, orig_amount:3700, new_amount:3870.0, orig_cost:4.449999809956782, new_cost:4.25, ratio:0.9560229778289795, last_sale_price:4.999999523162842]
[2020-05-06 15:51:50.354582] INFO: Position: after split: PositionStock(asset:Equity(177 [601398.SHA]), amount:3870.0, cost_basis:4.25, last_sale_price:5.229999542236328)
[2020-05-06 15:51:50.355404] INFO: Position: returning cash: 1.0
[2020-05-06 15:51:50.672968] INFO: algo: handle_splits get splits [dt:2017-07-12 00:00:00+00:00] [asset:Equity(57 [601169.SHA]), ratio:0.8108974099159241]
[2020-05-06 15:51:50.674110] INFO: Position: position stock handle split[sid:57, orig_amount:1800, new_amount:2219.0, orig_cost:9.069999696604178, new_cost:7.35, ratio:0.8108974099159241, last_sale_price:7.589999675750732]
[2020-05-06 15:51:50.675063] INFO: Position: after split: PositionStock(asset:Equity(57 [601169.SHA]), amount:2219.0, cost_basis:7.35, last_sale_price:9.359999656677246)
[2020-05-06 15:51:50.675892] INFO: Position: returning cash: 5.79
[2020-05-06 15:51:51.119625] INFO: algo: handle_splits get splits [dt:2017-07-13 00:00:00+00:00] [asset:Equity(234 [601288.SHA]), ratio:0.9526463747024536]
[2020-05-06 15:51:51.120889] INFO: Position: position stock handle split[sid:234, orig_amount:5200, new_amount:5458.0, orig_cost:3.189999819682898, new_cost:3.04, ratio:0.9526463747024536, last_sale_price:3.4200003147125244]
[2020-05-06 15:51:51.121959] INFO: Position: after split: PositionStock(asset:Equity(234 [601288.SHA]), amount:5458.0, cost_basis:3.04, last_sale_price:3.5899999141693115)
[2020-05-06 15:51:51.122934] INFO: Position: returning cash: 1.64
[2020-05-06 15:51:51.446326] INFO: algo: handle_splits get splits [dt:2017-07-14 00:00:00+00:00] [asset:Equity(0 [601988.SHA]), ratio:0.9553804993629456]
[2020-05-06 15:51:51.447436] INFO: algo: handle_splits get splits [dt:2017-07-14 00:00:00+00:00] [asset:Equity(244 [601669.SHA]), ratio:0.9890905618667603]
[2020-05-06 15:51:51.448329] INFO: Position: position stock handle split[sid:0, orig_amount:4600, new_amount:4814.0, orig_cost:3.6399998667026114, new_cost:3.48, ratio:0.9553804993629456, last_sale_price:3.6399996280670166]
[2020-05-06 15:51:51.449126] INFO: Position: after split: PositionStock(asset:Equity(0 [601988.SHA]), amount:4814.0, cost_basis:3.48, last_sale_price:3.809999942779541)
[2020-05-06 15:51:51.449897] INFO: Position: returning cash: 3.04
[2020-05-06 15:51:52.642155] INFO: Performance: Simulated 186 trading days out of 186.
[2020-05-06 15:51:52.643538] INFO: Performance: first open: 2016-10-17 09:30:00+00:00
[2020-05-06 15:51:52.646049] INFO: Performance: last close: 2017-07-18 15:00:00+00:00
[2020-05-06 15:52:03.633574] INFO: moduleinvoker: backtest.v8 运行完成[71.829781s].
[2020-05-06 15:52:03.635208] INFO: moduleinvoker: trade.v4 运行完成[71.909634s].
# 获取股票代码
instruments = D.instruments()
# 确定起始时间
start_date = '2016-10-16'
# 结束时间
end_date = '2017-07-18'
# 获取股票总市值数据,返回DataFrame数据格式
market_cap_data = D.history_data(instruments,start_date,end_date,
fields=['pb_lf','amount'])
# 获取每日按小市值排序 (从低到高)的前三十只股票
daily_buy_stock = market_cap_data.groupby('date').apply(lambda df:df[(df['amount'] > 0)].sort_values('pb_lf')[:30])
# 回测参数设置,initialize函数只运行一次
def initialize(context):
# 手续费设置
context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
# 调仓规则(每月的第一天调仓)
context.schedule_function(rebalance, date_rule=date_rules.month_start(days_offset=0))
# 传入 整理好的调仓股票数据
context.daily_buy_stock = daily_buy_stock
# handle_data函数会每天运行一次
def handle_data(context,data):
pass
# 换仓函数
def rebalance(context, data):
# 当前的日期
date = data.current_dt.strftime('%Y-%m-%d')
# 根据日期获取调仓需要买入的股票的列表
stock_to_buy = list(context.daily_buy_stock.ix[date].instrument)
# 通过positions对象,使用列表生成式的方法获取目前持仓的股票列表
stock_hold_now = [equity.symbol for equity in context.portfolio.positions]
# 继续持有的股票:调仓时,如果买入的股票已经存在于目前的持仓里,那么应继续持有
no_need_to_sell = [i for i in stock_hold_now if i in stock_to_buy]
# 需要卖出的股票
stock_to_sell = [i for i in stock_hold_now if i not in no_need_to_sell]
# 卖出
for stock in stock_to_sell:
# 如果该股票停牌,则没法成交。因此需要用can_trade方法检查下该股票的状态
# 如果返回真值,则可以正常下单,否则会出错
# 因为stock是字符串格式,我们用symbol方法将其转化成平台可以接受的形式:Equity格式
if data.can_trade(context.symbol(stock)):
# order_target_percent是平台的一个下单接口,表明下单使得该股票的权重为0,
# 即卖出全部股票,可参考回测文档
context.order_target_percent(context.symbol(stock), 0)
# 如果当天没有买入的股票,就返回
if len(stock_to_buy) == 0:
return
# 等权重买入
weight = 1 / len(stock_to_buy)
# 买入
for stock in stock_to_buy:
if data.can_trade(context.symbol(stock)):
# 下单使得某只股票的持仓权重达到weight,因为
# weight大于0,因此是等权重买入
context.order_target_percent(context.symbol(stock), weight)
strategy_b = M.trade.v4(
instruments=instruments,
start_date=start_date,
end_date=end_date,
# 必须传入initialize,只在第一天运行
initialize=initialize,
# 必须传入handle_data,每个交易日都会运行
handle_data=handle_data,
# 买入以开盘价成交
order_price_field_buy='open',
# 卖出也以开盘价成交
order_price_field_sell='open',
# 策略本金
capital_base=1000000,
# 比较基准:沪深300
benchmark='000300.INDX',
)
[2020-05-06 15:53:04.075888] INFO: moduleinvoker: backtest.v8 开始运行..
[2020-05-06 15:53:04.080492] INFO: backtest: biglearning backtest:V8.3.4
[2020-05-06 15:53:04.081567] INFO: backtest: product_type:stock by specified
[2020-05-06 15:53:04.186685] INFO: moduleinvoker: cached.v2 开始运行..
[2020-05-06 15:53:13.801627] INFO: backtest: 读取股票行情完成:1417795
[2020-05-06 15:53:17.408483] INFO: moduleinvoker: cached.v2 运行完成[13.221781s].
[2020-05-06 15:53:18.185686] INFO: algo: TradingAlgorithm V1.6.7
[2020-05-06 15:53:18.675218] INFO: algo: trading transform...
[2020-05-06 15:53:19.823372] INFO: algo: handle_splits get splits [dt:2017-05-24 00:00:00+00:00] [asset:Equity(1142 [600823.SHA]), ratio:0.7059701681137085]
[2020-05-06 15:53:19.825297] INFO: Position: position stock handle split[sid:1142, orig_amount:5000, new_amount:7082.0, orig_cost:7.143396207973185, new_cost:5.04, ratio:0.7059701681137085, last_sale_price:4.7300004959106445]
[2020-05-06 15:53:19.826761] INFO: Position: after split: PositionStock(asset:Equity(1142 [600823.SHA]), amount:7082.0, cost_basis:5.04, last_sale_price:6.700000286102295)
[2020-05-06 15:53:19.827731] INFO: Position: returning cash: 2.14
[2020-05-06 15:53:19.836333] INFO: algo: handle_splits get splits [dt:2017-05-25 00:00:00+00:00] [asset:Equity(1442 [600000.SHA]), ratio:0.7595345377922058]
[2020-05-06 15:53:19.837437] INFO: algo: handle_splits get splits [dt:2017-05-25 00:00:00+00:00] [asset:Equity(575 [600269.SHA]), ratio:0.9707603454589844]
[2020-05-06 15:53:19.838422] INFO: Position: position stock handle split[sid:1442, orig_amount:2300, new_amount:3028.0, orig_cost:15.604782609538468, new_cost:11.85, ratio:0.7595345377922058, last_sale_price:11.75]
[2020-05-06 15:53:19.839316] INFO: Position: after split: PositionStock(asset:Equity(1442 [600000.SHA]), amount:3028.0, cost_basis:11.85, last_sale_price:15.470001220703125)
[2020-05-06 15:53:19.840163] INFO: Position: returning cash: 2.0
[2020-05-06 15:53:19.841043] INFO: Position: position stock handle split[sid:575, orig_amount:6600, new_amount:6798.0, orig_cost:4.850986252451417, new_cost:4.71, ratio:0.9707603454589844, last_sale_price:4.980000019073486]
[2020-05-06 15:53:19.841913] INFO: Position: after split: PositionStock(asset:Equity(575 [600269.SHA]), amount:6798.0, cost_basis:4.71, last_sale_price:5.12999963760376)
[2020-05-06 15:53:19.842741] INFO: Position: returning cash: 3.96
[2020-05-06 15:53:19.974132] INFO: algo: handle_splits get splits [dt:2017-06-14 00:00:00+00:00] [asset:Equity(2623 [600019.SHA]), ratio:0.9680852293968201]
[2020-05-06 15:53:19.975320] INFO: Position: position stock handle split[sid:2623, orig_amount:5500, new_amount:5681.0, orig_cost:6.259999761649258, new_cost:6.06, ratio:0.9680852293968201, last_sale_price:6.370001316070557]
[2020-05-06 15:53:19.976310] INFO: Position: after split: PositionStock(asset:Equity(2623 [600019.SHA]), amount:5681.0, cost_basis:6.06, last_sale_price:6.580000400543213)
[2020-05-06 15:53:19.977180] INFO: Position: returning cash: 2.03
[2020-05-06 15:53:19.992784] INFO: algo: handle_splits get splits [dt:2017-06-15 00:00:00+00:00] [asset:Equity(2166 [600308.SHA]), ratio:0.9905661344528198]
[2020-05-06 15:53:19.994243] INFO: Position: position stock handle split[sid:2166, orig_amount:6900, new_amount:6965.0, orig_cost:5.049999817112266, new_cost:5.0, ratio:0.9905661344528198, last_sale_price:5.250000476837158]
[2020-05-06 15:53:19.995567] INFO: Position: after split: PositionStock(asset:Equity(2166 [600308.SHA]), amount:6965.0, cost_basis:5.0, last_sale_price:5.300000190734863)
[2020-05-06 15:53:19.996785] INFO: Position: returning cash: 3.75
[2020-05-06 15:53:20.031354] INFO: algo: handle_splits get splits [dt:2017-06-23 00:00:00+00:00] [asset:Equity(1265 [600508.SHA]), ratio:0.9911348819732666]
[2020-05-06 15:53:20.032627] INFO: Position: position stock handle split[sid:1265, orig_amount:3200, new_amount:3228.0, orig_cost:10.970302976612976, new_cost:10.87, ratio:0.9911348819732666, last_sale_price:11.180001258850098]
[2020-05-06 15:53:20.033670] INFO: Position: after split: PositionStock(asset:Equity(1265 [600508.SHA]), amount:3228.0, cost_basis:10.87, last_sale_price:11.279999732971191)
[2020-05-06 15:53:20.034703] INFO: Position: returning cash: 6.96
[2020-05-06 15:53:20.056142] INFO: algo: handle_splits get splits [dt:2017-06-28 00:00:00+00:00] [asset:Equity(2155 [000543.SZA]), ratio:0.9915397763252258]
[2020-05-06 15:53:20.057417] INFO: Position: position stock handle split[sid:2155, orig_amount:6400, new_amount:6454.0, orig_cost:5.459999818642441, new_cost:5.41, ratio:0.9915397763252258, last_sale_price:5.860000133514404]
[2020-05-06 15:53:20.058590] INFO: Position: after split: PositionStock(asset:Equity(2155 [000543.SZA]), amount:6454.0, cost_basis:5.41, last_sale_price:5.909999847412109)
[2020-05-06 15:53:20.059835] INFO: Position: returning cash: 3.56
[2020-05-06 15:53:20.096983] INFO: algo: handle_splits get splits [dt:2017-06-29 00:00:00+00:00] [asset:Equity(1315 [600015.SHA]), ratio:0.8202643990516663]
[2020-05-06 15:53:20.098796] INFO: Position: position stock handle split[sid:1315, orig_amount:3200, new_amount:3901.0, orig_cost:10.540694881382015, new_cost:8.65, ratio:0.8202643990516663, last_sale_price:9.310001373291016]
[2020-05-06 15:53:20.100343] INFO: Position: after split: PositionStock(asset:Equity(1315 [600015.SHA]), amount:3901.0, cost_basis:8.65, last_sale_price:11.350000381469727)
[2020-05-06 15:53:20.101719] INFO: Position: returning cash: 1.69
[2020-05-06 15:53:20.116764] INFO: algo: handle_splits get splits [dt:2017-06-30 00:00:00+00:00] [asset:Equity(980 [000898.SZA]), ratio:0.9876326322555542]
[2020-05-06 15:53:20.118282] INFO: algo: handle_splits get splits [dt:2017-06-30 00:00:00+00:00] [asset:Equity(2269 [600805.SHA]), ratio:0.9866844415664673]
[2020-05-06 15:53:20.119750] INFO: Position: position stock handle split[sid:980, orig_amount:6800, new_amount:6885.0, orig_cost:4.675849280832558, new_cost:4.62, ratio:0.9876326322555542, last_sale_price:5.590000629425049]
[2020-05-06 15:53:20.121239] INFO: Position: after split: PositionStock(asset:Equity(980 [000898.SZA]), amount:6885.0, cost_basis:4.62, last_sale_price:5.659999847412109)
[2020-05-06 15:53:20.122508] INFO: Position: returning cash: 0.85
[2020-05-06 15:53:20.123847] INFO: Position: position stock handle split[sid:2269, orig_amount:4900, new_amount:4966.0, orig_cost:7.0700015415205435, new_cost:6.98, ratio:0.9866844415664673, last_sale_price:7.410000324249268]
[2020-05-06 15:53:20.125079] INFO: Position: after split: PositionStock(asset:Equity(2269 [600805.SHA]), amount:4966.0, cost_basis:6.98, last_sale_price:7.510000228881836)
[2020-05-06 15:53:20.126270] INFO: Position: returning cash: 0.94
[2020-05-06 15:53:20.138970] INFO: algo: handle_splits get splits [dt:2017-07-03 00:00:00+00:00] [asset:Equity(666 [600026.SHA]), ratio:0.9713425636291504]
[2020-05-06 15:53:20.140081] INFO: Position: position stock handle split[sid:666, orig_amount:5600, new_amount:5765.0, orig_cost:6.448571295122949, new_cost:6.26, ratio:0.9713425636291504, last_sale_price:6.440001487731934]
[2020-05-06 15:53:20.140962] INFO: Position: after split: PositionStock(asset:Equity(666 [600026.SHA]), amount:5765.0, cost_basis:6.26, last_sale_price:6.630000114440918)
[2020-05-06 15:53:20.192350] INFO: Position: returning cash: 1.39
[2020-05-06 15:53:20.323131] INFO: algo: handle_splits get splits [dt:2017-07-05 00:00:00+00:00] [asset:Equity(717 [601818.SHA]), ratio:0.9751861691474915]
[2020-05-06 15:53:20.324841] INFO: Position: position stock handle split[sid:717, orig_amount:9200, new_amount:9434.0, orig_cost:3.878527564070901, new_cost:3.78, ratio:0.9751861691474915, last_sale_price:3.9300005435943604]
[2020-05-06 15:53:20.326216] INFO: Position: after split: PositionStock(asset:Equity(717 [601818.SHA]), amount:9434.0, cost_basis:3.78, last_sale_price:4.03000020980835)
[2020-05-06 15:53:20.327434] INFO: Position: returning cash: 0.38
[2020-05-06 15:53:20.341388] INFO: algo: handle_splits get splits [dt:2017-07-06 00:00:00+00:00] [asset:Equity(274 [600755.SHA]), ratio:0.9864253401756287]
[2020-05-06 15:53:20.342537] INFO: algo: handle_splits get splits [dt:2017-07-06 00:00:00+00:00] [asset:Equity(948 [600016.SHA]), ratio:0.9804877638816833]
[2020-05-06 15:53:20.392493] INFO: Position: position stock handle split[sid:274, orig_amount:4000, new_amount:4055.0, orig_cost:8.320000715016663, new_cost:8.21, ratio:0.9864253401756287, last_sale_price:8.720000267028809]
[2020-05-06 15:53:20.394022] INFO: Position: after split: PositionStock(asset:Equity(274 [600755.SHA]), amount:4055.0, cost_basis:8.21, last_sale_price:8.84000015258789)
[2020-05-06 15:53:20.395287] INFO: Position: returning cash: 0.4
[2020-05-06 15:53:20.396594] INFO: Position: position stock handle split[sid:948, orig_amount:4500, new_amount:4589.0, orig_cost:8.523269642570007, new_cost:8.36, ratio:0.9804877638816833, last_sale_price:8.039999008178711]
[2020-05-06 15:53:20.397824] INFO: Position: after split: PositionStock(asset:Equity(948 [600016.SHA]), amount:4589.0, cost_basis:8.36, last_sale_price:8.199999809265137)
[2020-05-06 15:53:20.399050] INFO: Position: returning cash: 4.44
[2020-05-06 15:53:20.412264] INFO: algo: handle_splits get splits [dt:2017-07-07 00:00:00+00:00] [asset:Equity(1025 [601328.SHA]), ratio:0.9563814401626587]
[2020-05-06 15:53:20.413829] INFO: Position: position stock handle split[sid:1025, orig_amount:6000, new_amount:6273.0, orig_cost:5.703785241455276, new_cost:5.45, ratio:0.9563814401626587, last_sale_price:5.920001029968262]
[2020-05-06 15:53:20.415229] INFO: Position: after split: PositionStock(asset:Equity(1025 [601328.SHA]), amount:6273.0, cost_basis:5.45, last_sale_price:6.190000057220459)
[2020-05-06 15:53:20.416483] INFO: Position: returning cash: 3.83
[2020-05-06 15:53:20.438686] INFO: algo: handle_splits get splits [dt:2017-07-11 00:00:00+00:00] [asset:Equity(3141 [601398.SHA]), ratio:0.9560229778289795]
[2020-05-06 15:53:20.439786] INFO: Position: position stock handle split[sid:3141, orig_amount:7100, new_amount:7426.0, orig_cost:5.239999772551947, new_cost:5.01, ratio:0.9560229778289795, last_sale_price:4.999999523162842]
[2020-05-06 15:53:20.440761] INFO: Position: after split: PositionStock(asset:Equity(3141 [601398.SHA]), amount:7426.0, cost_basis:5.01, last_sale_price:5.229999542236328)
[2020-05-06 15:53:20.492276] INFO: Position: returning cash: 3.0
[2020-05-06 15:53:20.503896] INFO: algo: handle_splits get splits [dt:2017-07-12 00:00:00+00:00] [asset:Equity(2793 [601169.SHA]), ratio:0.8108974099159241]
[2020-05-06 15:53:20.505498] INFO: Position: position stock handle split[sid:2793, orig_amount:4000, new_amount:4932.0, orig_cost:9.149999636782415, new_cost:7.42, ratio:0.8108974099159241, last_sale_price:7.589999675750732]
[2020-05-06 15:53:20.506830] INFO: Position: after split: PositionStock(asset:Equity(2793 [601169.SHA]), amount:4932.0, cost_basis:7.42, last_sale_price:9.359999656677246)
[2020-05-06 15:53:20.508314] INFO: Position: returning cash: 6.12
[2020-05-06 15:53:20.521999] INFO: algo: handle_splits get splits [dt:2017-07-13 00:00:00+00:00] [asset:Equity(1473 [601288.SHA]), ratio:0.9526463747024536]
[2020-05-06 15:53:20.523778] INFO: Position: position stock handle split[sid:1473, orig_amount:10600, new_amount:11126.0, orig_cost:3.1915358721700855, new_cost:3.04, ratio:0.9526463747024536, last_sale_price:3.4200003147125244]
[2020-05-06 15:53:20.524869] INFO: Position: after split: PositionStock(asset:Equity(1473 [601288.SHA]), amount:11126.0, cost_basis:3.04, last_sale_price:3.5899999141693115)
[2020-05-06 15:53:20.525972] INFO: Position: returning cash: 3.07
[2020-05-06 15:53:20.537499] INFO: algo: handle_splits get splits [dt:2017-07-14 00:00:00+00:00] [asset:Equity(1785 [601988.SHA]), ratio:0.9553804993629456]
[2020-05-06 15:53:20.538975] INFO: Position: position stock handle split[sid:1785, orig_amount:10100, new_amount:10571.0, orig_cost:3.4202920934668515, new_cost:3.27, ratio:0.9553804993629456, last_sale_price:3.6399996280670166]
[2020-05-06 15:53:20.592430] INFO: Position: after split: PositionStock(asset:Equity(1785 [601988.SHA]), amount:10571.0, cost_basis:3.27, last_sale_price:3.809999942779541)
[2020-05-06 15:53:20.593590] INFO: Position: returning cash: 2.56
[2020-05-06 15:53:20.611475] INFO: Performance: Simulated 186 trading days out of 186.
[2020-05-06 15:53:20.612595] INFO: Performance: first open: 2016-10-17 09:30:00+00:00
[2020-05-06 15:53:20.613541] INFO: Performance: last close: 2017-07-18 15:00:00+00:00
[2020-05-06 15:53:24.920137] INFO: moduleinvoker: backtest.v8 运行完成[20.844238s].
[2020-05-06 15:53:24.921952] INFO: moduleinvoker: trade.v4 运行完成[20.879806s].
# 基础参数配置
class conf:
start_date = '2012-01-01'
end_date='2017-07-18'
# split_date 之前的数据用于训练,之后的数据用作效果评估
split_date = '2016-10-16'
# D.instruments: https://bigquant.com/docs/data_instruments.html
instruments = D.instruments(start_date, split_date)
# 机器学习目标标注函数
# 如下标注函数等价于 min(max((持有期间的收益 * 100), -20), 20) + 20 (后面的M.fast_auto_labeler会做取整操作)
# 说明:max/min这里将标注分数限定在区间[-20, 20],+20将分数变为非负数 (StockRanker要求标注分数非负整数)
label_expr = ['return * 100', 'where(label > {0}, {0}, where(label < -{0}, -{0}, label)) + {0}'.format(20)]
# 持有天数,用于计算label_expr中的return值(收益)
hold_days = 5
# 特征 https://bigquant.com/docs/data_features.html,你可以通过表达式构造任何特征
features = [
'rank_pb_lf_0',
]
# 给数据做标注:给每一行数据(样本)打分,一般分数越高表示越好
m1 = M.fast_auto_labeler.v8(
instruments=conf.instruments, start_date=conf.start_date, end_date=conf.split_date,
label_expr=conf.label_expr, hold_days=conf.hold_days,
benchmark='000300.SHA', sell_at='open', buy_at='open')
# 计算特征数据
m2 = M.general_feature_extractor.v5(
instruments=conf.instruments, start_date=conf.start_date, end_date=conf.split_date,
features=conf.features)
# 数据预处理:缺失数据处理,数据规范化,T.get_stock_ranker_default_transforms为StockRanker模型做数据预处理
m3 = M.transform.v2(
data=m2.data, transforms=T.get_stock_ranker_default_transforms(),
drop_null=True, astype='int32', except_columns=['date', 'instrument'],
clip_lower=0, clip_upper=200000000)
# 合并标注和特征数据
m4 = M.join.v2(data1=m1.data, data2=m3.data, on=['date', 'instrument'], sort=True)
# StockRanker机器学习训练
m5 = M.stock_ranker_train.v6(training_ds=m4.data, features=conf.features)
## 量化回测 https://bigquant.com/docs/module_trade.html
# 回测引擎:准备数据,只执行一次
def prepare(context):
# context.start_date / end_date,回测的时候,为trader传入参数;在实盘运行的时候,由系统替换为实盘日期
n1 = M.general_feature_extractor.v5(
instruments=D.instruments(),
start_date=context.start_date, end_date=context.end_date,
features=conf.features)
n2 = M.transform.v2(
data=n1.data, transforms=T.get_stock_ranker_default_transforms(),
drop_null=True, astype='int32', except_columns=['date', 'instrument'],
clip_lower=0, clip_upper=200000000)
n3 = M.stock_ranker_predict.v5(model=context.options['model'], data=n2.data)
context.instruments = n3.instruments
context.options['predictions'] = n3.predictions
# 回测引擎:初始化函数,只执行一次
def initialize(context):
# 加载预测数据
context.ranker_prediction = context.options['predictions'].read_df()
# 系统已经设置了默认的交易手续费和滑点,要修改手续费可使用如下函数
context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
# 预测数据,通过options传入进来,使用 read_df 函数,加载到内存 (DataFrame)
# 设置买入的股票数量,这里买入预测股票列表排名靠前的5只
stock_count = 5
# 每只的股票的权重,如下的权重分配会使得靠前的股票分配多一点的资金,[0.339160, 0.213986, 0.169580, ..]
context.stock_weights = T.norm([1 / math.log(i + 2) for i in range(0, stock_count)])
# 设置每只股票占用的最大资金比例
context.max_cash_per_instrument = 0.2
# 回测引擎:每日数据处理函数,每天执行一次
def handle_data(context, data):
# 按日期过滤得到今日的预测数据
ranker_prediction = context.ranker_prediction[
context.ranker_prediction.date == data.current_dt.strftime('%Y-%m-%d')]
# 1. 资金分配
# 平均持仓时间是hold_days,每日都将买入股票,每日预期使用 1/hold_days 的资金
# 实际操作中,会存在一定的买入误差,所以在前hold_days天,等量使用资金;之后,尽量使用剩余资金(这里设置最多用等量的1.5倍)
is_staging = context.trading_day_index < context.options['hold_days'] # 是否在建仓期间(前 hold_days 天)
cash_avg = context.portfolio.portfolio_value / context.options['hold_days']
cash_for_buy = min(context.portfolio.cash, (1 if is_staging else 1.5) * cash_avg)
cash_for_sell = cash_avg - (context.portfolio.cash - cash_for_buy)
positions = {e.symbol: p.amount * p.last_sale_price
for e, p in context.perf_tracker.position_tracker.positions.items()}
# 2. 生成卖出订单:hold_days天之后才开始卖出;对持仓的股票,按StockRanker预测的排序末位淘汰
if not is_staging and cash_for_sell > 0:
equities = {e.symbol: e for e, p in context.perf_tracker.position_tracker.positions.items()}
instruments = list(reversed(list(ranker_prediction.instrument[ranker_prediction.instrument.apply(
lambda x: x in equities and not context.has_unfinished_sell_order(equities[x]))])))
# print('rank order for sell %s' % instruments)
for instrument in instruments:
context.order_target(context.symbol(instrument), 0)
cash_for_sell -= positions[instrument]
if cash_for_sell <= 0:
break
# 3. 生成买入订单:按StockRanker预测的排序,买入前面的stock_count只股票
buy_cash_weights = context.stock_weights
buy_instruments = list(ranker_prediction.instrument[:len(buy_cash_weights)])
max_cash_per_instrument = context.portfolio.portfolio_value * context.max_cash_per_instrument
for i, instrument in enumerate(buy_instruments):
cash = cash_for_buy * buy_cash_weights[i]
if cash > max_cash_per_instrument - positions.get(instrument, 0):
# 确保股票持仓量不会超过每次股票最大的占用资金量
cash = max_cash_per_instrument - positions.get(instrument, 0)
if cash > 0:
context.order_value(context.symbol(instrument), cash)
# 调用交易引擎
strategy_c = M.trade.v4(
instruments=conf.instruments,
start_date=conf.split_date,
end_date=conf.end_date,
prepare=prepare,
initialize=initialize,
handle_data=handle_data,
order_price_field_buy='open', # 表示 开盘 时买入
order_price_field_sell='close', # 表示 收盘 前卖出
capital_base=1000000, # 初始资金
benchmark='000300.SHA', # 比较基准,不影响回测结果
# 通过 options 参数传递预测数据和参数给回测引擎
options={'hold_days': conf.hold_days, 'model': m5.model}
)
[2020-05-06 15:48:06.635458] INFO: moduleinvoker: fast_auto_labeler.v8 开始运行..
[2020-05-06 15:48:06.643582] INFO: moduleinvoker: 命中缓存
[2020-05-06 15:48:06.649259] INFO: moduleinvoker: fast_auto_labeler.v8 运行完成[0.0138s].
[2020-05-06 15:48:06.652493] INFO: moduleinvoker: general_feature_extractor.v5 开始运行..
[2020-05-06 15:48:06.662239] INFO: moduleinvoker: 命中缓存
[2020-05-06 15:48:06.663605] INFO: moduleinvoker: general_feature_extractor.v5 运行完成[0.011122s].
[2020-05-06 15:48:06.669318] INFO: moduleinvoker: transform.v2 开始运行..
[2020-05-06 15:48:06.704916] INFO: moduleinvoker: 命中缓存
[2020-05-06 15:48:06.706417] INFO: moduleinvoker: transform.v2 运行完成[0.037093s].
[2020-05-06 15:48:06.708736] INFO: moduleinvoker: join.v2 开始运行..
[2020-05-06 15:48:06.938721] INFO: moduleinvoker: 命中缓存
[2020-05-06 15:48:06.940389] INFO: moduleinvoker: join.v2 运行完成[0.231631s].
[2020-05-06 15:48:06.942630] INFO: moduleinvoker: stock_ranker_train.v6 开始运行..
[2020-05-06 15:48:07.203404] INFO: moduleinvoker: 命中缓存
[2020-05-06 15:48:07.260389] INFO: moduleinvoker: stock_ranker_train.v6 运行完成[0.317733s].
[2020-05-06 15:48:07.308270] INFO: moduleinvoker: backtest.v8 开始运行..
[2020-05-06 15:48:07.313292] INFO: backtest: biglearning backtest:V8.3.4
[2020-05-06 15:48:07.351524] INFO: moduleinvoker: general_feature_extractor.v5 开始运行..
[2020-05-06 15:48:08.205539] INFO: moduleinvoker: 命中缓存
[2020-05-06 15:48:08.207161] INFO: moduleinvoker: general_feature_extractor.v5 运行完成[0.855646s].
[2020-05-06 15:48:08.213443] INFO: moduleinvoker: transform.v2 开始运行..
[2020-05-06 15:48:08.414870] INFO: moduleinvoker: 命中缓存
[2020-05-06 15:48:08.416480] INFO: moduleinvoker: transform.v2 运行完成[0.203031s].
[2020-05-06 15:48:08.418629] INFO: moduleinvoker: stock_ranker_predict.v5 开始运行..
[2020-05-06 15:48:09.475098] INFO: StockRanker预测: /y_2016 ..
[2020-05-06 15:48:10.540246] INFO: StockRanker预测: /y_2017 ..
[2020-05-06 15:48:14.522549] INFO: moduleinvoker: stock_ranker_predict.v5 运行完成[6.103897s].
[2020-05-06 15:48:14.524029] INFO: backtest: product_type:stock by specified
[2020-05-06 15:48:14.630054] INFO: moduleinvoker: cached.v2 开始运行..
[2020-05-06 15:48:23.213254] INFO: backtest: 读取股票行情完成:1411903
[2020-05-06 15:48:26.823918] INFO: moduleinvoker: cached.v2 运行完成[12.193852s].
[2020-05-06 15:48:31.072381] INFO: algo: TradingAlgorithm V1.6.7
[2020-05-06 15:48:31.757320] INFO: algo: trading transform...
[2020-05-06 15:48:33.487858] INFO: algo: handle_splits get splits [dt:2017-05-25 00:00:00+00:00] [asset:Equity(571 [600269.SHA]), ratio:0.9707603454589844]
[2020-05-06 15:48:33.489062] INFO: Position: position stock handle split[sid:571, orig_amount:33700, new_amount:34715.0, orig_cost:5.130771827693063, new_cost:4.98, ratio:0.9707603454589844, last_sale_price:4.980000019073486]
[2020-05-06 15:48:33.489959] INFO: Position: after split: PositionStock(asset:Equity(571 [600269.SHA]), amount:34715.0, cost_basis:4.98, last_sale_price:5.12999963760376)
[2020-05-06 15:48:33.491890] INFO: Position: returning cash: 0.28
[2020-05-06 15:48:33.759115] INFO: algo: handle_splits get splits [dt:2017-06-29 00:00:00+00:00] [asset:Equity(1308 [600015.SHA]), ratio:0.8202643990516663]
[2020-05-06 15:48:33.760304] INFO: Position: position stock handle split[sid:1308, orig_amount:19300, new_amount:23528.0, orig_cost:10.973420121278586, new_cost:9.0, ratio:0.8202643990516663, last_sale_price:9.310001373291016]
[2020-05-06 15:48:33.761372] INFO: Position: after split: PositionStock(asset:Equity(1308 [600015.SHA]), amount:23528.0, cost_basis:9.0, last_sale_price:11.350000381469727)
[2020-05-06 15:48:33.762514] INFO: Position: returning cash: 9.3
[2020-05-06 15:48:33.777802] INFO: algo: handle_splits get splits [dt:2017-06-30 00:00:00+00:00] [asset:Equity(973 [000898.SZA]), ratio:0.9876326322555542]
[2020-05-06 15:48:33.778891] INFO: Position: position stock handle split[sid:973, orig_amount:38500, new_amount:38982.0, orig_cost:5.534883405085108, new_cost:5.47, ratio:0.9876326322555542, last_sale_price:5.590000629425049]
[2020-05-06 15:48:33.780018] INFO: Position: after split: PositionStock(asset:Equity(973 [000898.SZA]), amount:38982.0, cost_basis:5.47, last_sale_price:5.659999847412109)
[2020-05-06 15:48:33.780996] INFO: Position: returning cash: 0.59
[2020-05-06 15:48:33.916983] INFO: algo: handle_splits get splits [dt:2017-07-13 00:00:00+00:00] [asset:Equity(1466 [601288.SHA]), ratio:0.9526463747024536]
[2020-05-06 15:48:33.918795] INFO: Position: position stock handle split[sid:1466, orig_amount:62600, new_amount:65711.0, orig_cost:3.5201277818378673, new_cost:3.35, ratio:0.9526463747024536, last_sale_price:3.4200003147125244]
[2020-05-06 15:48:33.920264] INFO: Position: after split: PositionStock(asset:Equity(1466 [601288.SHA]), amount:65711.0, cost_basis:3.35, last_sale_price:3.5899999141693115)
[2020-05-06 15:48:33.921527] INFO: Position: returning cash: 2.35
[2020-05-06 15:48:34.002730] INFO: Performance: Simulated 186 trading days out of 186.
[2020-05-06 15:48:34.004258] INFO: Performance: first open: 2016-10-17 09:30:00+00:00
[2020-05-06 15:48:34.005557] INFO: Performance: last close: 2017-07-18 15:00:00+00:00
[2020-05-06 15:48:38.804279] INFO: moduleinvoker: backtest.v8 运行完成[31.496021s].
[2020-05-06 15:48:38.805798] INFO: moduleinvoker: trade.v4 运行完成[31.542036s].
关于策略组合的相关模块 M.multi_strategy_analysis 的详细信息,可以参考:代码补全和文档帮助
m = M.multi_strategy_analysis.v1(
raw_perfs=[strategy_a.raw_perf, strategy_b.raw_perf, strategy_c.raw_perf],
weights=[1/3, 1/3, 1/3]
)
[2020-05-06 15:58:16.558650] INFO: moduleinvoker: backtest.v6 开始运行..
[2020-05-06 15:58:17.709610] INFO: moduleinvoker: 命中缓存
[2020-05-06 15:58:18.573570] INFO: moduleinvoker: backtest.v6 运行完成[2.014932s].
[2020-05-06 15:58:18.574866] INFO: moduleinvoker: trade.v1 运行完成[2.023915s].
[2020-05-06 15:58:18.576140] INFO: moduleinvoker: multi_strategy_analysis.v1 运行完成[2.24383s].