def gen_suspended_days(d): from rqdatac.client import get_client stocks = rqdatac.all_instruments('CS').order_book_id.tolist() suspended_days = get_client().execute( 'get_suspended_days', stocks, START_DATE, convert_date_to_date_int(datetime.date.today())) with h5py.File(os.path.join(d, 'suspended_days.h5'), 'w') as h5: for order_book_id, days in suspended_days.items(): h5[order_book_id] = days
def gen_yield_curve(d): yield_curve = rqdatac.get_yield_curve(start_date=START_DATE, end_date=datetime.date.today()) yield_curve.index = [ convert_date_to_date_int(d) for d in yield_curve.index ] yield_curve.index.name = 'date' with h5py.File(os.path.join(d, 'yield_curve.h5'), 'w') as f: f.create_dataset('data', data=yield_curve.to_records())
def raw_history_bars(self, instrument, frequency, start_dt=None, end_dt=None, length=None): env = Environment.get_instance() now = env.calendar_dt today = now.date() today_int = convert_date_to_date_int(today) yesterday = datetime.combine(env.data_proxy.get_previous_trading_date(today), time=time(hour=23, minute=59, second=59)) history_bars = EMPTY_BARS today_bars = EMPTY_BARS if end_dt and start_dt: end_dt = min(now, end_dt) if start_dt > end_dt: return EMPTY_BARS if end_dt.date == today: start_time = convert_dt_to_int(start_dt) % 1000000 if start_dt.date() == today else None end_time = convert_dt_to_int(end_dt) % 1000000 today_bars = self._inday_bars.bars(instrument, frequency, today_int, start_time, end_time) if start_dt.date() < today: history_bars = self._hist_source.raw_history_bars( instrument, frequency, start_dt=start_dt, end_dt=min(end_dt, yesterday) ) elif start_dt and length: if start_dt.date() > today: return EMPTY_BARS if start_dt.date() < today: history_bars = self._hist_source.raw_history_bars( instrument, frequency, start_dt=start_dt, length=length) left = length - len(history_bars) if history_bars is not None else length start_time = convert_dt_to_int(start_dt) % 1000000 if start_dt.date() == today else None today_bars = self._inday_bars.get_bars(instrument, frequency, today_int, start_time)[:left] elif end_dt and length: end_dt = min(now, end_dt) if end_dt.date() == today: end_time = convert_dt_to_int(end_dt) % 1000000 today_bars = self._inday_bars.get_bars(instrument, frequency, today_int, end_time=end_time)[-length:] left = length - len(today_bars) if today_bars is not None else length if left > 0: history_bars = self._hist_source.raw_history_bars( instrument, frequency, end_dt=min(end_dt, yesterday), length=left) else: raise RuntimeError if history_bars is not None and today_bars is not None: return np.concatenate([history_bars, today_bars]) elif history_bars is not None: return history_bars else: return today_bars
def get_yield_curve(self, start_date, end_date, tenor): d1 = convert_date_to_date_int(start_date) d2 = convert_date_to_date_int(end_date) s = self._data['date'].searchsorted(d1) e = self._data['date'].searchsorted(d2, side='right') if e == len(self._data): e -= 1 if self._data[e]['date'] == d2: e += 1 if e < s: return None df = pandas.DataFrame(self._data[s:e]) df.index = pandas.to_datetime([str(d) for d in df['date']]) del df['date'] if tenor is not None: return df[tenor] return df
def gen_dividends(d): stocks = rqdatac.all_instruments().order_book_id.tolist() dividend = rqdatac.get_dividend(stocks) dividend.reset_index(inplace=True) dividend.rename( columns={'declaration_announcement_date': 'announcement_date'}, inplace=True) for f in ('book_closure_date', 'ex_dividend_date', 'payable_date', 'announcement_date'): dividend[f] = [convert_date_to_date_int(d) for d in dividend[f]] dividend.set_index(['order_book_id', 'book_closure_date'], inplace=True) with h5py.File(os.path.join(d, 'dividends.h5'), 'w') as h5: for order_book_id in dividend.index.levels[0]: h5[order_book_id] = dividend.loc[order_book_id].to_records()
def gen_trading_dates(d): dates = rqdatac.get_trading_dates(start_date=START_DATE, end_date='2999-01-01') dates = np.array([convert_date_to_date_int(d) for d in dates]) np.save(os.path.join(d, 'trading_dates.npy'), dates, allow_pickle=False)