any pd freqs allowed
This commit is contained in:
2
setup.py
2
setup.py
@ -2,7 +2,7 @@ from setuptools import setup, find_packages
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='ttools',
|
name='ttools',
|
||||||
version='0.1.1',
|
version='0.1.2',
|
||||||
packages=['ttools'],
|
packages=['ttools'],
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'vectorbtpro',
|
'vectorbtpro',
|
||||||
|
|||||||
@ -12,6 +12,8 @@ class AnchoredIndicator:
|
|||||||
Parameters:
|
Parameters:
|
||||||
- indicator_name: str, the name of the vectorbt indicator.
|
- indicator_name: str, the name of the vectorbt indicator.
|
||||||
- anchor: 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').
|
||||||
|
Any valid frequency string ('D', 'H', 'T', 'W', etc.). can be used as it uses pd.Grouper(freq=anchor)
|
||||||
|
see https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases
|
||||||
"""
|
"""
|
||||||
self.indicator_name = indicator_name
|
self.indicator_name = indicator_name
|
||||||
self.indicator = vbt.indicator(indicator_name)
|
self.indicator = vbt.indicator(indicator_name)
|
||||||
@ -24,20 +26,20 @@ class AnchoredIndicator:
|
|||||||
|
|
||||||
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).
|
||||||
- anchor: 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'). Override for anchor on the instance.
|
||||||
|
Any valid frequency string ('D', 'H', 'T', 'W', etc.). can be used as it uses pd.Grouper(freq=anchor)
|
||||||
|
see https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases
|
||||||
- *args, **kwargs: Arguments and keyword arguments passed to the indicator.
|
- *args, **kwargs: Arguments and keyword arguments passed to the indicator.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if anchor is None:
|
if anchor is None:
|
||||||
anchor = self.anchor
|
anchor = self.anchor
|
||||||
|
|
||||||
# Group by the specified frequency
|
# Use pd.Grouper for splitting by any valid frequency string
|
||||||
if anchor == 'D':
|
try:
|
||||||
grouped_data = data.groupby(data.index.date)
|
|
||||||
elif anchor in ['H', 'T']:
|
|
||||||
grouped_data = data.groupby(pd.Grouper(freq=anchor))
|
grouped_data = data.groupby(pd.Grouper(freq=anchor))
|
||||||
else:
|
except ValueError as e:
|
||||||
raise ValueError("Invalid anchor value. Use 'D' (day), 'H' (hour), or 'T' (minute).")
|
raise ValueError(f"Invalid anchor value: {anchor}. Check pandas Grouper frequencies.") from e
|
||||||
|
|
||||||
# Run the indicator function for each group and concatenate the results
|
# Run the indicator function for each group and concatenate the results
|
||||||
results = []
|
results = []
|
||||||
|
|||||||
Reference in New Issue
Block a user