TA-Lib / ta-lib-python

Python wrapper for TA-Lib (http://ta-lib.org/).
http://ta-lib.github.io/ta-lib-python
Other
9.46k stars 1.74k forks source link

problems with talib and Bollinger Bands #549

Closed theanonymoustrader closed 1 year ago

theanonymoustrader commented 1 year ago

Hi everybody!

I'm trying to create two strategies. 1) Buy at lower band and sell the opened position at upper band. 2) Buy when the close price crossover the upper band and set the SL at the basis line. The stop loss follow the basis line at every new close.

Now. About the first strategy, I've wrote this:

> class DetBB(Strategy):
    close = data.Close
    open = data.Open
    upperband, middleband, lowerband = talib.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)
    def init(self):
        close = self.data.Close
        open = self.data.Open
        upperband = talib.BBANDS(self.data.Close, timeperiod=20, nbdevup=2, nbdevdn=2, matype=0)
        lowerband = talib.BBANDS(self.data.Close, timeperiod=20, nbdevup=2, nbdevdn=2, matype=0)
    def next(self):
        if crossover(self.open, self.lowerband):
            tp_teorico = self.upperband[0]
            if(math.isnan(tp_teorico)):
                print("not opening")
            else:
                self.buy(stop=None,sl=None,limit=None,tp=self.upperband[0])

> bt = Backtest(data, DetBB,
              cash=10000000, commission=.002,
              exclusive_orders=True)

Essentially, the backtest is not opening positions, because the value of tp_periodico are always Nan.

About the second strategy, how I can wrote a code that updates my stoploss automatically? Thanks

theanonymoustrader commented 1 year ago

Wrong forum sorry