diff --git a/backtesting/backtesting.py b/backtesting/backtesting.py index d356b211..ca0567a9 100644 --- a/backtesting/backtesting.py +++ b/backtesting/backtesting.py @@ -840,7 +840,8 @@ def _adjusted_price(self, size=None, price=None) -> float: Long/short `price`, adjusted for spread. In long positions, the adjusted price is a fraction higher, and vice versa. """ - return (price or self.last_price) * (1 + copysign(self._spread, size)) + spread = self._spread[self._i] if isinstance(self._spread, np.ndarray) else self._spread + return (price or self.last_price) * (1 + copysign(spread, size)) @property def equity(self) -> float: @@ -1210,8 +1211,11 @@ def __init__(self, if not isinstance(data, pd.DataFrame): raise TypeError("`data` must be a pandas.DataFrame with columns") if not isinstance(spread, Number): - raise TypeError('`spread` must be a float value, percent of ' - 'entry order price') + if hasattr(spread, '__len__') and len(spread) == len(data): + spread = np.asarray(spread, dtype=float) + else: + raise TypeError('`spread` must be a float value (percent of ' + 'entry order price) or a sequence of the same length as data') if not isinstance(commission, (Number, tuple)) and not callable(commission): raise TypeError('`commission` must be a float percent of order value, ' 'a tuple of `(fixed, relative)` commission, '