From efe516791aa66b9e653f1bca953df88d098c8934 Mon Sep 17 00:00:00 2001 From: Kernc Date: Fri, 17 Jan 2020 02:08:19 +0100 Subject: [PATCH] ENH: Add parameter agg= to lib.resample_apply() --- backtesting/lib.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/backtesting/lib.py b/backtesting/lib.py index 69250e6..fce3c90 100644 --- a/backtesting/lib.py +++ b/backtesting/lib.py @@ -133,7 +133,9 @@ def quantile(series, quantile=None): def resample_apply(rule: str, func: Callable, series, - *args, **kwargs): + *args, + agg='last', + **kwargs): """ Apply `func` (such as an indicator) to `series`, resampled to a time frame specified by `rule`. When called from inside @@ -155,6 +157,10 @@ def resample_apply(rule: str, resampling limitations, this only works when input series has a datetime index. + `agg` is the aggregation function to use on resampled groups of data. + Default value is `"last"`, which may be suitable for closing prices, + but you might prefer another (e.g. 'max' for peaks, or similar). + Finally, any `*args` and `**kwargs` that are not already eaten by implicit `backtesting.backtesting.Strategy.I` call are passed to `func`. @@ -203,7 +209,7 @@ def resample_apply(rule: str, 'or a `Strategy.data.*` array' series = series.to_series() - resampled = series.resample(rule, label='right').agg('last').dropna() + resampled = series.resample(rule, label='right').agg(agg).dropna() resampled.name = _as_str(series) + '[' + rule + ']' # Check first few stack frames if we are being called from