From ea2596f8ffd2d868614c32dc779af1997ae1667c Mon Sep 17 00:00:00 2001 From: SoftDevDanial <64815604+SoftDevDanial@users.noreply.github.com> Date: Tue, 8 Sep 2020 00:17:34 +0800 Subject: [PATCH] Create inbar.py --- pandas_ta/momentum/inbar.py | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 pandas_ta/momentum/inbar.py diff --git a/pandas_ta/momentum/inbar.py b/pandas_ta/momentum/inbar.py new file mode 100644 index 0000000..3b8abd1 --- /dev/null +++ b/pandas_ta/momentum/inbar.py @@ -0,0 +1,61 @@ +import pandas as pd +from pandas_ta.utils import get_offset , verify_series , zero + +def inbar(self , open , high ,low , close , offset = None , **kwargs ): + """Indicator: Inside Bar""" + # Validate arguments + close = verify_series(close).apply(zero) + open = verify_series(open).apply(zero) + high = verify_series(high).apply(zero) + low = verify_series(low).apply(zero) + offset = get_offset(offset) + prevBar = 1 + + # Calculate Result + bodyStat = (close >= open).rename('bodystat').replace({True: 1 , False:-1}) + isIn = ((high < high.shift(prevBar)) & (low > low.shift(prevBar))).rename('isin') + res = pd.Series(index = close.index , dtype = 'int64') + + for i in close.index: + if isIn[i] == True: + res[i] = bodyStat[i] + else: + res[i] = 0 + + # Offset + if offset != 0: + res = res.shift(offset) + + # Handle fills + if 'fillna' in kwargs: + res.fillna(kwargs['fillna'], inplace=True) + + if 'fill_method' in kwargs: + res.fillna(method=kwargs['fill_method'], inplace=True) + + # Name and Categorize it + res.name = "InBar" + res.category = 'insidebar' + + return res + +inbar.__doc__ = \ +"""Inside Bar +Sources: + https://www.tradingview.com/script/IyIGN1WO-Inside-Bar/ +Calculation: + Default Inputs: + drift=1 + isIn = ((high < high.shift(prevBar)) & (low > low.shift(prevBar))) + bodyStat = (close >= open) +Args: + high (pd.Series): Series of 'high's + low (pd.Series): Series of 'low's + close (pd.Series): Series of 'close's + offset (int): How many periods to offset the result. Default: 0 +Kwargs: + fillna (value, optional): pd.DataFrame.fillna(value) + fill_method (value, optional): Type of fill method +Returns: + pd.Series: New feature +"""