finlab-python / finlab_crypto

Documentation
https://finlab-python.github.io/finlab_crypto/index.html
GNU General Public License v3.0
267 stars 98 forks source link

There are some similar code in Strategy and Filter #8

Open jrycw opened 3 years ago

jrycw commented 3 years ago

__init__, __call__ , set_parameters and show_parameters methods looks similar in Strategy and Filter. My suggestion is using Inheritance to refractor the code as following:

class BaseDec:

    delattr_clsname = {'Strategy'}

    def __init__(self, **default_parameters):
        self.func = None
        self._variables = None
        self.filters = {}
        self._default_parameters = default_parameters
        self.set_parameters(default_parameters)

    def __call__(self, func):
        self.func = func
        return self

    def set_parameters(self, variables):
        if type(self).__name__ in self.delattr_clsname:
          stop_vars = ['sl_stop', 'tp_stop', 'ts_stop']
          for svar in stop_vars:
              if hasattr(self, svar):
                  delattr(self, svar)

        if variables:
            for key, val in variables.items():
                setattr(self, key, val)

        self._variables = variables

    def show_parameters(self):
        print(self._variables)

class Filter(BaseDec):
    ...

class Strategy(BaseDec):
    ...
koreal6803 commented 3 years ago

Thanks for pointing out. I will try to merge it when I have extra time! Thank you!