def __init__(self, db, table, mtgox_cli, max_hours_ago=4, verbose=1): self.verbose = verbose self.db = db self.table = table self.cursor = db.cursor() self.mtgox = mtgox_cli self.mtgox.call(self.load_from_last_stored, on_event=('connected', 'partial_download')) self.mtgox.evt.listen('trade_fetch', self.on_trade_fetch) self.cursor.execute("""SELECT tid FROM [%s] ORDER BY tid DESC LIMIT 1""" % self.table) res = self.cursor.fetchone() self.prev_last = None # Did we request to load data and it did not finish yet ? self._pending = False self.last_tid = 0 if res is None else res[0] if max_hours_ago is not None: self.last_tid = max(self.last_tid, mtgox.calc_tid(max_hours_ago)) # Otherwise, if self.last_tid == 0 then be ready to download the # entire trade history! if verbose: print(self.last_tid)
def trades_from_db(db, hours_ago=Decimal('1.0'), raw_tid=None, rounding=True, table='btcusd'): if raw_tid is not None: since = raw_tid else: since = mtgox.calc_tid(hours_ago) if rounding: # Round to start on some exact minute. tstruct = list(time.gmtime(since/1e6)) tstruct[5] = 0 # 0 seconds tstruct[4] = 0 # 0 minutes since = int(calendar.timegm(tstruct) * 1e6) return db.execute("""SELECT tid, timestamp, price, amount FROM [%s] WHERE tid > ? ORDER BY tid ASC""" % table, (since, )), since / 1e6