class ScidHandler(object): def __init__(self, symbol, datadir, volume_precision): self.symbol = symbol self.filename = os.path.join(datadir, "%s.scid" % symbol) self.volume_precision = volume_precision self.load() def load(self): print 'Loading data file', self.filename if os.path.exists(self.filename): self.scid = ScidFile() self.scid.load(self.filename) else: self.scid = ScidFile.create(self.filename) self.scid.seek(self.scid.length) def ticker_update(self, data): latest_id = int(data['id']) price = float(data['price']) / 100000000 volume = int(float(data['volume'])*10**self.volume_precision) date = datetime.datetime.fromtimestamp(float(data['timestamp'])) print self.symbol, date, price, float(volume)/10**self.volume_precision # Datetime, Open, High, Low, Close, NumTrades, TotalVolume, BidVolume, AskVolume): try: rec = ScidRecord(date, price, price, price, price, 1, volume, 0, 0) self.scid.write(rec.to_struct()) self.scid.fp.flush() pickle.dump( latest_id, open(mpex_id_filename, "wb" )) except Exception as e: print str(e)
class ScidHandler(object): def __init__(self, symbol, datadir, disable_history, volume_precision, history_length): self.symbol = symbol self.filename = os.path.join(datadir, "%s.scid" % symbol) self.volume_precision = volume_precision self.history_length = history_length self.load() if not disable_history: try: self.download_historical() except Exception as e: # We don't want to continue; if we receive new data from live feed, # gap inside scid file won't be filled anymore, so we must wait # until historical feed is available again raise Exception("Historical download failed: %s, use -y to disable history" % str(e)) def load(self): print 'Loading data file', self.filename if os.path.exists(self.filename): self.scid = ScidFile() self.scid.load(self.filename) else: self.scid = ScidFile.create(self.filename) self.scid.seek(self.scid.length) def download_historical(self): length = self.scid.length if not length: from_timestamp = 0 else: self.scid.seek(self.scid.length-1) rec = ScidRecord.from_struct(self.scid.readOne()) from_timestamp = int(time.mktime(rec.DateTime.timetuple())) + 1 print 'Downloading historical data' self.scid.seek(self.scid.length) for rec in bitcoincharts_history(self.symbol, from_timestamp, self.volume_precision, self.history_length, True): self.scid.write(rec.to_struct()) self.scid.fp.flush() def ticker_update(self, data): price = float(data['price']) volume = int(float(data['volume'])*10**self.volume_precision) date = datetime.fromtimestamp(float(data['timestamp'])) # Fixes corrupted structure provided by some exchanges volume = 0 if volume < 0 else volume print self.symbol, date, price, float(volume)/10**self.volume_precision # Datetime, Open, High, Low, Close, NumTrades, TotalVolume, BidVolume, AskVolume): try: rec = ScidRecord(date, price, price, price, price, 1, volume, 0, 0) self.scid.write(rec.to_struct()) self.scid.fp.flush() except Exception as e: print str(e)
class ScidHandler(object): def __init__(self, symbol, datadir, disable_history, volume_precision): self.symbol = symbol self.filename = os.path.join(datadir, "%s.scid" % symbol) self.volume_precision = volume_precision self.load() if not disable_history: try: self.download_historical() except Exception as e: # We don't want to continue; if we receive new data from live feed, # gap inside scid file won't be filled anymore, so we must wait # until historical feed is available again raise Exception("Historical download failed: %s, use -y to disable history" % str(e)) def load(self): print 'Loading data file', self.filename if os.path.exists(self.filename): self.scid = ScidFile() self.scid.load(self.filename) else: self.scid = ScidFile.create(self.filename) self.scid.seek(self.scid.length) def download_historical(self): length = self.scid.length if not length: from_timestamp = 0 else: self.scid.seek(self.scid.length-1) rec = ScidRecord.from_struct(self.scid.readOne()) from_timestamp = int(time.mktime(rec.DateTime.timetuple())) + 1 print 'Downloading historical data' self.scid.seek(self.scid.length) for rec in bitcoincharts_history(self.symbol, from_timestamp, self.volume_precision, True): self.scid.write(rec.to_struct()) self.scid.fp.flush() def ticker_update(self, data): price = float(data['price']) volume = int(float(data['volume'])*10**self.volume_precision) date = datetime.datetime.fromtimestamp(float(data['timestamp'])) print self.symbol, date, price, float(volume)/10**self.volume_precision # Datetime, Open, High, Low, Close, NumTrades, TotalVolume, BidVolume, AskVolume): try: rec = ScidRecord(date, price, price, price, price, 1, volume, 0, 0) self.scid.write(rec.to_struct()) self.scid.fp.flush() except Exception as e: print str(e)