-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistoricalData.py
More file actions
55 lines (39 loc) · 1.65 KB
/
Copy pathHistoricalData.py
File metadata and controls
55 lines (39 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from abc import ABC, abstractmethod
import datetime
from DB import SqliteDB, Event
class HistoricalData(ABC):
@abstractmethod
def __init__(self, starting_timestamp: datetime.datetime):
pass
@abstractmethod
def update_timestamp(self, timestamp: datetime.datetime):
pass
@abstractmethod
def get_events(self, ticker: str, begin: datetime.datetime, end: datetime.datetime) -> list[Event]:
pass
@abstractmethod
def get_current_price(self, ticker: str) -> int:
pass
class SQLHistoricalData(HistoricalData):
_current_timestamp: datetime.datetime
_db: SqliteDB
def __init__(self, db: SqliteDB):
self._current_timestamp = datetime.datetime.now()
self._db = db
def get_events(self, begin: datetime.datetime, end: datetime.datetime, ticker: str | None = None):
if begin > self._current_timestamp:
begin = self._current_timestamp
if end > self._current_timestamp:
end = self._current_timestamp
return self._db.get_events(begin=[begin, end], ticker=ticker)
def get_events_unrestricted(self, begin: datetime.datetime, end: datetime.datetime):
return self._db.get_events(begin=[begin, end])
def update_timestamp(self, timestamp: datetime.datetime):
self._current_timestamp = timestamp
def get_timestamp(self) -> datetime.datetime:
return self._current_timestamp
def get_current_price(self, ticker: str) -> int | None:
latest_ohlcv = self._db.get_latest_event(ticker, 'OHLCV', self._current_timestamp)
if latest_ohlcv is None:
return None
return latest_ohlcv.open