text
stringlengths
1
93.6k
uniBar = {
"barTimestamp": self.startTimestamp,
"tickTimestamp": tick["timestamp"],
"open": self.open,
"high": self.high,
"low": self.low,
"close": self.close,
"volume": self.volume,
}
self.startTimestamp = (
int(tick["timestamp"]) // self.deltaTimestamp
) * self.deltaTimestamp
self.endTimestamp = self.startTimestamp + self.deltaTimestamp
self.open = self.high = self.low = self.close = tick["bidPrice"]
self.volume = tick["bidVolume"] + tick["askVolume"]
if uniBar:
return (uniBar, True)
else:
self.high = max(tick["bidPrice"], self.high)
self.low = min(tick["bidPrice"], self.low)
self.close = tick["bidPrice"]
self.volume += tick["bidVolume"] + tick["askVolume"]
uniBar = {
"barTimestamp": self.startTimestamp,
"tickTimestamp": tick["timestamp"],
"open": self.open,
"high": self.high,
"low": self.low,
"close": self.close,
"volume": self.volume,
}
return (uniBar, False)
def _aggregateWithTicks(self, tick):
if not self.endTimestamp or tick["timestamp"] >= self.endTimestamp:
self.startTimestamp = (
int(tick["timestamp"]) // self.deltaTimestamp
) * self.deltaTimestamp
self.endTimestamp = self.startTimestamp + self.deltaTimestamp
self.open = self.high = self.low = tick["bidPrice"]
self.volume = tick["bidVolume"] + tick["askVolume"]
self.barCount += 1
else:
self.high = max(tick["bidPrice"], self.high)
self.low = min(tick["bidPrice"], self.low)
self.volume += tick["bidVolume"] + tick["askVolume"]
return {
"barTimestamp": self.startTimestamp,
"tickTimestamp": tick["timestamp"],
"open": self.open,
"high": self.high,
"low": self.low,
"close": tick["bidPrice"],
"volume": self.volume,
}
class HST509(Output):
def __init__(self, path, path_suffix, output_dir, timeframe, symbol):
# Initialize variables in parent constructor
super().__init__(timeframe, path_suffix, symbol, output_dir)
# Build header (148 Bytes in total)
header = bytearray()
header += pack("<i", 400) # Version
header += bytearray(
"(C)opyright 2003, MetaQuotes Software Corp.".ljust(
64, "\x00" # Copyright
),
"latin1",
"ignore",
)
header += bytearray(symbol.ljust(12, "\x00"), "latin1", "ignore") # Symbol
header += pack("<i", timeframe) # Period
header += pack("<i", 5) # Digits, using the default value of HST format
header += pack("<i", int(time.time())) # Time of sign (database creation)
header += pack("<i", 0) # Time of last synchronization
header += bytearray(13 * 4) # Space for future use
self.path.write(header)
def pack_ticks(self, tick):
# Transform universal bar list to binary bar data (44 Bytes per bar)
(uniBar, newUniBar) = self._aggregate(tick)
if newUniBar:
self.path.write(self._packUniBar(uniBar))
def _packUniBar(self, uniBar):
bar = bytearray()
bar += pack("<i", uniBar["barTimestamp"]) # Time
bar += pack("<d", uniBar["open"]) # Open
bar += pack("<d", uniBar["low"]) # Low
bar += pack("<d", uniBar["high"]) # High
bar += pack("<d", uniBar["close"]) # Close
bar += pack("<d", max(uniBar["volume"], 1.0)) # Volume