KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Technical Analysis using embedPy

  • Technical Analysis using embedPy

    Posted by hoffmanroni on June 28, 2021 at 12:00 am

    Hello All,

    I’m just starting out with embedPy and wondering if aoneone can help.  I’m trying to use a module in python for some technical analysis.

    I was trying to use this module

    https://technical-analysis-library-in-python.readthedocs.io/en/latest/

    I’ve just got some bar data something like

    t:([]sym:30#`A;open:30?50f;high:30?50f;low:30?50f;close:30?50f;vol:30?10000)

    I’m having some difficulty passing the dataframe back and forth and can’t seem to get it to work.  Wondering if someone could help me through this first example on their website, making this work properly from q.

    import pandas as pd from ta.utils 
    import dropna from ta.volatility 
    import BollingerBands 
    # Load datas 
    df = pd.read_csv('ta/tests/data/datas.csv', sep=',') 
    # Clean NaN values 
    df = dropna(df) 
    # Initialize Bollinger Bands Indicator 
    indicator_bb = BollingerBands(close=df["Close"], window=20, window_dev=2) 
    # Add Bollinger Bands features 
    df['bb_bbm'] = indicator_bb.bollinger_mavg() 
    df['bb_bbh'] = indicator_bb.bollinger_hband() 
    df['bb_bbl'] = indicator_bb.bollinger_lband() 
    # Add Bollinger Band high indicator 
    df['bb_bbhi'] = indicator_bb.bollinger_hband_indicator() 
    # Add Bollinger Band low indicator 
    df['bb_bbli'] = indicator_bb.bollinger_lband_indicator()

     

    Many thanks!

     

    hoffmanroni replied 9 months ago 1 Member · 1 Reply
  • 1 Reply
  • hoffmanroni

    Member
    June 29, 2021 at 12:00 am

    FYI if anyone is interested I was able to get this working now

    py script

    import pandas as pd from ta.utils 
    import dropna from ta.volatility 
    import BollingerBands 
    def returnBB(df, window=20, window_dev=2): 
        indicator_bb = BollingerBands(close=df["close"], window=20, window_dev=2) 
        # Add Bollinger Bands features 
        df['bb_bbm'] = indicator_bb.bollinger_mavg() 
        df['bb_bbh'] = indicator_bb.bollinger_hband() 
        df['bb_bbl'] = indicator_bb.bollinger_lband() 
        # Add Bollinger Band high indicator 
        df['bb_bbhi'] = indicator_bb.bollinger_hband_indicator() 
        # Add Bollinger Band low indicator 
        df['bb_bbli'] = indicator_bb.bollinger_lband_indicator() 
    return df

    and then q script

    //load above .p script l bb.p

    //make returnBB py func callable in q

    func:.p.get`returnBB

    //conv t to dataframe, pass to the py func and then conv back to qtable .ml.df2tab[func[.ml.tab2df[t]]]

Log in to reply.