Example #1
0
    def __init__(self, index, columns, ndim, freq=None, column_only_select=None,
                 group_select=None, grouped_ndim=None, **kwargs):
        config = dict(
            index=index,
            columns=columns,
            ndim=ndim,
            freq=freq,
            column_only_select=column_only_select,
            group_select=group_select,
            grouped_ndim=grouped_ndim,
        )

        checks.assert_not_none(index)
        checks.assert_not_none(columns)
        checks.assert_not_none(ndim)
        if not isinstance(index, pd.Index):
            index = pd.Index(index)
        if not isinstance(columns, pd.Index):
            columns = pd.Index(columns)

        self._index = index
        self._columns = columns
        self._ndim = ndim
        self._freq = freq
        self._column_only_select = column_only_select
        self._group_select = group_select
        self._grouper = ColumnGrouper(columns, **kwargs)
        self._grouped_ndim = grouped_ndim

        PandasIndexer.__init__(self)
        Configured.__init__(self, **merge_dicts(config, self._grouper._config))
Example #2
0
    def __init__(self, wrapper, mapped_arr, col_arr, idx_arr=None):
        Configured.__init__(
            self,
            wrapper=wrapper,
            mapped_arr=mapped_arr,
            col_arr=col_arr,
            idx_arr=idx_arr
        )
        checks.assert_type(wrapper, ArrayWrapper)
        if not isinstance(mapped_arr, np.ndarray):
            mapped_arr = np.asarray(mapped_arr)
        if not isinstance(col_arr, np.ndarray):
            col_arr = np.asarray(col_arr)
        checks.assert_shape_equal(mapped_arr, col_arr, axis=0)
        if idx_arr is not None:
            if not isinstance(idx_arr, np.ndarray):
                idx_arr = np.asarray(idx_arr)
            checks.assert_shape_equal(mapped_arr, idx_arr, axis=0)

        self._wrapper = wrapper
        self._mapped_arr = mapped_arr
        self._col_arr = col_arr
        self._idx_arr = idx_arr

        PandasIndexer.__init__(self, _mapped_array_indexing_func)
Example #3
0
    def __init__(self, wrapper: ArrayWrapper, **kwargs) -> None:
        checks.assert_instance_of(wrapper, ArrayWrapper)
        self._wrapper = wrapper

        Configured.__init__(self, wrapper=wrapper, **kwargs)
        PandasIndexer.__init__(self)
        AttrResolver.__init__(self)
Example #4
0
    def __init__(self,
                 main_price,
                 init_capital,
                 orders,
                 cash,
                 shares,
                 freq=None,
                 year_freq=None,
                 levy_alpha=None,
                 risk_free=None,
                 required_return=None,
                 cutoff=None,
                 factor_returns=None,
                 incl_unrealized_stats=False):
        # Perform checks
        checks.assert_type(main_price, (pd.Series, pd.DataFrame))
        if checks.is_frame(main_price):
            checks.assert_type(init_capital, pd.Series)
            checks.assert_same(main_price.columns, init_capital.index)
        else:
            checks.assert_ndim(init_capital, 0)
        checks.assert_same_meta(main_price, cash)
        checks.assert_same_meta(main_price, shares)

        # Store passed arguments
        self._main_price = main_price
        self._init_capital = init_capital
        self._orders = orders
        self._cash = cash
        self._shares = shares
        self._incl_unrealized_stats = incl_unrealized_stats

        freq = main_price.vbt(freq=freq).freq
        if freq is None:
            raise ValueError(
                "Couldn't parse the frequency of index. You must set `freq`.")
        self._freq = freq

        year_freq = main_price.vbt.returns(year_freq=year_freq).year_freq
        if freq is None:
            raise ValueError("You must set `year_freq`.")
        self._year_freq = year_freq

        # Parameters
        self._levy_alpha = defaults.portfolio[
            'levy_alpha'] if levy_alpha is None else levy_alpha
        self._risk_free = defaults.portfolio[
            'risk_free'] if risk_free is None else risk_free
        self._required_return = defaults.portfolio[
            'required_return'] if required_return is None else required_return
        self._cutoff = defaults.portfolio[
            'cutoff'] if cutoff is None else cutoff
        self._factor_returns = defaults.portfolio[
            'factor_returns'] if factor_returns is None else factor_returns

        # Supercharge
        PandasIndexer.__init__(self, _indexing_func)
        self.wrapper = ArrayWrapper.from_obj(main_price, freq=freq)
Example #5
0
    def __init__(self, records_arr, ts, freq=None, idx_field='end_idx'):
        Records.__init__(self,
                         records_arr,
                         ArrayWrapper.from_obj(ts, freq=freq),
                         idx_field=idx_field)
        PandasIndexer.__init__(self, _indexing_func)

        if not all(field in records_arr.dtype.names
                   for field in drawdown_dt.names):
            raise ValueError(
                "Records array must have all fields defined in drawdown_dt")

        self.ts = ts
Example #6
0
    def __init__(self, records_arr, main_price, freq=None, idx_field='idx'):
        Records.__init__(self,
                         records_arr,
                         ArrayWrapper.from_obj(main_price, freq=freq),
                         idx_field=idx_field)
        PandasIndexer.__init__(self, _indexing_func)

        if not all(field in records_arr.dtype.names
                   for field in order_dt.names):
            raise ValueError(
                "Records array must have all fields defined in order_dt")

        self.main_price = main_price
Example #7
0
    def __init__(self,
                 index,
                 columns,
                 ndim,
                 freq=None,
                 column_only_select=None,
                 group_select=None,
                 grouped_ndim=None,
                 group_by=None,
                 allow_enable=True,
                 allow_disable=True,
                 allow_modify=True):
        Configured.__init__(self,
                            index=index,
                            columns=columns,
                            ndim=ndim,
                            freq=freq,
                            column_only_select=column_only_select,
                            group_select=group_select,
                            grouped_ndim=grouped_ndim,
                            group_by=group_by,
                            allow_enable=allow_enable,
                            allow_disable=allow_disable,
                            allow_modify=allow_modify)

        checks.assert_not_none(index)
        checks.assert_not_none(columns)
        checks.assert_not_none(ndim)
        if not isinstance(index, pd.Index):
            index = pd.Index(index)
        if not isinstance(columns, pd.Index):
            columns = pd.Index(columns)

        self._index = index
        self._columns = columns
        self._ndim = ndim
        self._freq = freq
        self._column_only_select = column_only_select
        self._group_select = group_select
        self._grouper = ColumnGrouper(columns,
                                      group_by=group_by,
                                      allow_enable=allow_enable,
                                      allow_disable=allow_disable,
                                      allow_modify=allow_modify)
        self._grouped_ndim = grouped_ndim

        PandasIndexer.__init__(self,
                               _indexing_func,
                               column_only_select=column_only_select,
                               group_select=group_select)
Example #8
0
    def __init__(self, wrapper, records_arr, ts, idx_field='end_idx'):
        Records.__init__(self, wrapper, records_arr, idx_field=idx_field)
        Configured.__init__(self,
                            wrapper=wrapper,
                            records_arr=records_arr,
                            ts=ts,
                            idx_field=idx_field)
        self._ts = ts

        if not all(field in records_arr.dtype.names
                   for field in drawdown_dt.names):
            raise ValueError(
                "Records array must have all fields defined in drawdown_dt")

        PandasIndexer.__init__(self, _indexing_func)
Example #9
0
    def __init__(self, wrapper, records_arr, close, idx_field='exit_idx'):
        Records.__init__(self, wrapper, records_arr, idx_field=idx_field)
        Configured.__init__(self,
                            wrapper=wrapper,
                            records_arr=records_arr,
                            close=close,
                            idx_field=idx_field)
        self.close = close

        if not all(field in records_arr.dtype.names
                   for field in event_dt.names):
            raise ValueError(
                "Records array must have all fields defined in event_dt")

        PandasIndexer.__init__(self, _indexing_func)
Example #10
0
    def __init__(self, records_arr, wrapper, idx_field=None):
        if not isinstance(records_arr, np.ndarray):
            records_arr = np.asarray(records_arr)
        checks.assert_not_none(records_arr.dtype.fields)
        checks.assert_value_in('col', records_arr.dtype.names)
        checks.assert_type(wrapper, ArrayWrapper)
        if idx_field is not None:
            checks.assert_value_in(idx_field, records_arr.dtype.names)
        else:
            if 'idx' in records_arr.dtype.names:
                idx_field = 'idx'

        self.records_arr = records_arr
        self.wrapper = wrapper
        self.idx_field = idx_field

        PandasIndexer.__init__(self, _records_indexing_func)
Example #11
0
    def __init__(self, mapped_arr, col_arr, wrapper, idx_arr=None):
        if not isinstance(mapped_arr, np.ndarray):
            mapped_arr = np.asarray(mapped_arr)
        if not isinstance(col_arr, np.ndarray):
            col_arr = np.asarray(col_arr)
        checks.assert_same_shape(mapped_arr, col_arr, axis=0)
        checks.assert_type(wrapper, ArrayWrapper)
        if idx_arr is not None:
            if not isinstance(idx_arr, np.ndarray):
                idx_arr = np.asarray(idx_arr)
            checks.assert_same_shape(mapped_arr, idx_arr, axis=0)

        self.mapped_arr = mapped_arr
        self.col_arr = col_arr
        self.wrapper = wrapper
        self.idx_arr = idx_arr

        PandasIndexer.__init__(self, _mapped_indexing_func)
Example #12
0
        def __init__(self, ts_list, output_list, param_list, mapper_list,
                     name):
            perform_init_checks(ts_list, output_list, param_list, mapper_list,
                                name)

            for i, ts_name in enumerate(ts_names):
                setattr(self, f'_{ts_name}', ts_list[i])
            self.wrapper = ArrayWrapper.from_obj(ts_list[0])
            for i, output_name in enumerate(output_names):
                setattr(self, f'_{output_name}', output_list[i])
            for i, param_name in enumerate(param_names):
                setattr(self, f'_{param_name}_array', param_list[i])
                setattr(self, f'_{param_name}_mapper', mapper_list[i])
            if len(param_names) > 1:
                setattr(self, '_tuple_mapper', mapper_list[-1])
            setattr(self, '_name', name)

            # Initialize indexers
            PandasIndexer.__init__(self, indexing_func)
            ParamIndexer.__init__(self, mapper_list, indexing_func)
Example #13
0
    def __init__(self,
                 index: tp.IndexLike,
                 columns: tp.IndexLike,
                 ndim: int,
                 freq: tp.Optional[tp.FrequencyLike] = None,
                 column_only_select: tp.Optional[bool] = None,
                 group_select: tp.Optional[bool] = None,
                 grouped_ndim: tp.Optional[int] = None,
                 **kwargs) -> None:
        config = dict(
            index=index,
            columns=columns,
            ndim=ndim,
            freq=freq,
            column_only_select=column_only_select,
            group_select=group_select,
            grouped_ndim=grouped_ndim,
        )

        checks.assert_not_none(index)
        checks.assert_not_none(columns)
        checks.assert_not_none(ndim)
        if not isinstance(index, pd.Index):
            index = pd.Index(index)
        if not isinstance(columns, pd.Index):
            columns = pd.Index(columns)

        self._index = index
        self._columns = columns
        self._ndim = ndim
        self._freq = freq
        self._column_only_select = column_only_select
        self._group_select = group_select
        self._grouper = ColumnGrouper(columns, **kwargs)
        self._grouped_ndim = grouped_ndim

        PandasIndexer.__init__(self)
        Configured.__init__(self, **merge_dicts(config, self._grouper._config))
Example #14
0
    def __init__(self, wrapper, **kwargs):
        checks.assert_type(wrapper, ArrayWrapper)
        self._wrapper = wrapper

        Configured.__init__(self, wrapper=wrapper, **kwargs)
        PandasIndexer.__init__(self)
Example #15
0
    def __init__(self, index, group_by=None):
        self.index = index
        self.group_by = None
        self.group_by = self.resolve_group_by(group_by=group_by)

        PandasIndexer.__init__(self, _indexing_func)