Esempio n. 1
0
    def generate_stop_loss(self,
                           ts,
                           stops,
                           trailing=False,
                           relative=True,
                           as_columns=None,
                           broadcast_kwargs={}):
        """See `vectorbt.signals.nb.generate_stop_loss_nb`.

        Arguments will be broadcasted using `vectorbt.utils.reshape_fns.broadcast`
        with `broadcast_kwargs`. Argument `stops` can be either a single number, an array of 
        numbers, or a 3D array, where each matrix corresponds to a single configuration. 
        Use `as_columns` as a top-level column level.

        Example:
            For each entry in `signals`, set stop loss for 10% and 20% below the price `ts`:

            ```python-repl
            >>> print(signals.vbt.signals.generate_stop_loss(ts, [0.1, 0.2]))
            stop_loss                   0.1                  0.2
                            a      b      c      a      b      c
            2018-01-01  False  False  False  False  False  False
            2018-01-02  False   True  False  False  False  False
            2018-01-03  False  False  False  False  False  False
            2018-01-04  False   True   True  False   True   True
            2018-01-05  False  False  False  False  False  False
            ```"""
        entries = self._obj
        checks.assert_type(ts, (pd.Series, pd.DataFrame))

        entries, ts = reshape_fns.broadcast(entries,
                                            ts,
                                            **broadcast_kwargs,
                                            writeable=True)
        stops = reshape_fns.broadcast_to_array_of(stops,
                                                  entries.vbt.to_2d_array())
        exits = nb.generate_stop_loss_nb(entries.vbt.to_2d_array(),
                                         ts.vbt.to_2d_array(), stops, trailing,
                                         relative)

        # Build column hierarchy
        if as_columns is not None:
            param_columns = as_columns
        else:
            name = 'trail_stop' if trailing else 'stop_loss'
            param_columns = index_fns.index_from_values(stops, name=name)
        columns = index_fns.combine_indexes(param_columns, entries.vbt.columns)
        return entries.vbt.wrap_array(exits, columns=columns)
Esempio n. 2
0
def build_column_hierarchy(param_list, level_names, ts_columns):
    """For each parameter in `param_list`, create a new column level with parameter values. 
    Combine this level with columns `ts_columns` using Cartesian product."""
    checks.assert_same_shape(param_list, level_names, axis=0)
    param_indexes = [
        index_fns.index_from_values(param_list[i], name=level_names[i])
        for i in range(len(param_list))
    ]
    param_columns = None
    for param_index in param_indexes:
        if param_columns is None:
            param_columns = param_index
        else:
            param_columns = index_fns.stack_indexes(param_columns, param_index)
    if param_columns is not None:
        return index_fns.combine_indexes(param_columns, ts_columns)
    return ts_columns
Esempio n. 3
0
def compare(obj,
            other,
            compare_func,
            multiple=False,
            name=None,
            as_columns=None,
            **kwargs):
    """Compares `obj` to `other` to generate signals.

    Both will be broadcasted together. Set `multiple` to `True` to compare with multiple arguments. In this case,
    a new column level will be created with the name `name`.

    See `vectorbt.utils.accessors.Base_Accessor.combine_with`."""
    if multiple:
        if as_columns is None:
            as_columns = index_fns.index_from_values(other, name=name)
        return obj.vbt.combine_with_multiple(other,
                                             combine_func=compare_func,
                                             as_columns=as_columns,
                                             concat=True,
                                             **kwargs)
    return obj.vbt.combine_with(other, combine_func=compare_func, **kwargs)