Exemple #1
0
def main(opts, args, api, streamer, searcher):
    if not args:
        print 'Need at least one symbol to lookup.'
        return

    for i in xrange(len(args)):
        args[i] += opts.suffix or ''

    stdout = make_writer(sys.stdout)
    stdout.writerow(HEADER)
    market_writer_map = {}

    def dump(price):
        if not price['MarketId']:
            return
        s, market = markets[price['MarketId']]
        row = price_to_row(market, price)
        stdout.writerow(row)
        market_writer_map[price['MarketId']].writerow(row)

    markets, unknown = base.threaded_lookup(searcher, args)
    if unknown:
        print '# Unknown:', ', '.join(unknown)

    for market_id, (ric, market) in markets.iteritems():
        fp = file(base.filename_for(opts, market, kind='ticks'), 'a', 1)
        writer = csv.writer(fp, quoting=csv.QUOTE_ALL)
        market_writer_map[market_id] = writer
        if os.path.getsize(fp.name) == 0:
            writer.writerow(HEADER)
        streamer.prices.listen(dump, market_id)

    raw_input()
    streamer.stop()
Exemple #2
0
def main(opts, args, api, streamer, searcher):
    if not args:
        print 'Need at least one symbol to lookup.'
        return

    for i in xrange(len(args)):
        args[i] += opts.suffix or ''

    def fetch(market):
        bars = api.market_bars(market['MarketId'],
            interval=opts.interval.upper(),
            span=opts.span, bars=opts.bars)['PriceBars']

        if opts.raw:
            pad_count = 0
        else:
            orig = len(bars)
            bars = pad_bars(bars, interval=opts.span * 60)
            pad_count = len(bars) - orig
            if opts.chop:
                bars = bars[-opts.bars:]

        filename = base.filename_for(opts, market,
            kind='bars_%s%s' % (opts.span, opts.interval[0].upper()))

        with file(filename, 'w') as fp:
            writer = csv.writer(fp, quoting=csv.QUOTE_ALL)
            write = writer.writerow
            write(('UtcTime', 'Open', 'High', 'Low', 'Close'))
            for bar in bars:
                write((tsformat(bar),
                      bar['Open'], bar['High'], bar['Low'],
                      bar['Close']))

        print 'Wrote %d bars for %d/%s to %r (padded:%d, first:%r, last:%r)' %\
            (len(bars), market['MarketId'], market['Name'], filename,
             pad_count, bars and tsformat(bars[0]),
                        bars and tsformat(bars[-1]))

    markets, unknown = base.threaded_lookup(searcher, args)
    for unk in unknown:
        print 'Can\'t find %r' % (unk,)

    tp = base.ThreadPool()
    for s, market in markets.values():
        tp.put(fetch, market)
    tp.join()
Exemple #3
0
def main(opts, args, api, streamer, searcher):
    if not args:
        print 'Need at least one symbol to lookup.'
        return

    for i in xrange(len(args)):
        args[i] += opts.suffix or ''

    writer = csv.writer(sys.stdout, quoting=csv.QUOTE_ALL)
    lock = threading.Lock()

    def write(*row):
        with lock:
            writer.writerow(row)

    markets, unknown = base.threaded_lookup(searcher, args)
    write('RIC', 'MarketId', 'Description')
    for unk in unknown:
        write(unk.upper(), '-', '-')
    for ric, market in markets.itervalues():
        write(ric.upper(), market['MarketId'], market['Name'])
Exemple #4
0
def main(opts, args, api, streamer, searcher):
    if not args:
        print 'Need at least one symbol to lookup.'
        return

    for i in xrange(len(args)):
        args[i] += opts.suffix or ''

    tailer = LogTailer()

    button = urwid.Button(u'Exit')
    div = urwid.Divider()
    pile = urwid.Pile([button, div, tailer.text])
    top = urwid.Filler(pile, valign='top')

    markets = None
    data = {}

    def on_exit_clicked(button):
        raise urwid.ExitMainLoop()

    urwid.connect_signal(button, 'click', on_exit_clicked)

    table = Table(lambda price: price['MarketId'])
    table.add_field('TickDate', (lambda _, cur: tsformat(cur)), width=8)
    table.add_field('Name', (lambda last, cur: cur), width=35)
    for field in 'Price', 'Spread', 'SprdPct', 'Bid', 'Offer', 'High', 'Low', 'Change':
        table.add_field(field, (lambda _, cur: cur), width=9)
    table.end_fields()

    status = urwid.Text('', align='right')
    pp = urwid.Pile([status, urwid.Divider(), table._columns, urwid.Divider(), pile])
    top = urwid.Filler(pp, valign='top')
    loop = urwid.MainLoop(top, PALETTE)

    waker = base.ThreadWaker()
    loop.watch_file(waker.fileno(), waker.callback)

    def redraw_state(map):
        bits = []
        attrmap = {lightstreamer.STATE_CONNECTING: 'reconnecting',
                   lightstreamer.STATE_CONNECTED: 'online',
                   lightstreamer.STATE_DISCONNECTED: 'offline',
                   lightstreamer.STATE_RECONNECTING: 'reconnecting'}

        for short, name in ('TRADING', cityindex.AS_TRADING), \
                           ('STREAMING', cityindex.AS_STREAMING), \
                           ('ACCOUNT', cityindex.AS_ACCOUNT):
            attr = attrmap.get(map.get(name))
            bits.append((attr, short + '   '))
        status.set_text(bits)

    streamer.on_state(lambda map: waker.put(redraw_state, map))

    def on_price_update(price):
        if price['MarketId']:
            ric, market = markets[price['MarketId']]
            price['RIC'] = ric.upper()
            price['Spread'] = price['Bid'] - price['Offer']
            price['SprdPct'] = (price['Spread'] / price['Price']) * 100
            waker.put(table.update, price)

    markets, unknown = base.threaded_lookup(searcher, args)
    for market_id, (ric, market) in markets.iteritems():
        table.append({'MarketId': market_id})
        streamer.prices.listen(on_price_update, market_id)

    loop.run()
    streamer.stop()