This commit is contained in:
David Brazda
2024-10-10 10:23:05 +02:00
parent 8d5036c5aa
commit 6cfee7aa4c
6 changed files with 33 additions and 17 deletions

View File

@ -1,7 +1,19 @@
# ttools # ttools
A Python library for tools, utilities, and helpers for my trading research workflow. A Python library for tools, utilities, and helpers for my trading research workflow.
# Modules Modules:
# utils
## vbtutils
Contains helpers for vbtpro
`AnchoredIndicator` - allows runing any vbt indicator in anchored mode (reset by Day, Hour etc.)
Example usage:
```python
from ttools import AnchoredIndicator
mom_standard = vbt.indicator("talib:MOM").run(t1data.data["BAC"].close)
mom_anchored_d = AnchoredIndicator("talib:MOM", anchor='D').run(t1data.data["BAC"].close)
```
- `utils.vbtutils` - contains helpers for vbtpro
- `AnchoredIndicator` - allows runing any vbt indicator in anchored mode (reset by Day, Hour etc.)

View File

@ -2,10 +2,10 @@ from setuptools import setup, find_packages
setup( setup(
name='ttools', name='ttools',
version='0.1.0', version='0.1.1',
packages=find_packages(), packages=['ttools'],
install_requires=[ install_requires=[
'vectorbtpro', 'vectorbtpro',
# list your dependencies here # list your dependencies here
], ],
) ) #find_packages()

View File

@ -0,0 +1,3 @@
import ttools
#ttools.AnchoredIndicator("talib:MOM")

1
ttools/__init__.py Normal file
View File

@ -0,0 +1 @@
from .vbtutils import AnchoredIndicator

View File

@ -5,39 +5,39 @@ class AnchoredIndicator:
""" """
Allows to run any VBT indicator in anchored mode (reset per Day, Hour, or Minute). Allows to run any VBT indicator in anchored mode (reset per Day, Hour, or Minute).
""" """
def __init__(self, indicator_name: str, split_by='D'): def __init__(self, indicator_name: str, anchor='D'):
""" """
Initialize with the name of the indicator (e.g., "talib:MOM"). Initialize with the name of the indicator (e.g., "talib:MOM").
Parameters: Parameters:
- indicator_name: str, the name of the vectorbt indicator. - indicator_name: str, the name of the vectorbt indicator.
- split_by: str, 'D' for day, 'H' for hour, 'T' for minute (default is 'D'). - anchor: str, 'D' for day, 'H' for hour, 'T' for minute (default is 'D').
""" """
self.indicator_name = indicator_name self.indicator_name = indicator_name
self.indicator = vbt.indicator(indicator_name) self.indicator = vbt.indicator(indicator_name)
self.split_by = split_by self.anchor = anchor
def run(self, data, split_by=None, *args, **kwargs): def run(self, data, anchor=None, *args, **kwargs):
""" """
Run the indicator on a Series or DataFrame by splitting it by day, hour, or minute, 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. applying the indicator to each group, and concatenating the results.
Parameters: Parameters:
- data: pd.Series or pd.DataFrame, the input data series or dataframe (e.g., close prices). - 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'). - anchor: str, 'D' for day, 'H' for hour, 'T' for minute (default is 'D').
- *args, **kwargs: Arguments and keyword arguments passed to the indicator. - *args, **kwargs: Arguments and keyword arguments passed to the indicator.
""" """
if split_by is None: if anchor is None:
split_by = self.split_by anchor = self.anchor
# Group by the specified frequency # Group by the specified frequency
if split_by == 'D': if anchor == 'D':
grouped_data = data.groupby(data.index.date) grouped_data = data.groupby(data.index.date)
elif split_by in ['H', 'T']: elif anchor in ['H', 'T']:
grouped_data = data.groupby(pd.Grouper(freq=split_by)) grouped_data = data.groupby(pd.Grouper(freq=anchor))
else: else:
raise ValueError("Invalid split_by value. Use 'D' (day), 'H' (hour), or 'T' (minute).") raise ValueError("Invalid anchor value. Use 'D' (day), 'H' (hour), or 'T' (minute).")
# Run the indicator function for each group and concatenate the results # Run the indicator function for each group and concatenate the results
results = [] results = []

View File