Daily datafeed works BUT intraday doesn't
-
Hello,
I downloaded a daily data from yahoo and my intraday from tingoo.
My Code looks like this:
import os, sys, argparse import pandas as pd import backtrader as bt from Strategien.GoldenCross import GoldenCross import datetime from tiingo import TiingoClient cerebro = bt.Cerebro() cerebro.broker.setcash(100000) symbol = "AAPL" path = "/Users/me/Desktop/allgemein/Visual Studio/Stock Data/S&P500 Aktien 1H/" + symbol + ".csv" pathIndex = "/Users/me/Desktop/allgemein/Visual Studio/Stock Data/Index EOD Data/^GSPC.csv" # read csv, parse date, use dates col as index stock_prices = pd.read_csv(pathIndex, index_col=[0], parse_dates=[0]) #stock_prices['date'] = pd.to_datetime(stock_prices['date']) #object to datetime #stock_prices.date = pd.to_datetime(stock_prices.date, format='%Y-%m-%dT%H:%M:%S.%fZ') print(stock_prices.dtypes) print(stock_prices) # create data feed pandas df feed = bt.feeds.PandasData(dataname=stock_prices) # add feed cerebro.adddata(feed) #cerebro.addstrategy(GoldenCross) cerebro.run() cerebro.plot(style='candle')
If I use the pathIndex as feed, then I get plotted my chart with cerebro. But if I use the path of my tiingo data, which is an intraday data (1H data), than I get the output gives me an error called: 'numpy.int64' object has no attribute 'to_pydatetime'.
I also have to mention, that my intraday format looks like this...dtype: object date close high low open 0 2017-01-03T15:00:00.000Z 115.450 115.815 115.400 115.600 1 2017-01-03T16:00:00.000Z 115.370 115.670 115.135 115.450 2 2017-01-03T17:00:00.000Z 115.470 115.525 115.270 115.365 3 2017-01-03T18:00:00.000Z 115.235 115.495 115.235 115.475 4 2017-01-03T19:00:00.000Z 115.435 115.445 115.160 115.235 ... ... ... ... ... ...
thank you for your help,
best regards Christian -
Backtrader defaults to daily. Backtrader cannot tell your timeframe and compression. Try to add in the timeframe and compression in the adddata.
cerebro.adddata(feed, timeframe=bt.Timeframe.Minutes, compression=60)
timeframe (default: TimeFrame.Days)
Potential values: Ticks, Seconds, Minutes, Days, Weeks, Months and Years
compression (default: 1)
Number of actual bars per bar. Informative. Only effective in Data Resampling/Replaying.
-
@run-out I may have this wrong, you might have to use resample.
-
Hey @run-out thank you for your answer. I tried to use:
cerebro.adddata(feed, timeframe=bt.Timeframe.Minutes, compression=60)
Now the error sounds like:
"TypeError: adddata() got an unexpected keyword argument 'timeframe'" -
@chhrissi2909 said in Daily datafeed works BUT intraday doesn't:
'numpy.int64' object has no attribute 'to_pydatetime'
Yeah, that first response was my mistake. I suspect your error is form the tingo date format going to pandas then to backtrader? I'm really not sure but maybe give this a try.
df = pd.read_csv("data/tiingo.csv", index_col=0, parse_dates=True, infer_datetime_format=True) df.index.name = "datetime" data = bt.feeds.PandasData(dataname=df) cerebro.adddata(data) cerebro.run()
-
@run-out I tired it, but this ist more or less my old code^^.. So it does not work.. Does anyone have an other idea? I think you first thinking of resample could be a good way, but I don't know how this works.
Because you are completely right, the defaults value is for daily data (and this data also works for mer) but the intraday data doesn't..
-
@chhrissi2909 Could you paste all of your code in please including the new method and show your errors and output? Thanks.
-
@run-out here the code:
import os, sys, argparse import pandas as pd import backtrader as bt from Strategien.GoldenCross import GoldenCross import datetime from tiingo import TiingoClient cerebro = bt.Cerebro() cerebro.broker.setcash(100000) symbol = "AAPL" path = "/Users/me/Desktop/allgemein/Visual Studio/Stock Data/S&P500 Aktien 1H/" + symbol + ".csv" pathIndex = "/Users/me/Desktop/allgemein/Visual Studio/Stock Data/Index EOD Data/^GSPC.csv" # read csv, parse date, use dates col as index stock_prices = pd.read_csv(path, index_col=0, parse_dates=True, infer_datetime_format=True) ##print(stock_prices.dtypes) print(stock_prices) # create data feed pandas df stock_prices.index.name = "datetime" data = bt.feeds.PandasData(dataname=stock_prices) cerebro.adddata(data) cerebro.run() cerebro.plot(style='candle')
-
For everybody in the Future :D (I fixed the problem to my self):
import os, sys, argparse import pandas as pd import backtrader as bt from Strategien.GoldenCross import GoldenCross import datetime from tiingo import TiingoClient cerebro = bt.Cerebro() cerebro.broker.setcash(100000) symbol = "AAPL" path = "/Users/me/Desktop/allgemein/Visual Studio/Stock Data/S&P500 Aktien 1H/" + symbol + ".csv" pathIndex = "/Users/me/Desktop/allgemein/Visual Studio/Stock Data/Index EOD Data/^GSPC.csv" # read csv, parse date, use dates col as index stock_prices = pd.read_csv(path, index_col=1, parse_dates=True, infer_datetime_format=True) # create data feed pandas df stock_prices.index.name = "datetime" data = bt.feeds.PandasData(dataname=stock_prices) cerebro.adddata(data) cerebro.run() cerebro.plot(volume=False) cerebro.plot(style='candle')
there are 2 key lines, the first is, that for intraday data, if there is no volume you have to tell that backtrader like this:
cerebro.plot(volume=False)
And the second key line was how to read the csv:
stock_prices = pd.read_csv(path, index_col=1, parse_dates=True, infer_datetime_format=True) Thanks for all your help :)
-
@chhrissi2909 Why is there no volume in intraday ? we do get it for each candle ! ?