def sim_func(results, dates, param): model_name = param['model'][0] x_norm = TRAINED_MODEL[model_name]['x_norm'] all_dates = TRAINED_MODEL[model_name]['dates'] + dates all_results = subset_results_for_dates(results, all_dates) names, X, Y, vol_normal, full_tickers, full_dates, x_norm = get_signal(all_results, with_y=True, vol_normal=TRAINED_MODEL[model_name]['vol_normalization'], x_norm=x_norm, normalize_y=False) full_dates = [dt.strftime('%Y%m%d') for dt in full_dates] model = TRAINED_MODEL[model_name]['model'] if not CLASSIFY: y_pred_mean = param['y_pred_mean'] y_pred_std = param['y_pred_std'] #import pdb; pdb.set_trace() prediction = model.predict(X) print prediction[-10:] trade_idx = np.where(prediction > (Trade_Thresh * y_pred_std + y_pred_mean) ) else : prediction = model.predict(X) print prediction[-10:] trade_idx = np.where(prediction > 0) pnl = np.array(Y)[trade_idx] - Cost full_dates = np.array(full_dates)[trade_idx] new_pnl = reorganize_pnl(pnl, full_dates, dates) print_summary(new_pnl, 'OutSample') return new_pnl
def sim_func(result, dates, param): mv_back = param['mv_back'][0] mv_front = param['mv_front'][0] tf_back = param['tf_back'][0] tf_front = param['tf_front'][0] model_name = param['model_name'][0] pre_dates = TRAINED_MODEL[model_name]['dates'] mv_all_dates = pre_dates[(-mv_back-mv_front):] + dates mv_all_dates_dt = [datetime.datetime.strptime(dt, '%Y%m%d').date() for dt in mv_all_dates] mv_new_result = subset_result_for_dates(result, mv_all_dates_dt) mv_new_result_mat = to_matrix(mv_new_result) mv_trade_dates, mv_position = generating_pos_position(mv_new_result_mat, TRAINED_MODEL[model_name]['loadings'][param['mv_factor_ind'][0]], mv_all_dates, mv_back, mv_front, mean_revert=True) tf_all_dates = pre_dates[(-tf_back-tf_front):] + dates tf_all_dates_dt = [datetime.datetime.strptime(dt, '%Y%m%d').date() for dt in tf_all_dates] tf_new_result = subset_result_for_dates(result, tf_all_dates_dt) tf_new_result_mat = to_matrix(tf_new_result) tf_trade_dates, tf_position = generating_pos_position(tf_new_result_mat, TRAINED_MODEL[model_name]['loadings'][param['tf_factor_ind'][0]], tf_all_dates, tf_back, tf_front, mean_revert=True) mv_position = np.array(mv_position) tf_position = np.array(tf_position) #if param['mv_value'] < 0: #mv_position = np.zeros(mv_position.shape) #if param['tf_value'] < 0: #tf_position = np.zeros(tf_position.shape) position = mv_position + tf_position future_return = mv_new_result_mat[(mv_back+mv_front-1):,:] pnl_mat = position * future_return pnl = np.sum(pnl_mat, axis=1) pos = np.sum(position, axis=1) pnl_array = np.array(zip(dates, pnl, pos, pos, pos), dtype=[('date', 'S8'), ('total_pnl', float), ('volume', float), ('max_position', float), ('min_position', float)]) print print_summary(pnl_array, 'OutSample') return pnl_array