diff --git a/src/dexorder/final_ohlc.py b/src/dexorder/final_ohlc.py index bd4d9ef..2a3c247 100644 --- a/src/dexorder/final_ohlc.py +++ b/src/dexorder/final_ohlc.py @@ -150,6 +150,7 @@ class OHLCFile: # first we write the "final" rows which means rows that have been closed and will get no more data. for row in self._final_rows: self.file.write(OHLCFile.row_bytes(row)) + self._final_rows.clear() # apply any pending changes to the current row if self._pending is not None: self._cur = self._pending @@ -214,19 +215,25 @@ class OHLCFileSeries: self.base_dir = base_dir self.symbol = symbol self.series_start: Optional[datetime] = None # timestamp of the first datum in the series - self.write_offset: int = 0 + self.write_offset: Optional[int] = None self.last_flush: Optional[tuple[datetime,dec]] = None self.quote_file = None self.dirty_files = set() self.quote: Optional[tuple[datetime,dec]] = None + + @property + def quote_filename(self): + return os.path.join(self.base_dir, self.symbol, 'quote.csv') + + def update(self, time: datetime, price: dec): # # load quote file # if self.quote_file is None: - quote_filename = os.path.join(self.base_dir, self.symbol, 'quote.csv') + quote_filename = self.quote_filename try: self.quote_file = open(quote_filename, 'br+') # load existing quote file @@ -289,14 +296,28 @@ class OHLCFileSeries: # # flush quote file # - self.quote_file.write(f'{ts},{price:f}'.encode('ascii')) - self.quote_file.flush() - self.quote_file.seek(self.write_offset) + try: + start_of_column = self.quote_file.tell() + self.quote_file.write(f'{ts},{price:f}'.encode('ascii')) + self.quote_file.truncate() + self.quote_file.flush() + self.quote_file.seek(start_of_column) + except IOError: + log.exception(f'While saving quote file {self.quote_filename}') - # remember where we were so we can forward-fill again next time + # remember the last flush point so we can forward-fill next time self.last_flush = self.quote + def __hash__(self): + return hash((self.base_dir,self.symbol)) + + + def __eq__(self, other): + return other.base_dir == self.base_dir and other.symbol == self.symbol + + + class FinalOHLCRepository: """ Used for backfill when all data is known to be final. Keeps a simple table of current NativeOHLC candles.