def __init__(self, *args, **kwargs): self._logger = logging.getLogger("jpbank.quants") self._logger.info("{0} initializing...".format(self.__class__.__name__)) self._start_date = kwargs.get('start_date', date(2001, 1, 1)) self._end_date = kwargs.get('end_date', date.today()) self._ar_diff = kwargs.get('ar_diff', 3) self._frequency = kwargs.get('frequency', 'weekly') if self._frequency == 'daily': self._date_list = cf.create_daily_datelist(self._start_date, self._end_date) self._term = int(kwargs.get('term_year', 3) * 261) elif self._frequency == 'weekly': self._date_list = cf.create_weekly_datelist(self._start_date, self._end_date) self._term = int(kwargs.get('term_year', 3) * 52) else: self._date_list = cf.create_monthly_datelist(self._start_date, self._end_date) self._term = int(kwargs.get('term_year', 3) * 12) self._ccy_list = kwargs.get('ccy_list', np.sort(['USDJPY Index', 'EURJPY Index', 'AUDJPY Index', 'GBPJPY Index', 'CADJPY Index', 'CHFJPY Index', 'NZDJPY Index']).tolist()) self._order = kwargs.get('order', 0) if self._order == -1: self._reg = 'nc' elif self._order == 0: self._reg = 'c' elif self._oder == 1: self._reg = 'ct' self._fx_rate_df = np.log(self._get_fx_rate()) self._logger.info("{0} initialized.".format(self.__class__.__name__))
def create_macro_index(self, ticker, shifts=False): macro_query = "SELECT ValueDate, Ticker, Last \ FROM bbg_marketprice \ WHERE ValueDate BETWEEN '{0}' AND '{1}' \ AND Ticker = '{2}'"\ .format(self._start_date, self._end_date, ticker) with util.DBConnector(DBName='marketdb') as db_conn: macro_df = db_conn.get_data(macro_query).pivot(index='ValueDate', columns='Ticker', values='Last')\ .fillna(method='ffill') #import pdb;pdb.set_trace() if shifts: macro_df = macro_df.shift(1).dropna(axis=0) date_df = pd.DataFrame(cf.create_daily_datelist(start_date=self._start_date, end_date=self._end_date), columns=['ValueDate']) macro_df = pd.merge(date_df, macro_df.reset_index('ValueDate'), on='ValueDate', how='left')\ .set_index('ValueDate')\ .fillna(method='ffill')\ .fillna(method='bfill') return macro_df
def create_factor(self, ticker_list): target_df = self._input_data.query("@self._start_date <= ValueDate <= @self._end_date & Ticker in @ticker_list")\ .pivot(index='ValueDate', columns='Ticker', values='Last')\ .fillna(method='ffill') date_df = pd.DataFrame(cf.create_daily_datelist( start_date=self._start_date, end_date=self._end_date), columns=['ValueDate']) return pd.merge(date_df, target_df.reset_index('ValueDate'), on='ValueDate', how='left')\ .set_index('ValueDate')\ .fillna(method='ffill')\ .fillna(method='bfill')
def create_macro_index(self, ticker, shifts=False): macro_df = self._input_data.query("'{0}' <= ValueDate <= '{1}'Ticker '{2}'" .format(self._start_date, self._end_date, ticker)).pivot(index='ValueDate', columns='Ticker', values='Last')\ .fillna(method='ffill') if shifts: macro_df = macro_df.shift(1).dropna(axis=0) date_df = pd.DataFrame(cf.create_daily_datelist( start_date=self._start_date, end_date=self._end_date), columns=['ValueDate']) macro_df = pd.merge(date_df, macro_df.reset_index('ValueDate'), on='ValueDate', how='left')\ .set_index('ValueDate')\ .fillna(method='ffill')\ .fillna(method='bfill') return macro_df
def create_factor(self, ticker_list): factor_query = "SELECT ValueDate, Ticker, Last \ FROM bbg_marketprice \ WHERE ValueDate BETWEEN '{0}' AND '{1}' \ AND Ticker IN ('{2}')"\ .format(self._start_date, self._end_date, "','".join(ticker_list)) with util.DBConnector(DBName='marketdb') as db_conn: factor_df = db_conn.get_data(factor_query).pivot(index='ValueDate', columns='Ticker', values='Last')\ .fillna(method='ffill') date_df = pd.DataFrame(cf.create_daily_datelist(start_date=self._start_date, end_date=self._end_date), columns=['ValueDate']) factor_df = pd.merge(date_df, factor_df.reset_index('ValueDate'), on='ValueDate', how='left')\ .set_index('ValueDate')\ .fillna(method='ffill')\ .fillna(method='bfill') return factor_df
def __init__(self, **kwargs): super().__init__(**kwargs) self._logger.info("{0} initializing...".format( self.__class__.__name__)) self._start_date = kwargs.get('start_date', None) self._end_date = kwargs.get('end_date', None) interval = kwargs.get('interval', 1) frequency = kwargs.get('frequency', 'weekly') if frequency == 'daily': date_list = cf.create_daily_datelist(self._start_date, self._end_date) elif frequency == 'weekly': date_list = cf.create_weekly_datelist(self._start_date, self._end_date) else: date_list = cf.create_monthly_datelist(self._start_date, self._end_date) coint_vec_file = kwargs.get('coint_Vec_file', './input/coint_vec.csv') self._weight_df = cf.convert_date_format(pd.read_csv(coint_vec_file))\ .set_index('ValueDate').loc[date_list] self._fx_rate_df = np.log( cf.get_fx_rate(self._start_date, self._end_date, self._weight_df.columns.tolist())).loc[date_list] #import pdb;pdb.set_trace() self._coint_index_df = pd.DataFrame( (self._fx_rate_df * self._weight_df[self._fx_rate_df.columns]).sum(axis=1), columns=['Price']).loc[date_list] #self._index_return_df = pd.DataFrame(self._coint_index_df[interval:] - self._coint_index_df[:-interval], # index = self._fx_rate_df.index[:-interval], # columns = ['Return'])#.loc[date_list] self._logger.info("{0} initialized.".format(self.__class__.__name__))