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

Passing historical trades to strategy



  • Hi,

    New to backtrader so still in the process of learning. I want to pass historical trades as a separate dataframe to my strategy along with ohlcv dataframe. Could you link me to an article that describes how I may be able to do that and then reference the columns in the strategy class?

    I do,

        data = bt.feeds.PandasData(dataname=trades)
        # trades is a pandas dataframe with ['timestamp', 'side', 'price', 'amount'] as the columns.
        cerebro.adddata(data)
    

    My Strategy Class is as follows:

    class StrategyOne(bt.Strategy):
    
        def __init__(self):
            self.dataprice = self.datas[0].price
    
        def log(self, text, datetime=None):
            datetime = datetime or self.datas[0].datetime(0)
            print('{}, {}'.format(datetime, text))
    
        def next(self):
            self.log('{} Close: {}'.format(len(self), self.dataprice[0]))
    

    When I run the strategy, it says
    AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'price'

    What would be the correct way to approach this?

    Many thanks





  • @vladisld aah thanks so much. Let me try it out. Just to clarify (and I should have mentioned this previously) by historical trades, I meant the market trades and not just my trades. So, the 2 dataframes will have different time indexing. One way, is that i group the trades by ohlcv time intervals so they have the same time indexing as ohlcv data and json dump all the trades for the particular time intervals as another column into ohlcv but it will be nice to have the flexibility to pass two dataframe containing different information independent of each others time indexing if that makes sense



  • For adding in an extra line you can find information here.

    Of course with different time frames, you will need to resample your date, information for which can be found here.

    If you still have trouble let us know and give us sample data. But give the above a try first.


Log in to reply
 

});