text
stringlengths
1
93.6k
self.path.close()
def _addBar(
self, barTimestamp, tickTimestamp, uniBar_open, high, low, close, volume
):
self.uniBars += [
{
"barTimestamp": barTimestamp,
"tickTimestamp": tickTimestamp,
"open": uniBar_open,
"high": high,
"low": low,
"close": close,
"volume": volume,
}
]
def string_to_timestamp(s):
try_microseconds = s[20:]
if try_microseconds == "":
microseconds = int(s[20:])
else:
microseconds = 0
return datetime.datetime(
int(s[0:4]), # Year
int(s[5:7]), # Month
int(s[8:10]), # Day
int(s[11:13]), # Hour
int(s[14:16]), # Minute
int(s[17:19]), # Second
microseconds, # Microseconds
datetime.timezone.utc,
)
class CSV(Input):
def __init__(self, path):
super().__init__(path)
self._map_obj = mmap.mmap(self.path.fileno(), 0, access=mmap.ACCESS_READ)
def __iter__(self):
return self
def __next__(self):
line = self._map_obj.readline()
if line:
isLastRow = self._map_obj.tell() == self._map_obj.size()
return (self._parseLine(line), isLastRow)
raise StopIteration
def _parseLine(self, line):
tick = line.split(b",")
return {
# Storing timestamp as float to preserve its precision.
# 'timestamp': time.mktime(datetime.datetime.strptime(tick[0], '%Y.%m.%d %H:%M:%S.%f').replace(tzinfo=datetime.timezone.utc).timetuple()),
"timestamp": string_to_timestamp(tick[0]).timestamp(),
"bidPrice": float(tick[1]),
"askPrice": float(tick[2]),
"bidVolume": float(tick[3]),
"askVolume": float(tick[4]), # float() handles ending '\n' character
}
class Output:
def __init__(self, timeframe, path_suffix, symbol, output_dir):
self.deltaTimestamp = timeframe * 60
self.endTimestamp = None
self.barCount = 0
self.filename = "%s%d%s" % (symbol, timeframe, path_suffix)
self.fullname = os.path.join(output_dir, self.filename)
try:
os.remove(
self.fullname
) # Remove existing output file before creating an appended new one
except (OSError, IOError) as e:
pass
try:
self.path = open(self.fullname, "wb")
except OSError as e:
print(
"[ERROR] '%s' raised when tried to open for appending the file '%s'"
% (e.strerror, e.filename)
)
sys.exit(1)
def __del__(self):
self.path.close()
def finalize(self):
pass
def _aggregate(self, tick):
if not self.endTimestamp or tick["timestamp"] >= self.endTimestamp:
uniBar = None
if self.endTimestamp: