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.04k stars 987 forks source link

how to get correct 15min value by using resample_apply with 1min data #1143

Open zha0yangchen opened 1 month ago

zha0yangchen commented 1 month ago

Expected Behavior

Expected data should same as this high = pd.DataFrame({'high': self.data.High}, index=self.data.index).resample('15min').agg('max').dropna().values[:, 0][-500:]

Actual Behavior

but when i use self.High = resample_apply('15min', None, self.data.High) and print( self.High[-1], high[-1]), the value of self.high is incorrect, but high[-1] is corrct.

Steps to Reproduce

1. 2. 3.


python code goes here

Additional info

BL0987 commented 3 weeks ago

I get better results by resampling the dataframe prior to using it in the backtest:

e.g

import pandas as pd import yfinance as yf

df = yf.download('AAPL', period='5y', interval='1d') # rounding=True

df1 = df.resample('2d').agg({'Open': 'first', 'High': 'max', 'Low': 'min', 'Close': 'last', 'Volume': 'sum'})

Hope it helps