Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
 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)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
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)
    parser.add_option("-b", "--bootstrap", dest="bootstrap", default=None, metavar="FILE",
                  help="Bootstrap history from a .csv file")

    (options, args) = parser.parse_args()

    if options.bootstrap:
        base_filename, _, _ = options.bootstrap.rpartition('.')
        scid_filename = "%s.scid" % base_filename

        if os.path.exists(scid_filename):
            print "{} already exists, aborting bootstrap.".format(scid_filename)
            sys.exit()

        with open(options.bootstrap) as f:
            data = f.read().split("\n")
            scid = ScidFile.create(scid_filename)
            for rec in scid_from_csv(data, base_filename, options.precision):
                scid.write(rec.to_struct())
            scid.fp.flush()
            scid.close()
            print "Bootstrap finished."
            sys.exit()

    if options.precision < 0 or options.precision > 8:
        print "Precision must be between 0 and 8"
        sys.exit()

    # Symbols to watch    
    symbols = options.symbols.split(',')
    scids = ScidLoader(options.datadir, options.disable_history, options.precision, options.length)