For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

How can I use transform a pandas series into a line in bt.Indicator



  • Hi,

    I was used to do calculations with pandas data frame and the original talib. Then returned the results as a pandas series / python list, hoping it could work as an indicator in the backtrader framework.
    I found it raises exception if I directly set the value to a backtrader line in Indicator __init__. I was wondering if there were any method that I can do the transformation.

    Many thanks!



  • @yoghurtshawn Maybe this will help, it takes a pandas dataframe and converts it to data that backtrader can work with:

    from pandas_datareader import data as pdr
    import backtrader as bt
    
    class PandasData(bt.feeds.PandasData):
        lines = ('adj_close',)
    
        params = (
            ('datetime', None),
            ('open','open'),
            ('high','high'),
            ('low','low'),
            ('close','close'),
            ('volume','volume'),
            ('openinterest',None),
            ('adj_close','adj_close')
        )
    
    df = pdr.get_data_yahoo('AAPL', start=datetime(2017, 8, 13), end=datetime(2018, 8, 14),interval='d')
    
    # Set my column names
    df.columns=['high', 'low', 'open', 'close', 'volume', 'adj_close']
    
    data=PandasData(dataname=df)
    cerebro.adddata(data)
    
    


  • Sorry for the late reply.

    My problem is, say, I already have a lot of codes available constructing all kinds of self defined indicators, but all these are not related to backtrader system at all. All these return pandas series as the exact indicator values.

    How can I make these predefined indicators compatible with backtrader?

    In my opinion, it requires a method like convert_series_to_line, which can make a pandas series into a linebuffer object (maybe call it in the init of strategy class).

    Is there anything availble? Or how should I do?

    Many thanks!



  • On the first glance you can write bt indicator which reads your pandas data and at each next() access rows of your pandas dataframe and assign values to bt lines.


  • administrators

    See: Docs - Indicator Development Only the implementation of next is required.



  • @ab_trader Yes, that's one solution, but not vectorized one, I was wondering if there were any other with better performance.

    Many thanks!



  • @backtrader Would you offer some vectorized implementation?
    Many thanks!


Log in to reply
 

});