MCPcopy Create free account
hub / github.com/kernc/backtesting.py / resample_apply

Function resample_apply

backtesting/lib.py:207–335  ·  view source on GitHub ↗

Apply `func` (such as an indicator) to `series`, resampled to a time frame specified by `rule`. When called from inside `backtesting.backtesting.Strategy.init`, the result (returned) series will be automatically wrapped in `backtesting.backtesting.Strategy.I` wrapper method.

(rule: str,
                   func: Optional[Callable[..., Sequence]],
                   series: Union[pd.Series, pd.DataFrame, _Array],
                   *args,
                   agg: Optional[Union[str, dict]] = None,
                   **kwargs)

Source from the content-addressed store, hash-verified

205
206
207def resample_apply(rule: str,
208 func: Optional[Callable[..., Sequence]],
209 series: Union[pd.Series, pd.DataFrame, _Array],
210 *args,
211 agg: Optional[Union[str, dict]] = None,
212 **kwargs):
213 """
214 Apply `func` (such as an indicator) to `series`, resampled to
215 a time frame specified by `rule`. When called from inside
216 `backtesting.backtesting.Strategy.init`,
217 the result (returned) series will be automatically wrapped in
218 `backtesting.backtesting.Strategy.I`
219 wrapper method.
220
221 `rule` is a valid [Pandas offset string] indicating
222 a time frame to resample `series` to.
223
224 [Pandas offset string]: \
225http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases
226
227 `func` is the indicator function to apply on the resampled series.
228
229 `series` is a data series (or array), such as any of the
230 `backtesting.backtesting.Strategy.data` series. Due to pandas
231 resampling limitations, this only works when input series
232 has a datetime index.
233
234 `agg` is the aggregation function to use on resampled groups of data.
235 Valid values are anything accepted by `pandas/resample/.agg()`.
236 Default value for dataframe input is `OHLCV_AGG` dictionary.
237 Default value for series input is the appropriate entry from `OHLCV_AGG`
238 if series has a matching name, or otherwise the value `"last"`,
239 which is suitable for closing prices,
240 but you might prefer another (e.g. `"max"` for peaks, or similar).
241
242 Finally, any `*args` and `**kwargs` that are not already eaten by
243 implicit `backtesting.backtesting.Strategy.I` call
244 are passed to `func`.
245
246 For example, if we have a typical moving average function
247 `SMA(values, lookback_period)`, _hourly_ data source, and need to
248 apply the moving average MA(10) on a _daily_ time frame,
249 but don't want to plot the resulting indicator, we can do:
250
251 class System(Strategy):
252 def init(self):
253 self.sma = resample_apply(
254 'D', SMA, self.data.Close, 10, plot=False)
255
256 The above short snippet is roughly equivalent to:
257
258 class System(Strategy):
259 def init(self):
260 # Strategy exposes `self.data` as raw NumPy arrays.
261 # Let's convert closing prices back to pandas Series.
262 close = self.data.Close.s
263
264 # Resample to daily resolution. Aggregate groups

Callers 3

initMethod · 0.90
initMethod · 0.90
test_resample_applyMethod · 0.90

Calls 2

_as_strFunction · 0.85
strategy_IFunction · 0.85

Tested by 2

initMethod · 0.72
test_resample_applyMethod · 0.72