Beispiel #1
0
 def ingest(
     environ: Mapping,
     asset_db_writer: AssetDBWriter,
     minute_bar_writer: BcolzMinuteBarWriter,
     daily_bar_writer: BcolzDailyBarWriter,
     adjustment_writer: SQLiteAdjustmentWriter,
     calendar: TradingCalendar,
     start_session: pd.Timestamp,
     end_session: pd.Timestamp,
     cache: dataframe_cache,
     show_progress: bool,
     output_dir: Text,
 ) -> NoReturn:
     sid_map = list(zip(range(len(symbols)), symbols))
     asset_db_writer.write(
         futures=_get_metadata(sid_map),
         exchanges=pd.DataFrame(data=[['bitmex', 'UTC']],
                                columns=['exchange', 'timezone']),
     )
     minute_bar_writer.write(
         _get_minute_bars(sid_map, start_session, end_session, cache),
         show_progress=show_progress,
     )
     daily_bar_writer.write(
         _get_daily_bars(sid_map, start_session, end_session, cache),
         show_progress=show_progress,
     )
Beispiel #2
0
class TestWrite(WithInstanceTmpDir, ZiplineTestCase):
    def init_instance_fixtures(self):
        super(TestWrite, self).init_instance_fixtures()
        self.assets_db_path = path = os.path.join(
            self.instance_tmpdir.path,
            'assets.db',
        )
        self.writer = AssetDBWriter(path)

    def new_asset_finder(self):
        return AssetFinder(self.assets_db_path)

    def test_write_multiple_exchanges(self):
        # Incrementing by two so that start and end dates for each
        # generated Asset don't overlap (each Asset's end_date is the
        # day after its start date).
        dates = pd.date_range('2013-01-01', freq='2D', periods=5, tz='UTC')
        sids = list(range(5))
        df = pd.DataFrame.from_records([
            {
                'sid': sid,
                'real_sid': str(sid),
                'currency': 'USD',
                'symbol': str(sid),
                'start_date': date.value,
                'end_date': (date + timedelta(days=1)).value,

                # Change the exchange with each mapping period. We don't
                # currently support point in time exchange information,
                # so we just take the most recent by end date.
                'exchange': 'EXCHANGE-%d-%d' % (sid, n),
            } for n, date in enumerate(dates) for sid in sids
        ])
        self.writer.write(equities=df)

        reader = self.new_asset_finder()
        equities = reader.retrieve_all(reader.sids)

        for eq in equities:
            expected_exchange = 'EXCHANGE-%d-%d' % (eq.sid, len(dates) - 1)
            assert_equal(eq.exchange, expected_exchange)

    def test_write_direct(self):
        # don't include anything with a default to test that those work.
        equities = pd.DataFrame({
            'sid': [0, 1],
            'real_sid': ['0', '1'],
            'currency': ['USD', 'CAD'],
            'asset_name': ['Ayy Inc.', 'Lmao LP'],
            # the full exchange name
            'exchange': ['NYSE', 'TSE'],
        })
        equity_symbol_mappings = pd.DataFrame({
            'sid': [0, 1],
            'symbol': ['AYY', 'LMAO'],
            'company_symbol': ['AYY', 'LMAO'],
            'share_class_symbol': ['', ''],
        })
        exchanges = pd.DataFrame({
            'exchange': ['NYSE', 'TSE'],
            'country_code': ['US', 'JP'],
        })

        self.writer.write_direct(
            equities=equities,
            equity_symbol_mappings=equity_symbol_mappings,
            exchanges=exchanges,
        )

        reader = self.new_asset_finder()

        equities = reader.retrieve_all(reader.sids)
        expected_equities = [
            Equity(
                0,
                '0',
                ExchangeInfo('NYSE', 'NYSE', 'US'),
                currency='USD',
                symbol='AYY',
                asset_name='Ayy Inc.',
                start_date=pd.Timestamp(0, tz='UTC'),
                end_date=pd.Timestamp.max.tz_localize('UTC'),
                first_traded=None,
                auto_close_date=None,
                tick_size=0.01,
                multiplier=1.0,
            ),
            Equity(
                1,
                '1',
                ExchangeInfo('TSE', 'TSE', 'JP'),
                currency='CAD',
                symbol='LMAO',
                asset_name='Lmao LP',
                start_date=pd.Timestamp(0, tz='UTC'),
                end_date=pd.Timestamp.max.tz_localize('UTC'),
                first_traded=None,
                auto_close_date=None,
                tick_size=0.01,
                multiplier=1.0,
            )
        ]
        assert_equal(equities, expected_equities)

        exchange_info = reader.exchange_info
        expected_exchange_info = {
            'NYSE': ExchangeInfo('NYSE', 'NYSE', 'US'),
            'TSE': ExchangeInfo('TSE', 'TSE', 'JP'),
        }
        assert_equal(exchange_info, expected_exchange_info)