简体   繁体   中英

How To Handle date format in GenericCSVData in Backtrade

I was trying a simple example that prints datetime and close price from my csv. It's printing date correctly but for the time it's printing 23:59:59 only. Can you please help me to check what am I doing wrong?

Here is my code:

import datetime
import backtrader as bt
import backtrader.feeds as btfeeds

class PrintClose(bt.Strategy):

    def __init__(self):
        self.dataclose = self.datas[0].close

    def log(self, txt, dt=None):
        dt = dt or self.datas[0].datetime.datetime(0).strftime('%Y-%m-%d %H:%M:%S%z')
        print(f'{dt} {txt}')  # Print date and close

    def next(self):
        self.log(self.dataclose[0])


if __name__ == '__main__':
    cerebro = bt.Cerebro()
    cerebro.addstrategy(PrintClose)
    datapath = './RELIANCE_formatted.csv'

    data = btfeeds.GenericCSVData(
        dataname=datapath,
        dtformat=('%Y-%m-%d %H:%M:%S%z'),
        timestamp=0,
        open=1,
        high=2,
        low=3,
        close=4,
        volume=5,
        openinterest=-1
    )

    cerebro.adddata(data)
    cerebro.run()

Output :

2016-07-04 23:59:59 489.1
2016-07-04 23:59:59 491.45
2016-07-04 23:59:59 489.8
2016-07-04 23:59:59 488.75
2016-07-04 23:59:59 488.75
2016-07-04 23:59:59 489.15
2016-07-04 23:59:59 488.55
2016-07-05 23:59:59 486.95
2016-07-05 23:59:59 486.9

Expected Output:

2016-07-04 09:15:00+05:30 489.1
2016-07-04 10:15:00+05:30 491.45
2016-07-04 11:15:00+05:30 489.8
2016-07-04 12:15:00+05:30 488.75
2016-07-04 13:15:00+05:30 488.75
2016-07-04 14:15:00+05:30 489.15
2016-07-04 15:15:00+05:30 488.55
2016-07-05 09:15:00+05:30 486.95
2016-07-05 10:15:00+05:30 486.9

And here is my csv

date,open,high,low,close,volume
2016-07-04 09:15:00+05:30,483.4,490.35,483.4,489.1,950630
2016-07-04 10:15:00+05:30,489.1,492.05,488.55,491.45,603618
2016-07-04 11:15:00+05:30,491.7,491.95,489.4,489.8,514331
2016-07-04 12:15:00+05:30,489.8,490.65,488.15,488.75,374728
2016-07-04 13:15:00+05:30,488.85,489.55,488.25,488.75,31432
2016-07-04 14:15:00+05:30,488.75,490.3,487.45,489.15,511010
2016-07-04 15:15:00+05:30,489.1,489.25,487.95,488.55,323005
2016-07-05 09:15:00+05:30,488.55,490.1,486.5,486.95,441230
2016-07-05 10:15:00+05:30,486.9,488.05,485.55,486.9,320247

I finally got it working, with help from @run-out for looking into timeframe and compression. And then changing dtformat. Here is the solution.

data = btfeeds.GenericCSVData(
    dataname=datapath,
    datetime=0,
    open=1,
    high=2,
    low=3,
    close=4,
    volume=5,
    openinterest=-1,
    dtformat=('%Y-%m-%d %H:%M:%S+05:30'),
    timeframe=bt.TimeFrame.Minutes, 
    compression=60,
)

You need to tell Backtrader the timeframe and compression, otherwise it has no idea what intervals it's working with.

 data = btfeeds.GenericCSVData(
        dataname=datapath,
        dtformat=('%Y-%m-%d %H:%M:%S%Z'),
        # timestamp=0, I don't think you need this. 
        timeframe=bt.TimeFrame.Minutes, 
        compression=60,
        open=1,
        high=2,
        low=3,
        close=4,
        volume=5,
        openinterest=-1
    )

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