简体   繁体   中英

How to check fed data in backtrader?

import backtrader as bt
cerebro = bt.Cerebro()
data = bt.feeds.GenericCSVData(
    dataname='data.csv',
    # Do not pass values before this date
    fromdate=dt.datetime(2000, 1, 1),
    # Do not pass values after this date
    todate=dt.datetime.now().date(),
    datetime=0,
    high=5,
    low=6,
    open=4,
    close=8,
    volume=10,
    openinterest=-1
)
cerebro.adddata(data)

How to check wheater data is fed properly? Such as print open and close ? How to plot it (candle)?

You can see your data by printing a log from next . This is a standard setup I use.

class TestPositions(bt.Strategy):
    def __init__(self):
        pass

    def log(self, txt, dt=None):
        """ Logging function fot this strategy"""
        dt = dt or self.data.datetime[0]
        if isinstance(dt, float):
            dt = bt.num2date(dt)
        print("%s, %s" % (dt, txt))

    def print_signal(self):
        self.log(
            f"o {self.datas[0].open[0]:7.2f} "
            f"h {self.datas[0].high[0]:7.2f} "
            f"l {self.datas[0].low[0]:7.2f} "
            f"c {self.datas[0].close[0]:7.2f} "
            f"v {self.datas[0].volume[0]:7.0f} "
        )

    def next(self):
        self.print_signal()
        

To plot you just use cerebro.plot() after you run cerebro.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM