05. Multi-Timeframe Strategy

A multiple timeframe strategy simply means that you’re looking at more than one timeframe to analyze market patterns. This can give you a big and clear picture vs getting a close-up of the action. Looking at more than one timeframe can easily become overwhelming, but if it’s done right, it’ll give you the best entries and the best profits.

Following are the benefits of multiple timeframe strategy :

  • While adding a longer time frame, it gives you confirmation of a trend direction.

  • While adding a shorter time frame, it gives more accurate (and faster) trade entry.

To create a MTF strategy, you can resample a lower TF input data into higher TFs. For eg: if you want to do analysis on daily chart while keeping reference of weekly TF, you can input day level data and resample 1W data from it. Here's an example below:

self.primary_df_w = self.primary_df.fillna(0).resample('W-FRI').agg({'open': 'first',
                                                                           'high': 'max',
                                                                           'low': 'min',
                                                                           'close': 'last',
                                                                           'volume': 'sum'
                                                                           }).ffill(axis=0).replace(to_replace=0,
                                                                                                    method='ffill')

In the example above, the primary_df containing daily data is resampled in weekly aggregated OHLCV data. In addition, the dataset is replaces NA with 0 and forward fills as well.

Below is an example where weekly RSI is calculated from the aggregated weekly data

self.primary_df_w['RSI_W'] = rsi(
            close=self.primary_df_w['close'], window=14)
        column_names = ['RSI_W']
        self.primary_df_w = self.primary_df_w.reindex(columns=column_names)

Here the weekly RSI is calculated and the weekly dataframe is filled with RSI_W data.

To merge the daily and weekly data, you can use the merge method

 self.primary_df = pd.merge(self.primary_df, self.primary_df_w,
                          left_on=[ 'datetime'],
                          right_on=[ 'datetime'],
                          how='left'
                          )
self.primary_df['RSI_W'] = self.primary_df['RSI_W'].fillna(method="ffill")

It is always a best practice to forward fill the data so that there are no missing values. Here's how the entire code snippet looks:

def setup_strategy(self):
        self.primary_df['RSI'] = rsi(
            close=self.primary_df['close'], window=14)

        self.primary_df_w = self.primary_df.fillna(0).resample('W-FRI').agg({'open': 'first',
                                                                           'high': 'max',
                                                                           'low': 'min',
                                                                           'close': 'last',
                                                                           'volume': 'sum'
                                                                           }).ffill(axis=0).replace(to_replace=0,
                                                                                                    method='ffill')
        self.primary_df_w['RSI_W'] = rsi(
            close=self.primary_df_w['close'], window=14)
        column_names = ['RSI_W']
        self.primary_df_w = self.primary_df_w.reindex(columns=column_names)

        print("Current DF is:\n", self.primary_df_w.tail(10))

        print("Testing before merge")

        self.primary_df = pd.merge(self.primary_df, self.primary_df_w,
                          left_on=[ 'datetime'],
                          right_on=[ 'datetime'],
                          how='left'
                          )

        self.primary_df['RSI_W'] = self.primary_df['RSI_W'].fillna(method="ffill")

        print("Current DF is:\n", self.primary_df.head(10))

Last updated