nzem-time 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nzem_time-1.0.0/PKG-INFO +7 -0
- nzem_time-1.0.0/README.md +0 -0
- nzem_time-1.0.0/pyproject.toml +13 -0
- nzem_time-1.0.0/setup.cfg +4 -0
- nzem_time-1.0.0/src/nzem_time/__init__.py +9 -0
- nzem_time-1.0.0/src/nzem_time/_common.py +10 -0
- nzem_time-1.0.0/src/nzem_time/fiveminperiods.py +47 -0
- nzem_time-1.0.0/src/nzem_time/tradingperiods.py +58 -0
- nzem_time-1.0.0/src/nzem_time.egg-info/PKG-INFO +7 -0
- nzem_time-1.0.0/src/nzem_time.egg-info/SOURCES.txt +10 -0
- nzem_time-1.0.0/src/nzem_time.egg-info/dependency_links.txt +1 -0
- nzem_time-1.0.0/src/nzem_time.egg-info/top_level.txt +1 -0
nzem_time-1.0.0/PKG-INFO
ADDED
|
File without changes
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "nzem-time"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Trading Period Utilities for NZEM"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
authors = [
|
|
12
|
+
{name = "Your Name"}
|
|
13
|
+
]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# _common.py
|
|
2
|
+
|
|
3
|
+
from datetime import datetime, timedelta, timezone, time
|
|
4
|
+
from zoneinfo import ZoneInfo
|
|
5
|
+
from typing import Tuple
|
|
6
|
+
|
|
7
|
+
NZ = ZoneInfo("Pacific/Auckland")
|
|
8
|
+
UTC = timezone.utc
|
|
9
|
+
EPOCH_UTC = datetime(1970, 1, 1, tzinfo=timezone.utc)
|
|
10
|
+
PERIOD_SECONDS = 300
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
from .tradingperiods import TradingPeriod
|
|
2
|
+
from ._common import PERIOD_SECONDS
|
|
3
|
+
from typing import Tuple
|
|
4
|
+
|
|
5
|
+
class FiveMinPeriod:
|
|
6
|
+
__slots__ = ("_value",)
|
|
7
|
+
|
|
8
|
+
def __init__(self, value: int):
|
|
9
|
+
self._value = value
|
|
10
|
+
|
|
11
|
+
def __repr__(self):
|
|
12
|
+
return f"FiveMinPeriod(tp={self.tp}, period={self.period})"
|
|
13
|
+
|
|
14
|
+
@classmethod
|
|
15
|
+
def from_datetime(cls, dt):
|
|
16
|
+
return cls(int(dt.timestamp()) // PERIOD_SECONDS)
|
|
17
|
+
|
|
18
|
+
@property
|
|
19
|
+
def index(self) -> int:
|
|
20
|
+
return self._value
|
|
21
|
+
|
|
22
|
+
@property
|
|
23
|
+
def tp(self):
|
|
24
|
+
return TradingPeriod(self._value // 6)
|
|
25
|
+
|
|
26
|
+
@property
|
|
27
|
+
def period(self):
|
|
28
|
+
# 1-6
|
|
29
|
+
return (self._value % 6) + 1
|
|
30
|
+
|
|
31
|
+
def next(self, jump=1):
|
|
32
|
+
return FiveMinPeriod(self._value + jump)
|
|
33
|
+
|
|
34
|
+
def prev(self, jump=1):
|
|
35
|
+
return FiveMinPeriod(self._value - jump)
|
|
36
|
+
|
|
37
|
+
@staticmethod
|
|
38
|
+
def range(start, end_exclusive):
|
|
39
|
+
current = start._value
|
|
40
|
+
end = end_exclusive._value
|
|
41
|
+
|
|
42
|
+
while current < end:
|
|
43
|
+
yield FiveMinPeriod(current)
|
|
44
|
+
current += 1
|
|
45
|
+
|
|
46
|
+
def get_five_min_periods(tp: TradingPeriod) -> Tuple[FiveMinPeriod, FiveMinPeriod, FiveMinPeriod, FiveMinPeriod, FiveMinPeriod, FiveMinPeriod]:
|
|
47
|
+
return tuple(FiveMinPeriod(tp,i) for i in range(1,7))
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
from datetime import datetime, timedelta
|
|
2
|
+
from ._common import EPOCH_UTC, NZ
|
|
3
|
+
|
|
4
|
+
class TradingPeriod:
|
|
5
|
+
__slots__ = ("_halfhourssince")
|
|
6
|
+
|
|
7
|
+
def __init__(self, hhs: int):
|
|
8
|
+
object.__setattr__(self, "_halfhourssince", hhs)
|
|
9
|
+
|
|
10
|
+
def __repr__(self):
|
|
11
|
+
return f"TradingPeriod(date={self.as_datetime_nz.date()}, period={self.period})"
|
|
12
|
+
|
|
13
|
+
def __setattr__(self, name, value):
|
|
14
|
+
raise AttributeError(f"{type(self).__name__} is immutable.")
|
|
15
|
+
|
|
16
|
+
@classmethod
|
|
17
|
+
def from_datetime(cls, dt: datetime):
|
|
18
|
+
half_hours_since = int(dt.timestamp()) // (1800)
|
|
19
|
+
return TradingPeriod(half_hours_since)
|
|
20
|
+
|
|
21
|
+
@property
|
|
22
|
+
def as_datetime_utc(self) -> datetime:
|
|
23
|
+
return EPOCH_UTC + timedelta(minutes=30 * self._halfhourssince)
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def as_datetime_nz(self):
|
|
27
|
+
return (EPOCH_UTC + timedelta(minutes=self._halfhourssince*30)).astimezone(NZ)
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
def period(self):
|
|
31
|
+
dt = self.as_datetime_nz
|
|
32
|
+
d = dt.date()
|
|
33
|
+
return int(
|
|
34
|
+
dt.timestamp() -
|
|
35
|
+
datetime(d.year, d.month, d.day,0,tzinfo=NZ).timestamp()
|
|
36
|
+
) // 1800 + 1
|
|
37
|
+
|
|
38
|
+
@staticmethod
|
|
39
|
+
def range(start, end_exclusive):
|
|
40
|
+
current = start
|
|
41
|
+
while current._halfhourssince < end_exclusive._halfhourssince:
|
|
42
|
+
yield current
|
|
43
|
+
current = current.next()
|
|
44
|
+
|
|
45
|
+
def next(self, jump=1):
|
|
46
|
+
return TradingPeriod(self._halfhourssince + jump)
|
|
47
|
+
|
|
48
|
+
def prev(self, jump=1):
|
|
49
|
+
return TradingPeriod(self._halfhourssince - jump)
|
|
50
|
+
|
|
51
|
+
def __hash__(self):
|
|
52
|
+
return hash(self._halfhourssince)
|
|
53
|
+
|
|
54
|
+
def __eq__(self, other):
|
|
55
|
+
return (
|
|
56
|
+
isinstance(other, TradingPeriod)
|
|
57
|
+
and self._halfhourssince == other._halfhourssince
|
|
58
|
+
)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
src/nzem_time/__init__.py
|
|
4
|
+
src/nzem_time/_common.py
|
|
5
|
+
src/nzem_time/fiveminperiods.py
|
|
6
|
+
src/nzem_time/tradingperiods.py
|
|
7
|
+
src/nzem_time.egg-info/PKG-INFO
|
|
8
|
+
src/nzem_time.egg-info/SOURCES.txt
|
|
9
|
+
src/nzem_time.egg-info/dependency_links.txt
|
|
10
|
+
src/nzem_time.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
nzem_time
|