From dab81f0ac14a2ca84fd8474c8f30f8242dd84d6d Mon Sep 17 00:00:00 2001 From: David Brazda Date: Thu, 17 Oct 2024 12:20:57 +0200 Subject: [PATCH] isrising,isfalling --- README.md | 4 +++- setup.py | 2 +- ttools/__init__.py | 2 +- ttools/vbtutils.py | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d53c20e..f0dbfaa 100644 --- a/README.md +++ b/README.md @@ -39,4 +39,6 @@ forced_exits_window = create_mask_from_window(exits, forced_exit_start, forced_e exits = exits | forced_exits_window exits.tail(20) -``` \ No newline at end of file +``` + +`isrising(series,n)`,`isfalling(series, n)` - returns mask where the condition is satisfied of consecutive rising or falling elements \ No newline at end of file diff --git a/setup.py b/setup.py index 7ed6305..ff923f7 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup, find_packages setup( name='ttools', - version='0.1.8', + version='0.1.9', packages=find_packages(), install_requires=[ 'vectorbtpro', diff --git a/ttools/__init__.py b/ttools/__init__.py index 4b808fc..6ee21d5 100644 --- a/ttools/__init__.py +++ b/ttools/__init__.py @@ -1 +1 @@ -from .vbtutils import AnchoredIndicator, create_mask_from_window \ No newline at end of file +from .vbtutils import AnchoredIndicator, create_mask_from_window, isrising, isfalling \ No newline at end of file diff --git a/ttools/vbtutils.py b/ttools/vbtutils.py index d368ff7..76f1607 100644 --- a/ttools/vbtutils.py +++ b/ttools/vbtutils.py @@ -4,6 +4,42 @@ import pandas_market_calendars as mcal from typing import Any import datetime +def isrising(series: pd.Series, n: int) -> pd.Series: + """ + Checks if a series is rising over a given window size. + + Parameters + ---------- + series : pd.Series + Input series + n : int + Window size + + Returns + ------- + pd.Series + Boolean mask indicating when the series is falling + """ + return series.rolling(n).apply(lambda x: (x == sorted(x, reverse=True)).all(), raw=False).fillna(False).astype(bool) + +def isfalling(series: pd.Series, n: int) -> pd.Series: + """ + Checks if a series is falling over a given window size. + + Parameters + ---------- + series : pd.Series + Input series + n : int + Window size + + Returns + ------- + pd.Series + Boolean mask indicating when the series is falling + """ + return series.rolling(n).apply(lambda x: (x == sorted(x, reverse=True)).all(), raw=False).fillna(False).astype(bool) + def create_mask_from_window(series: Any, entry_window_opens:int, entry_window_closes:int, use_cal: bool = True): """ Accepts series and window range (number of minutes from market start) and returns boolean mask denoting