M.multi_strategy_analysis¶
定义¶
-
M.multi_strategy_analysis.
v2
(raw_perf_1=None, raw_perf_2=None, raw_perf_3=None, raw_perfs=[], weights='', rebalance_period=0, start_date=None, end_date=None, benchmark='000300.SHA', prepare=None, initialize=None, before_trading_start=None, handle_data=None, options={})¶ 多策略组合收益分析
参数: - raw_perf_1 (DataSource) – 策略收益原始数据第1个 (一般自来于 交易引擎 的输出的raw_perf字段);默认值是None。
- raw_perf_2 (DataSource) – 策略收益原始数据第2个 (一般自来于 交易引擎 的输出的raw_perf字段);默认值是None。
- raw_perf_3 (DataSource) – 策略收益原始数据第3个 (一般自来于 交易引擎 的输出的raw_perf字段);默认值是None。
- raw_perfs (列表) – 策略收益原始数据列表 (一般自来于 交易引擎 的输出的raw_perf字段);默认值是[]。
- weights (字符串|列表) – 每个回测结果的权重;默认值是。
- rebalance_period (int) – 再平衡周期;默认值是0。
- start_date (str) – 开始日期,默认是raw_perfs里的最小日期;默认值是None。
- end_date (str) – 结束日期,默认是raw_perfs里的最大日期;默认值是None。
- benchmark (str) – 基准指数;默认值是000300.SHA。
- prepare (函数) – 回调函数,准备数据;默认值是None。
- initialize (函数) – 回调函数,初始化函数;默认值是None。
- before_trading_start (函数) – 回调函数 ,策略交易单位周期开始前执行, I.code_python;默认值是None。
- handle_data (函数) – 回调函数,策略主体逻辑函数;默认值是None。
- options (字典) – 用户自定义数据,在回调函数中要用到的变量,需要从这里传入,并通过 context.options 使用;默认值是{}。
返回: - .raw_perf: 回测结果原始数据
返回类型: Outputs
模块源代码¶
import pandas as pd
from zipline.finance.commission import PerOrder
from biglearning.api import M
import biglearning.api.tools as T
# 是否自动缓存结果,默认为True。一般对于需要很长计算时间的(超过1分钟),启用缓存(True);否则禁用缓存(False)
bigquant_cacheable = False
# 如果模块已经不推荐使用,设置这个值为推荐使用的版本或者模块名,比如 v2
bigquant_deprecated = None
def default_prepare(context):
data = {}
for i, perf in enumerate(context.options['raw_perfs']):
perf_df = perf.read_df()
df = pd.DataFrame()
df['close'] = perf_df['algorithm_period_return'] + 1
df['high'] = df['close'] * (1 + 0.005)
df['low'] = df['close'] * (1 - 0.005)
df['open'] = df['close'].shift(1).fillna(1)
df['volume'] = 1e10
df['price'] = df['open']
df['factor'] = 1.0
df['adjust_factor'] = 1.0
df.index = pd.Series(df.index).apply(lambda x: pd.to_datetime(x.date))
data['s%s' % (i + 1)] = df
panel = pd.Panel(data)
context.instruments = panel
def default_initialize(context):
context.set_commission(PerOrder(buy_cost=0, sell_cost=0, min_cost=0))
context.options['weights'] = T.norm(context.options['weights'])
def default_handle_data(context, data):
rebalance_period = context.options.get('rebalance_period', 0)
if (rebalance_period == 0 and context.trading_day_index > 0) or \
(rebalance_period != 0 and context.trading_day_index % rebalance_period != 0):
return
# rebalance
for i, w in enumerate(context.options['weights']):
context.order_target_percent(context.symbol('s%s' % (i + 1)), w)
def bigquant_run(raw_perfs,
weights,
rebalance_period=0,
start_date=None,
end_date=None,
benchmark='000300.SHA',
prepare=None,
initialize=None,
before_trading_start=None,
handle_data=None,
options=None):
if start_date is None or end_date is None:
perf_index = [perf.read_df().index for perf in raw_perfs]
start_date = start_date or min([pi.min() for pi in perf_index]).strftime('%Y-%m-%d')
end_date = end_date or max([pi.max() for pi in perf_index]).strftime('%Y-%m-%d')
options = options.copy() if options is not None else {}
options['raw_perfs'] = raw_perfs
options['weights'] = weights
options['rebalance_period'] = rebalance_period
return M.trade.v1(
instruments=None,
start_date=start_date,
end_date=end_date,
prepare=prepare or default_prepare,
initialize=initialize or default_initialize,
before_trading_start=before_trading_start,
handle_data=handle_data or default_handle_data,
order_price_field_buy='open', # 表示 开盘 时买入
order_price_field_sell='close', # 表示 收盘 前卖出
capital_base=1000000, # 初始资金
benchmark=benchmark, # 比较基准,不影响回测结果
auto_cancel_non_tradable_orders=False,
backtest_only=True,
volume_limit=0,
options=options
)
def bigquant_postrun(outputs):
return outputs
if __name__ == '__main__':
# 测试代码
pass
示例代码¶
m9 = M.multi_strategy_analysis.v1(
raw_perfs=[m6.raw_perf, m7.raw_perf],
weights=[0.6, 0.4]
)