I am hoping to get some help regarding code arrangement/organization using OOP concepts. My question is whether my usage of class/object selection/declaration and instantiation are sensible and what are the ways to make them more sensible.

Essentially there are 3 classes, and 1 main function that makes use of them.

class SigGen(object):

    def __init__(self, bars, method):
        self.bars= bars
        self.method = method
        self.rlzd = get_changes(self.bars) # this is an imported function from another module

    def gen_sig(self, *args):
        self.sig = self.method(args)

    ....maybe more methods will be created later, no other methods right now.


  1. Is the use of the get_changes function to set self.rlzd appropriate during the instantiation?

  2. Is the use of gen_sig to create "sig" attribute outside of instantiation appropriate?

    Here is the main function:

    for isym, sym in enumerate(symbol_list):
        bars= get_data_for_symbol(sym)
        for im, m in enumerate(method_list):
            strat = SigGen(sym, bars, m) 
            for iwindow, window in enumerate(windows):
                args = get_args(m, window)
                plot.create_sig_plots(strat, iwindow, window)
            sig[m.__name__] = pd.concat(df_list, axis =1) 
            if aggregate_w == 'y':
                for i_agg, agg_method in enumerate(agg_method_list):
                    agg_object = aggregator(signal_df = sig[m.__name__],
                                            rlzd = strat.rlzd,
                                            lookback = lookback_list[i_agg])
                    agg_sig[m.__name__] = agg_method(agg_object) 
                    if plot.running_agg:
                        plot.create_agg_plots( agg_sig[sym][m.__name__], rlzd,  agg_method.__name__, len(windows))
  3. Do I need to create an agg_object above (class declaration below)? The only reason I did it is that in the declaration of various methods in Aggregator class, I don't have to pass in all the values used in instantiation of the object.

    class Aggregator(object):
        def __init__(self, signal_df, rebal_freq, realized, lookback=30):
            self.all_signals_df = signal_df
            self.rlzd = realized
            self.lookback = lookback
        def running_perf(self):
            hits= np.sign(self.all_sign_df) == np.sign(self.rlzd) 
            running_performance = pd.rolling_mean(hits, self.lookback)
            agg_sig = pd.DataFrame(self.all_sig_df.lookup(running_performance.index, best_window))
            return agg_sig

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.