pythonback-testing

Getting "errored with exception: 'int' object is not callable"


I am new to python, I am creating a project to backtest stock trading statergies using backtester however i am getting this error and i have spent almost a day trying to fix it but unable to fix it please let me know what i can do to fix it.

I am getting this error "RuntimeError: Indicator "291" errored with exception: 'int' object is not callable"

class HighLow(Strategy):
    
    timeperiod= 10
    
    def init(self):
        high_data = data.High[0:10]
        low_data = data.Low[0:10]

        highest=[]
        for h in high_data:
            highest.append(h)
        highest.sort()
        self.highest_price =int(highest[-1])
        self.lowest_price=int(highest[0])
        
        sum_all = self.highest_price + self.lowest_price

        self.average_price = int(sum_all/2)

        self.hl = self.I(self.average_price)

        
    def next(self):
        if crossover(self.hl,self.highest_price):
            self.buy()
        elif crossover(self.h1,self.average_price):
            self.position.close()

bt2= Backtest(data,HighLow,cash=10000)

stats= bt2.run()
TypeError                                 Traceback (most recent call last)
File ~\anaconda3\lib\site-packages\backtesting\backtesting.py:127, in Strategy.I(self, func, name, plot, overlay, color, scatter, *args, **kwargs)
    126 try:
--> 127     value = func(*args, **kwargs)
    128 except Exception as e:

TypeError: 'int' object is not callable

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
Cell In[101], line 31
     27             self.position.close()
     29 bt2= Backtest(data,HighLow,cash=10000)
---> 31 stats= bt2.run()

File ~\anaconda3\lib\site-packages\backtesting\backtesting.py:1139, in Backtest.run(self, **kwargs)
   1136 broker: _Broker = self._broker(data=data)
   1137 strategy: Strategy = self._strategy(broker, data, kwargs)
-> 1139 strategy.init()
   1140 data._update()  # Strategy.init might have changed/added to data.df
   1142 # Indicators used in Strategy.next()

Cell In[101], line 20, in HighLow.init(self)
     16 sum_all = self.highest_price + self.lowest_price
     18 self.average_price = int(sum_all/2)
---> 20 self.hl = self.I(self.average_price)

File ~\anaconda3\lib\site-packages\backtesting\backtesting.py:129, in Strategy.I(self, func, name, plot, overlay, color, scatter, *args, **kwargs)
    127     value = func(*args, **kwargs)
    128 except Exception as e:
--> 129     raise RuntimeError(f'Indicator "{name}" errored with exception: {e}')
    131 if isinstance(value, pd.DataFrame):
    132     value = value.values.T

RuntimeError: Indicator "291" errored with exception: 'int' object is not callable

Solution

  • The issue is here:

    self.hl = self.I(self.average_price)
    

    and is caused by the fact that self.average_price is an int and not a function.

    It looks like the prototype for I is supposed to be

    Strategy.I(self, func, name, plot, overlay, color, scatter, *args, **kwargs)
    

    so as written, self.average_price is getting interpreted as the func parameter