Example #1
0
def test():
    """ A basic test that creates and encodes dumpfiles in all supported
    encodings, and decodes them and compares the output."""

    from time import time
    from oanda.prices import f2i
    from itertools import izip

    def ntime(): return int(time()*1000)

    messages = [
        (ntime(), ntime(), 'O', 'EUR/USD', f2i(1.47019), f2i(1.47020)),
        (ntime(), ntime(), 'O', 'USD/CAD', f2i(1.0602), f2i(1.0606)),
        (ntime(), ntime(), 'O', 'XAU/USD', f2i(804.03), f2i(804.24)),
        (ntime(), ntime(), 'O', 'EUR/USD', f2i(1.47010), f2i(1.47012)),
        (ntime(), ntime(), 'O', 'EUR/USD', f2i(1.47009), f2i(1.47012)),
        ]

    for codec in sorted(_codecs):
        print 'Testing: %s' % codec
        fn = '/tmp/testdump.%s' % codec

        # Open a new file and create some material in the specific encoding.
        f = open(fn, 'wb')
        encode, decode, msgsize = getcodec(codec)
        for data in messages:
            packet = encode(*data)
            ## trace(len(packet))
            f.write(packet)
        f.close()

        dumpf = opendump(fn)
        assert dumpf.codecname == codec
        for msg, u in izip(messages, dumpf):
            assert tuple(u) == msg, u
Example #2
0
    def handle(self, ei, _):
        """ Convert the data and dispatch it to the clients. """
        tick = ei.tick
        bid, ask = f2i(tick.bid), f2i(tick.ask)
        ts = sec2milli(tick.timestamp)

        # Reschedule kill call for a later time.
        if self.killsecs:
            if self.d is not None:
                self.d.cancel()
            self.d = reactor.callLater(self.killsecs, self.on_idle)

        self.dispatch.event(ts, ei.pair, bid, ask)
Example #3
0
    def handle(self, ei, _):
        """ Convert the data and dispatch it to the clients. """
        tick = ei.tick
        bid, ask = f2i(tick.bid), f2i(tick.ask)
        ts = sec2milli(tick.timestamp)

        # Reschedule kill call for a later time.
        if self.killsecs:
            if self.d is not None:
                self.d.cancel()
            self.d = reactor.callLater(self.killsecs, self.on_idle)

        self.dispatch.event(ts, ei.pair, bid, ask)
Example #4
0
    def gen(self):
        spair = str(self.pair)
        xrates = self.factory.xrates

        ts = sec2milli(time())
        if spair in xrates:
            (_, bid, ask) = xrates[spair]
        else:
            bid, ask = f2i(self.init_bid), f2i(self.init_ask)

        delta = f2i(self.mininc) * randint(-2, +2)
        bid += delta
        ask += delta

        self.m.dispatch.event(ts, self.pair, bid, ask)

        if not self.stopping:
            reactor.callLater(random() * self.tdelay, self.gen)
Example #5
0
    def gen(self):
        spair = str(self.pair)
        xrates = self.factory.xrates

        ts = sec2milli(time())
        if spair in xrates:
            (_, bid, ask) = xrates[spair]
        else:
            bid, ask = f2i(self.init_bid), f2i(self.init_ask)

        delta = f2i(self.mininc) * randint(-2, +2)
        bid += delta
        ask += delta

        self.m.dispatch.event(ts, self.pair, bid, ask)

        if not self.stopping:
            reactor.callLater(random() * self.tdelay, self.gen)
Example #6
0
def test():
    """ A basic test that creates and encodes dumpfiles in all supported
    encodings, and decodes them and compares the output."""

    from time import time
    from oanda.prices import f2i
    from itertools import izip

    def ntime():
        return int(time() * 1000)

    messages = [
        (ntime(), ntime(), 'O', 'EUR/USD', f2i(1.47019), f2i(1.47020)),
        (ntime(), ntime(), 'O', 'USD/CAD', f2i(1.0602), f2i(1.0606)),
        (ntime(), ntime(), 'O', 'XAU/USD', f2i(804.03), f2i(804.24)),
        (ntime(), ntime(), 'O', 'EUR/USD', f2i(1.47010), f2i(1.47012)),
        (ntime(), ntime(), 'O', 'EUR/USD', f2i(1.47009), f2i(1.47012)),
    ]

    for codec in sorted(_codecs):
        print 'Testing: %s' % codec
        fn = '/tmp/testdump.%s' % codec

        # Open a new file and create some material in the specific encoding.
        f = open(fn, 'wb')
        encode, decode, msgsize = getcodec(codec)
        for data in messages:
            packet = encode(*data)
            ## trace(len(packet))
            f.write(packet)
        f.close()

        dumpf = opendump(fn)
        assert dumpf.codecname == codec
        for msg, u in izip(messages, dumpf):
            assert tuple(u) == msg, u