kernc / backtesting.py

:mag_right: :chart_with_upwards_trend: :snake: :moneybag: Backtest trading strategies in Python.
https://kernc.github.io/backtesting.py/
GNU Affero General Public License v3.0
5.39k stars 1.05k forks source link

How to use resample_apply with the pandas 'origin' parameter #952

Closed houseofai closed 1 year ago

houseofai commented 1 year ago

Expected Behavior

Pandas allows an origin parameter in its resample method to adjust the grouping.

from collections import OrderedDict
df.resample('1H', origin='start').agg(
    OrderedDict([
        ('Open', 'first'),
        ('High', 'max'),
        ('Low', 'min'),
        ('Close', 'last'),
    ])
).head()

Which is useful if I don't want the grouping to start on a rounded time. e.g. on 15mins data, the resampling is every hour, starting at 10:30: Screenshot from 2023-03-30 12-57-47

Actual Behavior

With the out-of-the-box resampling method the output is (as expected):

resample_apply('1H', lambda x:x, df, agg=OrderedDict([
        ('Open', 'first'),
        ('High', 'max'),
        ('Low', 'min'),
        ('Close', 'last'),
    ])).head()

gives: Screenshot from 2023-03-30 13-12-33

How can I pass the origin parameter to the resample_apply method?

houseofai commented 1 year ago

Oh.... I just resampled the df first... posting my solution in case someone needs it:

df = self.data.df
data = df.resample('1H', origin='start').agg(
   OrderedDict([
                ('Open', 'first'),
                ('High', 'max'),
                ('Low', 'min'),
                ('Close', 'last'),
            ])
        )
kernc commented 1 year ago

You should only work with origin='end' (as we achieve with label='right' :thinking:) to avoid look-ahead bias.

kevwilhelm95 commented 1 week ago

However, working with label = "right" removes that fractal nature of the chart with multiple timeframes and it doesn't match what we are looking at on a real chart. An option to include label = 'left' should be included.