O'Reilly logo

Test-Driven Python Development by Siddharta Govindaraj

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Code Smells and Refactoring

This exercise asks us to refactor the Stock class and extract all the moving average related calculations into a new class.

The following is the code that we start with:

def get_crossover_signal(self, on_date): NUM_DAYS = self.LONG_TERM_TIMESPAN + 1 closing_price_list = \ self.history.get_closing_price_list(on_date, NUM_DAYS) if len(closing_price_list) < NUM_DAYS: return StockSignal.neutral long_term_series = \ closing_price_list[-self.LONG_TERM_TIMESPAN:] prev_long_term_series = \ closing_price_list[-self.LONG_TERM_TIMESPAN-1:-1] short_term_series = \ closing_price_list[-self.SHORT_TERM_TIMESPAN:] prev_short_term_series = \ closing_price_list[-self.SHORT_TERM_TIMESPAN-1:-1] long_term_ma = sum([update.value for update ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required