edtechre / pybroker

Algorithmic Trading in Python with Machine Learning
https://www.pybroker.com
Other
2.09k stars 262 forks source link

Indicator might be wrong when applying filter on backtesting data #102

Closed robin2008 closed 8 months ago

robin2008 commented 8 months ago

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: https://github.com/edtechre/pybroker/blob/6b659b555edaf6f2c1f195a5b19fe3c62eb9555f/src/pybroker/strategy.py#L1204-L1222

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.

edtechre commented 8 months ago

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.

robin2008 commented 8 months ago

@edtechre

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