예제 #1
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])
예제 #2
0
    def test_subscribe_quotes(self, name, datastore):
        start_date = date(2011, 1, 1)
        end_date = date(2011, 1, 5)
        sub_key = HistDataSubscriptionKey(
            inst_id=10,
            provider_id=Broker.IB,
            subscription_type=QuoteSubscriptionType(),
            from_date=start_date,
            to_date=end_date)

        date_val = start_date

        expect_val = []
        for i in range(1, 5):
            persistable = Quote(timestamp=date_to_unixtimemillis(date_val),
                                bid=18 + i,
                                ask=19 + i,
                                bid_size=200,
                                ask_size=500,
                                inst_id=10)
            datastore.save_quote(persistable)
            expect_val.append(persistable)
            date_val = date_val + timedelta(days=1)

        actual_val = datastore.load_mktdata(sub_key)
        self.assertEqual(expect_val, actual_val)
예제 #3
0
    def get_latest_price(self):
        price = self.inst_data_mgr.get_latest_price(1)
        self.assertIsNone(price)

        bar1 = Bar(inst_id=1, open=20, high=21, low=19, close=20.5)
        self.inst_data_mgr.on_bar(bar1)
        price = self.inst_data_mgr.get_latest_price(1)
        self.assertEqual(20.5, price)

        bar1 = Bar(inst_id=1,
                   open=20,
                   high=21,
                   low=19,
                   close=20.5,
                   adj_close=22)
        self.inst_data_mgr.on_bar(bar1)
        price = self.inst_data_mgr.get_latest_price(1)
        self.assertEqual(22, price)

        quote1 = Quote(inst_id=1, bid=18, ask=19, bid_size=200, ask_size=500)
        self.inst_data_mgr.on_quote(quote1)
        price = self.inst_data_mgr.get_latest_price(1)
        self.assertEqual(18.5, price)

        trade1 = Trade(inst_id=1, price=20, size=200)
        self.inst_data_mgr.on_bar(trade1)
        price = self.inst_data_mgr.get_latest_price(1)
        self.assertEqual(20, price)
예제 #4
0
    def test_quote_processor(self):
        config = SimConfig()
        processor = QuoteProcessor()

        order = NewOrderRequest(cl_id='test',
                                cl_ord_id=1,
                                inst_id=1,
                                action=OrdAction.BUY,
                                type=OrdType.LIMIT,
                                qty=1000,
                                limit_price=18.5)
        quote = Quote(bid=18, ask=19, bid_size=200, ask_size=500)

        self.assertEqual(19, processor.get_price(order, quote, config))
        self.assertEqual(500, processor.get_qty(order, quote, config))

        order2 = NewOrderRequest(cl_id='test',
                                 cl_ord_id=2,
                                 inst_id=1,
                                 action=OrdAction.SELL,
                                 type=OrdType.LIMIT,
                                 qty=1000,
                                 limit_price=18.5)
        self.assertEqual(18, processor.get_price(order2, quote, config))
        self.assertEqual(200, processor.get_qty(order2, quote, config))
예제 #5
0
 def test_quote(self, name, serializer):
     item = Quote(bid=18,
                  ask=19,
                  bid_size=200,
                  ask_size=500,
                  inst_id=1,
                  timestamp=datetime.datetime.now())
     SerializerTest.ser_deser(name, serializer, item)
예제 #6
0
    def test_get_quote(self):
        quote = self.inst_data_mgr.get_quote(1)
        self.assertIsNone(quote)

        quote1 = Quote(inst_id=1, bid=18, ask=19, bid_size=200, ask_size=500)
        self.inst_data_mgr.on_quote(quote1)
        quote = self.inst_data_mgr.get_quote(1)
        self.assertEqual(quote1, quote)
예제 #7
0
    def load_quotes(self, sub_key):
        from_timestamp = DateUtils.date_to_unixtimemillis(sub_key.from_date)
        to_timestamp = DateUtils.date_to_unixtimemillis(sub_key.to_date)

        bound_stmt = self.query_quotes_stmt.bind([sub_key.inst_id, from_timestamp, to_timestamp])
        result_list = self.session.execute(bound_stmt)

        return [Quote(inst_id=r.inst_id, timestamp=r.timestamp, bid=r.bid, ask=r.ask, bid_size=r.bid_size,
                      ask_size=r.ask_size) for r in result_list]
예제 #8
0
 def test_quote(self, name, datastore):
     persistable = Quote(timestamp=clock.now(),
                         bid=18,
                         ask=19,
                         bid_size=200,
                         ask_size=500,
                         inst_id=999)
     DataStoreTest.save_load(name, persistable, datastore,
                             datastore.save_quote, 'quotes')
예제 #9
0
    def test_market_order_handler(self):
        handler = MarketOrderHandler(self.config)

        order = NewOrderRequest(cl_id='test', cl_ord_id=1, inst_id=1, action=OrdAction.BUY, type=OrdType.LIMIT,
                                qty=1000, limit_price=18.5)

        quote = Quote(inst_id=1, bid=18, ask=19, bid_size=200, ask_size=500)
        trade = Trade(inst_id=1, price=20, size=200)

        fill_info = handler.process_w_price_qty(order, 18, 500)
        self.assertEquals(18, fill_info.fill_price)
        self.assertEquals(500, fill_info.fill_qty)
예제 #10
0
    def __emit_market_data(self, field, record):
        if record.quote_req and (
                                field == swigibpy.BID or field == swigibpy.BID_SIZE or field == swigibpy.ASK or field == swigibpy.ASK_SIZE) and record.bid > 0 and record.ask > 0:
            self.data_event_bus.on_next(Quote(inst_id=record.inst_id, timestamp=self.app_context.clock.now(),
                                              bid=record.bid,
                                              bid_size=record.bid_size,
                                              ask=record.ask,
                                              ask_size=record.ask_size))

        if record.trade_req and (field == swigibpy.LAST or field == swigibpy.LAST_SIZE) and record.last > 0:
            self.data_event_bus.on_next(
                Trade(inst_id=record.inst_id, timestamp=self.app_context.clock.now(), price=record.last,
                      size=record.last_size))
예제 #11
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])
예제 #12
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])
예제 #13
0
    def test_on_market_date_update(self):

        ord_req = NewOrderRequest(cl_id='test', cl_ord_id=1, portf_id="test", broker_id="Dummy", inst_id=1,
                                  action=OrdAction.BUY, type=OrdType.LIMIT, qty=1000, limit_price=18.5,
                                  timestamp=0)
        order1 = self.portfolio.on_new_ord_req(ord_req)

        er1 = ExecutionReport(cl_id='test', cl_ord_id=1, ord_id=1, er_id=1, inst_id=1, last_qty=500, last_price=18.4,
                              status=OrdStatus.PARTIALLY_FILLED, timestamp=1)
        self.app_context.order_mgr.on_exec_report(er1)

        expected_cash = 100000 - 500 * 18.4
        expected_stock_value = 500 * 18.4
        expected_total_equity = expected_cash + expected_stock_value

        self.assertEqual(expected_cash, self.portfolio.cash)
        self.assertEqual(expected_stock_value, self.portfolio.performance_series.get_by_idx(0, 'stock_value'))
        self.assertEqual(expected_total_equity, self.portfolio.performance_series.get_by_idx(0, 'total_equity'))

        self.portfolio.on_trade(Trade(inst_id=1, price=20, size=1000, timestamp=2))
        expected_cash = 100000 - 500 * 18.4
        expected_stock_value = 500 * 20
        expected_total_equity = expected_cash + expected_stock_value
        self.assertEqual(expected_cash, self.portfolio.cash)
        self.assertEqual(expected_stock_value, self.portfolio.performance_series.get_by_idx(1, 'stock_value'))
        self.assertEqual(expected_total_equity, self.portfolio.performance_series.get_by_idx(1, 'total_equity'))

        self.portfolio.on_bar(Bar(inst_id=1, close=16, adj_close=16, vol=1000, timestamp=3))
        expected_cash = 100000 - 500 * 18.4
        expected_stock_value = 500 * 16
        expected_total_equity = expected_cash + expected_stock_value
        self.assertEqual(expected_cash, self.portfolio.cash)
        self.assertEqual(expected_stock_value, self.portfolio.performance_series.get_by_idx(2, 'stock_value'))
        self.assertEqual(expected_total_equity, self.portfolio.performance_series.get_by_idx(2, 'total_equity'))

        self.portfolio.on_quote(Quote(inst_id=1, bid=16, ask=18, timestamp=4))
        expected_cash = 100000 - 500 * 18.4
        expected_stock_value = 500 * 17
        expected_total_equity = expected_cash + expected_stock_value
        self.assertEqual(expected_cash, self.portfolio.cash)
        self.assertEqual(expected_stock_value, self.portfolio.performance_series.get_by_idx(3, 'stock_value'))
        self.assertEqual(expected_total_equity, self.portfolio.performance_series.get_by_idx(3, 'total_equity'))
예제 #14
0
    def load_all(self, clazz):
        result_list = self.session.execute("select * from %s " % clazz)

        if clazz == 'sequences':
            return {result.id: result.seq for result in result_list}
        else:
            results = []
            for r in result_list:
                if clazz == 'bars':
                    obj = Bar(inst_id=r.inst_id, type=r.type, size=r.size, begin_time=r.begin_time,
                              timestamp=r.timestamp,
                              open=r.open, high=r.high, low=r.low, close=r.close, vol=r.vol, adj_close=r.adj_close)
                elif clazz == 'trades':
                    obj = Trade(inst_id=r.inst_id, timestamp=r.timestamp, price=r.price, size=r.size)
                elif clazz == 'quotes':
                    obj = Quote(inst_id=r.inst_id, timestamp=r.timestamp, bid=r.bid, ask=r.ask, bid_size=r.bid_size,
                                ask_size=r.ask_size)
                elif clazz == 'market_depths':
                    obj = MarketDepth(inst_id=r.inst_id, provider_id=r.provider_id, timestamp=r.timestamp,
                                      position=r.position, operation=r.operation, side=r.side, price=r.price,
                                      size=r.size)
                elif clazz == 'instruments':
                    obj = Instrument(inst_id=r.inst_id, name=r.name, type=r.type, symbol=r.symbol, exch_id=r.exch_id,
                                     ccy_id=r.ccy_id, alt_symbol=r.alt_symbol, alt_exch_id=r.alt_exch_id,
                                     sector=r.sector,
                                     industry=r.industry, und_inst_id=r.und_inst_id, expiry_date=r.expiry_date,
                                     factor=r.factor,
                                     strike=r.strike, put_call=r.put_call, margin=r.margin)
                elif clazz == 'currencies':
                    obj = Currency(ccy_id=r.ccy_id, name=r.name)
                elif clazz == 'exchanges':
                    obj = Exchange(exch_id=r.exch_id, name=r.name)
                else:
                    obj = self.serializer.deserialize(r.data)
                results.append(obj)

            return results
예제 #15
0
    def test_multi_subscriptions(self, name, datastore):
        start_date = date(2011, 1, 1)
        end_date = date(2011, 1, 5)

        sub_key1 = HistDataSubscriptionKey(
            inst_id=99,
            provider_id=Broker.IB,
            subscription_type=BarSubscriptionType(bar_type=BarType.Time,
                                                  bar_size=BarSize.D1),
            from_date=start_date,
            to_date=end_date)

        sub_key2 = HistDataSubscriptionKey(
            inst_id=99,
            provider_id=Broker.IB,
            subscription_type=QuoteSubscriptionType(),
            from_date=start_date,
            to_date=end_date)

        sub_key3 = HistDataSubscriptionKey(
            inst_id=99,
            provider_id=Broker.IB,
            subscription_type=TradeSubscriptionType(),
            from_date=start_date,
            to_date=end_date)

        expect_val = []

        # out of range
        persistable = Bar(timestamp=date_to_unixtimemillis(date(2010, 12, 31)),
                          type=BarType.Time,
                          size=BarSize.D1,
                          inst_id=99,
                          open=18,
                          high=19,
                          low=17,
                          close=17.5,
                          vol=100)
        datastore.save_bar(persistable)

        persistable = Bar(timestamp=date_to_unixtimemillis(date(2011, 1, 1)),
                          type=BarType.Time,
                          size=BarSize.D1,
                          inst_id=99,
                          open=28,
                          high=29,
                          low=27,
                          close=27.5,
                          vol=100)
        datastore.save_bar(persistable)
        expect_val.append(persistable)

        persistable = Trade(timestamp=date_to_unixtimemillis(date(2011, 1, 2)),
                            price=20,
                            size=200,
                            inst_id=99)
        datastore.save_trade(persistable)
        expect_val.append(persistable)

        persistable = Trade(timestamp=date_to_unixtimemillis(date(2011, 1, 3)),
                            price=30,
                            size=200,
                            inst_id=99)
        datastore.save_trade(persistable)
        expect_val.append(persistable)

        # not same instrument
        persistable = Quote(timestamp=date_to_unixtimemillis(date(2011, 1, 3)),
                            bid=18,
                            ask=19,
                            bid_size=200,
                            ask_size=500,
                            inst_id=11)
        datastore.save_quote(persistable)

        persistable = Quote(timestamp=date_to_unixtimemillis(date(2011, 1, 4)),
                            bid=18,
                            ask=19,
                            bid_size=200,
                            ask_size=500,
                            inst_id=99)
        datastore.save_quote(persistable)
        expect_val.append(persistable)

        # out of range
        persistable = Quote(timestamp=date_to_unixtimemillis(date(2011, 1, 5)),
                            bid=28,
                            ask=29,
                            bid_size=200,
                            ask_size=500,
                            inst_id=99)
        datastore.save_quote(persistable)

        actual_val = datastore.load_mktdata(sub_key1, sub_key2, sub_key3)
        self.assertEqual(expect_val, actual_val)
예제 #16
0
 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())
예제 #17
0
import zerorpc

from algotrader.event.market_data import Bar, Quote, Trade
from algotrader.event.order import NewOrderRequest, OrdAction, OrdType

c = zerorpc.Client()
c.connect("tcp://127.0.0.1:14242")

bar = Bar(open=18, high=19, low=17, close=17.5, vol=1000)
quote = Quote(bid=18, ask=19, bid_size=200, ask_size=500)
trade = Trade(price=20, size=200)
order = NewOrderRequest(ord_id=1,
                        inst_id=1,
                        action=OrdAction.BUY,
                        type=OrdType.LIMIT,
                        qty=1000,
                        limit_price=18.5)

print c.on_order(order)