<jupyter_start><jupyter_text>Meta Data Parameters----------------------<jupyter_code>freq = '10b'
universe = Universe('custom', ['zz800'])
batch = 16
neutralized_risk = ['SIZE'] + industry_styles
risk_model = 'short'
pre_process = [winsorize_normal, standardize]
post_process = [winsorize_normal, standardize]
warm_start = 0
data_source = 'postgres+psycopg2://postgres:we083826@192.168.0.102/alpha'
dask_cluster = '192.168.0.102:8786'

horizon = map_freq(freq)<jupyter_output><empty_output><jupyter_text>Helper function to train / Precit a model-------------------<jupyter_code>def train_daily_model(params):
    ref_date, meta_model = params
    data_meta = DataMeta(freq=freq,
                         universe=universe,
                         batch=batch,
                         neutralized_risk=neutralized_risk,
                         risk_model=risk_model,
                         pre_process=pre_process,
                         post_process=post_process,
                         warm_start=warm_start,
                         data_source=data_source)

    return train_model(ref_date=ref_date, alpha_model=meta_model, data_meta=data_meta)
def predict_daily_model(params):
    ref_date, alpha_model = params
    data_meta = DataMeta(freq=freq,
                         universe=universe,
                         batch=batch,
                         neutralized_risk=neutralized_risk,
                         risk_model=risk_model,
                         pre_process=pre_process,
                         post_process=post_process,
                         warm_start=warm_start,
                         data_source=data_source)
    return predict_by_model(ref_date=ref_date, alpha_model=alpha_model, data_meta=data_meta)<jupyter_output><empty_output><jupyter_text>Strategy Settings---------------------<jupyter_code>start_date = '2010-02-01'
end_date = '2018-01-29'
industry_category = 'sw_adj'
industry_level = 1
industries = industry_list(industry_category, industry_level)
styles = ['SIZE']
benchmark = 905
turn_over_target_base = 0.30
executor = NaiveExecutor()

ref_dates = makeSchedule(firstDate=start_date,
                         endDate=end_date,
                         tenor=freq,
                         calendar='china.sse',
                         dateGenerationRule=DateGeneration.Backward)
ref_dates = [ref_date.strftime('%Y-%m-%d') for ref_date in ref_dates]
# Features

base1 = LAST('roe_q')
base2 = CSRes('ep_q', base1)

features = {'f01': base1,
            'f02': base2,
            'f03': CSRes(CSRes('market_confidence_15d', base1), base2),
            'f04': CSRes(CSRes('val_q', base1), base2),
            'f05': CSRes(CSRes('ILLIQUIDITY', base1), base2),
            'f06': CSRes(CSRes('OperatingRevenueGrowRate', base1), base2),
            'f07': CSRes(CSRes('NetCFGrowth', base1), base2),
            'f08': CSRes(CSRes('con_pe_rolling_order', base1), base2),
            'f09': CSRes(CSRes('con_pb_rolling_order', base1), base2),
            'f10': CSRes(CSRes('DebtEquityRatio', base1), base2)}

weights = {'f01': 1.0,
           'f02': 1.0,
           'f03': 0.25,
           'f04': 0.25,
           'f05': 0.25,
           'f06': 0.25,
           'f07': 0.25,
           'f08': -0.25,
           'f09': -0.25,
           'f10': -0.25}

const_model = ConstLinearModel(features=features, weights=weights)
# Model

# meta_model = XGBTrainer(features=features,
#                         objective='reg:linear',
#                         booster='gbtree',
#                         tree_method='hist',
#                         n_estimators=2000,
#                         learning_rate=0.01,
#                         early_stopping_rounds=30,
#                         subsample=0.25,
#                         colsample_bytree=1.,
#                         n_jobs=1,
#                         eval_sample=0.3)

meta_model = LassoRegression(alpha=0.01, features=features)<jupyter_output><empty_output><jupyter_text>Train / Predict Models with Dask Cluster--------------------------  Training Phase<jupyter_code>from dask.distributed import Client
client = Client(dask_cluster)
%%time

tasks = client.map(train_daily_model, [(ref_date, meta_model) for ref_date in ref_dates])
models = client.gather(tasks)<jupyter_output>Wall time: 2min 53s<jupyter_text>Predicting Phase<jupyter_code>%%time

tasks = client.map(predict_daily_model, list(zip(ref_dates, models)))
predictions1 = client.gather(tasks)
%%time

tasks = client.map(predict_daily_model, [(ref_date, const_model) for ref_date in ref_dates])
predictions2 = client.gather(tasks)<jupyter_output>Wall time: 1min 23s<jupyter_text>Portfolio Rebalance-----------------------<jupyter_code># constraints setting
total_risks = industries + styles + ['benchmark']

b_type = []
l_val = []
u_val = []

for j, name in enumerate(total_risks):
    if name == 'benchmark':
        b_type.append(BoundaryType.RELATIVE)
        l_val.append(0.8)
        u_val.append(1.0)
    elif name == 'SIZE':
        b_type.append(BoundaryType.ABSOLUTE)
        l_val.append(0.)
        u_val.append(0.)
    else:
        b_type.append(BoundaryType.RELATIVE)
        l_val.append(1.0)
        u_val.append(1.0)
        

bounds = create_box_bounds(total_risks, b_type, l_val, u_val)
%%time

engine = SqlEngine(data_source)

rets = []
turn_overs = []
previous_pos = pd.DataFrame()

trade_dates = ref_dates

for i, ref_date in enumerate(trade_dates):
    er = 0.0 * predictions1[i].values.flatten().astype(float) + 1.0 * predictions2[i].values.flatten().astype(float)
    codes = predictions2[i].index.values.astype(int).tolist()
    industry_data = engine.fetch_industry_matrix(ref_date, codes, category=industry_category, level=industry_level)
    industry_exp = industry_data[industries].values
    industry_names = industry_data.industry_name.values
    style_exp = engine.fetch_risk_model(ref_date, codes, risk_model=risk_model)[1][styles].values
    benchmark_w = engine.fetch_benchmark(ref_date, benchmark, codes).weight.values
    is_in_benchmark = (benchmark_w > 0.).astype(float)
    
    risk_exp = np.concatenate([industry_exp, style_exp, is_in_benchmark.reshape((-1, 1))], axis=1)
    cons_mat = pd.DataFrame(risk_exp, columns=total_risks)
    constraint = LinearConstraints(bounds=bounds,
                                   cons_mat=cons_mat,
                                   backbone=benchmark_w)
    
    lbound = np.maximum(0., benchmark_w - 0.01)
    ubound = 0.01 + benchmark_w
    
    if previous_pos.empty:
        current_position = None
        turn_over_target = None
    else:
        previous_pos.set_index('code', inplace=True)
        remained_pos = previous_pos.loc[codes]

        remained_pos.fillna(0., inplace=True)
        turn_over_target = turn_over_target_base
        current_position = remained_pos.weight.values
        
    try:
        target_pos, _ = er_portfolio_analysis(er,
                                              industry_names,
                                              None,
                                              constraint,
                                              False,
                                              benchmark_w,
                                              method='risk_neutral',
                                              turn_over_target=turn_over_target,
                                              current_position=current_position,
                                              lbound=lbound,
                                              ubound=ubound)
    except ValueError:
        alpha_logger.info('{0} full re-balance'.format(ref_date))
        target_pos, _ = er_portfolio_analysis(er,
                                              industry_names,
                                              None,
                                              constraint,
                                              False,
                                              benchmark_w,
                                              method='risk_neutral',
                                              lbound=lbound,
                                              ubound=ubound)
        
    target_pos['code'] = codes
    
    turn_over, executed_pos = executor.execute(target_pos=target_pos)
    executed_codes = executed_pos.code.tolist()
    
    dx_returns = engine.fetch_dx_return(ref_date, executed_codes, horizon=horizon, offset=1)
    result = pd.merge(executed_pos, dx_returns, on=['code'])
    ret = result.weight.values @ (np.exp(result.dx.values) - 1.)
    rets.append(np.log(1. + ret))
    
    executor.set_current(executed_pos)
    turn_overs.append(turn_over)
    previous_pos = executed_pos

    alpha_logger.info('{0} is finished'.format(ref_date))
ret_df = pd.DataFrame({'returns': rets, 'turn_over': turn_overs}, index=trade_dates)

# index return
index_return = engine.fetch_dx_return_index_range(benchmark,
                                                  dates=trade_dates,
                                                  horizon=horizon,
                                                  offset=1).set_index('trade_date')
ret_df['index'] = index_return['dx']

ret_df.loc[advanceDateByCalendar('china.sse', trade_dates[-1], freq)] = 0.
ret_df = ret_df.shift(1)
ret_df.iloc[0] = 0.
ret_df['tc_cost'] = ret_df.turn_over * 0.002
ret_df['returns'] = ret_df['returns'] - ret_df['index']

ret_df[['returns', 'tc_cost']].cumsum().plot(figsize=(12, 6),
                                             title='Fixed frequency rebalanced: {0}'.format(freq),
                                             secondary_y='tc_cost')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup