Пример #1
0
 def get_instrument_quote_close_dict_by_period(db_session, instrument_id, start_date, end_date, is_primary=False):
     quote_close_dto_list = QuoteCloseService.get_instrument_quote_close_list_by_period(db_session, instrument_id,
                                                                                        start_date, end_date,
                                                                                        is_primary)
     # 转化为字典类型
     quote_close_dto_dict = {}
     for quote_close in quote_close_dto_list:
         quote_close_dto_dict[quote_close.tradeDate] = quote_close
     # 校验缺失的收盘价数据
     missing_dates = []
     holidays = TradingDayService.get_holiday_list(db_session, start_date, end_date)
     trade_date = start_date
     while trade_date <= end_date:
         if DateTimeUtils.is_trading_day(trade_date, holidays):
             if trade_date not in quote_close_dto_dict:
                 # 将缺失的日期添加到诊断中
                 missing_dates.append(trade_date)
                 # 是否需要将缺失的数据按None补齐
                 quote_close_dto_dict[trade_date] = None
         trade_date += timedelta(days=1)
     # 添加诊断信息
     diagnostics = []
     if len(missing_dates) != 0:
         message = '标的物%s在[%s, %s]时段内中缺失%d条收盘数据' % (
             instrument_id, DateTimeUtils.date2str(start_date), DateTimeUtils.date2str(end_date), len(missing_dates))
         diagnostics.append(DiagnosticDTO(DiagnosticType.WARNING, message, missing_dates))
         logging.debug(message)
     return quote_close_dto_dict, diagnostics
Пример #2
0
 def import_atm_vol_from_csv_file(file_path, run_id):
     fname, ext = os.path.splitext(file_path)
     valuation_date = DateTimeUtils.str2date(fname.split('_')[1],
                                             pattern='%Y%m%d')
     df = pd.read_csv(file_path)
     atm_quote_list = []
     current_time = datetime.now()
     for index, row in df.iterrows():
         atm_quote = OtcAtmQuote()
         atm_quote.uuid = uuid.uuid4()
         atm_quote.varietyType = row['udly'].strip()
         atm_quote.underlyer = row['code'].strip()
         atm_quote.legalEntityName = row['ctpt'].strip()
         atm_quote.expireDate = DateTimeUtils.str2date(row['exe_date'])
         if row['type'] == 'am':
             atm_quote.productType = OptionProductType.VANILLA_AMERICAN.name
         if row['type'] == 'eu':
             atm_quote.productType = OptionProductType.VANILLA_EUROPEAN.name
         atm_quote.ptm = float(row['ptm'])
         atm_quote.askVol = float(row['ask_vol_cal'])
         atm_quote.bidVol = float(row['bid_vol_cal'])
         atm_quote.volEdge = float(row['vol_edge_cal'])
         atm_quote.source = 'EXCEL_%s' % run_id
         atm_quote.valuationDate = valuation_date
         atm_quote.updatedAt = current_time
         atm_quote_list.append(atm_quote)
     db_sessin = create_db_session()
     db_sessin.add_all(atm_quote_list)
     db_sessin.commit()
Пример #3
0
 def convert_data_to_position_snapshot(clean_position_dict):
     """
     格式化为otc_position_snapshot表model
     :param clean_position_dict:
     :return:
     """
     position_snapshots = []
     for trans_code, clean_position in clean_position_dict.items():
         # todo 需要删除continue
         if clean_position is None:
             continue
         report_date = DateTimeUtils.str2date(
             clean_position.get('report_date'))
         position_date = DateTimeUtils.str2date(
             clean_position.get('position_date'))
         item = OTCPositionSnapshot(
             transCode=clean_position.get('trans_code'),
             recordId=clean_position.get('record_id'),
             reportDate=report_date,
             positionDate=position_date,
             instrumentId=clean_position.get('underlyer'),
             mainBodyName=clean_position.get('main_body_name'),
             tradeNotional=clean_position.get('trade_notional'),
             impliedVol=clean_position.get('implied_vol'),
             interestRate=clean_position.get('interest_rate'),
             dividend=clean_position.get('dividend'),
             updatedAt=datetime.now())
         position_snapshots.append(item)
     return position_snapshots
Пример #4
0
 def check_date_params(db_session, start_date, end_date, window):
     if start_date > date.today() or end_date > date.today():
         raise CustomException('所选日期不能超过今天')
     if TradingDayService.get_effective_days_num(db_session, start_date,
                                                 end_date) < window:
         raise CustomException('所选日期范围[%s,%s]交易日个数小于窗口大小,无法进行计算' %
                               (DateTimeUtils.date2str(start_date),
                                DateTimeUtils.date2str(end_date)))
Пример #5
0
 async def get_instrument_id_list(self,
                                  trade_date=DateTimeUtils.date2str(
                                      date.today()),
                                  filtering=True):
     with self.make_session() as db_session:
         instrument_id_list = InstrumentService.\
             get_instrument_id_list(db_session, DateTimeUtils.str2date(trade_date), filtering)
     return instrument_id_list
Пример #6
0
 async def get_commodity_future_contract_order(self, variety_type,
                                               start_date, end_date):
     with self.make_session() as db_session:
         fc_info_list = CommodityFutureContractService.get_future_contract_order_dto(
             db_session, variety_type, [],
             DateTimeUtils.str2date(start_date),
             DateTimeUtils.str2date(end_date))
         fc_info_schema = FutureContractInfoSchema(many=True)
         return fc_info_schema.dump(fc_info_list).data
Пример #7
0
 def calc_instrument_realized_vol(db_session,
                                  instrument_id,
                                  trade_date,
                                  windows,
                                  is_primary=False):
     if trade_date > date.today():
         raise CustomException('所选日期不能超过今天')
     # TODO: 如果不是交易日,当前默认使用上一个交易日
     holidays = TradingCalendarRepo.get_holiday_list(db_session)
     start_date = DateTimeUtils.get_trading_day(trade_date,
                                                holidays,
                                                special_dates=[],
                                                step=max(windows),
                                                direction=-1)
     quote_close_dto_dict, diagnostics = QuoteCloseService.get_instrument_quote_close_dict_by_period(
         db_session, instrument_id, start_date, trade_date, is_primary)
     realized_vol_dto_list = []
     for window in windows:
         return_rates = []
         # 判断是否为节假日, 非节假日start_date为: trade_date往前走window-1天, 节假日start_date为: trade_date往前走window天
         if DateTimeUtils.is_trading_day(trade_date, holidays):
             start_date = DateTimeUtils.get_trading_day(trade_date,
                                                        holidays,
                                                        special_dates=[],
                                                        step=window - 1,
                                                        direction=-1)
         else:
             start_date = DateTimeUtils.get_trading_day(trade_date,
                                                        holidays,
                                                        special_dates=[],
                                                        step=window,
                                                        direction=-1)
         # 只处理在窗口范围日期内的数据
         for key in quote_close_dto_dict:
             if start_date <= key <= trade_date:
                 quote_close = quote_close_dto_dict.get(key)
                 if quote_close is not None and quote_close.returnRate is not None:
                     return_rates.append(quote_close.returnRate)
         if len(return_rates) < window:
             logging.debug('窗口大小大于回报率个数,窗口大小:%d,回报率个数:%d' %
                           (window, len(return_rates)))
         realized_vol = HistoricalVolAlgo.calc_one_realized_vol(
             return_rates)
         if realized_vol is None:
             # 如果计算得到的结果为None,直接跳过处理
             logging.debug('计算得到的值为空,窗口:%d' % window)
             continue
         dto = RealizedVolDTO()
         dto.window = window
         dto.vol = realized_vol
         realized_vol_dto_list.append(dto)
     return realized_vol_dto_list, diagnostics
    def get_future_contract_order(db_session, contract_type, exist_dates,
                                  start_date, end_date):
        holidays = TradingCalendarRepo.get_holiday_list(db_session)
        special_dates = []
        if start_date > end_date:
            logging.error(
                "日期指定非法:%s 大于 %s" % DateTimeUtils.date2str(start_date),
                DateTimeUtils.date2str(end_date))
            return None
        else:
            all_dates = [
                DateTimeUtils.str2date(date.strftime("%Y-%m-%d"))
                for date in rrule(DAILY, dtstart=start_date, until=end_date)
            ]
            if exist_dates:
                missing_dates = list(set(all_dates) - set(exist_dates))
            else:
                missing_dates = all_dates
            listed_date, delisted_date = DateTimeUtils.offset_trade_date(
                start_date, end_date, holidays, special_dates)
            close_dict = CommodityFutureContractService.get_corresponding_close(
                db_session, contract_type, listed_date, delisted_date)
            instruments_list = db_session.query(Instrument).filter(
                Instrument.contractType == contract_type)

            fc_list = []
            if close_dict:
                for trade_date in missing_dates:
                    if DateTimeUtils.is_trading_day(trade_date, holidays,
                                                    special_dates):
                        logging.info("处理交易日%s" %
                                     DateTimeUtils.date2str(trade_date))
                        previous_trade_date, _ = DateTimeUtils.offset_trade_date(
                            trade_date, trade_date, holidays, special_dates)
                        pri, sec = FutureContractInfoAlgo.calc_one_future_contract_info(
                            instruments_list, close_dict, contract_type,
                            previous_trade_date)
                        if pri:
                            fc_info = FutureContractInfo(
                                contractType=contract_type,
                                tradeDate=trade_date,
                                primaryContractId=pri,
                                secondaryContractId=sec)
                            logging.info(
                                '合约的主力和次主力为:%s %s %s %s' %
                                (fc_info.contractType,
                                 DateTimeUtils.date2str(fc_info.tradeDate),
                                 fc_info.primaryContractId,
                                 fc_info.secondaryContractId))
                            fc_list.append(fc_info)
                    else:
                        logging.info("跳过非交易日%s" %
                                     DateTimeUtils.date2str(trade_date))
            else:
                fc_list = None
            return fc_list
Пример #9
0
 def delete_data_by_dates(db_session, start_date, end_date):
     """
     根据日期和标的删除数据
     :param db_session:
     :param start_date:
     :param end_date:
     :return:
     """
     logger.info('准备删除: %s 至 %s时间段内的标数据' % (start_date, end_date))
     db_session.query(ROTCPosition). \
         filter(ROTCPosition.REPORTDATE >= DateTimeUtils.date2str(start_date),
                ROTCPosition.REPORTDATE <= DateTimeUtils.date2str(end_date)). \
         delete(synchronize_session=False)
     db_session.commit()
     logger.info('成功删除: %s 至 %s时间段内的标的数据' % (start_date, end_date))
Пример #10
0
 async def get_historical_and_neutral_vol_list(self,
                                               instrument_ids,
                                               start_date,
                                               end_date,
                                               window,
                                               is_primary=False):
     with self.make_session() as db_session:
         start_date_obj = DateTimeUtils.str2date(start_date)
         end_date_obj = DateTimeUtils.str2date(end_date)
         neutral_vol_list, diagnostics = HistoricalVolService.calc_historical_and_neutral_vol_list(
             db_session, instrument_ids, start_date_obj, end_date_obj,
             window, is_primary)
     neutral_vol_schema = NeutralVolSchema(many=True)
     return DiagnosticResponse(
         neutral_vol_schema.dump(neutral_vol_list).data, diagnostics)
Пример #11
0
 async def get_wing_model(
         self,
         contract_type,
         observed_date,
         start_strike=None,
         end_strike=None,
         point_num=None,
         step=None,
         strike_type=FittingModelStrikeType.valueOf('STRIKE'),
         days_in_year=365,
         calendar_name=None):
     with self.make_session() as db_session:
         observed_date = DateTimeUtils.str2date(observed_date)
         if OptionStructureRepo.check_commodity(db_session, contract_type):
             vol_surface_dto = WingModelService.calc_commodity_vol_surface(
                 db_session, contract_type, observed_date, start_strike,
                 end_strike, point_num, step,
                 FittingModelStrikeType.valueOf(strike_type.upper()),
                 days_in_year, calendar_name)
         else:
             vol_surface_dto = WingModelService.calc_fund_vol_surface(
                 db_session, contract_type, observed_date, start_strike,
                 end_strike, point_num, step,
                 FittingModelStrikeType.valueOf(strike_type.upper()),
                 days_in_year, calendar_name)
         vol_surface_schema = VolSurfaceSchema()
         return vol_surface_schema.dump(vol_surface_dto).data
Пример #12
0
 def get_primary_instrument_id(db_session, instrument_id, trade_date,
                               is_primary):
     if InstrumentService.is_commodity_variety_type(db_session,
                                                    instrument_id):
         # 如果传入的是大宗合约类型, 则直接获取当天的主力合约代码
         variety_type = instrument_id
     else:
         if is_primary is False:
             return instrument_id
         # 如果传入的是具体的大宗标的代码, 则直接获取当天的主力合约代码
         variety_type = InstrumentService.get_commodity_variety_type(
             db_session, instrument_id)
     # 如果获取到的合约种类为空, 则说明该标的不是大宗商品, 直接返回标的代码
     if variety_type is None:
         instrument = InstrumentRepo.get_instrument(db_session,
                                                    instrument_id)
         if instrument is None:
             raise CustomException('不支持标的%s' % instrument_id)
         return instrument_id
     else:
         future_contract = CommodityFutureContractRepo.get_commodity_future_contract(
             db_session, variety_type, trade_date)
         if future_contract is None or future_contract.primaryContractId is None:
             raise CustomException(
                 "合约类型%s在%s下无主力合约数据" %
                 (variety_type, DateTimeUtils.date2str(trade_date)))
         return future_contract.primaryContractId
Пример #13
0
 def calc_instrument_vol_cone(db_session,
                              instrument_id,
                              start_date,
                              end_date,
                              windows,
                              percentiles,
                              is_primary=False):
     origin_start_date = start_date
     holidays = TradingDayService.get_holiday_list(db_session)
     # 判断是否时节假日
     if DateTimeUtils.is_trading_day(end_date, holidays):
         start_date = TradingDayService.go_trading_day(db_session,
                                                       start_date,
                                                       max(windows) - 1,
                                                       direction=-1)
     else:
         start_date = TradingDayService.go_trading_day(db_session,
                                                       start_date,
                                                       max(windows),
                                                       direction=-1)
     quote_close_dto_dict, diagnostics = QuoteCloseService.\
         get_instrument_quote_close_dict_by_period(db_session, instrument_id, start_date, end_date, is_primary)
     vol_cone_dto_list = HistoricalVolService.\
         calc_multiple_window_percentiles_by_quotes(origin_start_date, end_date, quote_close_dto_dict, windows,
                                                    percentiles)
     return vol_cone_dto_list, diagnostics
Пример #14
0
 async def get_instrument_rolling_vol(self,
                                      instrument_id,
                                      start_date,
                                      end_date,
                                      window,
                                      is_primary=False):
     with self.make_session() as db_session:
         start_date_obj = DateTimeUtils.str2date(start_date)
         end_date_obj = DateTimeUtils.str2date(end_date)
         realized_vol_dto_list, diagnostics = HistoricalVolService.calc_instrument_rolling_vol(
             db_session, instrument_id, start_date_obj, end_date_obj,
             window, is_primary)
     realized_vol_schema = RealizedVolSchema(
         many=True, exclude=['instrumentId', 'percentile'])
     return DiagnosticResponse(
         realized_vol_schema.dump(realized_vol_dto_list).data, diagnostics)
Пример #15
0
    def expire_date_fun(term_list, db_session):
        """
        根据当前日期和特定步长天数列表,除去节假日和双休,计算到期交易日
        [1,3,5,10,22,44,66,123]: 特定步长天数列表
        使用business_calendar库的Calendar方法

        :param date: term_list(特定步长天数列表)
        :return: expire_date_dict
        """

        # 获取当前时间
        current_day = datetime.date.today()

        # 获取节假日
        holidays = TradingCalendarRepo.get_holiday_list(db_session)

        # # todo 获取特殊日
        # special_dates = load_special_dates(db_session, underlier)
        special_dates = []
        # 根据step获取交易日是哪天,并与步长天数组成字典
        # 将到期日与天数组成一个字典
        expire_date_dict = {}
        for step_date in term_list:
            expire_date = DateTimeUtils.get_trading_day(
                current_day, holidays, special_dates, step_date)
            expire_date = expire_date.strftime('%Y-%m-%d')
            expire_date_dict[expire_date] = step_date

        return expire_date_dict
Пример #16
0
    def import_vol_surface_from_excel_file():
        """
        解析excel文件保存到 vol surface表
        :return:
        """
        instrument_ids = sys.argv[1].split(",")
        dir_case = sys.argv[3]
        valuation_date = DateTimeUtils.str2date(sys.argv[2])
        data = xlrd.open_workbook(dir_case)

        for instrument_id in instrument_ids:
            try:
                # 判断是否获取到了spot price
                instrument_dict = VolSurfaceExcelService.get_instrument_spot(
                    instrument_id)
                spot_price = instrument_dict.get('lastPrice')
                if spot_price is None:
                    logging.error('发送请求获取标的: %s的成功, 但是没有spot_price' %
                                  instrument_id)
                    continue
                logging.info('发送请求获取标的: %s的spot price成功' % instrument_id)
                logging.info('开始解析excel,标的为: %s' % instrument_id)
                table = data.sheet_by_name(instrument_id)
                vol_surface_dto = VolSurfaceExcelService.convert_to_vol_surface_dto(
                    instrument_id, valuation_date, table, spot_price)
                logging.info('解析excel成功,标的为: %s' % instrument_id)
                vol_surface_schema = VolSurfaceSchema()
                # 序列化对象
                vol_surface = vol_surface_schema.dump(vol_surface_dto).data
                # 发送请求
                VolSurfaceExcelService.send_post_request(vol_surface)
            except Exception as e:
                logging.error('标的: %s,出现错误: %s' % (instrument_id, e))
Пример #17
0
 async def get_instrument_realized_vol(
         self,
         instrument_id,
         trade_date,
         windows=[1, 3, 5, 10, 22, 44, 66, 132],
         is_primary=False):
     with self.make_session() as db_session:
         trade_date_obj = DateTimeUtils.str2date(trade_date)
         if trade_date_obj > date.today():
             raise CustomException('所选日期不能超过今天')
         realized_vol_dto_list, diagnostics = HistoricalVolService.calc_instrument_realized_vol(
             db_session, instrument_id, DateTimeUtils.str2date(trade_date),
             windows, is_primary)
     realized_vol_schema = RealizedVolSchema(
         many=True, exclude=['tradeDate', 'instrumentId', 'percentile'])
     return DiagnosticResponse(
         realized_vol_schema.dump(realized_vol_dto_list).data, diagnostics)
Пример #18
0
 def go_trading_day(db_session, current_date, step, direction=1):
     special_dates = []
     holidays = TradingCalendarRepo.get_holiday_list(db_session)
     return DateTimeUtils.get_trading_day(current_date,
                                          holidays,
                                          special_dates,
                                          step,
                                          direction=direction)
Пример #19
0
 def get_otc_asset_tool_report(self,
                               start_date=None,
                               end_date=None,
                               page=None,
                               page_size=None,
                               current_user=None):
     with self.make_session() as db_session:
         if start_date is None or end_date is None:
             raise CustomException('所选日期不能为空')
         if (page is None
                 and page_size is not None) or (page is not None
                                                and page_size is None):
             raise CustomException('分页参数page和page_size必须同时存在或不存在')
         DateTimeUtils.validate(start_date, REPORT_DATE_PATTERN)
         DateTimeUtils.validate(end_date, REPORT_DATE_PATTERN)
         reports = OTCReportService.get_otc_asset_tool_report(
             db_session, start_date, end_date, page, page_size)
     return reports
 async def get_implied_vol_report(self, instrument_id, report_date):
     with self.make_session() as db_session:
         report_date_obj = DateTimeUtils.str2date(report_date)
         if report_date_obj > date.today():
             raise CustomException('所选日期不能超过今天')
         vol_report_dto_list = OTCPositionSnapshotService.get_implied_vol_report(
             db_session, instrument_id, report_date_obj)
     implied_vol_report_schema = ImpliedVolReportSchema(many=True,
                                                        exclude=[])
     return implied_vol_report_schema.dump(vol_report_dto_list).data
Пример #21
0
 async def get_heat_map(self, trade_date=None):
     with self.make_session() as db_session:
         if trade_date is None:
             trade_date = datetime.now().date()
         # 求上一个交易日
         holidays = TradingDayService.get_holiday_list(db_session)
         trade_date = DateTimeUtils.get_trading_day(trade_date, holidays, special_dates=[], step=2, direction=-1)
         heat_map_dto_list, diagnostics = OtcAtmQuoteService.get_heat_map(db_session, trade_date)
     heat_map_schema = HeatMapSchema(many=True, exclude=[])
     return DiagnosticResponse(heat_map_schema.dump(heat_map_dto_list).data, diagnostics)
Пример #22
0
 async def get_instrument_vol_cone(
         self,
         instrument_id,
         start_date,
         end_date,
         windows=[1, 3, 5, 10, 22, 44, 66, 132],
         percentiles=[0, 0.1, 0.25, 0.50, 0.75, 0.90, 1],
         is_primary=False):
     with self.make_session() as db_session:
         start_date_obj = DateTimeUtils.str2date(start_date)
         end_date_obj = DateTimeUtils.str2date(end_date)
         vol_cone_dto_list, diagnostics = HistoricalVolService.calc_instrument_vol_cone(
             db_session, instrument_id, start_date_obj, end_date_obj,
             windows, [percentile * 100 for percentile in percentiles],
             is_primary)
     vol_cone_schema = VolConeSchema(
         many=True,
         exclude=['vols.instrumentId', 'vols.tradeDate', 'vols.window'])
     return DiagnosticResponse(
         vol_cone_schema.dump(vol_cone_dto_list).data, diagnostics)
Пример #23
0
 def update_eod_date(start_date_str=EODDateConfig.eod_start_date,
                     end_date_str=DateTimeUtils.date2str(date.today()),
                     eod_cutoff_time=EODDateConfig.eod_cutoff_time):
     end_date = DateTimeUtils.str2date(end_date_str)
     start_date = DateTimeUtils.str2date(start_date_str)
     # 获取节假日信息
     db_session = create_db_session()
     # TODO: 需要从配置文件读取
     if not TradingDayService.is_trading_day(db_session, end_date):
         # 如果当天不是交易日则直接计算上一个交易日的值
         end_date = TradingDayService.go_trading_day(db_session, end_date, 1, -1)
     else:
         # 如果是交易日,小于cutoff time还是返回上一个交易日
         if datetime.now().time() < DateTimeUtils.str2time(eod_cutoff_time):
             end_date = TradingDayService.go_trading_day(db_session, end_date, 1, -1)
         else:
             end_date = end_date
     # 设置变量到airflow variable
     AirflowVariableUtils.set_eod_start_date(start_date)
     AirflowVariableUtils.set_eod_end_date(end_date)
Пример #24
0
 def calc_instrument_atm_vol_list(db_session, underlyer, start_date,
                                  end_date, is_primary):
     atm_quote_list = OtcAtmQuoteService.get_instrument_atm_quote_list_by_period(
         db_session, underlyer, start_date, end_date, is_primary)
     # TODO:去掉重复数据,相同valuation date, underlyer, source, expire date的数据需要去重
     # 根据valuation date进行分类
     atm_quote_dict = {}
     for atm_quote in atm_quote_list:
         if atm_quote.valuationDate not in atm_quote_dict:
             atm_quote_dict[atm_quote.valuationDate] = []
         atm_quote_dict[atm_quote.valuationDate].append(atm_quote)
     # 取一个自然月后到期日的前后3天的ask_vol和bid_vol的平均值作为当天的atm_vol
     atm_vol_dict = {}
     for valuation_date in atm_quote_dict:
         quote_list = atm_quote_dict[valuation_date]
         atm_vol = OtcAtmQuoteService.calc_one_atm_vol(
             valuation_date, quote_list)
         if atm_vol is not None:
             atm_vol_dict[valuation_date] = atm_vol
     # 检测哪些日期缺失
     missing_dates = []
     holidays = TradingDayService.get_holiday_list(db_session, start_date,
                                                   end_date)
     trade_date = start_date
     while trade_date <= end_date:
         if DateTimeUtils.is_trading_day(trade_date, holidays):
             if trade_date not in atm_vol_dict:
                 # 将缺失的日期添加到诊断中
                 missing_dates.append(trade_date)
                 # TODO: 是否需要将缺失的数据按0补足
         trade_date += timedelta(days=1)
     # 添加诊断信息
     diagnostics = []
     if len(missing_dates) != 0:
         message = '标的物%s在[%s, %s]时段内中缺失%d条ATM Vol数据' % (
             underlyer, DateTimeUtils.date2str(start_date),
             DateTimeUtils.date2str(end_date), len(missing_dates))
         diagnostics.append(
             DiagnosticDTO(DiagnosticType.WARNING, message, missing_dates))
         logging.error(message)
     return atm_vol_dict.values(), diagnostics
    def import_otc_trade_snapshots(start_date, end_date, force_update):
        """
        根据开始结束日期导入oracle数据库中的保证金中心r_otc_position和r_otc_tradedate数据
        :param start_date:
        :param end_date:
        :param force_update: 强制更新
        :return:
        """
        db_session = Session()
        try:
            logger.info('开始导入: %s 至 %s的数据' % (start_date, end_date))
            # 时间段分割
            date_quantum_list = DateTimeUtils.date_quantum_partition(
                start_date, end_date, step=0)
            for dates in date_quantum_list:
                start_date, end_date = dates[0], dates[1]
                # r_otc_postion
                r_otc_positions = OracleROTCPositionRepo.get_otc_position_by_date(
                    start_date, end_date)
                if len(r_otc_positions) != 0:
                    # 转换数据
                    r_otc_positions = OTCTradeSnapshotsService.transform_r_otc_position_model(
                        r_otc_positions)
                    r_otc_position_dict = OTCTradeSnapshotsService.r_otc_objs_to_dict(
                        r_otc_positions)
                    # 查询数据平台数据
                    existing_record_ids = ROTCPositionRepo.get_record_ids_by_date(
                        db_session, start_date, end_date)
                    # 保存到数据平台
                    OTCTradeSnapshotsService.save_r_otc_position(
                        db_session, r_otc_position_dict, existing_record_ids,
                        start_date, end_date, force_update)

                # r_otc_tradedata
                r_otc_tradedatas = OracleROTCTradedataRepo.get_otc_tradedata_by_date(
                    start_date, end_date)
                if len(r_otc_tradedatas) != 0:
                    # 转换数据
                    r_otc_tradedatas = OTCTradeSnapshotsService.transform_r_otc_tradedata_model(
                        r_otc_tradedatas)
                    r_otc_tradedata_dict = OTCTradeSnapshotsService.r_otc_objs_to_dict(
                        r_otc_tradedatas)
                    # 查询数据平台数据
                    existing_record_ids = ROTCTradedataRepo.get_record_ids_by_date(
                        db_session, start_date, end_date)
                    # 保存到数据平台
                    OTCTradeSnapshotsService.save_r_otc_tradedata(
                        db_session, r_otc_tradedata_dict, existing_record_ids,
                        start_date, end_date, force_update)
        except Exception as e:
            logger.error('导入数据异常, 异常信息为: %s' % e)
            db_session.close()
            raise Exception(e)
Пример #26
0
    def get_otc_position_by_date(start_date, end_date):
        """
        根据时间段获取数据
        :param start_date:
        :param end_date:
        :return:
        """
        logger.info('开始获取%s 至 %s时间段内r_otc_postion表的数据' %
                    (start_date, end_date))
        oracle_session = Oracle_Session()
        r_otc_positions = oracle_session.query(ROTCPosition).filter(
            ROTCPosition.REPORTDATE >= DateTimeUtils.date2str(start_date),
            ROTCPosition.REPORTDATE <= DateTimeUtils.date2str(end_date)).all()
        oracle_session.close()
        if len(r_otc_positions) == 0 or r_otc_positions is None:
            logger.info('在%s 至 %s时间段内r_otc_postion表没有数据' %
                        (start_date, end_date))
            return []
        logger.info('在%s 至 %s时间段内的获取了 %d条r_otc_postion表的数据' %
                    (start_date, end_date, len(r_otc_positions)))

        return r_otc_positions
Пример #27
0
 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
Пример #28
0
    def get_record_ids_by_date(db_session, start_date, end_date):
        """
        根据传入的时间,获取数据
        :param db_session:
        :param start_date:
        :param end_date:
        :return:
        """
        logger.info('开始获取数据平台r_otc_position表 %s 到 %s 的数据' %
                    (start_date, end_date))
        record_ids = db_session.query(ROTCPosition.RECORDID).filter(
            ROTCPosition.REPORTDATE >= DateTimeUtils.date2str(start_date),
            ROTCPosition.REPORTDATE <= DateTimeUtils.date2str(end_date)).all()
        if len(record_ids) == 0 or record_ids is None:
            logger.info('数据平台r_otc_position表 %s 到 %s 之间没有数据' %
                        (start_date, end_date))
            return []
        record_ids = [record_id.RECORDID for record_id in record_ids]
        logger.info('数据平台r_otc_position表 %s 到 %s 数据长度为: %d' %
                    (start_date, end_date, len(record_ids)))

        return record_ids
Пример #29
0
 async def save_otc_atm_quote_list(self):
     # 如果标的物代码不为空,则忽略instrument_ids
     # TODO: 需要完善
     with self.make_session() as db_session:
         atm_quote_list = []
         dto = OtcAtmQuoteDTO()
         dto.uuid = 'f405e2f8-cb14-11e9-80de-00163e08c03e'
         dto.underlyer = 'RS911.CZC'
         dto.valuationDate = DateTimeUtils.str2date('2019-09-03')
         dto.askVol = 0.1
         dto.bidVol = 0.2
         atm_quote_list.append(dto)
         atm_quote_dto_list = OtcAtmQuoteService.save_otc_atm_quote_list(db_session, atm_quote_list)
     otc_atm_quote_schema = OtcAtmQuoteSchema(many=True, exclude=[])
     return otc_atm_quote_schema.dump(atm_quote_dto_list).data
Пример #30
0
 async def get_latest_vol_surface(
         self,
         instrument_id,
         valuation_date,
         strike_type=VolSurfaceStrikeType.PERCENT.name,
         instance=VolSurfaceInstanceType.CLOSE.name,
         is_primary=False):
     with self.make_session() as db_session:
         vol_surface_dto = ImpliedVolService.get_latest_vol_surface(
             db_session, instrument_id,
             DateTimeUtils.str2date(valuation_date), strike_type, instance,
             is_primary)
         if vol_surface_dto is None:
             raise CustomException('没有找到对应的波动率曲面')
     vol_surface_schema = VolSurfaceSchema(exclude=[])
     return vol_surface_schema.dump(vol_surface_dto).data