Esempio n. 1
0
def main():
    """
    Download the entire history of trades from MtGox.
    If there is partial download, the download will resume from the last
    stored transaction id.
    """
    mtgox_client, tradefetch, db = mtgox_tradehist.setup_client(
            verbose=1, max_hours_ago=None)
    mtgox_client.evt.listen('done', lambda _: reactor.stop())
    reactor.run()
Esempio n. 2
0
def main():
    hours_ago = 48 # 2 days of data
    mtgox_client, tradefetch, db = mtgox_tradehist.setup_client(verbose=0,
            max_hours_ago=hours_ago, dbname='mtgox_trades3.db')
    plot = Demo(db, max_hours_ago=hours_ago,
            candle_minute_duration=30, # 2 candles per hour
            plot_hours=48)             # 2 days, 96 candles

    print('Loading from database..')
    plot.load_from_db()

    print('Setting up MtGox client..')

    def store_new_trade(trade):
        if trade.timestamp is None:
            return
        elif not plot.loaded:
            print("still fetching..")
            return
        print("> trade", trade)
        tradefetch.store_trade(trade)
        task.deferLater(reactor, 0, plot.load_from_db)

    def stop_accepting_trades(client):
        print("no more live trades for you")
        plot.loaded = False

    def start_accepting_trades(client):
        print("what is done is done")
        plot.load_from_db()
        plot.loaded = True

    # During loading from database, we will receive several 'partial_download'
    # events representing that some part of the requested data is already
    # available.
    mtgox_client.evt.listen('partial_download', lambda _: plot.load_from_db())
    # If the database has been loaded, trades from the streaming data will
    # be stored as they come in.
    mtgox_client.evt.listen('trade', store_new_trade)

    # Each time we connect, listen from trades.
    mtgox_client.call('subscribe_type', 'trades', on_event='connected')
    # After database is loaded, start storing new trades.
    mtgox_client.call(start_accepting_trades, on_event='done')
    # When we disconnect, we need to identify that we need to fetch
    # trades again once we reconnect.
    mtgox_client.call(stop_accepting_trades, on_event='disconnected')
    # Note that the object is listening for the 'connected' event
    # such that it will download the trades we missed while
    # disconnected and then emit a 'done' event after it has fetched
    # all such trades.
    #
    # Nice eh ?


    print('Showing GUI..')
    plot.show()
    plot.raise_()

    reactor.addSystemEventTrigger('after', 'shutdown', app.quit)
    reactor.run()