Writing the mean-reversion signal generators

We want our decision-making algorithm to recalculate trading signals on every price or order update. Let's create a generate_signals_and_think() method inside the MeanReversionTrader class to do this:

def generate_signals_and_think(self):    df_resampled = self.df_prices\        .resample(self.resample_interval)\        .ffill()\        .dropna()    resampled_len = len(df_resampled.index)    if resampled_len < self.mean_periods:        print(            'Insufficient data size to calculate logic. Need',            self.mean_periods - resampled_len, 'more.'        )        return    mean = df_resampled.tail(self.mean_periods).mean()[self.symbol]    # Signal flag calculation    is_signal_buy = mean > self.ask_price    is_signal_sell = mean < self.bid_price    print( 'is_signal_buy:', ...

Get Mastering Python for Finance - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.