def save_realized_vol(db_session, valuation_date, realized_vol_dict, existed_realized_vol_dict, force_update): missing_realized_vol_list, existed_realized_vol_list, uuids = [], [], [] for instrument_id, windows_dict in realized_vol_dict.items(): for windows, realized_vol in windows_dict.items(): if instrument_id in existed_realized_vol_dict and windows in existed_realized_vol_dict[ instrument_id]: existed_realized_vol_list.append(realized_vol) uuids.append( existed_realized_vol_dict[instrument_id][windows].uuid) else: missing_realized_vol_list.append(realized_vol) if len(missing_realized_vol_list) != 0: # 保存历史波动率 RealizedVolRepo.save_realized_vol_list(db_session, missing_realized_vol_list) else: logging.info('交易日为: %s时, 没有历史波动率需要保存' % valuation_date) # 强制更新 if force_update is True: if len(existed_realized_vol_list) != 0: # 删除历史波动率 RealizedVolRepo.delete_realized_vol_by_uuids(db_session, uuids) # 保存历史波动率 RealizedVolRepo.save_realized_vol_list( db_session, existed_realized_vol_list) else: logging.info('交易日为: %s时, 没有历史波动率需要强制更新' % valuation_date)
def delete_position_snapshot_by_report_date(db_session, report_date): """ 通过日期删除数据 :param db_session: :param report_date: :return: """ logging.info('开始删除otc_position_snapshot表的数据,日期为 %s' % report_date) db_session.query(OTCPositionSnapshot).filter(OTCPositionSnapshot.reportDate == report_date).delete() db_session.commit() logging.info('删除数据成功')
def get_trans_codes_by_report_date(db_session, report_date): """ 根据日期获取当天所有数据 :param db_session: :param report_date: :return: """ logging.info('开始获取日期为: %s的trans_codes' % report_date) trans_codes = db_session.query(OTCPositionSnapshot.transCode).\ filter(OTCPositionSnapshot.reportDate == report_date).all() if trans_codes is None or len(trans_codes) == 0: logging.info('日期为: %s,没有获取到trans_codes' % report_date) return [] trans_codes = [_[0] for _ in trans_codes] return trans_codes
def calc_one_atm_vol(valuation_date, atm_quote_list): effective_vol_list = [] effective_date_set = OtcAtmQuoteService.get_effective_expire_date_set( valuation_date) for quote in atm_quote_list: expire_date = quote.expireDate if expire_date not in effective_date_set: logging.info( 'ATM quote不在有效的到期范围内,当前日期: %s,估值日期: %s, Quote UUID: %s' % (DateTimeUtils.date2str(expire_date), DateTimeUtils.date2str(valuation_date), quote.uuid)) continue if quote.askVol is not None: effective_vol_list.append(quote.askVol) if quote.bidVol is not None: effective_vol_list.append(quote.bidVol) if len(effective_vol_list) == 0: return None atm_vol = RealizedVolDTO() atm_vol.tradeDate = valuation_date atm_vol.vol = np.mean(effective_vol_list) return atm_vol
def calc_one_instrument_heat_map(db_session, short_name, contract_type, instrument_id, start_date, end_date): logging.info('开始计算标的: %s的热力图' % instrument_id) diagnostic_list = [] # 计算realied vol序列的percentile与vol realized_vol_dto_list, diagnostics = HistoricalVolService. \ calc_instrument_rolling_vol(db_session, instrument_id, start_date, end_date, 22, is_primary=True) realized_vol = None realized_vol_percentile = None if len(realized_vol_dto_list) != 0: realized_vol_dto = OtcAtmQuoteService.get_last_vol( realized_vol_dto_list) realized_vol_list = [_.vol for _ in realized_vol_dto_list] realized_vol = realized_vol_dto.vol realized_vol_percentile = OtcAtmQuoteService.calc_percentile( realized_vol_list, realized_vol) if len(diagnostics) != 0: diagnostic_list.extend(diagnostics) # 计算implied vol序列的percentile与vol implied_vol_dto_list, diagnostics = OtcAtmQuoteService. \ calc_instrument_atm_vol_list(db_session, instrument_id, start_date, end_date, is_primary=True) implied_vol = None implied_vol_percentile = None if len(implied_vol_dto_list) != 0: implied_vol_dto = OtcAtmQuoteService.get_last_vol( implied_vol_dto_list) implied_vol_list = [_.vol for _ in implied_vol_dto_list] implied_vol = implied_vol_dto.vol implied_vol_percentile = OtcAtmQuoteService.calc_percentile( implied_vol_list, implied_vol) if len(diagnostics) != 0: diagnostic_list.extend(diagnostics) heat_map_dto = OtcAtmQuoteService. \ to_heat_map_dto(short_name, contract_type, realized_vol_percentile, realized_vol, implied_vol, implied_vol_percentile) return heat_map_dto, diagnostic_list