`pd.DateTimeIndex` is advised in backtesting.py library

Question:

I am using the backtesting.py library https://kernc.github.io/backtesting.py/doc/backtesting/index.html

What is this error?
UserWarning: Data index is not datetime. Assuming simple periods, but ‘pd.DateTimeIndex’ is advised. bt = Backtest(data, test2, cash=100000, commission=.002)

import pandas as pd
from backtesting import Strategy
from backtesting import Backtest


data = pd.read_csv('I:/algotrading/BTCUSDT.csv')
data.columns = ['Time','Open','High','Low','Close'];


class test2(Strategy):
    def init(self):
        pass

    def next(self):
        # OHLC
        self.open = self.data.Open
        self.high = self.data.High
        self.low = self.data.Low
        self.close = self.data.Close

        if(self.close > self.open):
                self.position.close()
                self.buy()

        elif(self.close < self.open):
                self.position.close()
                self.sell()


bt = Backtest(data, test2, cash=100000, commission=.002)
stats = bt.run()
print(stats)
bt.plot()
Asked By: Mojtaba Hajilari

||

Answers:

This code required a set_index.

data = pd.read_csv('BTCUSDT.csv', index_col='Time', parse_dates=True)
Answered By: Mojtaba Hajilari

this fixed the issue for me

data = pd.read_csv('BTCUSDT.csv')
data.index = pd.DatetimeIndex(df['Time'])
Answered By: PHANTOM-X
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.