I have a number of classes that implement smoothing techniques on a series of prices.
I am trying to figure the best way of implementing any of these smoothing classes within the __call__
function of another class, where some operation is then performed on the smoothed series:
i.e.
class NoSmoother:
def __init__(self, prices):
self.prices = prices
def __call__(self):
return self.prices
class MASmoother:
def __init__(self, prices):
self.prices = prices
def __call__(self, window):
return self.prices.rolling(window).mean().to_frame("price")
class ThatDoesSomethingWithSmoothedPrices():
def __init__(self, prices):
self.prices = prices
def __call__(self, smoother=ma_smoother, window=3)
smoothed_prices = SomeBuilderClassThatCallsTheCorrectSmootherClass()
As you can see, I would need the factory/builder class to implement NoSmoother
if say smoother = None, otherwise, it would call the appropriate smoothing class. Of course, the returned object can vary from simple to complex, for example, if I smooth the prices using a Kalman Filter, the class can expect many more parameters.
Currently, my code instantiates class ThatDoesSomethingWithSmoothedPrices()
with a price series, then calls by passing a **config.
Desired Output:
Ideally, I would like to be able to call any smoothing class from within the call function of
class ThatDoesSomethingWithSmoothedPrices()
.
Example implementation:
configs = {'smoother': MASmoother, 'window': 3}
processor = ThatDoesSomethingWithSmoothedPrices(prices)
output = processor(**config)
My attempt:
class Smoother:
def __init__(self, prices):
self.prices = prices
def __call__(self, smoother, *args, **kwargs):
return partial(smoother, **kwargs)
def ma_smoother(self, window: int = 3):
return self.prices.rolling(window).mean().to_frame("price")
def no_smoother(self):
return self.prices
class ThatDoesSomethingWithSmoothedPrices:
def __init__(self, prices):
self.prices = prices
def __call__(self, smooth_method = 'no_smoother'):
smoother = Smoother(prices)
prices_smoothed = smoother(**configs)
# do other things
if __name__ == '__main__':
configs = {'smoother': 'ma_smoother', window=3}
label = ThatDoesSomethingWithSmoothedPrices(**configs)
Any help greatly appreciated.
ThatDoesSomethingWithSmoothedPrices()
naming the smoothing method to be applied or none at all. – ContrerasThatDoesSomethingWithSmoothedPrices
, remains a class. What I had envisioned is some form of base class, with a lookup i.e NamedTuple that when called withinThatDoesSomethingWithSmoothedPrices
knows what smoother to apply? Hopefully I am making sense. – ContrerasThatDoesSomethingWithSmoothedPrices
to be a class. It can simply be a factory function which returns the correct smoother. – SideroliteThatDoesSomethingWithSmoothedPrices
takes the smoothed prices and creates labels off the back of these - it calls many different methods in order to construct the label i.e. it's purpose is associated with something quite different from simply smoothing the price. – Contreras