예제 #1
0
 def run(self):
     self.init_db()
     self.redis_client = Redis()
     self.handler = MtGoxWebSocketAPI(
         "ws://websocket.mtgox.com/mtgox?Currency=USD",
         trade_handler=self.handle_trade,
         ticker_handler=self.handle_ticker,
         depth_handler=self.handle_depth,
     )
     print dir(self.handler)
     print "attempting connect"
     self.handler.connect()
예제 #2
0
class Collector:
    def init_db(self):
        print "init db"
        engine = create_engine(DATABASE, echo=True)
        db.Base.metadata.create_all(engine)

        Session = sessionmaker(bind=engine)
        self.db_session = Session()

    def handle_trade(self, data, raw_data):
        trade = db.MtGoxTrade(
            type=data["type"],
            date=datetime.fromtimestamp(data["date"]),
            amount=data["amount"],
            price=data["price"],
            tid=data["tid"],
            price_currency=data["price_currency"],
            trade_type=data["trade_type"],
            primary=data["primary"],
            properties=data["properties"],
        )
        self.db_session.add(trade)
        self.db_session.commit()
        self.redis_client.publish("trades.mtgox", raw_data)

    def handle_ticker(self, data, raw_data):
        market_state = db.MtGoxTicker(
            timestamp=convert_mtgox_timestamp(data["now"]),
            # FIXME needs to be timezone aware
            time_received=datetime.utcnow(),
            symbol=data["high"]["currency"],
            high=data["high"]["value"],
            low=data["low"]["value"],
            avg=data["avg"]["value"],
            vwap=data["vwap"]["value"],
            vol=data["vol"]["value"],
            last_local=data["last_local"]["value"],
            last_orig=data["last_orig"]["value"],
            last_all=data["last_all"]["value"],
            last=data["last"]["value"],
            buy=data["buy"]["value"],
            sell=data["sell"]["value"],
            item=data["item"],
        )
        self.db_session.add(market_state)
        self.db_session.commit()
        self.redis_client.publish("ticker.mtgox", raw_data)

    def handle_depth(self, data, raw_data):
        depth = db.MtGoxDepth(
            type=data["type"],
            type_str=data["type_str"],
            price=data["price"],
            volume=data["volume"],
            currency=data["currency"],
            timestamp=convert_mtgox_timestamp(data["now"]),
        )
        self.db_session.add(depth)
        self.db_session.commit()
        self.redis_client.publish("depth.mtgox", raw_data)

    def run(self):
        self.init_db()
        self.redis_client = Redis()
        self.handler = MtGoxWebSocketAPI(
            "ws://websocket.mtgox.com/mtgox?Currency=USD",
            trade_handler=self.handle_trade,
            ticker_handler=self.handle_ticker,
            depth_handler=self.handle_depth,
        )
        print dir(self.handler)
        print "attempting connect"
        self.handler.connect()