def eod_counter_party_market_risk_by_underlyer_default_close_pd_run( valuation_date): r = utils.get_redis_conn(redis_ip) position_result = r.get(EOD_CUSTOM_POSITION_ + PE_DEFAULT_CLOSE.lower()) position = JSONDecoder().decode(bytes.decode(position_result)) if position: 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) reports = eod_market_risk_summary_report_pd.eod_counter_party_market_risk_by_underlyer_report( pd.DataFrame(position), all_sub_companies) 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': '交易对手分品种风险报告_交易-收盘-自然日', 'valuationDate': valuation_date, 'reports': reports } utils.call_request( report_ip, 'report-service', 'rptCounterPartyMarketRiskByUnderlyerReportCreateBatch', params, headers)
def eod_market_risk_detail_default_close_pd_run(valuation_date): r = utils.get_redis_conn(redis_ip) position_result = r.get(EOD_CUSTOM_POSITION_ + PE_DEFAULT_CLOSE.lower()) position = JSONDecoder().decode(bytes.decode(position_result)) if position: headers = utils.login(data_resource_ip, login_body) reports = eod_market_risk_detail_report_pd.eod_market_risk_detail_report( pd.DataFrame(position)) 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': '全市场分品种风险报告_交易-收盘-自然日', '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 eod_hst_pnl_run(pricing_environment): r = utils.get_redis_conn(ip) position_result = r.get(EOD_CUSTOM_POSITION_ + pricing_environment.lower()) position = JSONDecoder().decode(bytes.decode(position_result)) cash_flow_result = r.get(EOD_BASIC_CASH_FLOW) cash_flow = JSONDecoder().decode(bytes.decode(cash_flow_result)) underlyer_position_result = r.get(EOD_BASIC_UNDELRYER_POSITION_ + pricing_environment.lower()) underlyer_position = JSONDecoder().decode( bytes.decode(underlyer_position_result)) position_map_result = r.get(EOD_BASIC_POSITION_MAP) position_map = JSONDecoder().decode(bytes.decode(position_map_result)) headers = utils.login(ip, login_body) pe_description = get_pricing_environment_description( pricing_environment, headers) reports = eod_historical_pnl_by_underlyer_report(position, cash_flow, underlyer_position, position_map, pe_description) hst_pnl_result = JSONEncoder().encode(reports) r.set(EOD_CUSTOM_HST_PNL_ + pricing_environment.lower(), str(hst_pnl_result)) now_date = datetime.now().date() params = { 'reportName': HST_PNL_REPORT_ + pe_description, 'valuationDate': str(now_date), 'pnlHstReports': reports } utils.call_request(ip, 'report-service', 'rptPnlHstReportCreateBatch', params, headers)
def eod_position_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) risk_result = r.get(EOD_BASIC_RISKS_ + pricing_environment.lower()) risk = pd.read_msgpack(risk_result) cash_flow_result = r.get(EOD_BASIC_CASH_FLOW) cash_flow = pd.read_msgpack(cash_flow_result) listed_option_positions_result = r.get(EOD_BASIC_LISTED_OPTION_POSITION_ + pricing_environment.lower()) listed_option_positions = pd.read_msgpack(listed_option_positions_result) headers = utils.login(data_resource_ip, login_body) rpt = [] if not position.empty: rpt = eod_position_report_pd.eod_position_report( position, risk, cash_flow, listed_option_positions, pricing_environment, data_resource_ip, headers, valuation_date) position_result = JSONEncoder().encode(rpt) r.set(EOD_CUSTOM_POSITION_ + pricing_environment.lower(), str(position_result))
def eod_position_run(pricing_environment): r = utils.get_redis_conn(ip) position_result = r.get(EOD_BASIC_POSITIONS) position = JSONDecoder().decode(bytes.decode(position_result)) risk_result = r.get(EOD_BASIC_RISKS_ + pricing_environment.lower()) risk = JSONDecoder().decode(bytes.decode(risk_result)) cash_flow_result = r.get(EOD_BASIC_CASH_FLOW) cash_flow = JSONDecoder().decode(bytes.decode(cash_flow_result)) headers = utils.login(ip, login_body) reports = eod_position_report(position, risk, cash_flow, pricing_environment, ip, headers) position_result = JSONEncoder().encode(reports) r.set(EOD_CUSTOM_POSITION_ + pricing_environment.lower(), str(position_result)) now_date = datetime.now().date() pe_description = get_pricing_environment_description( pricing_environment, headers) params = { 'reportName': POSITION_REPORT_ + pe_description, 'valuationDate': str(now_date), 'positionReports': reports } utils.call_request(ip, 'report-service', 'rptPositionReportCreateBatch', params, headers)
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 basic_cash_flow_run(): headers = utils.login(ip, login_body) cash_flow = get_cash_flow(ip, headers) cash_flow_result = JSONEncoder().encode(cash_flow) r = utils.get_redis_conn(ip) r.set(EOD_BASIC_CASH_FLOW, str(cash_flow_result)) print('Basic CashFlow Data Has Save To Redis')
def basic_portfolio_trades_run(): headers = utils.login(ip, login_body) portfolio_trades = basic_portfolios.get_portfolio_trades(ip, headers) portfolio_trades_result = JSONEncoder().encode(portfolio_trades) r = utils.get_redis_conn(ip) r.set(INTRADAY_BASIC_PORTFOLIO_TRADES, str(portfolio_trades_result)) print('Basic portfolioTrades Data Has Save To Redis')
def basic_cash_flow_pd_run(): headers = utils.login(ip, login_body) cash_flow = basic_cashflows_pd.get_cash_flow(ip, headers) cash_flow_result = cash_flow.to_msgpack(compress='zlib') r = utils.get_redis_conn(redis_ip) r.set(INTRADAY_BASIC_CASH_FLOW, cash_flow_result) print('Basic CashFlow Pd Data Has Save To Redis')
def basic_cash_flow_today_run(): headers = utils.login(ip, login_body) cash_flow_today = basic_cashflows.get_cash_flows_today(ip, headers) cash_flow_today_result = JSONEncoder().encode(cash_flow_today) r = utils.get_redis_conn(ip) r.set(INTRADAY_BASIC_CASH_FLOW_TODAY, str(cash_flow_today_result)) print('Basic CashFlowToday Data Has Save To Redis')
def basic_portfolio_trades_pd_run(): headers = utils.login(ip, login_body) portfolio_trades = basic_portfolios_pd.get_portfolio_trades(ip, headers) portfolio_trades_result = portfolio_trades.to_msgpack(compress='zlib') r = utils.get_redis_conn(redis_ip) r.set(INTRADAY_BASIC_PORTFOLIO_TRADES, portfolio_trades_result) print('Basic portfolioTrades Pd Data Has Save To Redis')
def get_basic_underlyer_position(price_env): headers = utils.login(ip, login_body) underlyer_position = get_underlyer_positions(ip, headers, price_env) underlyer_position_result = JSONEncoder().encode(underlyer_position) r = utils.get_redis_conn(ip) r.set(EOD_BASIC_UNDELRYER_POSITION_ + price_env.lower(), str(underlyer_position_result)) print(price_env + ' Basic UnderlyerPosition Data Has Save To Redis')
def get_t2_trade_date(): headers = login(bct_host, bct_login_body) trading_date_result = get_trading_date_by_offset(-2, bct_host, headers) if 'error' in trading_date_result: raise RuntimeError('T-2交易日获取失败:{}'.format(str(trading_date_result))) trading_date = trading_date_result.get('result') datetime.strptime(trading_date, date_fmt) print(trading_date) return trading_date
def basic_cash_flow_today_pd_run(): headers = utils.login(data_resource_ip, login_body) # cash_flow_today = basic_cashflows_pd.get_cash_flows_today(data_resource_ip, headers) cash_flow_today = pd.DataFrame() cash_flow_today_result = cash_flow_today.to_msgpack(compress='zlib') r = utils.get_redis_conn(redis_ip) r.set(EOD_BASIC_CASH_FLOW_TODAY, cash_flow_today_result) print('Basic CashFlowToday Pd Data Has Save To Redis')
def basic_underlyer_position_run(): headers = utils.login(ip, login_body) underlyer_position = basic_listed_positions.get_underlyer_positions( ip, headers, PE_DEFAULT_INTRADAY) underlyer_position_result = JSONEncoder().encode(underlyer_position) r = utils.get_redis_conn(ip) r.set(INTRADAY_BASIC_UNDELRYER_POSITION, str(underlyer_position_result)) print('Basic UnderlyerPosition Data Has Save To Redis')
def basic_position_run(): headers = utils.login(ip, login_body) position, expiring_pos, position_map = get_positions(ip, headers) position_map_result = JSONEncoder().encode(position_map) position_result = JSONEncoder().encode(position) r = utils.get_redis_conn(ip) r.set(EOD_BASIC_POSITIONS, str(position_result)) r.set(EOD_BASIC_POSITION_MAP, str(position_map_result)) print('Basic Position Data Has Save To Redis')
def mock_trades(): headers = utils.login(ip, login_body) instruments = utils.call_request(ip, 'market-data-service', 'mktQuotesListPaged', {}, headers)['result']['page'] trades = list() trade_date = datetime(2019, 8, 16) while len(trades) < trades_size: trades.append(gen_trade_data(instruments, trade_date)) save_trade_to_db(trades) print('done')
def basic_risks_run(pricing_environment): headers = utils.login(ip, login_body) valuation_time = datetime.now() r = utils.get_redis_conn(ip) position_result = r.get(EOD_BASIC_POSITIONS) position = JSONDecoder().decode(bytes.decode(position_result)) risk, price_data = get_risks([position], pricing_environment, valuation_time, ip, headers) risk_result = JSONEncoder().encode(risk) r.set(EOD_BASIC_RISKS_ + pricing_environment.lower(), str(risk_result)) print('Basic Risk Data In ' + pricing_environment + ' Has Save To Redis')
def basic_position_pd_run(valuation_date): headers = utils.login(data_resource_ip, login_body) position, position_map = basic_positions_pd.get_eod_positions( data_resource_ip, headers, valuation_date) position_result = position.to_msgpack(compress='zlib') position_map_result = position_map.to_msgpack(compress='zlib') r = utils.get_redis_conn(redis_ip) r.set(EOD_BASIC_POSITIONS, position_result) r.set(EOD_BASIC_POSITION_MAP, position_map_result) print('Basic Position Pd Data Has Save To Redis')
def finanical_otc_client_fund_run(): headers = utils.login(ip, login_body) now_date = datetime.now().date() reports = get_finanical_otc_client_fund(headers, ip) params = { "reportName": "finanical_otc_client_fund_report", "valuationDate": str(now_date), "finanicalOtcClientFundReports": reports } utils.call_request(ip, 'report-service', 'rptFinanicalOtcClientFundReportCreateBatch', params, headers)
def update_market_data(market_data_type): real_time_market_data_map = {} bad_instrument = set() instrument_map = {} instrument_id_map = {} header = utils.login('10.1.5.16', login_body) market_data_ip_list = get_ip_list() for this_ip in market_data_ip_list: instrument_map.clear() try: instrument_list = \ utils.call_request(this_ip, 'market-data-service', 'mktInstrumentsListPaged', {}, header)['result'][ 'page'] except Exception as e: print("failed update market data for: " + this_ip + ",Exception:" + str(e)) continue quote_list = [] for instrument in instrument_list: if not_valid_instrument(instrument): continue instrument_id = get_instrument_id(instrument).upper() instrument_id_map[instrument_id] = instrument['instrumentId'] instrument_type = instrument_type_map[instrument['instrumentType']] quote_param = real_time_market_data_map.get(instrument_id, None) if quote_param is None: if instrument_id not in bad_instrument: id_list = instrument_map.get(instrument_type, []) id_list.append(instrument_id) instrument_map[instrument_type] = id_list bad_instrument.add(instrument_id) else: quote_list.append(quote_param) for (ins_type, ids) in instrument_map.items(): if len(ids) == 0: continue market_datas = real_time_market_data_get(ins_type, ids) for market_data in market_datas: temp_id = market_data['instrumentId'].upper() instrument_id = instrument_id_map.get(temp_id, None) if instrument_id is None: continue temp_quote_param = prepare_quote_param(market_data_type, market_data, instrument_id, ins_type) bad_instrument.discard(temp_id) real_time_market_data_map[temp_id] = temp_quote_param quote_list.append(temp_quote_param) utils.call_request(this_ip, 'market-data-service', 'mktQuoteSaveBatch', {'quotes': quote_list}, header) print('done for ' + this_ip) print('all done')
def basic_otc_company_type_run(): sub_companies = basic_otc_company_type.get_sub_company_from_oracle() sub_companies_name = list( set(map(lambda i: i.get('clientName'), sub_companies))) r = utils.get_redis_conn(redis_ip) r.set(EOD_BASIC_SUB_COMPANIES_FROM_ORACLE, JSONEncoder().encode(sub_companies_name)) print(' Basic SubCompanies Info from Oracle Has Save To Redis') headers = utils.login(data_resource_ip, login_body) params = {"infos": sub_companies} utils.call_request(report_ip, 'reference-data-service', 'refCompanyTypeInfoBatchCreate', params, headers)
def basic_position_run(): headers = utils.login(ip, login_body) position, expiring, position_index = basic_positions.get_intraday_positions( ip, headers) position_index_result = JSONEncoder().encode(position_index) position_result = JSONEncoder().encode(position) expiring_result = JSONEncoder().encode(expiring) r = utils.get_redis_conn(ip) r.set(INTRADAY_BASIC_EXPIRING, str(expiring_result)) r.set(INTRADAY_BASIC_POSITIONS, str(position_result)) r.set(INTRADAY_BASIC_POSITION_INDEX, str(position_index_result)) print('Basic Position Data Has Save To Redis')
def eod_subject_computing_run(pricing_environment): headers = utils.login(ip, login_body) report = get_eod_subject_computing(ip, headers, pricing_environment) pe_description = get_pricing_environment_description( pricing_environment, headers) params = { "reportName": '资产统计表_' + pe_description, 'reportType': RISK_REPORT_TYPE, "valuationDate": time.strftime("%Y-%m-%d", time.localtime()), "reports": [report] } utils.call_request(ip, 'report-service', 'rptCustomReportSaveBatch', params, headers)
def basic_risks_pd_run(pricing_environment, valuation_date): headers = utils.login(data_resource_ip, login_body) r = utils.get_redis_conn(redis_ip) position_result = r.get(EOD_BASIC_POSITIONS) position = pd.read_msgpack(position_result) if not position.empty: risk = basic_risks_pd.get_risks(position, pricing_environment, valuation_date, data_resource_ip, headers) risk_result = risk.to_msgpack(compress='zlib') r.set(EOD_BASIC_RISKS_ + pricing_environment.lower(), risk_result) print('Basic Risk Pd Data In ' + pricing_environment + ' Has Save To Redis')
def basic_underlyer_position_pd_run(): headers = utils.login(ip, login_body) underlyer_positions, listed_option_positions = basic_listed_positions_pd.get_underlyer_positions( ip, headers, PE_DEFAULT_INTRADAY) underlyer_positions_result = underlyer_positions.to_msgpack( compress='zlib') listed_option_positions_result = listed_option_positions.to_msgpack( compress='zlib') r = utils.get_redis_conn(redis_ip) r.set(INTRADAY_BASIC_UNDELRYER_POSITION, underlyer_positions_result) r.set(INTRADAY_BASIC_LISTED_OPTION_POSITION, listed_option_positions_result) print('Basic UnderlyerPosition Pd Data Has Save To Redis')
def get_basic_underlyer_position_pd(price_env): headers = utils.login(data_resource_ip, login_body) underlyer_position, listed_option_positions = basic_listed_positions_pd.get_underlyer_positions( data_resource_ip, headers, price_env) underlyer_position_result = underlyer_position.to_msgpack(compress='zlib') listed_option_positions_result = listed_option_positions.to_msgpack( compress='zlib') r = utils.get_redis_conn(redis_ip) r.set(EOD_BASIC_UNDELRYER_POSITION_ + price_env.lower(), underlyer_position_result) r.set(EOD_BASIC_LISTED_OPTION_POSITION_ + price_env.lower(), listed_option_positions_result) print(price_env + ' Basic UnderlyerPosition Pd Data Has Save To Redis')
def basic_risks_run(): valuation_time = datetime.now() pricing_environment = PE_DEFAULT_INTRADAY headers = utils.login(ip, login_body) r = utils.get_redis_conn(ip) position_result = r.get(INTRADAY_BASIC_POSITIONS) positions = JSONDecoder().decode(bytes.decode(position_result)) expiring_result = r.get(INTRADAY_BASIC_EXPIRING) expirings = JSONDecoder().decode(bytes.decode(expiring_result)) risk = basic_risks.get_risks([positions, expirings], pricing_environment, valuation_time, ip, headers)[0] risk_result = JSONEncoder().encode(risk) r.set(INTRADAY_BASIC_RISKS, str(risk_result)) print('Basic Risk Data Has Save To Redis')
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))