def setUp(self):
     self.time = 9000000000
     self.simluation_clock = SimulationClock()
     self.simluation_clock.reset()
     self.simluation_clock.update_time(self.time)
     self.input = DataSeries()
     self.event_bus = BarAggregatorTest.DummyEventBus()
Esempio n. 2
0
 def setUp(self):
     self.simluation_clock = SimulationClock()
     self.realtime_clock = RealTimeClock()
     self.simluation_clock.reset()
     self.endtime = []
     self.simluation_clock.update_time(ClockTest.ts)
Esempio n. 3
0
class ClockTest(TestCase):
    ts = 1467870720000

    def sim_action(self, *arg):
        self.endtime.append(self.simluation_clock.now())

    def realtime_action(self, *arg):
        self.endtime.append(self.realtime_clock.now())
    

    def setUp(self):
        self.simluation_clock = SimulationClock()
        self.realtime_clock = RealTimeClock()
        self.simluation_clock.reset()
        self.endtime = []
        self.simluation_clock.update_time(ClockTest.ts)

    def test_simulation_clock_current_date_time_with_bar(self):
        timestamp = ClockTest.ts + 10
        bar = Bar(timestamp=timestamp)
        self.simluation_clock.on_bar(bar)
        self.assertEquals(timestamp, self.simluation_clock.now())

    def test_simulation_clock_current_date_time_with_quote(self):
        timestamp = ClockTest.ts + 10
        quote = Quote(timestamp=timestamp)
        self.simluation_clock.on_quote(quote)
        self.assertEquals(timestamp, self.simluation_clock.now())

    def test_simulation_clock_current_date_time_with_trade(self):
        timestamp = ClockTest.ts + 10
        trade = Trade(timestamp=timestamp)
        self.simluation_clock.on_trade(trade)
        self.assertEquals(timestamp, self.simluation_clock.now())

    def test_simulation_clock_now(self):
        self.assertEquals(ClockTest.ts, self.simluation_clock.now())

    def test_simulation_clock_reset(self):
        self.simluation_clock.reset()
        self.assertEquals(0, self.simluation_clock.now())

    def test_simulation_clock_schedule_absolute(self):
        self.simluation_clock.schedule_absolute(ClockTest.ts + 1000, self.sim_action)

        self.assertEquals([], self.endtime)

        self.simluation_clock.update_time(ClockTest.ts + 999)
        self.assertEquals([], self.endtime)

        self.simluation_clock.update_time(ClockTest.ts + 1000)
        self.assertEquals([ClockTest.ts + 1000], self.endtime)

        self.simluation_clock.update_time(ClockTest.ts + 1001)
        self.assertEquals([ClockTest.ts + 1000], self.endtime)

    def test_simulation_schedule_absolute_beyond_trigger_time(self):
        self.simluation_clock.schedule_absolute(ClockTest.ts + 1000, self.sim_action)

        self.assertEquals([], self.endtime)

        self.simluation_clock.update_time(ClockTest.ts + 5000)
        self.assertEquals([ClockTest.ts + 5000], self.endtime)

    def test_simulation_schedule_relative(self):
        self.simluation_clock.schedule_relative(datetime.timedelta(seconds=3), self.sim_action)

        self.assertEquals([], self.endtime)

        self.simluation_clock.update_time(ClockTest.ts + 1000)
        self.assertEquals([], self.endtime)

        self.simluation_clock.update_time(ClockTest.ts + 3000)
        self.assertEquals([ClockTest.ts + 3000], self.endtime)

        self.simluation_clock.update_time(ClockTest.ts + 4000)
        self.assertEquals([ClockTest.ts + 3000], self.endtime)

    def test_simulation_schedule_relative_beyond_trigger_time(self):
        self.simluation_clock.schedule_relative(3000, self.sim_action, )

        self.assertEquals([], self.endtime)

        self.simluation_clock.update_time(ClockTest.ts + 5000)
        self.assertEquals([ClockTest.ts + 5000], self.endtime)

    @nottest
    def test_timestamp_conversion(self):
        dt = datetime.datetime(year=2000, month=1, day=1, hour=7, minute=30, second=30)
        ts = DateUtils.datetime_to_unixtimemillis(dt)
        self.assertEqual(946683030000, ts)

        dt2 = DateUtils.unixtimemillis_to_datetime(ts)
        self.assertEquals(dt, dt2)

        dt3 = datetime.datetime.fromtimestamp(0)

        ts2 = DateUtils.datetime_to_unixtimemillis(dt3)
        dt4 = DateUtils.unixtimemillis_to_datetime(ts2)
        self.assertEquals(0, ts2)
        self.assertEquals(dt3, dt4)

    def test_real_time_clock_now(self):
        ts = DateUtils.datetime_to_unixtimemillis(datetime.datetime.now())
        ts2 = self.realtime_clock.now()
        self.assertTrue(abs(ts - ts2) <= 10)
        time.sleep(2)
        ts3 = self.realtime_clock.now()
        self.assertAlmostEqual(ts3 - ts2, 2000, -2)

    def test_real_time_clock_schedule_absolute(self):
        start = self.realtime_clock.now()
        dt = DateUtils.unixtimemillis_to_datetime(start)
        abs_time = dt + datetime.timedelta(seconds=1)
        self.realtime_clock.schedule_absolute(abs_time, self.realtime_action)
        self.assertEquals([], self.endtime)
        time.sleep(1.1)
        self.assertEquals(1, len(self.endtime))
        self.assertAlmostEqual(1000, self.endtime[0] - start, -2)

    def test_real_time_clock_schedule_relative(self):
        start = self.realtime_clock.now()
        print start
        print DateUtils.unixtimemillis_to_datetime(start)
        self.realtime_clock.schedule_relative(datetime.timedelta(seconds=1), self.realtime_action)
        self.assertEquals([], self.endtime)
        time.sleep(1.1)
        self.assertEquals(1, len(self.endtime))
        self.assertAlmostEqual(1000, self.endtime[0] - start, -2)

    def test_real_time_clock_now(self):
        s1 = gevent.core.time()
        s2 = datetime.datetime.fromtimestamp(s1)
        print s1, s2
        s3 = self.realtime_clock.now()
        s4 = DateUtils.unixtimemillis_to_datetime(s3)
        print s3, s4

        self.assertAlmostEqual(s1 * 1000, s3, -2)
Esempio n. 4
0
 def setUp(self):
     self.simluation_clock = SimulationClock()
     self.realtime_clock = RealTimeClock()
     self.simluation_clock.reset()
     self.endtime = []
     self.simluation_clock.update_time(ClockTest.ts)
Esempio n. 5
0
class ClockTest(TestCase):
    ts = 1467870720000

    def sim_action(self, *arg):
        self.endtime.append(self.simluation_clock.now())

    def realtime_action(self, *arg):
        self.endtime.append(self.realtime_clock.now())

    def setUp(self):
        self.simluation_clock = SimulationClock()
        self.realtime_clock = RealTimeClock()
        self.simluation_clock.reset()
        self.endtime = []
        self.simluation_clock.update_time(ClockTest.ts)

    def test_simulation_clock_current_date_time_with_bar(self):
        timestamp = ClockTest.ts + 10
        bar = Bar(timestamp=timestamp)
        self.simluation_clock.on_bar(bar)
        self.assertEquals(timestamp, self.simluation_clock.now())

    def test_simulation_clock_current_date_time_with_quote(self):
        timestamp = ClockTest.ts + 10
        quote = Quote(timestamp=timestamp)
        self.simluation_clock.on_quote(quote)
        self.assertEquals(timestamp, self.simluation_clock.now())

    def test_simulation_clock_current_date_time_with_trade(self):
        timestamp = ClockTest.ts + 10
        trade = Trade(timestamp=timestamp)
        self.simluation_clock.on_trade(trade)
        self.assertEquals(timestamp, self.simluation_clock.now())

    def test_simulation_clock_now(self):
        self.assertEquals(ClockTest.ts, self.simluation_clock.now())

    def test_simulation_clock_reset(self):
        self.simluation_clock.reset()
        self.assertEquals(0, self.simluation_clock.now())

    def test_simulation_clock_schedule_absolute(self):
        self.simluation_clock.schedule_absolute(ClockTest.ts + 1000,
                                                self.sim_action)

        self.assertEquals([], self.endtime)

        self.simluation_clock.update_time(ClockTest.ts + 999)
        self.assertEquals([], self.endtime)

        self.simluation_clock.update_time(ClockTest.ts + 1000)
        self.assertEquals([ClockTest.ts + 1000], self.endtime)

        self.simluation_clock.update_time(ClockTest.ts + 1001)
        self.assertEquals([ClockTest.ts + 1000], self.endtime)

    def test_simulation_schedule_absolute_beyond_trigger_time(self):
        self.simluation_clock.schedule_absolute(ClockTest.ts + 1000,
                                                self.sim_action)

        self.assertEquals([], self.endtime)

        self.simluation_clock.update_time(ClockTest.ts + 5000)
        self.assertEquals([ClockTest.ts + 5000], self.endtime)

    def test_simulation_schedule_relative(self):
        self.simluation_clock.schedule_relative(datetime.timedelta(seconds=3),
                                                self.sim_action)

        self.assertEquals([], self.endtime)

        self.simluation_clock.update_time(ClockTest.ts + 1000)
        self.assertEquals([], self.endtime)

        self.simluation_clock.update_time(ClockTest.ts + 3000)
        self.assertEquals([ClockTest.ts + 3000], self.endtime)

        self.simluation_clock.update_time(ClockTest.ts + 4000)
        self.assertEquals([ClockTest.ts + 3000], self.endtime)

    def test_simulation_schedule_relative_beyond_trigger_time(self):
        self.simluation_clock.schedule_relative(
            3000,
            self.sim_action,
        )

        self.assertEquals([], self.endtime)

        self.simluation_clock.update_time(ClockTest.ts + 5000)
        self.assertEquals([ClockTest.ts + 5000], self.endtime)

    @nottest
    def test_timestamp_conversion(self):
        dt = datetime.datetime(year=2000,
                               month=1,
                               day=1,
                               hour=7,
                               minute=30,
                               second=30)
        ts = DateUtils.datetime_to_unixtimemillis(dt)
        self.assertEqual(946683030000, ts)

        dt2 = DateUtils.unixtimemillis_to_datetime(ts)
        self.assertEquals(dt, dt2)

        dt3 = datetime.datetime.fromtimestamp(0)

        ts2 = DateUtils.datetime_to_unixtimemillis(dt3)
        dt4 = DateUtils.unixtimemillis_to_datetime(ts2)
        self.assertEquals(0, ts2)
        self.assertEquals(dt3, dt4)

    def test_real_time_clock_now(self):
        ts = DateUtils.datetime_to_unixtimemillis(datetime.datetime.now())
        ts2 = self.realtime_clock.now()
        self.assertTrue(abs(ts - ts2) <= 10)
        time.sleep(2)
        ts3 = self.realtime_clock.now()
        self.assertAlmostEqual(ts3 - ts2, 2000, -2)

    def test_real_time_clock_schedule_absolute(self):
        start = self.realtime_clock.now()
        dt = DateUtils.unixtimemillis_to_datetime(start)
        abs_time = dt + datetime.timedelta(seconds=1)
        self.realtime_clock.schedule_absolute(abs_time, self.realtime_action)
        self.assertEquals([], self.endtime)
        time.sleep(1.1)
        self.assertEquals(1, len(self.endtime))
        self.assertAlmostEqual(1000, self.endtime[0] - start, -2)

    def test_real_time_clock_schedule_relative(self):
        start = self.realtime_clock.now()
        print start
        print DateUtils.unixtimemillis_to_datetime(start)
        self.realtime_clock.schedule_relative(datetime.timedelta(seconds=1),
                                              self.realtime_action)
        self.assertEquals([], self.endtime)
        time.sleep(1.1)
        self.assertEquals(1, len(self.endtime))
        self.assertAlmostEqual(1000, self.endtime[0] - start, -2)

    def test_real_time_clock_now(self):
        s1 = gevent.core.time()
        s2 = datetime.datetime.fromtimestamp(s1)
        print s1, s2
        s3 = self.realtime_clock.now()
        s4 = DateUtils.unixtimemillis_to_datetime(s3)
        print s3, s4

        self.assertAlmostEqual(s1 * 1000, s3, -2)
class BarAggregatorTest(TestCase):
    class DummyEventBus:
        def __init__(self):
            self.items = []

        def reset(self):
            self.items = []

        def on_next(self, item):
            print item
            self.items.append(item)

    def setUp(self):
        self.time = 9000000000
        self.simluation_clock = SimulationClock()
        self.simluation_clock.reset()
        self.simluation_clock.update_time(self.time)
        self.input = DataSeries()
        self.event_bus = BarAggregatorTest.DummyEventBus()

    def update(self, input, data):
        self.simluation_clock.update_time(data.timestamp)
        self.input.add(data.to_dict())

    def test_time_bar_from_trade(self):
        agg = BarAggregator(data_bus=self.event_bus, clock=self.simluation_clock, inst_id=1, input=self.input)
        agg.start()
        self.assertEqual(0, len(self.event_bus.items))

        self.time += 10000
        t = Trade(timestamp=self.time, inst_id=1, price=20, size=200)
        self.update(self.input, t)
        self.assertEqual(1, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        # expect get a aggregated bar at 9000059999
        self.time += 49999
        t = Trade(timestamp=self.time, inst_id=1, price=10, size=200)
        self.update(self.input, t)

        items = self.event_bus.items
        self.assertEqual(1, len(items))
        self.assertEqual(0, agg.count())
        self.assertEqual(
            Bar(inst_id=1, begin_time=9000000000, timestamp=9000059999, type=1, size=60, open=20, high=20, low=10,
                close=10, vol=400, adj_close=0), items[0])

    def test_time_bar_from_bid(self):
        agg = BarAggregator(data_bus=self.event_bus, clock=self.simluation_clock, inst_id=1, input=self.input,
                            input_type=BarInputType.Bid)
        agg.start()
        self.assertEqual(0, len(self.event_bus.items))

        self.time += 10000
        t = Quote(timestamp=self.time, inst_id=1, bid=30, bid_size=100)
        self.update(self.input, t)
        self.assertEqual(1, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 10000
        t = Quote(timestamp=self.time, inst_id=1, bid=10, bid_size=200)
        self.update(self.input, t)
        self.assertEqual(2, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 10000
        t = Quote(timestamp=self.time, inst_id=1, bid=70, bid_size=300)
        self.update(self.input, t)
        self.assertEqual(3, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 29998
        t = Quote(timestamp=self.time, inst_id=1, bid=50, bid_size=400)
        self.update(self.input, t)
        self.assertEqual(4, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 3
        self.simluation_clock.update_time(self.time)
        items = self.event_bus.items
        self.assertEqual(1, len(items))
        self.assertEqual(0, agg.count())
        self.assertEqual(
            Bar(inst_id=1, begin_time=9000000000, timestamp=9000059999, type=1, size=60, open=30, high=70, low=10,
                close=50, vol=1000, adj_close=0), items[0])

    def test_time_bar_from_ask(self):
        agg = BarAggregator(data_bus=self.event_bus, clock=self.simluation_clock, inst_id=1, input=self.input,
                            input_type=BarInputType.Ask)
        agg.start()
        self.assertEqual(0, len(self.event_bus.items))

        self.time += 10000
        t = Quote(timestamp=self.time, inst_id=1, ask=30, ask_size=100)
        self.update(self.input, t)
        self.assertEqual(1, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 60000
        t = Quote(timestamp=self.time, inst_id=1, ask=70, ask_size=300)
        self.update(self.input, t)
        items = self.event_bus.items
        self.assertEqual(1, len(items))
        self.assertEqual(1, agg.count())
        self.assertEqual(
            Bar(inst_id=1, begin_time=9000000000, timestamp=9000059999, type=1, size=60, open=30, high=30, low=30,
                close=30, vol=100, adj_close=0), items[0])

        self.event_bus.reset()

        self.time += 49999
        t = Quote(timestamp=self.time, inst_id=1, ask=20, ask_size=100)
        self.update(self.input, t)
        items = self.event_bus.items
        self.assertEqual(1, len(items))
        self.assertEqual(0, agg.count())
        self.assertEqual(
            Bar(inst_id=1, begin_time=9000060000, timestamp=9000119999, type=1, size=60, open=70, high=70, low=20,
                close=20, vol=400, adj_close=0), items[0])

    def test_time_bar_from_bidask(self):
        agg = BarAggregator(data_bus=self.event_bus, clock=self.simluation_clock, inst_id=1, input=self.input,
                            input_type=BarInputType.BidAsk)
        agg.start()
        self.assertEqual(0, len(self.event_bus.items))

        self.time += 10000
        t = Quote(timestamp=self.time, inst_id=1, ask=30, ask_size=100, bid=0, bid_size=200)
        self.update(self.input, t)
        self.assertEqual(1, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 10000
        t = Quote(timestamp=self.time, inst_id=1, ask=20, ask_size=150, bid=10, bid_size=0)
        self.update(self.input, t)
        self.assertEqual(2, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 39999
        t = Quote(timestamp=self.time, inst_id=1, ask=70, ask_size=300, bid=80, bid_size=10)
        self.update(self.input, t)
        items = self.event_bus.items
        self.assertEqual(1, len(items))
        self.assertEqual(0, agg.count())
        self.assertEqual(
            Bar(inst_id=1, begin_time=9000000000, timestamp=9000059999, type=1, size=60, open=30, high=80, low=20,
                close=80, vol=260, adj_close=0), items[0])

    def test_time_bar_from_mid(self):
        agg = BarAggregator(data_bus=self.event_bus, clock=self.simluation_clock, inst_id=1, input=self.input,
                            input_type=BarInputType.Middle)
        agg.start()
        self.assertEqual(0, len(self.event_bus.items))

        self.time += 59999
        t = Quote(timestamp=self.time, inst_id=1, ask=30, ask_size=100, bid=18, bid_size=200)
        self.update(self.input, t)
        items = self.event_bus.items
        self.assertEqual(1, len(items))
        self.assertEqual(0, agg.count())
        self.assertEqual(
            Bar(inst_id=1, begin_time=9000000000, timestamp=9000059999, type=1, size=60, open=24, high=24, low=24,
                close=24, vol=150, adj_close=0), items[0])

    def test_tick_bar_from_trade(self):
        agg = BarAggregator(data_bus=self.event_bus, clock=self.simluation_clock, inst_id=1, input=self.input,
                            output_bar_type=BarType.Tick, output_size=3)
        agg.start()
        self.assertEqual(0, len(self.event_bus.items))

        self.time += 60000
        t = Trade(timestamp=self.time, inst_id=1, price=20, size=200)
        self.update(self.input, t)
        self.assertEqual(1, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 60000
        t = Trade(timestamp=self.time, inst_id=1, price=80, size=100)
        self.update(self.input, t)
        self.assertEqual(2, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 60000
        t = Trade(timestamp=self.time, inst_id=1, price=10, size=200)
        self.update(self.input, t)

        items = self.event_bus.items
        self.assertEqual(1, len(items))
        self.assertEqual(0, agg.count())
        self.assertEqual(
            Bar(inst_id=1, begin_time=9000060000, timestamp=9000180000, type=BarType.Tick, size=3, open=20, high=80,
                low=10,
                close=10, vol=500, adj_close=0), items[0])

    def test_vol_bar_from_trade(self):
        agg = BarAggregator(data_bus=self.event_bus, clock=self.simluation_clock, inst_id=1, input=self.input,
                            output_bar_type=BarType.Volume, output_size=1000)
        agg.start()
        self.assertEqual(0, len(self.event_bus.items))

        self.time += 60000
        t = Trade(timestamp=self.time, inst_id=1, price=20, size=200)
        self.update(self.input, t)
        self.assertEqual(1, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 60000
        t = Trade(timestamp=self.time, inst_id=1, price=80, size=100)
        self.update(self.input, t)
        self.assertEqual(2, agg.count())
        self.assertTrue(len(self.event_bus.items) == 0)

        self.time += 60000
        t = Trade(timestamp=self.time, inst_id=1, price=10, size=1000)
        self.update(self.input, t)

        items = self.event_bus.items
        self.assertEqual(1, len(items))
        self.assertEqual(1, agg.count())
        self.assertEqual(
            Bar(inst_id=1, begin_time=9000060000, timestamp=9000180000, type=BarType.Volume, size=1000, open=20,
                high=80, low=10,
                close=10, vol=1000, adj_close=0), items[0])

        self.event_bus.reset()

        self.time += 60000
        t = Trade(timestamp=self.time, inst_id=1, price=50, size=2800)
        self.update(self.input, t)

        items = self.event_bus.items
        self.assertEqual(3, len(items))
        self.assertEqual(1, agg.count())
        self.assertEqual(
            Bar(inst_id=1, begin_time=9000180000, timestamp=9000240000, type=BarType.Volume, size=1000, open=10,
                high=50, low=10,
                close=50, vol=1000, adj_close=0), items[0])
        self.assertEqual(
            Bar(inst_id=1, begin_time=9000240000, timestamp=9000240000, type=BarType.Volume, size=1000, open=50,
                high=50, low=50,
                close=50, vol=1000, adj_close=0), items[1])
        self.assertEqual(
            Bar(inst_id=1, begin_time=9000240000, timestamp=9000240000, type=BarType.Volume, size=1000, open=50,
                high=50, low=50,
                close=50, vol=1000, adj_close=0), items[2])

        self.event_bus.reset()

        self.time += 60000
        t = Trade(timestamp=self.time, inst_id=1, price=20, size=900)
        self.update(self.input, t)

        items = self.event_bus.items
        self.assertEqual(1, len(items))
        self.assertEqual(0, agg.count())
        self.assertEqual(
            Bar(inst_id=1, begin_time=9000240000, timestamp=9000300000, type=BarType.Volume, size=1000, open=50,
                high=50, low=20,
                close=20, vol=1000, adj_close=0), items[0])
Esempio n. 7
0
 def __get_clock(self):
     if self.app_config.clock_type == Clock.RealTime:
         return RealTimeClock()
     return SimulationClock()