예제 #1
0
파일: ranges.py 프로젝트: vroomzel/vectorbt
    def from_ts(cls: tp.Type[RangesT],
                ts: tp.ArrayLike,
                gap_value: tp.Optional[tp.Scalar] = None,
                attach_ts: bool = True,
                wrapper_kwargs: tp.KwargsLike = None,
                **kwargs) -> RangesT:
        """Build `Ranges` from time series `ts`.

        Searches for sequences of

        * True values in boolean data (False acts as a gap),
        * positive values in integer data (-1 acts as a gap), and
        * non-NaN values in any other data (NaN acts as a gap).

        `**kwargs` will be passed to `Ranges.__init__`."""
        if wrapper_kwargs is None:
            wrapper_kwargs = {}

        ts_pd = to_pd_array(ts)
        ts_arr = to_2d_array(ts_pd)
        if gap_value is None:
            if np.issubdtype(ts_arr.dtype, np.bool_):
                gap_value = False
            elif np.issubdtype(ts_arr.dtype, np.integer):
                gap_value = -1
            else:
                gap_value = np.nan
        records_arr = nb.find_ranges_nb(ts_arr, gap_value)
        wrapper = ArrayWrapper.from_obj(ts_pd, **wrapper_kwargs)
        return cls(wrapper,
                   records_arr,
                   ts=ts_pd if attach_ts else None,
                   **kwargs)
예제 #2
0
 def from_obj(cls: tp.Type[ArrayWrapperT], obj: tp.ArrayLike, *args,
              **kwargs) -> ArrayWrapperT:
     """Derive metadata from an object."""
     pd_obj = to_pd_array(obj)
     index = index_fns.get_index(pd_obj, 0)
     columns = index_fns.get_index(pd_obj, 1)
     ndim = pd_obj.ndim
     return cls(index, columns, ndim, *args, **kwargs)
예제 #3
0
    def from_ts(cls: tp.Type[DrawdownsT], ts: tp.ArrayLike, idx_field: str = 'end_idx', **kwargs) -> DrawdownsT:
        """Build `Drawdowns` from time series `ts`.

        `**kwargs` such as `freq` will be passed to `Drawdowns.__init__`."""
        pd_ts = to_pd_array(ts)
        records_arr = nb.find_drawdowns_nb(pd_ts.vbt.to_2d_array())
        wrapper = ArrayWrapper.from_obj(pd_ts, **kwargs)
        return cls(wrapper, records_arr, pd_ts, idx_field=idx_field)
예제 #4
0
    def from_ts(cls: tp.Type[DrawdownsT],
                ts: tp.ArrayLike,
                attach_ts: bool = True,
                wrapper_kwargs: tp.KwargsLike = None,
                **kwargs) -> DrawdownsT:
        """Build `Drawdowns` from time series `ts`.

        `**kwargs` will be passed to `Drawdowns.__init__`."""
        ts_pd = to_pd_array(ts)
        records_arr = nb.get_drawdowns_nb(to_2d_array(ts_pd))
        wrapper = ArrayWrapper.from_obj(ts_pd, **merge_dicts({},
                                                             wrapper_kwargs))
        return cls(wrapper,
                   records_arr,
                   ts=ts_pd if attach_ts else None,
                   **kwargs)
예제 #5
0
    def plot_as_markers(
        self,
        y: tp.Optional[tp.ArrayLike] = None,
        **kwargs
    ) -> tp.Union[tp.BaseFigure, plotting.Scatter]:  # pragma: no cover
        """Plot Series as markers.

        Args:
            y (array_like): Y-axis values to plot markers on.
            **kwargs: Keyword arguments passed to `vectorbt.generic.accessors.GenericAccessor.scatterplot`.

        ## Example

        ```python-repl
        >>> ts = pd.Series([1, 2, 3, 2, 1], index=sig.index)
        >>> fig = ts.vbt.lineplot()
        >>> sig['b'].vbt.signals.plot_as_entry_markers(y=ts, fig=fig)
        >>> (~sig['b']).vbt.signals.plot_as_exit_markers(y=ts, fig=fig)
        ```

        ![](/vectorbt/docs/img/signals_plot_as_markers.svg)
        """
        from vectorbt._settings import settings
        plotting_cfg = settings['plotting']

        if y is None:
            y = pd.Series.vbt.empty_like(self._obj, 1)
        else:
            y = reshape_fns.to_pd_array(y)

        return y[self._obj].vbt.scatterplot(**merge_dicts(
            dict(trace_kwargs=dict(marker=dict(
                symbol='circle',
                color=plotting_cfg['contrast_color_schema']['blue'],
                size=7,
                line=dict(width=1,
                          color=adjust_lightness(
                              plotting_cfg['contrast_color_schema']
                              ['blue']))))), kwargs))