kernc / backtesting.py

:mag_right: :chart_with_upwards_trend: :snake: :moneybag: Backtest trading strategies in Python.
https://kernc.github.io/backtesting.py/
GNU Affero General Public License v3.0
5.55k stars 1.07k forks source link

no trades opened with bollinger bands #982

Closed theanonymoustrader closed 1 year ago

theanonymoustrader commented 1 year ago

Expected Behavior

Hello! I'm tryin' to build a strategy that buy if the closing price is under the lower BBands (and take profit at its opposite) and sell if the closing price is higher than the upper bollinger band; this two operations will be opened only if the BBandwith will be at 0.05 or less. Currently, no operation are opened. This is my strategy.

class LaStrategia(Strategy):

def init(self):
    self.close = self.data.Close

    self.upperband, self.middleband, self.lowerband = talib.BBANDS(self.close, timeperiod=20)

    self.bbwidth= (self.upperband-self.lowerband)/self.middleband

def next(self):
    #print(self.close[-1] , " " , self.lowerband[-1], " " , self.upperband[-1])
    if self.close[-1] <= self.lowerband[-1] and self.bbwidth <= 0.06:
        self.buy(tp=self.upperband)
    elif self.close[-1] >= self.upperband[-1] and self.bbwidth <= 0.06:
        self.sell(tp=self.lowerband)

`

I'm running python version 3.10 and backtesting.py 0.3.3 os osX last version. Thanks for the support.

kernc commented 1 year ago

See to it that it's not a case of https://github.com/kernc/backtesting.py/discussions/411. In any event, this issue is lacking details to reliably investigate/reproduce it.

theanonymoustrader commented 1 year ago

I've put 1.000.000 as cash and nothing changed. What kind of details do you want?

kernc commented 1 year ago

I see now, you're using your indicators wrong. See the tutorials.

What kind of details do you want?

Usually just a MWE. Something that can be cut and pasted to independently verify.