如何在 Backtrade 中处理 GenericCSVData 中的日期格式
How To Handle date format in GenericCSVData in Backtrade
我正在尝试一个简单的示例,该示例从我的 csv 中打印日期时间和收盘价。
它可以正确打印日期,但只打印 23:59:59
的时间。
你能帮我检查一下我做错了什么吗?
这是我的代码:
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()
输出:
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
预期输出:
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
这是我的 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
您需要告诉 Backtrader 时间范围和压缩,否则它不知道它使用的是什么间隔。
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
)
在@运行-out 的帮助下,我终于让它工作了,用于查看时间范围和压缩。然后改变 dtformat。这是解决方案。
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,
)
我正在尝试一个简单的示例,该示例从我的 csv 中打印日期时间和收盘价。
它可以正确打印日期,但只打印 23:59:59
的时间。
你能帮我检查一下我做错了什么吗?
这是我的代码:
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()
输出:
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
预期输出:
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
这是我的 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
您需要告诉 Backtrader 时间范围和压缩,否则它不知道它使用的是什么间隔。
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
)
在@运行-out 的帮助下,我终于让它工作了,用于查看时间范围和压缩。然后改变 dtformat。这是解决方案。
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,
)