def intraday_position_report(positions, risks, cash_flows, listed_option_positions, pricing_environment, domain, headers): """Return intraday position report. positions: basic position report, live positions(not expiring) risks: basic risk report cash_flows: basic cash flow report equity_postions: basic underlyer equity positions """ multi_asset_position_criteria = positions.productType.isin(_PRODUCT_TYPE_SPREADS) single_asset_positions = positions[~multi_asset_position_criteria] multi_asset_positions = positions[multi_asset_position_criteria] decay_time = (datetime.now() + timedelta(days=1)).replace(hour=9, minute=0, second=0, microsecond=0) params = { 'requests': ['delta'], 'pricingEnvironmentId': pricing_environment, 'valuationDateTime': decay_time.strftime(_datetime_fmt), 'timezone': None} report = process_single_asset_pos(single_asset_positions, cash_flows, risks, domain, headers, params) if not multi_asset_positions.empty: multi_asset_report = process_multi_asset_pos(multi_asset_positions, cash_flows, risks, domain, headers, params) report = pd.concat([report, multi_asset_report], sort=False) # Exchange positions if not listed_option_positions.empty: listed_report = process_listed_pos(listed_option_positions, domain, headers, params) report = pd.concat([report, listed_report], sort=False) pe_description = get_pricing_env_description(pricing_environment, domain, headers) report['pricingEnvironment'] = pe_description report['createdAt'] = str(datetime.now()) return remove_nan_and_inf(report.to_dict(orient='records'))
def eod_classic_scenarios_pd_run(valuation_date): r = utils.get_redis_conn(redis_ip) position_result = r.get(EOD_BASIC_POSITIONS) position = pd.read_msgpack(position_result) if not position.empty: sub_companies_result = r.get(EOD_BASIC_SUB_COMPANIES_FROM_ORACLE) all_sub_companies = JSONDecoder().decode( bytes.decode(sub_companies_result)) headers = utils.login(data_resource_ip, login_body) pe_description = utils.get_pricing_env_description( PE_DEFAULT_CLOSE, data_resource_ip, headers) reports = eod_classic_scenarios_report_pd.eod_classic_scenarios_report( position, all_sub_companies, data_resource_ip, headers, valuation_date, PE_DEFAULT_CLOSE) instrument_contract_result = r.get(EOD_BASIC_INSTRUMENT_CONTRACT_TYPE) instruments_contract_dict = JSONDecoder().decode( bytes.decode(instrument_contract_result)) for item in reports: item['exfsid'] = instruments_contract_dict.get( item.get('underlyerInstrumentId')) params = { 'reportName': CLASSIC_SCENARIO_MARKET_RISK_REPORT_ + pe_description, 'valuationDate': valuation_date, 'reports': reports } utils.call_request(report_ip, 'report-service', 'rptMarketRiskDetailReportCreateBatch', params, headers)
def eod_market_risk_by_book_underlyer_pd_run(pricing_environment, valuation_date): r = utils.get_redis_conn(redis_ip) position_result = r.get(EOD_CUSTOM_POSITION_ + pricing_environment.lower()) position = JSONDecoder().decode(bytes.decode(position_result)) if position: headers = utils.login(data_resource_ip, login_body) pe_description = utils.get_pricing_env_description( pricing_environment, data_resource_ip, headers) reports = eod_market_risk_by_book_underlyer_report_pd.eod_market_risk_by_book_underlyer_report( pd.DataFrame(position), pe_description) instrument_contract_result = r.get(EOD_BASIC_INSTRUMENT_CONTRACT_TYPE) instruments_contract_dict = JSONDecoder().decode( bytes.decode(instrument_contract_result)) for item in reports: item['exfsid'] = instruments_contract_dict.get( item.get('underlyerInstrumentId')) params = { 'reportName': MARKET_RISK_BY_BOOK_UNDERLYER_REPORT_ + pe_description, 'valuationDate': valuation_date, 'reports': reports } utils.call_request(report_ip, 'report-service', 'rptMarketRiskBySubUnderlyerReportCreateBatch', params, headers)
def get_risks(positions_list, pricing_environment, valuation_datetime, domain, headers): """Return pricing results of given positions. positions_list: list of basic positions(live, and possibly expiring positions)""" trade_ids = set() position_ids = set() for positions in positions_list: position_ids.update([p['positionId'] for p in positions.values()]) trade_ids.update([p['tradeId'] for p in positions.values()]) requests = ['price', 'delta', 'gamma', 'vega', 'theta', 'rho_r'] params = { 'requests': requests, 'tradeIds': list(trade_ids), 'pricingEnvironmentId': pricing_environment, 'valuationDateTime': valuation_datetime.strftime(_datetime_fmt), 'timezone': None } pricing_data = utils.call_request(domain, 'pricing-service', 'prcPrice', params, headers) if 'result' in pricing_data: risks = [] for r in pricing_data['result']: if r['positionId'] not in position_ids: continue position_ids.remove(r['positionId']) risks.append(r) r['isSuccess'] = True r['message'] = '' for req in requests: if req == 'rho_r': req = 'rhoR' if r[req] is None: r['isSuccess'] = False r['message'] = '定价返回空' break for d in pricing_data['diagnostics']: p_id = d['key'] if p_id in position_ids: position_ids.remove(p_id) risks.append({ 'positionId': p_id, 'isSuccess': False, 'message': d['message'] }) for p_id in position_ids: risks.append({ 'positionId': p_id, 'isSuccess': False, 'message': '定价返回空' }) pe_description = utils.get_pricing_env_description( pricing_environment, domain, headers) for risk in risks: risk['pricing_environment'] = pe_description return {r['positionId']: r for r in risks}, pricing_data['result'] else: raise RuntimeError('Failed to price trades.')
def get_risks(positions, pricing_environment, valuation_datetime, domain, headers): """Return pricing results of given positions. positions_list: list of basic positions(live, and possibly expiring positions)""" start = timer() positions_trade_id = list(positions.tradeId.unique()) trade_batches = list(utils.chunks(positions_trade_id, MAX_PRICING_TRADES_NUM)) pricing_result = [] pricing_diagnostics = [] for trade_batch in trade_batches: params = { 'requests': ['price', 'delta', 'gamma', 'vega', 'theta', 'rho_r'], 'tradeIds': trade_batch, 'pricingEnvironmentId': pricing_environment, 'valuationDateTime': '{}T00:00:00'.format(valuation_datetime), 'timezone': None } res = utils.call_request(domain, 'pricing-service', 'prcPrice', params, headers) pricing_result.extend(res.get('result') if 'result' in res else []) pricing_diagnostics.extend(res.get('diagnostics') if 'diagnostics' in res else []) end = timer() print('pricing all trades take ' + str(end - start) + ' seconds') if len(positions_trade_id) > 0 and len(pricing_result) > 0: start = timer() price_data = pd.DataFrame(pricing_result, dtype='double') risk_df = price_data # fill missing elements of cash flows risk_df[['vol', 'q']].fillna(0, inplace=True) # risk_df['underlyerPrice'].fillna(-1, inplace=True) # impossible value diagnostics_df = pd.DataFrame(pricing_diagnostics) if not diagnostics_df.empty: diagnostics_df.rename(columns={'key': 'positionId'}, inplace=True) diagnostics_df = diagnostics_df[~diagnostics_df.positionId.duplicated(keep='last')] risk_df = risk_df.merge(diagnostics_df, on='positionId', how='outer') risk_df['message'] = '' # pointless in jkzx # multi_asset_position_ids = list(positions[positions.productType.isin(_PRODUCT_TYPE_SPREADS)].tradeId.unique()) # risk_df['message'] = risk_df.apply(lambda row: get_message(row, multi_asset_position_ids), axis=1) pe_description = utils.get_pricing_env_description(pricing_environment, domain, headers) risk_df['pricing_environment'] = pe_description risk_df.set_index('positionId', inplace=True) for prc in ['qs', 'vols', 'underlyerPrices', 'deltas', 'gammas', 'vegas']: if prc not in risk_df.columns: risk_df[prc] = np.nan end = timer() print('normalize risk results take ' + str(end - start) + ' seconds') return risk_df else: raise RuntimeError('Failed to price trades.')
def real_time_pnl_pd_run(): r = utils.get_redis_conn(redis_ip) risk_result = r.get(INTRADAY_BASIC_RISKS) risk = pd.read_msgpack(risk_result) underlyer_position_result = r.get(INTRADAY_BASIC_UNDELRYER_POSITION) underlyer_position = pd.read_msgpack(underlyer_position_result) list_position_result = r.get(INTRADAY_BASIC_LISTED_OPTION_POSITION) list_position = pd.read_msgpack(list_position_result) cash_flow_today_result = r.get(INTRADAY_BASIC_CASH_FLOW_TODAY) cash_flow_today = pd.read_msgpack(cash_flow_today_result) live_position_index_result = r.get(INTRADAY_BASIC_POSITION_INDEX) live_position_index = pd.read_msgpack(live_position_index_result) today_terminated_position_index_result = r.get( INTRADAY_BASIC_TERMINATED_POSITION_INDEX) today_terminated_position_index = pd.read_msgpack( today_terminated_position_index_result) now_date = datetime.now().date() yst_date = now_date + timedelta(days=-1) yst_params = { 'reportName': POSITION_REPORT, 'valuationDate': str(yst_date) } headers = utils.login(ip, login_body) yst_position = utils.call_request(ip, 'report-service', 'rptLatestPositionReportByNameAndDate', yst_params, headers)['result'] yst_params['reportName'] = HST_PNL_REPORT yst_historical_pnl = utils.call_request( ip, 'report-service', 'rptLatestPnlHstReportByNameAndDate', yst_params, headers)['result'] yst_position = pd.DataFrame(yst_position) yst_historical_pnl = pd.DataFrame(yst_historical_pnl) pe_description = utils.get_pricing_env_description(PE_DEFAULT_INTRADAY, ip, headers) reports = intraday_daily_pnl_by_underlyer_report_pd.intraday_daily_pnl_by_underlyer_report( risk, cash_flow_today, underlyer_position, list_position, live_position_index, today_terminated_position_index, yst_position, yst_historical_pnl, pe_description) reports = utils.remove_nan_and_inf(reports.to_dict(orient='records')) pnl_result = JSONEncoder().encode(reports) r.set(PNL_QUEUE, str(pnl_result)) r.publish(PNL_QUEUE, str(pnl_result))
def real_time_risk_pd_run(): r = utils.get_redis_conn(redis_ip) position_result = r.get(INTRADAY_CUSTOM_POSITION) position = JSONDecoder().decode(bytes.decode(position_result)) underlyer_position_result = r.get(INTRADAY_BASIC_UNDELRYER_POSITION) underlyer_position = pd.read_msgpack(underlyer_position_result) headers = utils.login(ip, login_body) pe_description = utils.get_pricing_env_description(PE_DEFAULT_INTRADAY, ip, headers) reports = intraday_risk_by_underlyer_report_pd.intraday_risk_by_underlyer_report( pd.DataFrame(position), underlyer_position, ip, headers, pe_description) risk_result = JSONEncoder().encode(reports) r.set(RISK_QUEUE, str(risk_result)) r.publish(RISK_QUEUE, str(risk_result))
def eod_market_risk_summary_pd_run(pricing_environment, valuation_date): r = utils.get_redis_conn(redis_ip) position_result = r.get(EOD_CUSTOM_POSITION_ + pricing_environment.lower()) position = JSONDecoder().decode(bytes.decode(position_result)) if position: headers = utils.login(data_resource_ip, login_body) reports = eod_market_risk_summary_report_pd.eod_market_risk_summary_report( pd.DataFrame(position), pricing_environment) pe_description = utils.get_pricing_env_description( pricing_environment, data_resource_ip, headers) params = { 'reportName': MARKET_RISK_REPORT_ + pe_description, 'valuationDate': valuation_date, 'reports': reports } utils.call_request(report_ip, 'report-service', 'rptMarketRiskReportCreateBatch', params, headers)
def real_time_portfolio_trades_pd_run(): r = utils.get_redis_conn(redis_ip) risk_result = r.get(INTRADAY_BASIC_RISKS) risk = pd.read_msgpack(risk_result) position_index_result = r.get(INTRADAY_BASIC_POSITION_INDEX) position_index = pd.read_msgpack(position_index_result) portfolio_trades_result = r.get(INTRADAY_BASIC_PORTFOLIO_TRADES) portfolio_trades = pd.read_msgpack(portfolio_trades_result) headers = utils.login(ip, login_body) pe_description = utils.get_pricing_env_description(PE_DEFAULT_INTRADAY, ip, headers) portfolio_report = intraday_portfolio_trades_report_pd.intraday_portfolio_trades_report( risk, position_index, portfolio_trades, pe_description) portfolio_report_result = JSONEncoder().encode(portfolio_report) r.set(PORTFOLIO_RISK_QUEUE, str(portfolio_report_result)) r.publish(PORTFOLIO_RISK_QUEUE, str(portfolio_report_result))
def financial_otc_trade_run(pricing_environment): headers = utils.login(ip, login_body) r = utils.get_redis_conn(ip) now_date = datetime.now().date() position = get_all_positions(ip, headers) risk_result = r.get(EOD_BASIC_RISKS_ + pricing_environment.lower()) risk = JSONDecoder().decode(bytes.decode(risk_result)) cash_flows_result = r.get(EOD_BASIC_CASH_FLOW) cash_flows = JSONDecoder().decode(bytes.decode(cash_flows_result)) reports = get_financial_otc_trade(position, risk, cash_flows, headers, ip) pe_description = utils.get_pricing_env_description(pricing_environment, ip, headers) params = { 'reportName': FINANCIAL_OTC_TRADE_REPORT_ + pe_description, 'valuationDate': str(now_date), 'otcTradeReports': reports } utils.call_request(ip, 'report-service', 'rptOtcTradeReportCreateBatch', params, headers)
def real_time_pnl_run(): r = utils.get_redis_conn(ip) risk_result = r.get(INTRADAY_BASIC_RISKS) risk = JSONDecoder().decode(bytes.decode(risk_result)) position_index_result = r.get(INTRADAY_BASIC_POSITION_INDEX) position_index = JSONDecoder().decode(bytes.decode(position_index_result)) underlyer_position_result = r.get(INTRADAY_BASIC_UNDELRYER_POSITION) underlyer_position = JSONDecoder().decode( bytes.decode(underlyer_position_result)) cash_flow_today_result = r.get(INTRADAY_BASIC_CASH_FLOW_TODAY) cash_flow_today = JSONDecoder().decode( bytes.decode(cash_flow_today_result)) now_date = datetime.now().date() yst_date = now_date + timedelta(days=-1) yst_params = { 'reportName': POSITION_REPORT, 'valuationDate': str(yst_date) } headers = utils.login(ip, login_body) yst_position = utils.call_request(ip, 'report-service', 'rptLatestPositionReportByNameAndDate', yst_params, headers)['result'] yst_params['reportName'] = HST_PNL_REPORT yst_historical_pnl = utils.call_request( ip, 'report-service', 'rptLatestPnlHstReportByNameAndDate', yst_params, headers)['result'] pe_description = utils.get_pricing_env_description(PE_DEFAULT_INTRADAY, ip, headers) reports = intraday_daily_pnl_by_underlyer_report( risk, cash_flow_today, underlyer_position, position_index, yst_position, yst_historical_pnl, pe_description) pnl_result = JSONEncoder().encode(reports) r.set(PNL_QUEUE, str(pnl_result)) r.publish(PNL_QUEUE, str(pnl_result))
def eod_spot_scenarios_by_market_pd_run(pricing_environment, valuation_date): r = utils.get_redis_conn(redis_ip) position_result = r.get(EOD_BASIC_POSITIONS) position = pd.read_msgpack(position_result) if not position.empty: sub_companies_result = r.get(EOD_BASIC_SUB_COMPANIES_FROM_ORACLE) all_sub_companies = JSONDecoder().decode( bytes.decode(sub_companies_result)) headers = utils.login(data_resource_ip, login_body) pe_description = utils.get_pricing_env_description( pricing_environment, data_resource_ip, headers) reports = eod_spot_scenarios_by_market_report_pd.eod_spot_scenarios_by_market_report( pd.DataFrame(position), data_resource_ip, headers, pe_description, all_sub_companies, valuation_date, pricing_environment) instrument_contract_result = r.get(EOD_BASIC_INSTRUMENT_CONTRACT_TYPE) instruments_contract_dict = JSONDecoder().decode( bytes.decode(instrument_contract_result)) for item in reports: item['exfsid'] = instruments_contract_dict.get( item.get('instrumentId')) utils.call_request(report_ip, 'report-service', 'rptDeleteSpotScenariosReportByDate', {'valuationDate': valuation_date}, headers) chunk_size = 1000 for start in range(0, len(reports), chunk_size): end = start + chunk_size params = { 'valuationDate': valuation_date, 'reports': reports[start:end] } utils.call_request(report_ip, 'report-service', 'rptSpotScenariosReportCreateBatch', params, headers)