Skip to Content
Mastering Python for Finance - Second Edition
book

Mastering Python for Finance - Second Edition

by James Ma Weiming
April 2019
Intermediate to advanced
426 pages
11h 13m
English
Packt Publishing
Content preview from Mastering Python for Finance - Second Edition

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:', ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python for Finance - Second Edition

Python for Finance - Second Edition

Yuxing Yan
Python for Finance

Python for Finance

Yves Hilpisch

Publisher Resources

ISBN: 9781789346466Supplemental Content