Ejemplo n.º 1
0
def test_dtutil():
    import datetime
    date = 20170105
    dt = jutil.convert_int_to_datetime(date)

    assert jutil.shift(date, 2) == 20170119
    assert jutil.convert_datetime_to_int(jutil.shift(dt, 2)) == 20170119

    t = 145539
    dt = jutil.combine_date_time(date, t)
    assert jutil.split_date_time(dt) == (date, t)

    assert jutil.get_next_period_day(date, 'day', 1, 1) == 20170109
    assert jutil.get_next_period_day(date, 'week', 2, 0) == 20170116
    assert jutil.get_next_period_day(date, 'month', 1, 0) == 20170201

    date = 20170808
    assert jutil.get_next_period_day(20170831, 'day',
                                     extra_offset=1) == 20170904
    assert jutil.get_next_period_day(20170831, 'day', n=2,
                                     extra_offset=0) == 20170904
    assert jutil.get_next_period_day(20170831, 'day', n=7,
                                     extra_offset=0) == 20170911
    assert jutil.get_next_period_day(20170831, 'week',
                                     extra_offset=1) == 20170905
    assert jutil.get_next_period_day(20170831, 'month',
                                     extra_offset=0) == 20170901

    monthly = 20170101
    while monthly < 20180301:
        monthly = jutil.get_next_period_day(monthly, 'month', extra_offset=0)
        assert datetime.datetime.strptime(str(monthly), "%Y%m%d").weekday() < 5
Ejemplo n.º 2
0
 def go_next_rebalance_day(self):
     """update self.ctx.trade_date and last_date."""
     current_date = self.ctx.trade_date
     if self.ctx.trade_api.match_finished:
         next_period_day = jutil.get_next_period_day(current_date, self.ctx.strategy.period,
                                                      n=self.ctx.strategy.n_periods,
                                                      extra_offset=self.ctx.strategy.days_delay)
         # update current_date: next_period_day is a workday, but not necessarily a trade date
         if self.ctx.calendar.is_trade_date(next_period_day):
             current_date = next_period_day
         else:
             current_date = self.ctx.calendar.get_next_trade_date(next_period_day)
     
         # update re-balance date
         if self.current_rebalance_date > 0:
             self.last_rebalance_date = self.current_rebalance_date
         else:
             self.last_rebalance_date = current_date
         self.current_rebalance_date = current_date
     else:
         # TODO here we must make sure the matching will not last to next period
         current_date = self.ctx.calendar.get_next_trade_date(current_date)
 
     self.ctx.trade_date = current_date
     self.last_date = self.ctx.calendar.get_last_trade_date(current_date)
Ejemplo n.º 3
0
    def go_next_rebalance_day(self):
        """
        update self.ctx.trade_date and last_date.
        
        Returns
        -------
        bool
            Whether the backtest is finished.

        """
        current_date = self.ctx.trade_date
        if self.ctx.trade_api.match_finished:
            if self.ctx.strategy.period == 'day':
                # use trade dates array
                try:
                    current_date = self._get_next_trade_date(
                        current_date, self.ctx.strategy.n_periods)
                except IndexError:
                    return True
            else:
                # use natural week/month
                next_period_day = jutil.get_next_period_day(
                    current_date,
                    self.ctx.strategy.period,
                    n=self.ctx.strategy.n_periods,
                    extra_offset=self.ctx.strategy.days_delay)
                # update current_date: next_period_day is a workday, but not necessarily a trade date
                if self._is_trade_date(next_period_day):
                    current_date = next_period_day
                else:
                    try:
                        current_date = self._get_next_trade_date(
                            next_period_day)
                    except IndexError:
                        return True

            if current_date > self.end_date:
                return True

            # update re-balance date
            if self.current_rebalance_date > 0:
                self.last_rebalance_date = self.current_rebalance_date
            else:
                self.last_rebalance_date = current_date
            self.current_rebalance_date = current_date
        else:
            # TODO here we must make sure the matching will not last to next period
            try:
                current_date = self._get_next_trade_date(current_date)
            except IndexError:
                return True

        self.ctx.trade_date = current_date
        self.last_date = self._get_last_trade_date(current_date)
        return False
Ejemplo n.º 4
0
    def go_next_rebalance_day(self):
        """
        update self.ctx.trade_date and last_date.
        
        Returns
        -------
        bool
            Whether the backtest is finished.

        """
        current_date = self.ctx.trade_date
        if self.ctx.trade_api.match_finished:
            if self.ctx.strategy.period == 'day':
                # use trade dates array
                try:
                    current_date = self._get_next_trade_date(current_date, self.ctx.strategy.n_periods)
                except IndexError:
                    return True
            else:
                # use natural week/month
                next_period_day = jutil.get_next_period_day(current_date, self.ctx.strategy.period,
                                                            n=self.ctx.strategy.n_periods,
                                                            extra_offset=self.ctx.strategy.days_delay)
                # update current_date: next_period_day is a workday, but not necessarily a trade date
                if self._is_trade_date(next_period_day):
                    current_date = next_period_day
                else:
                    try:
                        current_date = self._get_next_trade_date(next_period_day)
                    except IndexError:
                        return True
                
            if current_date > self.end_date:
                return True

            # update re-balance date
            if self.current_rebalance_date > 0:
                self.last_rebalance_date = self.current_rebalance_date
            else:
                self.last_rebalance_date = current_date
            self.current_rebalance_date = current_date
        else:
            # TODO here we must make sure the matching will not last to next period
            try:
                current_date = self._get_next_trade_date(current_date)
            except IndexError:
                return True
    
        self.ctx.trade_date = current_date
        self.last_date = self._get_last_trade_date(current_date)
        return False
Ejemplo n.º 5
0
    def get_index_weights_daily_OLD(self, index, start_date, end_date):
        """
        Return all securities that have been in index during start_date and end_date.
        
        Parameters
        ----------
        index : str
        start_date : int
        end_date : int

        Returns
        -------
        res : pd.DataFrame
            Index is trade_date, columns are symbols.

        """
        # TODO: temparary api
        trade_dates = self.get_trade_date_range(start_date, end_date)
        start_date, end_date = trade_dates[0], trade_dates[-1]
        td = start_date

        dic = dict()
        symbols_set = set()
        while True:
            if td > end_date:
                break
            df = self.get_index_weights(index, td)
            # update_date = df['trade_date'].iat[0]
            # if update_date >= start_date and update_date <= end_date:
            symbols_set.update(set(df.index))
            dic[td] = df['weight']

            td = jutil.get_next_period_day(td, 'month', 1)
        merge = pd.concat(dic, axis=1).T
        merge = merge.fillna(0.0)  # for those which are not components
        res = pd.DataFrame(index=trade_dates,
                           columns=sorted(list(symbols_set)),
                           data=np.nan)
        res.update(merge)
        res = res.fillna(method='ffill')
        res = res.loc[start_date:end_date]
        return res
Ejemplo n.º 6
0
def test_dtutil():
    date = 20170808
    assert jutil.get_next_period_day(20170831, 'day',
                                     extra_offset=1) == 20170904
    assert jutil.get_next_period_day(20170831, 'day', n=2,
                                     extra_offset=0) == 20170904
    assert jutil.get_next_period_day(20170831, 'day', n=7,
                                     extra_offset=0) == 20170911
    assert jutil.get_next_period_day(20170831, 'week',
                                     extra_offset=1) == 20170905
    assert jutil.get_next_period_day(20170831, 'month',
                                     extra_offset=0) == 20170901

    monthly = 20170101
    while monthly < 20180301:
        monthly = jutil.get_next_period_day(monthly, 'month', extra_offset=0)
        assert datetime.datetime.strptime(str(monthly), "%Y%m%d").weekday() < 5