예제 #1
0
def build_trading_timeline(start, end):
    """ Build the daily-based index we will trade on """
    EMPTY_DATES = pd.date_range("2000/01/01", periods=0, tz=pytz.utc)
    now = dt.datetime.now(tz=pytz.utc)

    if not start:
        if not end:
            # Live trading until the end of the day
            bt_dates = EMPTY_DATES
            live_dates = pd.date_range(start=now, end=normalize_date_format("23h59"))
        else:
            end = normalize_date_format(end)
            if end < now:
                # Backtesting since a year before end
                bt_dates = pd.date_range(start=end - 360 * pd.datetools.day, end=end)
                live_dates = EMPTY_DATES
            elif end > now:
                # Live trading from now to end
                bt_dates = EMPTY_DATES
                live_dates = pd.date_range(start=now, end=end)
    else:
        start = normalize_date_format(start)
        if start < now:
            if not end:
                # Backtest for a year or until now
                end = start + 360 * pd.datetools.day
                if end > now:
                    end = now - pd.datetools.day
                live_dates = EMPTY_DATES
                bt_dates = pd.date_range(start=start, end=end)
            else:
                end = normalize_date_format(end)
                if end < now:
                    # Nothing to do, backtest from start to end
                    live_dates = EMPTY_DATES
                    bt_dates = pd.date_range(start=start, end=end)
                elif end > now:
                    # Hybrid timeline, backtest from start to end, live
                    # trade from now to end
                    bt_dates = pd.date_range(start=start, end=now - pd.datetools.day)

                    live_dates = pd.date_range(start=now, end=end)
        elif start > now:
            if not end:
                # Live trading from start to the end of the day
                bt_dates = EMPTY_DATES
                live_dates = pd.date_range(start=start, end=normalize_date_format("23h59"))
            else:
                # Live trading from start to end
                end = normalize_date_format(end)
                bt_dates = EMPTY_DATES
                live_dates = pd.date_range(start=start, end=end)

    return bt_dates + live_dates
예제 #2
0
 def test_normalize_human_date(self):
     human_date = '23h16'
     norm_date = time_utils.normalize_date_format(human_date)
     self.assertIsInstance(norm_date, dt.datetime)
     self.assertEqual(norm_date.minute, 16)
     self.assertEquals(norm_date.tzinfo, pytz.utc)
예제 #3
0
 def test_normalize_naive_date(self):
     naive_date = dt.datetime.now()
     norm_date = time_utils.normalize_date_format(naive_date)
     self.assertIsInstance(norm_date, dt.datetime)
     self.assertEquals(norm_date.tzinfo, pytz.utc)
예제 #4
0
 def test_normalize_epoch_date(self):
     epoch_date = 15334321432
     norm_date = time_utils.normalize_date_format(epoch_date)
     self.assertIsInstance(norm_date, dt.datetime)
     self.assertEquals(norm_date.tzinfo, pytz.utc)
예제 #5
0
def build_trading_timeline(start, end):
    ''' Build the daily-based index we will trade on '''
    EMPTY_DATES = pd.date_range('2000/01/01', periods=0, tz=pytz.utc)
    now = dt.datetime.now(tz=pytz.utc)

    if not start:
        if not end:
            # Live trading until the end of the day
            bt_dates = EMPTY_DATES
            live_dates = pd.date_range(
                start=now,
                end=normalize_date_format('23h59'))
        else:
            end = normalize_date_format(end)
            if end < now:
                # Backtesting since a year before end
                bt_dates = pd.date_range(
                    start=end - 360 * pd.datetools.day,
                    end=end)
                live_dates = EMPTY_DATES
            elif end > now:
                # Live trading from now to end
                bt_dates = EMPTY_DATES
                live_dates = pd.date_range(start=now, end=end)
    else:
        start = normalize_date_format(start)
        if start < now:
            if not end:
                # Backtest for a year or until now
                end = start + 360 * pd.datetools.day
                if end > now:
                    end = now - pd.datetools.day
                live_dates = EMPTY_DATES
                bt_dates = pd.date_range(
                    start=start, end=end)
            else:
                end = normalize_date_format(end)
                if end < now:
                    # Nothing to do, backtest from start to end
                    live_dates = EMPTY_DATES
                    bt_dates = pd.date_range(start=start, end=end)
                elif end > now:
                    # Hybrid timeline, backtest from start to end, live
                    # trade from now to end
                    bt_dates = pd.date_range(
                        start=start, end=now - pd.datetools.day)

                    live_dates = pd.date_range(start=now, end=end)
        elif start > now:
            if not end:
                # Live trading from start to the end of the day
                bt_dates = EMPTY_DATES
                live_dates = pd.date_range(
                    start=start,
                    end=normalize_date_format('23h59'))
            else:
                # Live trading from start to end
                end = normalize_date_format(end)
                bt_dates = EMPTY_DATES
                live_dates = pd.date_range(start=start, end=end)

    return bt_dates + live_dates
예제 #6
0
 def test_normalize_naive_date(self):
     naive_date = dt.datetime.now()
     norm_date = time_utils.normalize_date_format(naive_date)
     self.assertIsInstance(norm_date, dt.datetime)
     self.assertEquals(norm_date.tzinfo, pytz.utc)
예제 #7
0
 def test_normalize_human_date(self):
     human_date = '23h16'
     norm_date = time_utils.normalize_date_format(human_date)
     self.assertIsInstance(norm_date, dt.datetime)
     self.assertEqual(norm_date.minute, 16)
     self.assertEquals(norm_date.tzinfo, pytz.utc)
예제 #8
0
 def test_normalize_epoch_date(self):
     epoch_date = 15334321432
     norm_date = time_utils.normalize_date_format(epoch_date)
     self.assertIsInstance(norm_date, dt.datetime)
     self.assertEquals(norm_date.tzinfo, pytz.utc)