finlab_crypto
finlab_crypto copied to clipboard
There are some similar code in Strategy and Filter
__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):
...
Thanks for pointing out. I will try to merge it when I have extra time! Thank you!