Exemple #1
0
    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__))
Exemple #2
0
    def _create_return_df(self, target_day=None):
        if self._is_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)
        
        weekly_price = self._price_df.loc[date_list]
        if target_day is None:
            return  pd.DataFrame(np.log(np.array(weekly_price.iloc[1:]) \
                                      / np.array(weekly_price.iloc[:-1])),
                                 index = weekly_price.index[:-1],
                                 columns = ['Return'])
        else:
            target_date_list = cf.create_weekly_datelist(self._start_date, 
                                                         self._end_date, 
                                                         weeknum=target_day)
            target_price = self._price_df.loc[target_date_list]
            if target_date_list[0] > date_list[0]:
                if target_price.shape[0] < weekly_price.shape[0]:
                    weekly_price = weekly_price.iloc[:-1]
                elif target_price.shape[0] == weekly_price.shape[0]:
                    target_price = target_price.iloc[1:]
                    weekly_price = weekly_price.iloc[:-1]
            elif target_date_list[0] < date_list[0]:
                if target_price.shape[0] > weekly_price.shape[0]:
                    target_price = target_price.iloc[1:]
                elif target_price.shape[0] == weekly_price.shape[0]:
                    target_price = target_price.iloc[1:]
                    weekly_price = weekly_price.iloc[:-1]

            return  pd.DataFrame(np.log(np.array(target_price) \
                                      / np.array(weekly_price)),
                                 index = weekly_price.index,
                                 columns = ['Return'])
Exemple #3
0
    def __init__(self, *args, **kwargs):
        self._start_date = kwargs.get('StartDate', date(2000,1,1))
        self._end_date = kwargs.get('EndDate', date(2020, 4 , 17))
        self._target_ccy = kwargs.get('TargetCcy', ['ZAR', 'USD'])
        self._is_weekly = kwargs.get('is_weekly', True)
        self._label_tickers = kwargs.get('label_tickers', ['NFCIINDX Index', 'GSUSFCI Index'])
        threshold_list = kwargs.get('threshold', [5, 0.6])
        assert len(threshold_list) == len(self._label_tickers)
        self._threshold_dic = {}
        for i in range(len(self._label_tickers)):
            self._threshold_dic[self._label_tickers[i]] = threshold_list[i]

        self._price_ticker = [self._target_ccy[1] + self._target_ccy[0] + ' Index']
        self._price_df = self.create_factor(self._price_ticker)
        
        if self._is_weekly:
            self._date_list = cf.create_weekly_datelist(self._start_date, self._end_date)
        else:
            self._date_list = cf.create_monthly_datelist(self._start_date, self._end_date)

        self._surprise_ticker = 'CESI' + self._target_ccy[0] + ' Index'
        self._datachange_ticker = 'CECIC' + self._target_ccy[0] + ' Index'
        self._ctot_ticker = 'CTOT' + self._target_ccy[0] + ' Index'
        self._value_ticker = ['BISB' + ccy[:2] + 'N Index' for ccy in self._target_ccy]
        #self._value_ticker = 'CTTWBR' + self._target_ccy[0][:2] + 'N Index'
        self._carry_ticker_dic = {'USD':'USGG2YR Index',
                                  'ZAR':'GSAB2YR Index',
                                  'MXN':'GMXN02YR Index',
                                  'TRY':'GTRU2YR Index'}
 def _create_carry_df(self, col_name='Carry'):
     if self._is_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)
     carry_df = self.create_factor(self._carry_ticker)
     carry_df[col_name] = carry_df[self._carry_ticker[0]]\
                         -carry_df[self._carry_ticker[1]]
     return carry_df[[col_name]].loc[date_list]
    def _create_iv_df(self, vi_type='R', delta=25, vi_term='1W'):
        if self._is_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)

        target_ticker = self._target_ccy[1] + self._target_ccy[0] \
                      + str(delta) + vi_type + vi_term + ' BGN Curncy'
        return self.create_factor([target_ticker]).loc[date_list]
 def _create_rsi(self, rsi_param=7, col_name='RSI'):
     if self._is_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)
     return pd.DataFrame([ta.RSI(np.array(self._price_df.iloc[:, i]), rsi_param) 
                          for i in range(self._price_df.shape[1])],
                         index=[col_name],
                         columns=self._price_df.index).T.loc[date_list]
 def _create_ctot_df(self, col_name='CTOT'):
     if self._is_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)
     ctot_df = self.create_factor(self._ctot_ticker)
     ctot_df[col_name] = ctot_df['CTOT'+self._target_ccy[1]+' Index']\
                        -ctot_df['CTOT'+self._target_ccy[0]+' Index']
     return ctot_df[[col_name]].loc[date_list]
    def _create_value_df(self, col_name='Value'):
        if self._is_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)

        value_df = self.create_factor(self._value_ticker)
        value_df[col_name] = value_df['BISB'+self._target_ccy[1][:2]+'N Index']\
                            -value_df['BISB'+self._target_ccy[0][:2]+'N Index']
        return value_df[[col_name]].loc[date_list]
    def _create_surprise_df(self, col_name='Surprise'):
        if self._is_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)
        surprise_df = self.create_factor(self._surprise_ticker)
        surprise_df[col_name] = surprise_df['CESI'+self._target_ccy[1]+' Index'] \
                              - surprise_df['CESI'+self._target_ccy[0]+' Index']

        return surprise_df[[col_name]].loc[date_list]
Exemple #10
0
    def _create_datachange_df(self, col_name='DataChange'):
        if self._is_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)

        datachange_df = self.create_factor(self._datachange_ticker)
        if 'JPY' in self._price_ticker[0]:
            datachange_df[col_name] = datachange_df['CECIC'+self._target_ccy[1]+' Index']
        else:
            datachange_df[col_name] = datachange_df['CECIC'+self._target_ccy[1]+' Index']\
                                     -datachange_df['CECIC'+self._target_ccy[0]+' Index']

        return datachange_df[[col_name]].loc[date_list]
Exemple #11
0
    def _calc_aggregated_hitratio(self, return_df, is_monthly=True):
        if is_monthly:
            date_list = cf.create_monthly_datelist(
                start_date=return_df.index[0], end_date=return_df.index[-1])
        else:
            date_list = cf.create_weekly_datelist(
                start_date=return_df.index[0], end_date=return_df.index[-1])
        date_list = np.array(date_list)
        aggr_return = copy.deepcopy(return_df)
        aggr_return['AggregateDate'] = [date_list[date_list>=return_df.index[i]][0]
                                      if np.any(date_list>=return_df.index[i]) \
                                      else return_df.index[-1]\
                                      for i in range(return_df.shape[0])]

        grouped_df = aggr_return.groupby(['AggregateDate']).sum()
        return pd.DataFrame(
            grouped_df.where(grouped_df > 0).count() / grouped_df.shape[0]).T
Exemple #12
0
    def _create_label_df(self):
        if self._is_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)
        
        label_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(self._label_tickers))

        with util.DBConnector(DBName='marketdb') as db_conn:
            label_src_df = db_conn.get_data(label_query).pivot(index='ValueDate', 
                                                               columns='Ticker', 
                                                               values='Last').dropna(axis=0)
            
            label_src_df['NFCIINDX Index'] = [np.nan] + label_src_df['NFCIINDX Index'].iloc[:-1].tolist()

        label_src_df.dropna(axis=0, inplace=True)
        #import pdb;pdb.set_trace()
        for ticker in self._label_tickers:
            label_src_df[ticker] = self._normalize_data(label_src_df[[ticker]], term=104)
            #import pdb;pdb.set_trace()
            #label_src_df[ticker] = label_src_df[ticker].apply(lambda x: x if abs(x) < self._threshold_dic[ticker] else 0)
            
        import pdb;pdb.set_trace()
        label_src_df.to_csv('fc_label_src.csv')
        label_list = []
        for i in range(label_src_df.shape[0]):
            if label_src_df.iloc[i].max() < 0:
                label = [0, 0, 1]
            elif label_src_df.iloc[i][self._label_tickers[0]] > label_src_df.iloc[i][self._label_tickers[1]]:
                label = [1, 0, 0]
            else:
                label = [0, 1, 0]
            label_list.append(label)

        return pd.DataFrame(label_list, 
                            index=label_src_df.index, 
                            columns=self._label_tickers + ['N/A']).reindex(date_list)
Exemple #13
0
    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__))