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

Error extending pandas datafeed example



  • Hello, im trying to use a custom dataframe with columns:

    Open          High           Low         Close    volume  quote_asset_volume  number_of_trades  \ taker_buy_base_asset_volume  \taker_buy_quote_asset_volume  p0  p1  p2
    

    i have tried with: (also tried with different name of columns but the result is the same:)

    class PandasData(feed.PandasData):
        params = (
            ('datetime', None),
            ('open', -1),
            ('high', -1),
            ('low', -1),
            ('close', -1),
            ('volume', -1),
            ('quote_asset_volume', -1),
            ('number_of_trades', -1),
            ('taker_buy_base_asset_volume', -1),
            ('taker_buy_quote_asset_volume', -1),
            ('p0', -1),
            ('p1', -1),
            ('p2', -1)
        )
    
    data0 = feed.PandasData(dataname=testVF)
    cerebro.adddata(data0)
    

    im getting:

    'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'p0'
    

    i have tried using: self.close = self.data.l.p0 (in the strategy) and

    lines = ('quote_asset_volume', 'number_of_trades', 'taker_buy_base_asset_volume', 'taker_buy_quote_asset_volume', 'p0')
    

    (found here https://community.backtrader.com/topic/1777/struggling-to-implement-custom-pandas-data-feed/10),

    but i cant manage to get p0 to work.
    also i have not found any reference in the documentation on how to get multiple columns in the strategy:
    (for example in pandas id use

    .iloc[x:x1]
    

    i have tried with just data[x:y] but it gives problem and return just 1 column like data[0]



  • @lorrp1
    trying with:

    data1 = PandasDataa(dataname=testV[0:100])
    #print (data0)
    cerebro.adddata(data1)
    

    and:

    class PandasDataa(feed.DataBase):
    
        lines = ('quote_asset_volume', 'number_of_trades', 'taker_buy_base_asset_volume', 'taker_buy_quote_asset_volume', 'p0', 'p1', 'p2')
        params = (
            ('datetime', 0), #tried all with either -1 or the column index in the dataframe
            ('open', 1),
            ('high', 2),
            ('low', 3), 
            ('close', 4),
            ('volume', 5),
        )
    

    or removing the lines = {}
    return

    671         for i in range(start, end):
    --> 672             dst[i] = src[i + ago]
      673 
      674 
    
    IndexError: array index out of range
    

    or

    AttributeError: 'Plot_OldSync' object has no attribute 'mpyplot'
    

    so now its not even able to get the open/close/high/low like before



  • I"m having a bit of a hard time following you. But try this format out.

    import backtrader as bt
    
    class CustomPandasLoader(bt.feeds.PandasData):
        lines = (
            'quote_asset_volume',
            'number_of_trades',
            'taker_buy_base_asset_volume',
            'taker_buy_quote_asset_volume',
            'p0',
            'p1',
            'p2',
        )
        params = (
            ('quote_asset_volume', -1),
            ('number_of_trades', -1),
            ('taker_buy_base_asset_volume', -1),
            ('taker_buy_quote_asset_volume', -1),
            ('p0', -1),
            ('p1', -1),
            ('p2', -1)
        )
        datafields = bt.feeds.PandasData.datafields + (
            [
                'quote_asset_volume',
                'number_of_trades',
                'taker_buy_base_asset_volume',
                'taker_buy_quote_asset_volume',
                'p0',
                'p1',
                'p2',
            ]
        )
        
    data = CustomPandasLoader(
            dataname=yourdataframe
        )
    cerebro.adddata(data)
    

Log in to reply
 

});