コード例 #1
0
ファイル: test_api_shim.py プロジェクト: AdaoSmith/zipline
    def create_adjustments_reader(cls):
        path = cls.tempdir.getpath("test_adjustments.db")

        adj_writer = SQLiteAdjustmentWriter(
            path,
            cls.env.trading_days,
            MockDailyBarReader()
        )

        splits = pd.DataFrame([
            {
                'effective_date': str_to_seconds("2016-01-06"),
                'ratio': 0.5,
                'sid': cls.asset3.sid
            }
        ])

        # Mergers and Dividends are not tested, but we need to have these
        # anyway
        mergers = pd.DataFrame({}, columns=['effective_date', 'ratio', 'sid'])
        mergers.effective_date = mergers.effective_date.astype(np.int64)
        mergers.ratio = mergers.ratio.astype(np.float64)
        mergers.sid = mergers.sid.astype(np.int64)

        dividends = pd.DataFrame({}, columns=['ex_date', 'record_date',
                                              'declared_date', 'pay_date',
                                              'amount', 'sid'])
        dividends.amount = dividends.amount.astype(np.float64)
        dividends.sid = dividends.sid.astype(np.int64)

        adj_writer.write(splits, mergers, dividends)

        return SQLiteAdjustmentReader(path)
コード例 #2
0
ファイル: test_pipeline_algo.py プロジェクト: Mapgax/zipline
 def create_adjustment_reader(cls, tempdir):
     dbpath = tempdir.getpath('adjustments.sqlite')
     writer = SQLiteAdjustmentWriter(dbpath, cls.env.trading_days,
                                     MockDailyBarSpotReader())
     splits = DataFrame.from_records([
         {
             'effective_date': str_to_seconds('2014-06-09'),
             'ratio': (1 / 7.0),
             'sid': cls.AAPL,
         }
     ])
     mergers = DataFrame(
         {
             # Hackery to make the dtypes correct on an empty frame.
             'effective_date': array([], dtype=int),
             'ratio': array([], dtype=float),
             'sid': array([], dtype=int),
         },
         index=DatetimeIndex([]),
         columns=['effective_date', 'ratio', 'sid'],
     )
     dividends = DataFrame({
         'sid': array([], dtype=uint32),
         'amount': array([], dtype=float64),
         'record_date': array([], dtype='datetime64[ns]'),
         'ex_date': array([], dtype='datetime64[ns]'),
         'declared_date': array([], dtype='datetime64[ns]'),
         'pay_date': array([], dtype='datetime64[ns]'),
     })
     writer.write(splits, mergers, dividends)
     return SQLiteAdjustmentReader(dbpath)
コード例 #3
0
ファイル: synthetic.py プロジェクト: tutengfei/zipline
 def __init__(self):
     conn = sqlite3_connect(":memory:")
     writer = SQLiteAdjustmentWriter(conn)
     empty = DataFrame(
         {"sid": array([], dtype=uint32), "effective_date": array([], dtype=uint32), "ratio": array([], dtype=float)}
     )
     writer.write(splits=empty, mergers=empty, dividends=empty)
     super(NullAdjustmentReader, self).__init__(conn)
コード例 #4
0
    def setUpClass(cls):
        cls.test_data_dir = TempDirectory()
        cls.db_path = cls.test_data_dir.getpath("adjustments.db")
        all_days = TradingEnvironment().trading_days
        cls.calendar_days = all_days[all_days.slice_indexer(TEST_CALENDAR_START, TEST_CALENDAR_STOP)]
        daily_bar_reader = MockDailyBarSpotReader()
        writer = SQLiteAdjustmentWriter(cls.db_path, cls.calendar_days, daily_bar_reader)
        writer.write(SPLITS, MERGERS, DIVIDENDS)

        cls.assets = TEST_QUERY_ASSETS
        cls.asset_info = EQUITY_INFO
        cls.bcolz_writer = SyntheticDailyBarWriter(cls.asset_info, cls.calendar_days)
        cls.bcolz_path = cls.test_data_dir.getpath("equity_pricing.bcolz")
        cls.bcolz_writer.write(cls.bcolz_path, cls.calendar_days, cls.assets)
コード例 #5
0
ファイル: synthetic.py プロジェクト: AdaoSmith/zipline
 def __init__(self):
     conn = sqlite3_connect(':memory:')
     writer = SQLiteAdjustmentWriter(conn, None, None)
     empty = DataFrame({
         'sid': array([], dtype=uint32),
         'effective_date': array([], dtype=uint32),
         'ratio': array([], dtype=float),
     })
     empty_dividends = DataFrame({
         'sid': array([], dtype=uint32),
         'amount': array([], dtype=float64),
         'record_date': array([], dtype='datetime64[ns]'),
         'ex_date': array([], dtype='datetime64[ns]'),
         'declared_date': array([], dtype='datetime64[ns]'),
         'pay_date': array([], dtype='datetime64[ns]'),
     })
     writer.write(splits=empty, mergers=empty, dividends=empty_dividends)
     super(NullAdjustmentReader, self).__init__(conn)
コード例 #6
0
ファイル: synthetic.py プロジェクト: ntvis/zipline
 def __init__(self):
     conn = sqlite3_connect(":memory:")
     writer = SQLiteAdjustmentWriter(conn, None, None)
     empty = DataFrame(
         {"sid": array([], dtype=uint32), "effective_date": array([], dtype=uint32), "ratio": array([], dtype=float)}
     )
     empty_dividends = DataFrame(
         {
             "sid": array([], dtype=uint32),
             "amount": array([], dtype=float64),
             "record_date": array([], dtype="datetime64[ns]"),
             "ex_date": array([], dtype="datetime64[ns]"),
             "declared_date": array([], dtype="datetime64[ns]"),
             "pay_date": array([], dtype="datetime64[ns]"),
         }
     )
     writer.write(splits=empty, mergers=empty, dividends=empty_dividends)
     super(NullAdjustmentReader, self).__init__(conn)
コード例 #7
0
    def setUpClass(cls):
        cls.test_data_dir = TempDirectory()
        cls.db_path = cls.test_data_dir.getpath('adjustments.db')
        all_days = TradingEnvironment().trading_days
        cls.calendar_days = all_days[all_days.slice_indexer(
            TEST_CALENDAR_START, TEST_CALENDAR_STOP)]
        daily_bar_reader = MockDailyBarSpotReader()
        writer = SQLiteAdjustmentWriter(cls.db_path, cls.calendar_days,
                                        daily_bar_reader)
        writer.write(SPLITS, MERGERS, DIVIDENDS)

        cls.assets = TEST_QUERY_ASSETS
        cls.asset_info = EQUITY_INFO
        cls.bcolz_writer = SyntheticDailyBarWriter(
            cls.asset_info,
            cls.calendar_days,
        )
        cls.bcolz_path = cls.test_data_dir.getpath('equity_pricing.bcolz')
        cls.bcolz_writer.write(cls.bcolz_path, cls.calendar_days, cls.assets)
コード例 #8
0
def create_mock_adjustments(tempdir, days, splits=None, dividends=None,
                            mergers=None):
    path = tempdir.getpath("test_adjustments.db")

    # create a split for the last day
    writer = SQLiteAdjustmentWriter(path, days, MockDailyBarReader())
    if splits is None:
        splits = create_empty_splits_mergers_frame()
    else:
        splits = pd.DataFrame(splits)

    if mergers is None:
        mergers = create_empty_splits_mergers_frame()
    else:
        mergers = pd.DataFrame(mergers)

    if dividends is None:
        data = {
            # Hackery to make the dtypes correct on an empty frame.
            'ex_date': np.array([], dtype='datetime64[ns]'),
            'pay_date': np.array([], dtype='datetime64[ns]'),
            'record_date': np.array([], dtype='datetime64[ns]'),
            'declared_date': np.array([], dtype='datetime64[ns]'),
            'amount': np.array([], dtype=float),
            'sid': np.array([], dtype=int),
        }
        dividends = pd.DataFrame(
            data,
            index=pd.DatetimeIndex([], tz='UTC'),
            columns=['ex_date',
                     'pay_date',
                     'record_date',
                     'declared_date',
                     'amount',
                     'sid']
        )
    else:
        if not isinstance(dividends, pd.DataFrame):
            dividends = pd.DataFrame(dividends)

    writer.write(splits, mergers, dividends)

    return path
コード例 #9
0
 def create_adjustment_reader(cls, tempdir):
     dbpath = tempdir.getpath('adjustments.sqlite')
     writer = SQLiteAdjustmentWriter(dbpath, cls.env.trading_days,
                                     MockDailyBarSpotReader())
     splits = DataFrame.from_records([
         {
             'effective_date': str_to_seconds('2014-06-09'),
             'ratio': (1 / 7.0),
             'sid': cls.AAPL,
         }
     ])
     mergers = create_empty_splits_mergers_frame()
     dividends = DataFrame({
         'sid': array([], dtype=uint32),
         'amount': array([], dtype=float64),
         'record_date': array([], dtype='datetime64[ns]'),
         'ex_date': array([], dtype='datetime64[ns]'),
         'declared_date': array([], dtype='datetime64[ns]'),
         'pay_date': array([], dtype='datetime64[ns]'),
     })
     writer.write(splits, mergers, dividends)
     return SQLiteAdjustmentReader(dbpath)
コード例 #10
0
ファイル: test_pipeline_algo.py プロジェクト: mirizzi/zipline
 def create_adjustment_reader(cls, tempdir):
     dbpath = tempdir.getpath('adjustments.sqlite')
     writer = SQLiteAdjustmentWriter(dbpath)
     splits = DataFrame.from_records([{
         'effective_date':
         str_to_seconds('2014-06-09'),
         'ratio': (1 / 7.0),
         'sid':
         cls.AAPL,
     }])
     mergers = dividends = DataFrame(
         {
             # Hackery to make the dtypes correct on an empty frame.
             'effective_date': array([], dtype=int),
             'ratio': array([], dtype=float),
             'sid': array([], dtype=int),
         },
         index=DatetimeIndex([], tz='UTC'),
         columns=['effective_date', 'ratio', 'sid'],
     )
     writer.write(splits, mergers, dividends)
     return SQLiteAdjustmentReader(dbpath)
コード例 #11
0
 def create_adjustment_reader(cls, tempdir):
     dbpath = tempdir.getpath('adjustments.sqlite')
     writer = SQLiteAdjustmentWriter(dbpath)
     splits = DataFrame.from_records([
         {
             'effective_date': str_to_seconds('2014-06-09'),
             'ratio': (1 / 7.0),
             'sid': cls.AAPL,
         }
     ])
     mergers = dividends = DataFrame(
         {
             # Hackery to make the dtypes correct on an empty frame.
             'effective_date': array([], dtype=int),
             'ratio': array([], dtype=float),
             'sid': array([], dtype=int),
         },
         index=DatetimeIndex([], tz='UTC'),
         columns=['effective_date', 'ratio', 'sid'],
     )
     writer.write(splits, mergers, dividends)
     return SQLiteAdjustmentReader(dbpath)
コード例 #12
0
 def create_adjustment_reader(cls, tempdir):
     dbpath = tempdir.getpath('adjustments.sqlite')
     writer = SQLiteAdjustmentWriter(dbpath, cls.env.trading_days,
                                     MockDailyBarSpotReader())
     splits = DataFrame.from_records([{
         'effective_date':
         str_to_seconds('2014-06-09'),
         'ratio': (1 / 7.0),
         'sid':
         cls.AAPL,
     }])
     mergers = DataFrame(
         {
             # Hackery to make the dtypes correct on an empty frame.
             'effective_date': array([], dtype=int),
             'ratio': array([], dtype=float),
             'sid': array([], dtype=int),
         },
         index=DatetimeIndex([], tz='UTC'),
         columns=['effective_date', 'ratio', 'sid'],
     )
     dividends = DataFrame({
         'sid':
         array([], dtype=uint32),
         'amount':
         array([], dtype=float64),
         'record_date':
         array([], dtype='datetime64[ns]'),
         'ex_date':
         array([], dtype='datetime64[ns]'),
         'declared_date':
         array([], dtype='datetime64[ns]'),
         'pay_date':
         array([], dtype='datetime64[ns]'),
     })
     writer.write(splits, mergers, dividends)
     return SQLiteAdjustmentReader(dbpath)
コード例 #13
0
ファイル: synthetic.py プロジェクト: zmskei/zipline
 def __init__(self):
     conn = sqlite3_connect(':memory:')
     writer = SQLiteAdjustmentWriter(conn, None, None)
     empty = DataFrame({
         'sid': array([], dtype=uint32),
         'effective_date': array([], dtype=uint32),
         'ratio': array([], dtype=float),
     })
     empty_dividends = DataFrame({
         'sid':
         array([], dtype=uint32),
         'amount':
         array([], dtype=float64),
         'record_date':
         array([], dtype='datetime64[ns]'),
         'ex_date':
         array([], dtype='datetime64[ns]'),
         'declared_date':
         array([], dtype='datetime64[ns]'),
         'pay_date':
         array([], dtype='datetime64[ns]'),
     })
     writer.write(splits=empty, mergers=empty, dividends=empty_dividends)
     super(NullAdjustmentReader, self).__init__(conn)
コード例 #14
0
ファイル: test_benchmark.py プロジェクト: UpSea/zipline
    def setUpClass(cls):
        cls.env = TradingEnvironment()
        cls.tempdir = TempDirectory()

        cls.sim_params = factory.create_simulation_parameters()

        cls.env.write_data(equities_data={
            1: {
                "start_date": cls.sim_params.trading_days[0],
                "end_date": cls.sim_params.trading_days[-1] + timedelta(days=1)
            },
            2: {
                "start_date": cls.sim_params.trading_days[0],
                "end_date": cls.sim_params.trading_days[-1] + timedelta(days=1)
            },
            3: {
                "start_date": cls.sim_params.trading_days[100],
                "end_date": cls.sim_params.trading_days[-100]
            },
            4: {
                "start_date": cls.sim_params.trading_days[0],
                "end_date": cls.sim_params.trading_days[-1] + timedelta(days=1)
            }

        })

        dbpath = os.path.join(cls.tempdir.path, "adjustments.db")

        writer = SQLiteAdjustmentWriter(dbpath, cls.env.trading_days,
                                        MockDailyBarSpotReader())
        splits = mergers = create_empty_splits_mergers_frame()
        dividends = pd.DataFrame({
            'sid': np.array([], dtype=np.uint32),
            'amount': np.array([], dtype=np.float64),
            'declared_date': np.array([], dtype='datetime64[ns]'),
            'ex_date': np.array([], dtype='datetime64[ns]'),
            'pay_date': np.array([], dtype='datetime64[ns]'),
            'record_date': np.array([], dtype='datetime64[ns]'),
        })
        declared_date = cls.sim_params.trading_days[45]
        ex_date = cls.sim_params.trading_days[50]
        record_date = pay_date = cls.sim_params.trading_days[55]

        stock_dividends = pd.DataFrame({
            'sid': np.array([4], dtype=np.uint32),
            'payment_sid': np.array([5], dtype=np.uint32),
            'ratio': np.array([2], dtype=np.float64),
            'declared_date': np.array([declared_date], dtype='datetime64[ns]'),
            'ex_date': np.array([ex_date], dtype='datetime64[ns]'),
            'record_date': np.array([record_date], dtype='datetime64[ns]'),
            'pay_date': np.array([pay_date], dtype='datetime64[ns]'),
        })
        writer.write(splits, mergers, dividends,
                     stock_dividends=stock_dividends)

        cls.data_portal = create_data_portal(
            cls.env,
            cls.tempdir,
            cls.sim_params,
            [1, 2, 3, 4],
            adjustment_reader=SQLiteAdjustmentReader(dbpath)
        )
コード例 #15
0
ファイル: test_bar_data.py プロジェクト: UpSea/zipline
    def create_adjustments_reader(cls):
        path = cls.tempdir.getpath("test_adjustments.db")

        adj_writer = SQLiteAdjustmentWriter(
            path,
            cls.env.trading_days,
            MockDailyBarReader()
        )

        splits = pd.DataFrame([
            {
                'effective_date': str_to_seconds("2016-01-06"),
                'ratio': 0.5,
                'sid': cls.SPLIT_ASSET.sid
            },
            {
                'effective_date': str_to_seconds("2016-01-07"),
                'ratio': 0.5,
                'sid': cls.ILLIQUID_SPLIT_ASSET.sid
            }
        ])

        mergers = pd.DataFrame([
            {
                'effective_date': str_to_seconds("2016-01-06"),
                'ratio': 0.5,
                'sid': cls.MERGER_ASSET.sid
            },
            {
                'effective_date': str_to_seconds("2016-01-07"),
                'ratio': 0.6,
                'sid': cls.ILLIQUID_MERGER_ASSET.sid
            }
        ])

        # we're using a fake daily reader in the adjustments writer which
        # returns every daily price as 100, so dividend amounts of 2.0 and 4.0
        # correspond to 2% and 4% dividends, respectively.
        dividends = pd.DataFrame([
            {
                # only care about ex date, the other dates don't matter here
                'ex_date':
                    pd.Timestamp("2016-01-06", tz='UTC').to_datetime64(),
                'record_date':
                    pd.Timestamp("2016-01-06", tz='UTC').to_datetime64(),
                'declared_date':
                    pd.Timestamp("2016-01-06", tz='UTC').to_datetime64(),
                'pay_date':
                    pd.Timestamp("2016-01-06", tz='UTC').to_datetime64(),
                'amount': 2.0,
                'sid': cls.DIVIDEND_ASSET.sid
            },
            {
                'ex_date':
                    pd.Timestamp("2016-01-07", tz='UTC').to_datetime64(),
                'record_date':
                    pd.Timestamp("2016-01-07", tz='UTC').to_datetime64(),
                'declared_date':
                    pd.Timestamp("2016-01-07", tz='UTC').to_datetime64(),
                'pay_date':
                    pd.Timestamp("2016-01-07", tz='UTC').to_datetime64(),
                'amount': 4.0,
                'sid': cls.ILLIQUID_DIVIDEND_ASSET.sid
            }],
            columns=['ex_date',
                     'record_date',
                     'declared_date',
                     'pay_date',
                     'amount',
                     'sid']
        )

        adj_writer.write(splits, mergers, dividends)

        return SQLiteAdjustmentReader(path)
コード例 #16
0
    def create_adjustments_reader(cls):
        path = cls.tempdir.getpath("test_adjustments.db")

        adj_writer = SQLiteAdjustmentWriter(path, cls.env.trading_days,
                                            MockDailyBarReader())

        splits = pd.DataFrame([{
            'effective_date': str_to_seconds("2016-01-06"),
            'ratio': 0.5,
            'sid': cls.SPLIT_ASSET.sid
        }, {
            'effective_date': str_to_seconds("2016-01-07"),
            'ratio': 0.5,
            'sid': cls.ILLIQUID_SPLIT_ASSET.sid
        }])

        mergers = pd.DataFrame([{
            'effective_date': str_to_seconds("2016-01-06"),
            'ratio': 0.5,
            'sid': cls.MERGER_ASSET.sid
        }, {
            'effective_date': str_to_seconds("2016-01-07"),
            'ratio': 0.6,
            'sid': cls.ILLIQUID_MERGER_ASSET.sid
        }])

        # we're using a fake daily reader in the adjustments writer which
        # returns every daily price as 100, so dividend amounts of 2.0 and 4.0
        # correspond to 2% and 4% dividends, respectively.
        dividends = pd.DataFrame(
            [
                {
                    # only care about ex date, the other dates don't matter here
                    'ex_date':
                    pd.Timestamp("2016-01-06", tz='UTC').to_datetime64(),
                    'record_date':
                    pd.Timestamp("2016-01-06", tz='UTC').to_datetime64(),
                    'declared_date':
                    pd.Timestamp("2016-01-06", tz='UTC').to_datetime64(),
                    'pay_date':
                    pd.Timestamp("2016-01-06", tz='UTC').to_datetime64(),
                    'amount':
                    2.0,
                    'sid':
                    cls.DIVIDEND_ASSET.sid
                },
                {
                    'ex_date':
                    pd.Timestamp("2016-01-07", tz='UTC').to_datetime64(),
                    'record_date':
                    pd.Timestamp("2016-01-07", tz='UTC').to_datetime64(),
                    'declared_date':
                    pd.Timestamp("2016-01-07", tz='UTC').to_datetime64(),
                    'pay_date':
                    pd.Timestamp("2016-01-07", tz='UTC').to_datetime64(),
                    'amount':
                    4.0,
                    'sid':
                    cls.ILLIQUID_DIVIDEND_ASSET.sid
                }
            ],
            columns=[
                'ex_date', 'record_date', 'declared_date', 'pay_date',
                'amount', 'sid'
            ])

        adj_writer.write(splits, mergers, dividends)

        return SQLiteAdjustmentReader(path)
コード例 #17
0
    def setUpClass(cls):
        cls.env = TradingEnvironment()
        cls.tempdir = TempDirectory()

        cls.sim_params = factory.create_simulation_parameters()

        cls.env.write_data(
            equities_data={
                1: {
                    "start_date": cls.sim_params.trading_days[0],
                    "end_date": cls.sim_params.trading_days[-1] +
                    timedelta(days=1)
                },
                2: {
                    "start_date": cls.sim_params.trading_days[0],
                    "end_date": cls.sim_params.trading_days[-1] +
                    timedelta(days=1)
                },
                3: {
                    "start_date": cls.sim_params.trading_days[100],
                    "end_date": cls.sim_params.trading_days[-100]
                },
                4: {
                    "start_date": cls.sim_params.trading_days[0],
                    "end_date": cls.sim_params.trading_days[-1] +
                    timedelta(days=1)
                }
            })

        dbpath = os.path.join(cls.tempdir.path, "adjustments.db")

        writer = SQLiteAdjustmentWriter(dbpath, cls.env.trading_days,
                                        MockDailyBarSpotReader())
        splits = mergers = create_empty_splits_mergers_frame()
        dividends = pd.DataFrame({
            'sid':
            np.array([], dtype=np.uint32),
            'amount':
            np.array([], dtype=np.float64),
            'declared_date':
            np.array([], dtype='datetime64[ns]'),
            'ex_date':
            np.array([], dtype='datetime64[ns]'),
            'pay_date':
            np.array([], dtype='datetime64[ns]'),
            'record_date':
            np.array([], dtype='datetime64[ns]'),
        })
        declared_date = cls.sim_params.trading_days[45]
        ex_date = cls.sim_params.trading_days[50]
        record_date = pay_date = cls.sim_params.trading_days[55]

        stock_dividends = pd.DataFrame({
            'sid':
            np.array([4], dtype=np.uint32),
            'payment_sid':
            np.array([5], dtype=np.uint32),
            'ratio':
            np.array([2], dtype=np.float64),
            'declared_date':
            np.array([declared_date], dtype='datetime64[ns]'),
            'ex_date':
            np.array([ex_date], dtype='datetime64[ns]'),
            'record_date':
            np.array([record_date], dtype='datetime64[ns]'),
            'pay_date':
            np.array([pay_date], dtype='datetime64[ns]'),
        })
        writer.write(splits,
                     mergers,
                     dividends,
                     stock_dividends=stock_dividends)

        cls.data_portal = create_data_portal(
            cls.env,
            cls.tempdir,
            cls.sim_params, [1, 2, 3, 4],
            adjustment_reader=SQLiteAdjustmentReader(dbpath))