예제 #1
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])
예제 #2
0
    def load_trades(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_trades_stmt.bind([sub_key.inst_id, from_timestamp, to_timestamp])
        result_list = self.session.execute(bound_stmt)
        return [Trade(inst_id=r.inst_id, timestamp=r.timestamp, price=r.price, size=r.size) for r in result_list]
예제 #3
0
def traderRowParser(row):
    if row[1][0] == 'T':
        volPrice = row[1][1:]
        volume, price = re.split('@', volPrice)
        return Trade(instrument='Test', timestamp=datetime.strptime(row[0], '%Y%m%d%H%M%S%f'), price=price, size=volume)
    else:
        return None
예제 #4
0
    def test_subscribe_trades(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=TradeSubscriptionType(),
            from_date=start_date,
            to_date=end_date)

        date_val = start_date

        expect_val = []
        for i in range(1, 5):
            persistable = Trade(
                timestamp=DateUtils.date_to_unixtimemillis(date_val),
                price=20 + i,
                size=200 + i,
                inst_id=10)
            datastore.save_trade(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)
예제 #5
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)
예제 #6
0
 def test_trade(self, name, datastore):
     persistable = Trade(timestamp=clock.now(),
                         price=20,
                         size=200,
                         inst_id=999)
     DataStoreTest.save_load(name, persistable, datastore,
                             datastore.save_trade, 'trades')
예제 #7
0
    def test_get_trade(self):
        trade = self.inst_data_mgr.get_trade(1)
        self.assertIsNone(trade)

        trade1 = Trade(inst_id=1, price=20, size=200)
        self.inst_data_mgr.on_trade(trade1)
        trade = self.inst_data_mgr.get_trade(1)
        self.assertEqual(trade1, trade)
예제 #8
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)
예제 #9
0
    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])
예제 #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_trader_processor(self):
        config = SimConfig()
        processor = TradeProcessor()

        order = NewOrderRequest(cl_id='test',
                                cl_ord_id=1,
                                inst_id=1,
                                action=OrdAction.BUY,
                                type=OrdType.LIMIT,
                                qty=1000,
                                limit_price=18.5)
        trade = Trade(price=20, size=200)

        self.assertEqual(20, processor.get_price(order, trade, config))
        self.assertEqual(200, processor.get_qty(order, trade, config))
예제 #12
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'))
예제 #13
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
예제 #14
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)
예제 #15
0
 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())
예제 #16
0
 def test_trade(self, name, serializer):
     item = Trade(price=20,
                  size=200,
                  inst_id=1,
                  timestamp=datetime.datetime.now())
     SerializerTest.ser_deser(name, serializer, item)
예제 #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)