pandas-ta icon indicating copy to clipboard operation
pandas-ta copied to clipboard

Fair Value Gap Indicator

Open alighten-dev opened this issue 2 years ago • 5 comments

I'm running 0.3.14b0

I wrote a simple Fair Value Gap indicator. If you are interested in adding an additional indicator, this might be interesting. https://trendspider.com/blog/fair-value-gap-basics/

Takes a minimum percentage gap size (min_gap) It finds bull values where (Low(0) - High(2)) > min_gap It finds bear values where Low(2) - High(0) > min_gap

returns a single value of the percentage gap, FVG. Positive if bull gap, negative if bear gap.

from pandas_ta.utils import get_offset, verify_series
import numpy as np
import pandas as pd



def fvg(high, low, close, min_gap=None, **kwargs):
    """Indicator: FVG"""
    # Validate Arguments
    min_gap = int(min_gap) if min_gap and min_gap > 0 else 1    
    
    high = verify_series(high)
    low = verify_series(low)
    close = verify_series(close)

    if high is None or low is None or close is None: return

    min_gap_pct = min_gap/100
    min_gap_dol = min_gap_pct*close

    # bullish FVG
    fvg_bull = (low - high.shift(2))
    fvg_bull_result = ((fvg_bull / close) * 100)

    fvg_bear =  (low.shift(2) - high)
    fvg_bear_result = ((fvg_bear / close) * -100)

    fvg_bull_array = np.where(fvg_bull > min_gap_dol, fvg_bull_result, np.NaN)
    fvg_bull_series = pd.Series(fvg_bull_array)
    fvg_bull_series.index = high.index

    fvg_bear_array = np.where(fvg_bear > min_gap_dol, fvg_bear_result, np.NaN)
    fvg_bear_series = pd.Series(fvg_bear_array)
    fvg_bear_series.index = high.index

    fvg = fvg_bull_series.fillna(fvg_bear_series)


    # Handle fills
    if "fillna" in kwargs:
        fvg.fillna(kwargs["fillna"], inplace=True)
    if "fill_method" in kwargs:
        fvg.fillna(method=kwargs["fill_method"], inplace=True)

    # Name & Category
    fvg.name = f"FVG_{min_gap}"
    fvg.category = "trend"

    return fvg

def fvg_method(self, **kwargs):
    # high, low, close, min_gap=None,
    high = self._get_column(kwargs.pop("high", "high"))
    low = self._get_column(kwargs.pop("low", "low"))
    close = self._get_column(kwargs.pop("close", "close"))
    result = fvg(high=high, low=low, close=close, **kwargs)
    return self._post_process(result, **kwargs)


fvg.__doc__ = \
"""FVG
Calculates the Fair Value Gap
Sources:
    "https://trendspider.com/blog/fair-value-gap-basics/"
Calculation:
    Default Inputs:
       None
Args:
    open (pd.Series): Series of 'open's
    high (pd.Series): Series of 'high's
    low (pd.Series): Series of 'low's
    close (pd.Series): Series of 'close's
    min_gap (int): Minimum FVG Percentage of latest close
    
Kwargs:
    fillna (value, optional): pd.DataFrame.fillna(value)
    fill_method (value, optional): Type of fill method
Returns:
    pd.Series: New feature generated.
"""

alighten-dev avatar Nov 29 '22 19:11 alighten-dev

Hello @alighten-dev,

Sure. I have been preoccupied with other matters, so hopefully someone can help make a PR with the code you shared here. 😎

Kind Regards, KJ

twopirllc avatar Dec 05 '22 16:12 twopirllc

Hey everyone,

The implemented code by @alighten-dev, is working with no errors for me, and has good performance and benchmark, but it seems to create different outputs with tradingview's.

Thanks

aligheshlaghi97 avatar Jun 01 '23 17:06 aligheshlaghi97

Hey everyone,

The implemented code by @alighten-dev, is working with no errors for me, and has good performance and benchmark, but it seems to create different outputs with tradingview's.

Thanks

Why do you think there is a difference between python coded indicator and Trading View's pine coded indicator? Also, this is not the first case that is having this difference, so want to know why does this difference occur.

crazypythonista avatar Feb 25 '24 11:02 crazypythonista

we need this one

phpmac avatar Feb 27 '24 02:02 phpmac

tradeview

How do you use this

phpmac avatar Feb 27 '24 15:02 phpmac