backtrader2 / backtrader

Python Backtesting library for trading strategies
https://www.backtrader.com
GNU General Public License v3.0
221 stars 52 forks source link

bug: resample and plot error #83

Open dindom999 opened 2 years ago

dindom999 commented 2 years ago

It happened when resample 5m to 60m and plot. I am not sure it's from resampling or plotting. Any one can reproduce it by using the sample data in source code.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import time

import backtrader as bt

class St(bt.Strategy):
    params = dict(multi=True)

    def __init__(self):
        self.pp = pp = bt.ind.PivotPoint(self.data1)
        pp.plotinfo.plot = False  # deactivate plotting

        if self.p.multi:
            pp1 = pp()  # couple the entire indicators
            self.sellsignal = self.data0.close < pp1.s1
        else:
            self.sellsignal = self.data0.close < pp.s1()

    def next(self):
        txt = ','.join(
            ['%04d' % len(self),
             '%04d' % len(self.data0),
             '%04d' % len(self.data1),
             self.data.datetime.date(0).isoformat(),
             '%.2f' % self.data0.close[0],
             '%.2f' % self.pp.s1[0],
             '%.2f' % self.sellsignal[0]])

        print(txt)

cerebro = bt.cerebro.Cerebro()
# Data feed
data_file = r"D:\tmp\backtrader\datas\2006-min-005.txt"  # D:\tmp\backtrader\datas is where the backtrader source folder
data0 = bt.feeds.BacktraderCSVData(dataname=data_file,  timeframe=bt.TimeFrame.Minutes, compression=5)
cerebro.adddata(data0)

cerebro.resampledata(data0, timeframe=bt.TimeFrame.Minutes, compression=60)

cerebro.addanalyzer(bt.analyzers.Returns, _name="RE")
cerebro.addanalyzer(bt.analyzers.TradeAnalyzer, _name="TA")

cerebro.addstrategy(St)
btrs1 = cerebro.run()

pstart=0
pend=100

cerebro.plot(start=pstart, end=pend, style='candle')

60minutes view is obviously incorrect

image

But when I change plot end to 200 pstart=0 pend=200 It's correct now. Figure_0

dindom999 commented 2 years ago

I have fix this bug and fire a pull requests https://github.com/backtrader2/backtrader/pull/84/commits