Ejemplo n.º 1
0
    def test_daily_schedule(self):
        # Jan 2 and Jan 3 are skipped as New Year holiday
        # Jan 7 is skipped as weekend
        # Jan 8 is adjusted to Jan 9 with following convention
        start_date = Date(2012, 1, 1)
        s = Schedule(start_date, start_date + 7,
                     Period(length=1, units=TimeUnits.Days),
                     Calendar("China.SSE"), BizDayConventions.Preceding)

        expected = [
            Date(2011, 12, 30),
            Date(2012, 1, 4),
            Date(2012, 1, 5),
            Date(2012, 1, 6),
            Date(2012, 1, 9)
        ]
        self.check_dates(s, expected)

        # The schedule should skip Saturday 21st and Sunday 22rd.
        # Previously, it would adjust them to Friday 20th, resulting
        # in three copies of the same date.
        start_date = Date(2012, 1, 17)
        s = Schedule(start_date, start_date + 7,
                     Period(length=1, units=TimeUnits.Days),
                     Calendar("Target"), BizDayConventions.Preceding)
        expected = [
            Date(2012, 1, 17),
            Date(2012, 1, 18),
            Date(2012, 1, 19),
            Date(2012, 1, 20),
            Date(2012, 1, 23),
            Date(2012, 1, 24)
        ]
        self.check_dates(s, expected)
Ejemplo n.º 2
0
    def test_schedule_deep_copy(self):
        start_date = Date(2013, 3, 31)
        end_date = Date(2013, 7, 1)
        tenor = Period('1m')
        cal = Calendar('NullCalendar')
        sch = Schedule(start_date, end_date, tenor, cal)
        copied_sch = copy.deepcopy(sch)

        self.assertEqual(sch, copied_sch)
Ejemplo n.º 3
0
 def test_schedule_initialize_yearly(self):
     start_date = Date(2012, 2, 29)
     end_date = Date(2013, 3, 1)
     tenor = Period('1y')
     cal = Calendar('NullCalendar')
     sch = Schedule(start_date, end_date, tenor, cal)
     expected = [Date(2012, 2, 29), Date(2013, 2, 28), Date(2013, 3, 1)]
     for i in range(sch.size()):
         self.assertEqual(expected[i], sch[i])
Ejemplo n.º 4
0
    def test_period_pickle(self):
        p1 = Period('36m')

        f = tempfile.NamedTemporaryFile('w+b', delete=False)
        pickle.dump(p1, f)
        f.close()

        with open(f.name, 'rb') as f2:
            pickled_period = pickle.load(f2)
            self.assertEqual(p1, pickled_period)

        os.unlink(f.name)
Ejemplo n.º 5
0
 def test_schedule_initialize(self):
     start_date = Date(2013, 3, 31)
     end_date = Date(2013, 7, 1)
     tenor = Period('1m')
     cal = Calendar('NullCalendar')
     sch = Schedule(start_date, end_date, tenor, cal)
     expected = [
         Date(2013, 4, 1),
         Date(2013, 4, 30),
         Date(2013, 5, 31),
         Date(2013, 7, 1)
     ]
     for i in range(sch.size()):
         self.assertEqual(expected[i], sch[i])
Ejemplo n.º 6
0
    def test_schedule_pickle(self):
        start_date = Date(2013, 3, 31)
        end_date = Date(2013, 7, 1)
        tenor = Period('1m')
        cal = Calendar('NullCalendar')
        sch = Schedule(start_date, end_date, tenor, cal)

        f = tempfile.NamedTemporaryFile('w+b', delete=False)
        pickle.dump(sch, f)
        f.close()

        with open(f.name, 'rb') as f2:
            pickled_sch = pickle.load(f2)
            self.assertEqual(sch, pickled_sch)

        os.unlink(f.name)
Ejemplo n.º 7
0
        def test_advance_date(self):
            reference_date = Date(2014, 1, 31)
            sse_cal = Calendar('China.SSE')
            ib_cal = Calendar('China.IB')

            biz_day_conv = BizDayConventions.Following

            # test null period
            self.assertEqual(
                sse_cal.advance_date(reference_date, Period('0b'),
                                     biz_day_conv), Date(2014, 2, 7))

            # test negative period
            self.assertEqual(
                sse_cal.advance_date(reference_date, Period('-5b'),
                                     biz_day_conv), Date(2014, 1, 24))

            # The difference is caused by Feb 8 is SSE holiday but a working day for IB market
            self.assertEqual(
                sse_cal.advance_date(reference_date, Period('2b'),
                                     biz_day_conv), Date(2014, 2, 10))
            self.assertEqual(
                sse_cal.advance_date(reference_date, Period('2d'),
                                     biz_day_conv), Date(2014, 2, 7))
            self.assertEqual(
                ib_cal.advance_date(reference_date, Period('2b'),
                                    biz_day_conv), Date(2014, 2, 8))
            self.assertEqual(
                ib_cal.advance_date(reference_date, Period('2d'),
                                    biz_day_conv), Date(2014, 2, 7))

            biz_day_conv = BizDayConventions.ModifiedFollowing
            # May 31, 2014 is a holiday
            self.assertEqual(
                sse_cal.advance_date(reference_date, Period('4m'),
                                     biz_day_conv, True), Date(2014, 5, 30))
Ejemplo n.º 8
0
    def test_basic_arithmic(self):
        # test bad normalize
        test_priod = Period(length=1, units=TimeUnits.Years)
        test_priod._units = 10
        with self.assertRaises(TypeError):
            test_priod.normalize()

        # test plus method
        p1 = Period(length=0, units=TimeUnits.Days)
        p2 = Period(length=10, units=TimeUnits.Months)
        calculated = p1 + p2
        self.assertEqual(
            p2, calculated,
            "added value {0} should be equal to {1}".format(calculated, p2))

        p1 = Period(length=2, units=TimeUnits.Years)
        p2 = Period(length=13, units=TimeUnits.Months)
        calculated = p1 + p2
        expected = Period(length=37, units=TimeUnits.Months)
        self.assertEqual(
            expected, calculated,
            "added value {0} should be equal to {1}".format(
                calculated, expected))

        p2 = Period(length=2, units=TimeUnits.Weeks)
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2 = Period(length=2, units=TimeUnits.BDays)
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2 = Period(length=2, units=TimeUnits.Days)
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2._units = 10
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p1 = Period(length=13, units=TimeUnits.Months)
        p2 = Period(length=2, units=TimeUnits.Years)
        calculated = p1 + p2
        expected = Period(length=37, units=TimeUnits.Months)
        self.assertEqual(
            expected, calculated,
            "added value {0} should be equal to {1}".format(
                calculated, expected))

        p2 = Period(length=2, units=TimeUnits.Weeks)
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2 = Period(length=2, units=TimeUnits.BDays)
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2 = Period(length=2, units=TimeUnits.Days)
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2._units = 10
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p1 = Period(length=2, units=TimeUnits.Weeks)
        p2 = Period(length=7, units=TimeUnits.Days)
        calculated = p1 + p2
        expected = Period(length=21, units=TimeUnits.Days)
        self.assertEqual(
            expected, calculated,
            "added value {0} should be equal to {1}".format(
                calculated, expected))

        p2 = Period(length=2, units=TimeUnits.Months)
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2 = Period(length=2, units=TimeUnits.BDays)
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2 = Period(length=2, units=TimeUnits.Years)
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2._units = 10
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p1 = Period(length=7, units=TimeUnits.Days)
        p2 = Period(length=2, units=TimeUnits.Weeks)
        calculated = p1 + p2
        expected = Period(length=21, units=TimeUnits.Days)
        self.assertEqual(
            expected, calculated,
            "added value {0} should be equal to {1}".format(
                calculated, expected))

        p2 = Period(length=2, units=TimeUnits.Months)
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2 = Period(length=2, units=TimeUnits.BDays)
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2 = Period(length=2, units=TimeUnits.Years)
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2._units = 10
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p1 = Period(length=7, units=TimeUnits.BDays)

        p2 = Period(length=2, units=TimeUnits.Months)
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2 = Period(length=2, units=TimeUnits.Days)
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2 = Period(length=2, units=TimeUnits.Weeks)
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2 = Period(length=2, units=TimeUnits.Years)
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2._units = 10
        with self.assertRaises(ValueError):
            _ = p1 + p2

        p2 = Period(length=2, units=TimeUnits.BDays)
        self.assertEqual(p1 + p2, Period('9B'))

        # test negative operator
        p1 = Period(length=-13, units=TimeUnits.Weeks)
        p2 = -p1
        self.assertEqual(p2, Period(length=13, units=TimeUnits.Weeks))

        # test less operator
        p1 = Period(length=0, units=TimeUnits.Days)
        p2 = Period(length=-3, units=TimeUnits.BDays)
        self.assertTrue(p2 < p1)

        # test sub operator
        p1 = Period(length=0, units=TimeUnits.Days)
        p2 = Period(length=-3, units=TimeUnits.BDays)
        self.assertEqual(p1 - p2, Period('3b'))

        # test string representation
        p1 = Period(length=12, units=TimeUnits.Months)
        self.assertEqual("1Y", p1.__str__())
Ejemplo n.º 9
0
    def test_period_deep_copy(self):
        p1 = Period('36m')
        p2 = copy.deepcopy(p1)

        self.assertEqual(p1, p2)
Ejemplo n.º 10
0
    def test_weeksdays_algebra(self):
        two_weeks = Period(length=2, units=TimeUnits.Weeks)
        one_week = Period(length=1, units=TimeUnits.Weeks)
        three_days = Period(length=3, units=TimeUnits.Days)
        one_day = Period(length=1, units=TimeUnits.Days)

        n = 2
        flag = two_weeks / n == one_week
        self.assertTrue(
            flag, "division error: {0} / {1:d}"
            " not equal to {2}".format(two_weeks, n, one_week))

        n = 7
        flag = one_week / 7 == one_day
        self.assertTrue(
            flag, "division error: {0} / {1:d}"
            " not equal to {2}".format(one_week, n, one_day))

        sum_ = three_days
        sum_ += one_day
        flag = sum_ == Period(length=4, units=TimeUnits.Days)
        self.assertTrue(
            flag, "sum_ error: {0}"
            " + {1}"
            " != {2}".format(three_days, one_day,
                             Period(length=4, units=TimeUnits.Days)))

        sum_ += one_week
        flag = sum_ == Period(length=11, units=TimeUnits.Days)
        self.assertTrue(
            flag, "sum_ error: {0}"
            " + {1}"
            " + {2}"
            " != {3}".format(three_days, one_day, one_week,
                             Period(length=11, units=TimeUnits.Days)))

        seven_days = Period(length=7, units=TimeUnits.Days)
        flag = seven_days.length() == 7
        self.assertTrue(
            flag, "normalization error: seven_days.length"
            " is {0:d}"
            " instead of 7".format(seven_days.length()))
        flag = seven_days.units() == TimeUnits.Days
        self.assertTrue(
            flag, "normalization error: seven_days.units"
            " is {0:d}"
            " instead of {1:d}".format(seven_days.units(), TimeUnits.Days))

        normalized_seven_days = seven_days.normalize()
        flag = normalized_seven_days.length() == 1
        self.assertTrue(
            flag, "normalization error: normalized_seven_days.length"
            " is {0:d}"
            " instead of 1".format(normalized_seven_days.length()))
        flag = normalized_seven_days.units() == TimeUnits.Weeks
        self.assertTrue(
            flag, "normalization error: TwelveMonths.units"
            " is {0:d}"
            " instead of {1:d}".format(normalized_seven_days.units(),
                                       TimeUnits.Weeks))
Ejemplo n.º 11
0
    def test_years_months_algebra(self):
        one_year = Period(length=1, units=TimeUnits.Years)
        six_months = Period(length=6, units=TimeUnits.Months)
        three_months = Period(length=3, units=TimeUnits.Months)

        n = 4
        flag = one_year / n == three_months
        self.assertTrue(
            flag, "division error: {0} / {1:d}"
            " not equal to {2}".format(one_year, n, three_months))

        n = 2
        flag = one_year / n == six_months
        self.assertTrue(
            flag, "division error: {0} / {1:d}"
            " not equal to {2}".format(one_year, n, six_months))

        sum_ = three_months
        sum_ += six_months
        flag = sum_ == Period(length=9, units=TimeUnits.Months)
        self.assertTrue(
            flag, "sum error: {0}"
            " + {1}"
            " != {2}".format(three_months, six_months,
                             Period(length=9, units=TimeUnits.Months)))

        sum_ += one_year
        flag = sum_ == Period(length=21, units=TimeUnits.Months)
        self.assertTrue(
            flag, "sum error: {0}"
            " + {1}"
            " + {2}"
            " != {3}".format(three_months, six_months, one_year,
                             Period(length=21, units=TimeUnits.Months)))

        twelve_months = Period(length=12, units=TimeUnits.Months)
        flag = twelve_months.length() == 12
        self.assertTrue(
            flag, "normalization error: TwelveMonths.length"
            " is {0:d}"
            " instead of 12".format(twelve_months.length()))
        flag = twelve_months.units() == TimeUnits.Months
        self.assertTrue(
            flag, "normalization error: TwelveMonths.units"
            " is {0:d}"
            " instead of {1:d}".format(twelve_months.units(),
                                       TimeUnits.Months))

        normalized_twelve_months = Period(length=12, units=TimeUnits.Months)
        normalized_twelve_months = normalized_twelve_months.normalize()
        flag = normalized_twelve_months.length() == 1
        self.assertTrue(
            flag, "normalization error: TwelveMonths.length"
            " is {0:d}"
            " instead of 1".format(twelve_months.length()))
        flag = normalized_twelve_months.units() == TimeUnits.Years
        self.assertTrue(
            flag, "normalization error: TwelveMonths.units"
            " is {0:d}"
            " instead of {1:d}".format(twelve_months.units(), TimeUnits.Years))

        thirty_days = Period(length=30, units=TimeUnits.Days)
        normalized_thirty_days = thirty_days.normalize()
        flag = normalized_thirty_days.units() == TimeUnits.Days
        self.assertTrue(
            flag, "normalization error: ThirtyDays.units"
            " is {0:d}"
            " instead of {1:d}".format(normalized_thirty_days.units(),
                                       TimeUnits.Days))

        thirty_bdays = Period(length=30, units=TimeUnits.BDays)
        normalized_thirty_b_days = thirty_bdays.normalize()
        flag = normalized_thirty_b_days.units() == TimeUnits.BDays
        self.assertTrue(
            flag, "normalization error: ThirtyBDays.units"
            " is {0:d}"
            " instead of {1:d}".format(normalized_thirty_b_days.units(),
                                       TimeUnits.BDays))
Ejemplo n.º 12
0
    def test_comparing_operators(self):
        p1 = Period(length=0, units=TimeUnits.Days)
        p2 = Period(length=1, units=TimeUnits.Days)
        self.assertTrue(p1 < p2)

        p1 = Period(length=13, units=TimeUnits.Months)
        p2 = Period(length=1, units=TimeUnits.Years)
        self.assertTrue(not p1 < p2)

        p1 = Period(length=1, units=TimeUnits.Years)
        p2 = Period(length=13, units=TimeUnits.Months)
        self.assertTrue(p1 < p2)

        p1 = Period(length=13, units=TimeUnits.Days)
        p2 = Period(length=2, units=TimeUnits.Weeks)
        self.assertTrue(p1 < p2)

        p1 = Period(length=2, units=TimeUnits.Weeks)
        p2 = Period(length=13, units=TimeUnits.Days)
        self.assertTrue(not p1 < p2)

        p1 = Period(length=1, units=TimeUnits.Years)
        p2 = Period(length=56, units=TimeUnits.Weeks)
        self.assertTrue(p1 < p2)

        p1 = Period(length=56, units=TimeUnits.Weeks)
        p2 = Period(length=1, units=TimeUnits.Years)
        self.assertTrue(not p1 < p2)

        p1 = Period(length=21, units=TimeUnits.Weeks)
        p2 = Period(length=5, units=TimeUnits.Months)

        with self.assertRaises(ValueError):
            _ = p1 < p2

        p1 = Period(length=21, units=TimeUnits.BDays)
        with self.assertRaises(ValueError):
            _ = p1 < p2

        # test not equal operator
        p1 = Period(length=1, units=TimeUnits.Days)
        p2 = Period(length=1, units=TimeUnits.Days)
        self.assertTrue(not p1 != p2)

        p2 = Period(length=1, units=TimeUnits.Years)
        self.assertTrue(p1 != p2)

        # test greater than operator
        p1 = Period(length=1, units=TimeUnits.Days)
        p2 = Period(length=2, units=TimeUnits.Days)
        self.assertEqual(p1 < p2, not p1 > p2)
Ejemplo n.º 13
0
class TimeProvider:

    def __init__(self):
        logging.debug("TimeProvider __init__")

    def is_market_open(self, timezone):
        """
        Return True if the market is open, false otherwise
            - **timezone**: string representing the timezone
        """
        tz = pytz.timezone(timezone)
        now_time = datetime.now(tz=tz).strftime("%H:%M")
        return BankHolidays().is_work_day(datetime.now(tz=tz)) and Utils.is_between(
            str(now_time), ("07:55", "16:35")
        )

    def get_seconds_to_market_opening(self, from_time):
        """Return the amount of seconds from now to the next market opening,
        taking into account UK bank holidays and weekends"""
        today_opening = datetime(
            year=from_time.year,
            month=from_time.month,
            day=from_time.day,
            hour=8,
            minute=0,
            second=0,
            microsecond=0,
        )

        if from_time < today_opening and BankHolidays().is_work_day(from_time.date()):
            nextMarketOpening = today_opening
        else:
            # Get next working day
            nextWorkDate = BankHolidays().get_next_work_day(date=from_time.date())
            nextMarketOpening = datetime(
                year=nextWorkDate.year,
                month=nextWorkDate.month,
                day=nextWorkDate.day,
                hour=8,
                minute=0,
                second=0,
                microsecond=0,
            )
        # Calculate the delta from from_time to the next market opening
        return (nextMarketOpening - from_time).total_seconds()

    def wait_for(self, time_amount_type, amount=-1):
        """Wait for the specified amount of time.
        An TimeAmount type can be specified
        """
        if time_amount_type is TimeAmount.NEXT_MARKET_OPENING:
            amount = self.get_seconds_to_market_opening(datetime.now())
        elif time_amount_type is TimeAmount.SECONDS:
            if amount < 0:
                raise ValueError("Invalid amount of time to wait for")
        logging.info("Wait for {0:.2f} hours...".format(amount / 3600))
        time.sleep(amount)


# -------------------------------------------------------------------------------------------------------------    
# string to datetime -> calculate -> date string for d days ago
# today is August 13, 10 days ago means August 3

    def days_ago(d, start_date=None):
        if start_date==None:
            date = datetime.datetime.today() - datetime.timedelta(days=d)
        else:
            date = str_to_date(start_date) - datetime.timedelta(days=d)
        return date.strftime("%Y-%m-%d")

    def str_to_date(dt):
        year, month, day = (int(x) for x in dt.split('-'))    
        return datetime.date(year, month, day)

# transform to the valid data:
    # value "09/2007" to date 2007-09-01. 
    # value "2006" to date 2016-01-01.

    def parse_thisdate(text: str) -> datetime.date:
        parts = text.split('/')
        if len(parts) == 2:
            return datetime.date(int(parts[1]), int(parts[0]), 1)
        elif len(parts) == 1:
            return datetime.date(int(parts[0]), 1, 1)
        else:
            assert False, 'Unknown date format'

# creating datetime object from zip

    with zipfile.ZipFile(self.fname) as zf:
        with zf.open(zf.namelist()[0]) as infile:
            header = infile.readline()
            datestr, record_count = header.split(b':')
            self.month = int(datestr[2:4])
            self.day = int(datestr[4:6])
            self.year = int(datestr[6:10])
            utc_base_time = datetime(self.year, self.month, self.day)
            self.base_time = timezone('US/Eastern').localize(utc_base_time).timestamp()

# creating url which contains date

    date = pd.datetime.today() - pd.offsets.BDay(1)
    date=date.date()
    datestr = date.strftime('%Y%m%d')
    daystr=str(date.day)
    monthstr=str(date.month)
    yearstr=str(date.year)
    
    
    url='http://www.netfonds.no/quotes/exchange.php?'  
    url=url+'exchange=%s'
    url=url+'&at_day=' + daystr
    url=url+'&at_month=' +monthstr
    url=url+'&at_year=' +yearstr
    url=url+'&format=csv'

# date to string to date

    # date to string
    current_date = Date(2015, 7, 24) # create date object
    two_days_later = current_date + 2 # => Date(2015, 7, 26) 
    str(two_days_later) # => 2015-07-26
    current_date + '1M' # => Date(2015, 8, 24)
    current_date + Period('1M') # same with previous line # => Date(2015, 8, 24)

    current_date.strftime("%Y%m%d") # => '20150724'
    Date.strptime('20160115', '%Y%m%d') # => Date(2016, 1, 15)
    Date.strptime('2016-01-15', '%Y-%m-%d') # => Date(2016, 1, 15)
    Date.from_datetime(datetime.datetime(2015, 7, 24)) # => Date(2015, 7, 24)

    # string to date
    pd.to_datetime(pd.Series(["Jul 31, 2017","2010-10-01","2016/10/10","2014.06.10"]))
    pd.to_datetime(pd.Series(["11 Jul 2018","13.04.2015","30/12/2011"]),dayfirst=True)
    # providing a format could increase speed of conversion significantly
    pd.to_datetime(pd.Series(["12-11-2010 01:56","11-01-2012 22:10","28-02-2013 14:59"]), format='%d-%m-%Y %H:%M')

    import market_calendars as mcal
    cal_sse = mcal.get_calendar('China.SSE') # create chinese shanghai stock exchange calendar
    cal_sse.adjust_date('20130131') # => datetime.datetime(2013, 1, 31, 0, 0)
    cal_sse.adjust_date('20130131', return_string=True) # => '2013-01-31'
    cal_sse.adjust_date('2017/10/01') # => datetime.datetime(2017, 10, 9, 0, 0)
    cal_sse.adjust_date('2017/10/01', convention=2) # => datetime.datetime(2017, 9, 29, 0, 0)

# reading time stamp
    def read_hhmmss(field: str) -> int:
        # """Read a HH:MM:SS field and return us since midnight.""
        if field != "":
            hour = int(field[0:2])
            minute = int(field[3:5])
            second = int(field[6:8])
            return 1000000 * ((3600 * hour) + (60 * minute) + second)
        else:
            return 0


    def read_hhmmssmil(field: str) -> int:
        """Read a HH:MM:SS:MILL field and return us since midnight."""
        if field != "":
            hour = int(field[0:2])
            minute = int(field[3:5])
            second = int(field[6:8])
            msecs = int(field[9:])
            return ((1000000 * ((3600 * hour) + (60 * minute) + second)) +
                    (1000 * msecs))
        else:
            return 0


    def read_mmddccyy(field: str) -> np.datetime64:
        """Read a MM-DD-CCYY field and return a np.datetime64('D') type."""
        if field != "":
            month = int(field[0:2])
            day = int(field[3:5])
            year = int(field[6:10])
            return np.datetime64(
                datetime.date(year=year, month=month, day=day), 'D')
        else:
            return np.datetime64(datetime.date(year=1, month=1, day=1), 'D')


    def read_ccyymmdd(field: str) -> np.datetime64:
        """Read a CCYYMMDD field and return a np.datetime64('D') type."""
        if field != "":
            year = int(field[0:4])
            month = int(field[4:6])
            day = int(field[6:8])
            return np.datetime64(
                datetime.date(year=year, month=month, day=day), 'D')
        else:
            return np.datetime64(datetime.date(year=1, month=1, day=1), 'D')


    def read_timestamp_msg(dt_tm: str) -> Tuple[np.datetime64, int]:
        """Read a CCYYMMDD HH:MM:SS field."""
        if dt_tm != "":
            (date_str, time_str) = dt_tm.split(' ')
            dt = read_ccyymmdd(date_str)
            tm = read_hhmmss(time_str)
            return dt, tm
        else:
            return np.datetime64(datetime.date(year=1, month=1, day=1), 'D'), 0


    def read_hist_news_timestamp(dt_tm: str) -> Tuple[np.datetime64, int]:
        """Read a news story time"""
        if dt_tm != "":
            date_str = dt_tm[0:8]
            time_str = dt_tm[8:14]
            dt = read_ccyymmdd(date_str)
            tm = read_hhmmss_no_colon(time_str)
            return dt, tm
        else:
            return np.datetime64(datetime.date(year=1, month=1, day=1), 'D'), 0

# named granularity into seconds

    def granularity_to_time(s):
        """convert a named granularity into seconds.
        get value in seconds for named granularities: M1, M5 ... H1 etc.
        >>> print(granularity_to_time("M5"))
        300
        """
        mfact = {
            'S': 1,
            'M': 60,
            'H': 3600,
            'D': 86400,
            'W': 604800,
        }
        try:
            f, n = re.match("(?P<f>[SMHDW])(?:(?P<n>\d+)|)", s).groups()
            n = n if n else 1
            return mfact[f] * int(n)

        except Exception as e:
            raise ValueError(e)

# -------------------------------------------------------------------------------------------------------------
# UTC convertion and timezone
    from pytz import timezone
    from pytz import utc

    # We're using NYSE and NASDAQ, which are both in the easters timezone.
    MARKET_TIMEZONE = timezone("US/Eastern")

    def utc_to_market_time(self, timestamp):
        """Converts a UTC timestamp to local market time."""

        utc_time = utc.localize(timestamp)
        market_time = utc_time.astimezone(MARKET_TIMEZONE)

        return market_time

    def market_time_to_utc(self, timestamp):
        """Converts a timestamp in local market time to UTC."""

        market_time = MARKET_TIMEZONE.localize(timestamp)
        utc_time = market_time.astimezone(utc)

        return utc_time

    def as_market_time(self, year, month, day, hour=0, minute=0, second=0):
        """Creates a timestamp in market time."""

        market_time = datetime(year, month, day, hour, minute, second)
        return MARKET_TIMEZONE.localize(market_time)


    # Remove timezone information.
    def unlocalize(dateTime):
        return dateTime.replace(tzinfo=None)


    def localize(dateTime, timeZone):
        """Returns a datetime adjusted to a timezone:

        * If dateTime is a naive datetime (datetime with no timezone information), timezone information is added but date
        and time remains the same.
        * If dateTime is not a naive datetime, a datetime object with new tzinfo attribute is returned, adjusting the date
        and time data so the result is the same UTC time.
        """

        if datetime_is_naive(dateTime):
            ret = timeZone.localize(dateTime)
        else:
            ret = dateTime.astimezone(timeZone)
        return ret


    def as_utc(dateTime):
        return localize(dateTime, pytz.utc)


    def datetime_to_timestamp(dateTime):
        """ Converts a datetime.datetime to a UTC timestamp."""
        diff = as_utc(dateTime) - epoch_utc
        return diff.total_seconds()


    def timestamp_to_datetime(timeStamp, localized=True):
        """ Converts a UTC timestamp to a datetime.datetime."""
        ret = datetime.datetime.utcfromtimestamp(timeStamp)
        if localized:
            ret = localize(ret, pytz.utc)
        return ret


    epoch_utc = as_utc(datetime.datetime(1970, 1, 1))

    def utc_to_local(self, utc_dt):
        utc_dt = datetime.strptime(utc_dt, "%Y-%m-%d %H:%M:%S")
    local = utc_dt.replace(tzinfo=timezone.utc).astimezone(tz=None)
    return local.strftime("%Y-%m-%d %H:%M:%S")

    def utc_to_local(utc_dt):
        local = utc_dt.replace(tzinfo=timezone.utc).astimezone(tz=None)
        return local.strftime("%Y-%m-%d %H:%M:%S")

    utc_dt = datetime.strptime("2018-11-01 01:45:00", "%Y-%m-%d %H:%M:%S")
    print(utc_to_local(utc_dt))

# -------------------------------------------------------------------------------------------------------------
# Adjust dates in csv according to the time zone

        time_zone_difference = int(args[2])
        input_filename = args[1]
        output_filename = 'OUT_' + input_filename
        with open(output_filename, 'w') as w:
            with open(input_filename, 'r') as r:
                reader = csv.reader(r, delimiter=';')
                for row in reader:
                    print(row)
                    new_row = list(row)
                    ts = datetime.strptime(new_row[0], '%Y%m%d %H%M%S')
                    ts += timedelta(hours=time_zone_difference)
                    new_row[0] = ts.strftime('%Y%m%d %H%M%S')
                    w.write(';'.join(new_row) + '\n')
Ejemplo n.º 14
0
    def test_basic_functions(self):
        year = 2015
        month = 7
        day = 24
        str_repr = "{0}-{1:02d}-{2:02d}".format(year, month, day)
        inner_repr = "Date({0}, {1}, {2})".format(year, month, day)

        test_date = Date(year, month, day)
        self.assertEqual(
            str(test_date), str_repr, "date string:\n"
            "expected:   {0:s}\n"
            "calculated: {1:s}".format(str_repr, str(test_date)))

        self.assertEqual(
            repr(test_date), inner_repr, "date representation:\n"
            "expected:   {0:s}\n"
            "calculated: {1:s}".format(inner_repr, repr(test_date)))

        self.assertEqual(
            test_date.year(), year, "date year:\n"
            "expected:   {0:d}\n"
            "calculated: {1:d}".format(year, test_date.year()))

        self.assertEqual(
            test_date.month(), month, "date month:\n"
            "expected:   {0:d}\n"
            "calculated: {1:d}".format(month, test_date.month()))

        self.assertEqual(
            test_date.day_of_month(), day, "date day:\n"
            "expected:   {0:d}\n"
            "calculated: {1:d}".format(day, test_date.day_of_month()))

        self.assertEqual(
            test_date.day_of_year(), test_date - Date(2015, 1, 1) + 1,
            "date day:\n"
            "expected:   {0:d}\n"
            "calculated: {1:d}".format(test_date - Date(2015, 1, 1) + 1,
                                       test_date.day_of_year()))
        self.assertEqual(
            test_date.weekday(), 6, "date weekday:\n"
            "expected:   {0:d}\n"
            "calculated: {1:d}".format(5, test_date.weekday()))

        self.assertEqual(
            test_date.to_datetime(), dt.datetime(year, month, day),
            "date datetime representation\n"
            "expected:   {0}\n"
            "calculated: {1}".format(dt.datetime(year, month, day),
                                     test_date.to_datetime()))

        serial_number = test_date.serial_number
        serial_date = Date(serial_number=serial_number)

        self.assertEqual(
            serial_date, test_date, "date excel serial number representation\n"
            "expected:   {0:d}"
            "calculated: {1:d}".format(serial_date.serial_number,
                                       test_date.serial_number))

        # test comparisons
        previous_date = test_date - 1
        self.assertTrue(
            previous_date < test_date,
            "{0} is not earlier than {1}".format(previous_date, test_date))
        self.assertFalse(
            previous_date >= test_date,
            "{0} should not be later than or equal to {1}".format(
                previous_date, test_date))
        self.assertTrue((previous_date + 1) == test_date,
                        "{0} plus one day should be equal to {1}".format(
                            previous_date, test_date))

        # check static members
        self.assertEqual(Date.min_date(), Date(1901, 1, 1),
                         "min date is wrong")
        self.assertEqual(Date.max_date(), Date(2199, 12, 31),
                         "max date is wrong")
        self.assertEqual(Date.end_of_month(test_date), Date(year, month, 31),
                         "end of month is wrong")
        self.assertTrue(Date.is_end_of_month(Date(year, month, 31)),
                        "{0} should be the end of month")
        self.assertEqual(
            Date.next_weekday(test_date, test_date.weekday()), test_date,
            "{0}'s next same week day should be {1}".format(
                test_date, test_date))
        expected_date = dt.date.today()
        expected_date = dt.datetime(expected_date.year, expected_date.month,
                                    expected_date.day)
        self.assertEqual(
            Date.today_date().to_datetime(), expected_date, "today's date\n"
            "expected:   {0}\n"
            "calculated: {1}".format(expected_date, Date.today_date()))

        # nth-week day
        with self.assertRaises(ValueError):
            _ = Date.nth_weekday(0, Weekdays.Friday, 1, 2015)

        with self.assertRaises(ValueError):
            _ = Date.nth_weekday(6, Weekdays.Friday, 1, 2015)

        self.assertEqual(Date.nth_weekday(3, Weekdays.Wednesday, 8, 2015),
                         Date(2015, 8, 19))

        # check plus/sub

        three_weeks_after = test_date + '3W'
        expected_date = test_date + 21
        self.assertEqual(
            three_weeks_after, expected_date, "date + 3w period\n"
            "expected:   {0}\n"
            "calculated: {1}".format(expected_date, three_weeks_after))

        three_months_before = test_date - "3M"
        expected_date = Date(year, month - 3, day)
        self.assertEqual(
            three_months_before, expected_date, "date - 3m period\n"
            "expected:   {0}\n"
            "calculated: {1}".format(expected_date, three_months_before))

        three_months_before = test_date - Period("3M")
        expected_date = Date(year, month - 3, day)
        self.assertEqual(
            three_months_before, expected_date, "date - 3m period\n"
            "expected:   {0}\n"
            "calculated: {1}".format(expected_date, three_months_before))

        three_months_after = test_date + "3m"
        expected_date = Date(year, month + 3, day)
        self.assertEqual(
            three_months_after, expected_date, "date + 3m period\n"
            "expected:   {0}\n"
            "calculated: {1}".format(expected_date, three_months_after))

        one_year_and_two_months_before = test_date - "14m"
        expected_date = Date(year - 1, month - 2, day)
        self.assertEqual(
            one_year_and_two_months_before, expected_date,
            "date - 14m period\n"
            "expected:   {0}\n"
            "calculated: {1}".format(expected_date, three_months_before))

        one_year_and_two_months_before = test_date + "14m"
        expected_date = Date(year + 1, month + 2, day)
        self.assertEqual(
            one_year_and_two_months_before, expected_date,
            "date + 14m period\n"
            "expected:   {0}\n"
            "calculated: {1}".format(expected_date, three_months_before))

        five_months_after = test_date + "5m"
        expected_date = Date(year, month + 5, day)
        self.assertEqual(
            five_months_after, expected_date, "date + 5m period\n"
            "expected:   {0}\n"
            "calculated: {1}".format(expected_date, five_months_after))