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()
def update_all_future_contract_info(start_date, end_date, force_update=False, active_only=True): db_session = std.create_db_session() variety_types = InstrumentRepo.get_commodity_variety_types( db_session, active_only) exist_dict = CommodityFutureContractRepo.get_exist_future_contract_info( db_session, force_update, start_date, end_date, variety_types) for variety_type in variety_types: logging.info('正在处理的合约类型为:%s' % variety_type) fc_list = CommodityFutureContractService.get_future_contract_order( db_session, variety_type, exist_dict.get(variety_type), start_date, end_date) CommodityFutureContractRepo.insert_fc_info_list( db_session, fc_list)
def recon_one_instrument(self, params): instrument, start_date, end_date, holidays, dry_run, ignore_milestone = params db_session = std.create_db_session() try: logging.info("开始Recon标的物: %s" % instrument.instrumentId) # 获取标的物Milestone milestone = self.get_milestone(db_session, instrument) # 只处理标的物上市日期和退市日期之间的数据 if instrument.listedDate is not None and start_date < instrument.listedDate: start_date = instrument.listedDate if instrument.delistedDate is not None and end_date > instrument.delistedDate: end_date = instrument.delistedDate # 需要Recon的日期由API给出 quote_close_dates = self.request_quote_close_dates(instrument, start_date, end_date) if not quote_close_dates: unchecked_trading_dates = [] else: unchecked_trading_dates = [DateTimeUtils.str2date(x) for x in quote_close_dates] # 对缺数数据进行处理 existing_trading_dates = self.get_existing_trading_dates(db_session, instrument) # 计算出缺失数据 missing_trading_dates = self.get_missing_trading_dates(instrument, unchecked_trading_dates, existing_trading_dates) if not dry_run: self.upsert_missing_trading_dates(db_session, instrument, missing_trading_dates) # TODO 需要对多余的数据进行处理 # 计算出多余数据 extra_trading_dates = [] logging.info("Recon找到多余数据:%s -> %d 个Break" % (instrument.instrumentId, len(extra_trading_dates))) # 如果没有差值,则比较开始日期和Milestone,更新Milestone has_missing_dates = len(missing_trading_dates) != 0 has_extra_dates = len(extra_trading_dates) != 0 if (not has_missing_dates) and (not has_extra_dates) and (not dry_run): if start_date <= end_date: self.upsert_milestone(db_session, instrument, start_date, end_date, milestone) else: logging.info("标的物开始日期大于结束日期,不更新Milestone:%s [%s %s]" % ( instrument.instrumentId, start_date.strftime('%Y%m%d'), end_date.strftime('%Y%m%d'))) return instrument, (not has_missing_dates) and (not has_extra_dates) except Exception as e: logging.error("Recon失败,标的物:%s, 异常:%s" % (instrument.instrumentId, e)) return instrument, False finally: db_session.close()
def backfill_one_instrument(self, params): instrument, start_date, end_date, holidays, force_update = params logging.info("开始Backfill标的物: %s, startDate: %s, endDate: %s" % (instrument.instrumentId, start_date.strftime('%Y%m%d'), end_date.strftime('%Y%m%d'))) db_session = std.create_db_session() try: quote_close_breaks = self.get_recon_breaks( db_session, start_date, end_date, instrument, ReconFromType.QUOTE_CLOSE_RECON) ret = self.backfill_multiple_quote_close(db_session, instrument, quote_close_breaks, force_update) return instrument, ret except Exception as e: return instrument, False finally: db_session.close()
def backfill_one_instrument(self, params): instrument, start_date, end_date, holidays, force_update = params logging.info("开始Backfill标的物: %s, startDate: %s, endDate: %s" % (instrument.instrumentId, start_date.strftime('%Y%m%d'), end_date.strftime('%Y%m%d'))) db_session = std.create_db_session() try: special_dates = TradingCalendarRepo.load_special_dates( db_session, instrument) tick_snapshot_breaks = self.get_recon_breaks( db_session, start_date, end_date, instrument, ReconFromType.TICK_SNAPSHOT_RECON) ret = self.backfill_multiple_tick_snapshot(db_session, instrument, tick_snapshot_breaks, holidays, special_dates) return instrument, ret except Exception as e: return instrument, False finally: db_session.close()