ENH: Changes the default offset for time_rules.market_open to 1, and

makes it an offset from 13:30 UTC.

This is to be more consistent with the market_close, which is an offset
from 20:00 UTC.

This also makes market_open and market_close cache the dt to offset from
for each day.
This commit is contained in:
llllllllll
2014-11-02 21:10:37 -05:00
committed by Joe Jevnik
parent e1a55131ec
commit 9ddb033e67
2 changed files with 36 additions and 8 deletions
+7 -2
View File
@@ -260,9 +260,14 @@ class TestStatelessRules(RuleTestCase):
def test_AfterOpen(self):
should_trigger = AfterOpen(minutes=5, hours=1).should_trigger
for d in self.trading_days:
for m in islice(d, 65):
for m in islice(d, 64):
# Check the first 64 minutes of data.
# We use 64 because the offset is from market open
# at 13:30 UTC, meaning the first minute of data has an
# offset of 1.
self.assertFalse(should_trigger(m))
for m in islice(d, 65, None):
for m in islice(d, 64, None):
# Check the rest of the day.
self.assertTrue(should_trigger(m))
def test_BeforeClose(self):
+29 -6
View File
@@ -115,11 +115,11 @@ def _td_check(td):
seconds = td.total_seconds()
# 23400 seconds is 6 hours and 30 minutes.
if 0 <= seconds <= 23400:
if 60 <= seconds <= 23400:
return td
else:
raise ValueError('offset must be in between 0 minutes and 6 hours and'
' 30 minutes')
raise ValueError('offset must be in between 1 minute and 6 hours and'
' 30 minutes inclusive')
def _build_offset(offset, kwargs, default):
@@ -323,11 +323,23 @@ class AfterOpen(StatelessRule):
self.offset = _build_offset(
offset,
kwargs,
datetime.timedelta(), # Defaults to the first minute.
datetime.timedelta(minutes=1), # Defaults to the first minute.
)
self._dt = None
def should_trigger(self, dt):
return self.env.get_open_and_close(dt)[0] + self.offset <= dt
return self._get_open(dt) + self.offset <= dt
def _get_open(self, dt):
"""
Cache the open for each day.
"""
if self._dt is None or (self._dt.date() != dt.date()):
self._dt = self.env.get_open_and_close(dt)[0] \
- datetime.timedelta(minutes=1)
return self._dt
class BeforeClose(StatelessRule):
@@ -344,8 +356,19 @@ class BeforeClose(StatelessRule):
datetime.timedelta(minutes=1), # Defaults to the last minute.
)
self._dt = None
def should_trigger(self, dt):
return self.env.get_open_and_close(dt)[1] - self.offset < dt
return self._get_close(dt) - self.offset < dt
def _get_close(self, dt):
"""
Cache the close for each day.
"""
if self._dt is None or (self._dt.date() != dt.date()):
self._dt = self.env.get_open_and_close(dt)[1]
return self._dt
class NotHalfDay(StatelessRule):