def test_subscribe_bars(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=BarSubscriptionType(bar_type=BarType.Time, bar_size=BarSize.D1), from_date=start_date, to_date=end_date) date_val = start_date expect_val = [] for i in range(1, 5): persistable = Bar( timestamp=DateUtils.date_to_unixtimemillis(date_val), type=BarType.Time, size=BarSize.D1, inst_id=10, open=18 + i, high=19 + i, low=17 + i, close=17.5 + i, vol=100) datastore.save_bar(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)
def test_subscribe_market_depths(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=MarketDepthSubscriptionType(provider_id='20'), from_date=start_date, to_date=end_date) date_val = start_date expect_val = [] for i in range(1, 5): persistable = MarketDepth( timestamp=DateUtils.date_to_unixtimemillis(date_val), inst_id=10, provider_id='20', position=10 + i, operation=MDOperation.Insert, side=MDSide.Ask, price=10.1 + i, size=20) datastore.save_market_depth(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)
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)
def sub_hist_data(broker, inst_id, day_ago): sub_key = HistDataSubscriptionKey( inst_id=inst_id, provider_id=Broker.IB, subscription_type=BarSubscriptionType(data_type=Bar, bar_size=BarSize.D1), from_date=(today - timedelta(days=day_ago)), to_date=today) broker.subscribe_mktdata(sub_key) return sub_key
def _subscribe_market_data(self, feed, instruments, subscription_types): for instrument in instruments: for subscription_type in subscription_types: if isinstance(self.app_config, BacktestingConfig): sub_key = HistDataSubscriptionKey( inst_id=instrument.inst_id, provider_id=self.app_config.feed_id, subscription_type=subscription_type, from_date=self.app_config.from_date, to_date=self.app_config.to_date) else: sub_key = SubscriptionKey( inst_id=instrument.inst_id, provider_id=self.app_config.feed_id, subscription_type=subscription_type) self.feed.subscribe_mktdata(sub_key)
exchange_df = pd.DataFrame({ "exch_id": ["NYSE"], "name": ["New York Stock Exchange"] }) mgr = MockRefDataManager(inst_df, ccy_df, exchange_df) feed = PandaH5DataFeed('/Users/jchan/workspace/data/Equity/US/etf.h5', ref_data_mgr=mgr) today = date.today() startDate = date(2011, 1, 1) sub_key0 = HistDataSubscriptionKey(inst_id=0, provider_id=PandaH5DataFeed.ID, subscription_type=BarSubscriptionType( data_type=Bar, bar_size=BarSize.D1), from_date=startDate, to_date=today) sub_key1 = HistDataSubscriptionKey(inst_id=1, provider_id=PandaH5DataFeed.ID, subscription_type=BarSubscriptionType( data_type=Bar, bar_size=BarSize.D1), from_date=startDate, to_date=today) sub_key2 = HistDataSubscriptionKey(inst_id=2, provider_id=PandaH5DataFeed.ID, subscription_type=BarSubscriptionType( data_type=Bar, bar_size=BarSize.D1), from_date=startDate,
"Volume": 10000 * np.ones(num_days) }) df2 = df2.set_index(keys="dates") df_dict = {'SPY': df0, 'VXX': df1, 'XLV': df2} feed = PandasMemoryDataFeed(df_dict, ref_data_mgr=mgr) # today = date.today() # sub_key = HistDataSubscriptionKey(inst_id=3, provider_id=PandasMemoryDataFeed.ID, data_type=Bar, bar_size=BarSize.D1, # from_date=datetime(2010, 1, 1), to_date=today) sub_key0 = HistDataSubscriptionKey(inst_id=0, provider_id=PandasMemoryDataFeed.ID, subscription_type=BarSubscriptionType( data_type=Bar, bar_size=BarSize.D1), from_date=dates[0], to_date=dates[-1]) sub_key1 = HistDataSubscriptionKey(inst_id=1, provider_id=PandasMemoryDataFeed.ID, subscription_type=BarSubscriptionType( data_type=Bar, bar_size=BarSize.D1), from_date=dates[0], to_date=dates[-1]) sub_key2 = HistDataSubscriptionKey(inst_id=2, provider_id=PandasMemoryDataFeed.ID, subscription_type=BarSubscriptionType( data_type=Bar, bar_size=BarSize.D1), from_date=dates[0],
def __init__(self): super(GoogleDataFeed, self).__init__(system='google') def id(self): return Feed.Google def process_row(self, index, row): inst = self.__ref_data_mgr.get_inst(symbol=row['Symbol']) return Bar(inst_id=inst.inst_id, timestamp=DateUtils.datetime_to_unixtimemillis(index), open=row['Open'], high=row['High'], low=row['Low'], close=row['Close'], vol=row['Volume'], size=row['BarSize']) if __name__ == "__main__": feed = YahooDataFeed() today = date.today() sub_key = HistDataSubscriptionKey(inst_id=3, provider_id=YahooDataFeed.ID, subscription_type=BarSubscriptionType(data_type=Bar, bar_size=BarSize.D1), from_date=datetime(2010, 1, 1), to_date=today) logger.setLevel(logging.DEBUG) eventLogger = EventLogger() feed.subscribe_mktdata(sub_key)
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=DateUtils.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=DateUtils.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=DateUtils.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=DateUtils.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=DateUtils.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=DateUtils.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=DateUtils.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)