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

Handling unordered timestamps (bootstrapped data)



  • Hello,

    Thank you very much for creating and open sourcing this great library, it has been very useful to me.

    I'm using Block Bootstrap to create multiple replications of OHLCV data to calculate confidence intervals for some risk-return metrics. Because blocks of OHLCV data are randomly sampled, the resulting data has unordered timestamps. I'm trying to find the best way to work with such data in Backtrader. What I have tried so far:

    1. Feeding the Pandas DataFrame of OHLCV data with randomly ordered timestamps to Backtrader. The backtest seems to run without issues but the calculation of daily returns using the TimeReturns analyzer results in a series with lots of missing dates and a very large number (e.g., 853.00) as the return of the last day. I assume that Backtrader wasn't built to work with unordered timestamps. Is that correct or should I create a bug ticket?

    2. Dropping the DateTimeIndex of the DataFrame and passing it to Backtrader without any timestamps. That didn't work as some places in the Backtrader code require timestamped data.

    3. Creating a fake range of monotonically increasing dates (using the following code) and setting that as the index of the DataFrame. This works but it seems like an ugly hack. I'm wondering if there is a better way.

    fake_index = pd.date_range(start=datetime.datetime(1980, 1, 1), periods=len(raw_ohlcv), freq='D')
    raw_ohlcv = raw_ohlcv.set_index(fake_index, drop=True)
    

    Best regards,
    Bruno


Log in to reply
 

});