pythonback-testing

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


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()

Solution

  • This code required a set_index.

    data = pd.read_csv('BTCUSDT.csv', index_col='Time', parse_dates=True)