From 8d5036c5aafc79f121dcff0742edc314ae0fdaec Mon Sep 17 00:00:00 2001 From: David Brazda Date: Thu, 10 Oct 2024 09:42:25 +0200 Subject: [PATCH] Initial commit --- README.md | 5 +++++ setup.py | 11 ++++++++++ tests/vbt-anchored.py | 0 utils/__init__.py | 0 utils/vbtutils.py | 50 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+) create mode 100644 setup.py create mode 100644 tests/vbt-anchored.py create mode 100644 utils/__init__.py create mode 100644 utils/vbtutils.py diff --git a/README.md b/README.md index 2016c38..2d376da 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # ttools A Python library for tools, utilities, and helpers for my trading research workflow. + +# Modules + +- `utils.vbtutils` - contains helpers for vbtpro + - `AnchoredIndicator` - allows runing any vbt indicator in anchored mode (reset by Day, Hour etc.) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..915bd78 --- /dev/null +++ b/setup.py @@ -0,0 +1,11 @@ +from setuptools import setup, find_packages + +setup( + name='ttools', + version='0.1.0', + packages=find_packages(), + install_requires=[ + 'vectorbtpro', + # list your dependencies here + ], +) \ No newline at end of file diff --git a/tests/vbt-anchored.py b/tests/vbt-anchored.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/vbtutils.py b/utils/vbtutils.py new file mode 100644 index 0000000..8aab64f --- /dev/null +++ b/utils/vbtutils.py @@ -0,0 +1,50 @@ +import pandas as pd +import vectorbtpro as vbt + +class AnchoredIndicator: + """ + Allows to run any VBT indicator in anchored mode (reset per Day, Hour, or Minute). + """ + def __init__(self, indicator_name: str, split_by='D'): + """ + Initialize with the name of the indicator (e.g., "talib:MOM"). + + Parameters: + - indicator_name: str, the name of the vectorbt indicator. + - split_by: str, 'D' for day, 'H' for hour, 'T' for minute (default is 'D'). + """ + self.indicator_name = indicator_name + self.indicator = vbt.indicator(indicator_name) + self.split_by = split_by + + def run(self, data, split_by=None, *args, **kwargs): + """ + Run the indicator on a Series or DataFrame by splitting it by day, hour, or minute, + applying the indicator to each group, and concatenating the results. + + Parameters: + - data: pd.Series or pd.DataFrame, the input data series or dataframe (e.g., close prices). + - split_by: str, 'D' for day, 'H' for hour, 'T' for minute (default is 'D'). + - *args, **kwargs: Arguments and keyword arguments passed to the indicator. + """ + + if split_by is None: + split_by = self.split_by + + # Group by the specified frequency + if split_by == 'D': + grouped_data = data.groupby(data.index.date) + elif split_by in ['H', 'T']: + grouped_data = data.groupby(pd.Grouper(freq=split_by)) + else: + raise ValueError("Invalid split_by value. Use 'D' (day), 'H' (hour), or 'T' (minute).") + + # Run the indicator function for each group and concatenate the results + results = [] + for date, group in grouped_data: + # Run the indicator for each group's data + result = self.indicator.run(group, *args, **kwargs) + results.append(result) + + # Concatenate the results and return + return vbt.base.merging.row_stack_merge(results) \ No newline at end of file