Beispiel #1
0
    def rank(self,
             reset_by=None,
             after_false=False,
             allow_gaps=False,
             broadcast_kwargs={}):
        """See `vectorbt.signals.nb.rank_nb`.

        Example:
            Rank `False` values in `signals`:

            ```python-repl
            >>> not_signals = ~signals
            >>> print(not_signals)
                            a      b      c
            2018-01-01  False   True   True
            2018-01-02   True  False   True
            2018-01-03   True   True  False
            2018-01-04  False   True   True
            2018-01-05   True  False   True
            >>> print(not_signals.vbt.signals.rank())
                        a  b  c
            2018-01-01  0  1  1
            2018-01-02  1  0  2
            2018-01-03  2  1  0
            2018-01-04  0  2  1
            2018-01-05  1  0  2
            >>> print(not_signals.vbt.signals.rank(after_false=True))
                        a  b  c
            2018-01-01  0  0  0
            2018-01-02  1  0  0
            2018-01-03  2  1  0
            2018-01-04  0  2  1
            2018-01-05  1  0  2
            >>> print(not_signals.vbt.signals.rank(allow_gaps=True))
                        a  b  c
            2018-01-01  0  1  1
            2018-01-02  1  0  2
            2018-01-03  2  2  0
            2018-01-04  0  3  3
            2018-01-05  3  0  4
            >>> print(not_signals.vbt.signals.rank(reset_by=signals, allow_gaps=True))
                        a  b  c
            2018-01-01  0  1  1
            2018-01-02  1  0  2
            2018-01-03  2  1  0
            2018-01-04  0  2  1
            2018-01-05  1  0  2
            ```"""
        if reset_by is not None:
            obj, reset_by = reshape_fns.broadcast(self._obj, reset_by,
                                                  **broadcast_kwargs)
            reset_by = reset_by.vbt.to_2d_array()
        else:
            obj = self._obj
        ranked = nb.rank_nb(obj.vbt.to_2d_array(),
                            reset_by=reset_by,
                            after_false=after_false,
                            allow_gaps=allow_gaps)
        return obj.vbt.wrap_array(ranked)
Beispiel #2
0
    def rank(self,
             reset_by=None,
             after_false=False,
             allow_gaps=False,
             broadcast_kwargs=None,
             wrap_kwargs=None):
        """See `vectorbt.signals.nb.rank_nb`.

        ## Example

        Rank each True value in each partition in `sig`:
        ```python-repl
        >>> sig.vbt.signals.rank()
                    a  b  c
        2020-01-01  1  1  1
        2020-01-02  0  0  2
        2020-01-03  0  1  3
        2020-01-04  0  0  0
        2020-01-05  0  1  0

        >>> sig.vbt.signals.rank(after_false=True)
                    a  b  c
        2020-01-01  0  0  0
        2020-01-02  0  0  0
        2020-01-03  0  1  0
        2020-01-04  0  0  0
        2020-01-05  0  1  0

        >>> sig.vbt.signals.rank(allow_gaps=True)
                    a  b  c
        2020-01-01  1  1  1
        2020-01-02  0  0  2
        2020-01-03  0  2  3
        2020-01-04  0  0  0
        2020-01-05  0  3  0

        >>> sig.vbt.signals.rank(reset_by=~sig, allow_gaps=True)
                    a  b  c
        2020-01-01  1  1  1
        2020-01-02  0  0  2
        2020-01-03  0  1  3
        2020-01-04  0  0  0
        2020-01-05  0  1  0
        ```
        """
        if broadcast_kwargs is None:
            broadcast_kwargs = {}
        if reset_by is not None:
            obj, reset_by = reshape_fns.broadcast(self._obj, reset_by,
                                                  **broadcast_kwargs)
            reset_by = reset_by.vbt.to_2d_array()
        else:
            obj = self._obj
        ranked = nb.rank_nb(obj.vbt.to_2d_array(),
                            reset_by=reset_by,
                            after_false=after_false,
                            allow_gaps=allow_gaps)
        return obj.vbt.wrapper.wrap(ranked, **merge_dicts({}, wrap_kwargs))