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)
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 bitcoincharts_history(symbol, from_timestamp, volume_precision, log=False): #fetch 1 week at a time until we get to next week prev_timestamp = from_timestamp next_timestamp = from_timestamp + 604800 while (next_timestamp <= time.time()) url = '%s?start=%s&end=%s&symbol=%s' % (BITCOINCHARTS_TRADES_URL, prev_timestamp, next_timestamp, symbol) #print url req = urllib2.Request(url) for line in urllib2.urlopen(req).read().split('\n'): if not line: continue line = line.split(',') try: timestamp, price, volume = int(line[0]), float(line[1]), int(float(line[2])*10**volume_precision) if log: print symbol, datetime.datetime.fromtimestamp(timestamp), timestamp, price, float(volume)/10**volume_precision yield ScidRecord(datetime.datetime.fromtimestamp(timestamp), price, price, price, price, 1, volume, 0, 0) except ValueError: print line print "Corrupted data for symbol %s, skipping" % symbol prev_timestamp = next_timestamp next_timestamp = next_timestamp + 604800