-2

The error is as follows:

Traceback (most recent call last):
  File "/home/runner/stock/test.py", line 160, in <module>
    strategy = MyStrategy(data=df_1h, params={})
  File "/home/runner/stock/test.py", line 108, in __init__
    super().__init__(data, params)
TypeError: Strategy.__init__() missing 1 required positional argument: 'params'

The Class :

class MyStrategy(Strategy):
    def __init__(self, data, params):
        super().__init__(data, params)

    def init(self):
        pass

    def next(self):
        supertrend_1h = self.data['Supertrend']
        adx_1h = self.data['adx']

        supertrend_1d = self.data['Supertrend'].resample('1D').last().ffill()
        adx_1d = self.data['adx'].resample('1D').last().ffill()

        if crossover(supertrend_1h, adx_1h, crossover_above=True) and adx_1h[-1] > 21 and adx_1h[-1] > adx_1h[-2]:
            self.buy()
        elif crossover(supertrend_1d, adx_1d, crossover_above=False) and adx_1d[-1] > 21 and adx_1d[-1] > adx_1d[-2]:
            self.sell()


strategy = MyStrategy(data=df_1h, params={})

this class was generated by gpt so i have no idea how the library works,
i have tried doing some changes myself, but all were invain.

  • Have you tried specifying the parameters explicitly ? `super().__init__(data=data, params=params)` – rochard4u Jun 07 '23 at 10:27
  • i just did the error changed: Traceback (most recent call last): File "/home/runner/stock/test.py", line 160, in strategy = MyStrategy(data=df_1h, params={}) File "/home/runner/stock/test.py", line 108, in __init__ super().__init__(data=data, params=params) TypeError: Strategy.__init__() missing 1 required positional argument: 'broker' – user9110693 Jun 07 '23 at 10:31
  • 2
    *this class was generated by gpt so i have no idea how the library works* - you're likely going to need to look at how the library works. Particularly, you've inherited from the `Strategy` class in the library, and you're calling its constructor - so check the class documentation to understand what the constructor args are. – slothrop Jun 07 '23 at 10:35

1 Answers1

0

You are probably missing the self parameter in your super().__init__ call. -> super().__init__(self, data, params).

BTW: It always helps to provide an MRE when having problems like this!

EDIT1: So after a little bit of digging I found the source code without having to download shady stuff:

def __init__(self, broker, data, params):
    self._indicators = []
    self._broker: _Broker = broker
    self._data: _Data = data
    self._params = self._check_params(params)

This thing is the constructor, and you are missing the positional parameter broker. Please always read the documentation carefully, none of the provided answers on this site are trading advice and to my knowledge, using answers from here are at your own Risk.

EDIT2: This is the broker constructor to prevent this issue from reoccuring with a different class. You need to provide all the arguments as kwargs, like this:data=<your_data> and so on.

class _Broker:
    def __init__(self, *, data, cash, commission, margin,
                 trade_on_close, hedging, exclusive_orders, index):
NoBlockhit
  • 369
  • 2
  • 15