edtechre / pybroker

Algorithmic Trading in Python with Machine Learning

Home Page:https://www.pybroker.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Indicator might be wrong when applying filter on backtesting data

robin2008 opened this issue · comments

The backtest method supports between_time and days to filter data

            between_time: ``tuple[str, str]`` of times of day e.g.
                ('9:30', '16:00') used to filter the backtesting data
                (inclusive).
            days: Days (e.g. ``"mon"``, ``"tues"`` etc.) used to filter the
                backtesting data.

And when walkforward, then data sources seems to be filtered first and then computing indicators:

df = self._filter_dates(
df=df,
start_date=start_dt,
end_date=end_dt,
between_time=between_time,
days=day_ids,
)
tf_seconds = to_seconds(timeframe)
indicator_data = self._fetch_indicators(
df=df,
cache_date_fields=CacheDateFields(
start_date=start_dt,
end_date=end_dt,
tf_seconds=tf_seconds,
between_time=between_time,
days=day_ids,
),
disable_parallel=disable_parallel,
)

It means that the indicator calculation is based on the filtered discrete BAR data, although this could reduce the caculation, but I guess this is not an expected behavior as most of the indicators are senstive to the continuity of time series data.

Hi @robin2008,

This is the intended behavior. By default, you should compute indicators on the same data that you execute with your strategy.

If you want to compute indicators on unfiltered data, then you can omit the between_time filter and instead filter using ctx.dt in your execution function.

@edtechre

Thanks for the information, good to know it's intended behavior.