AttributeError: 'int' object has no attribute 'to_pydatetime'
-
Hi ,
I am feeding this information todata = bt.feeds.PandasData(dataname=ohlc_TCS)
error
AttributeError: 'int' object has no attribute 'to_pydatetime'
do i have to manually reset the any parameter, can some one help me on this.
from_date = "01-10-2020" to_date = "05-01-2021" # ohlc_TCS = fetchOHLCExtended("TCS",from_date, to_date, '5minute') ohlc_TCS.info() from_date = "01-10-2020" to_date = "05-01-2021" # cerebro = bt.Cerebro() cerebro.broker.set_cash(100000) cerebro.broker.setcommission(commission=0.001) data = bt.feeds.PandasData(dataname=ohlc_TCS) cerebro.adddata(data) cerebro.addstrategy(macdStrategy) print('Starting Portfolio Value : %0.2f' % cerebro.broker.getvalue()) cerebro.run() cerebro.plot() print('Final Portfolio Value : %0.2f' % cerebro.broker.getvalue())
-
Dont make new/more threads with similar/follow up questions. It will clutter the forum. Just follow up on the same thread.
-
from kiteconnect import KiteConnect import pandas as pd import datetime as dt import os import time import numpy as np import backtrader as bt from cerebro_stra import * cwd = os.chdir("D:\\Rajesh2025\\46. Algos_trading\\strategy") #generate trading session access_token = open("access_token.txt",'r').read() key_secret = open("api_keys.txt",'r').read().split() kite = KiteConnect(api_key=key_secret[0]) kite.set_access_token(access_token) #get dump of all NSE instruments instrument_dump = kite.instruments("NSE") instrument_df = pd.DataFrame(instrument_dump) def instrumentLookup(instrument_df,symbol): """Looks up instrument token for a given script from instrument dump""" try: return instrument_df[instrument_df.tradingsymbol==symbol].instrument_token.values[0] except: return -1 # Below is only for 5 minutes interveral def fetchOHLCExtended(ticker,from_date,to_date,interval): """extracts historical data and outputs in the form of dataframe inception date string format - dd-mm-yyyy""" instrument = instrumentLookup(instrument_df,ticker) from_date = dt.datetime.strptime(from_date, "%d-%m-%Y") to_date = dt.datetime.strptime(to_date, "%d-%m-%Y") data = pd.DataFrame(columns=['date', 'open', 'high', 'low', 'close', 'volume']) while True: if (to_date.date() - from_date.date() ) <= dt.timedelta(100): data = data.append(pd.DataFrame(kite.historical_data(instrument,from_date,to_date,interval)),ignore_index=True) break else: to_date = from_date + dt.timedelta(100) data = data.append(pd.DataFrame(kite.historical_data(instrument,from_date,to_date,interval)),ignore_index=True) from_date = to_date data.set_index("date") return data
i got above code from online tutorial,
-
@rajesh
compared to earlier, context is changed little bit, hence posted in different thread -
As requested in other thread please post few lines from ohlc_TCS. Without seeing that its not easy to understand the problem.
-
@rajanprabu
Hi @rajanprabu , this is complete code for ohlc_TCSfrom kiteconnect import KiteConnect import pandas as pd import datetime as dt import os import time import numpy as np import backtrader as bt from cerebro_stra import * cwd = os.chdir(".....\\strategy") #generate trading session access_token = open("access_token.txt",'r').read() key_secret = open("api_keys.txt",'r').read().split() kite = KiteConnect(api_key=key_secret[0]) kite.set_access_token(access_token) #get dump of all NSE instruments instrument_dump = kite.instruments("NSE") instrument_df = pd.DataFrame(instrument_dump) def instrumentLookup(instrument_df,symbol): """Looks up instrument token for a given script from instrument dump""" try: return instrument_df[instrument_df.tradingsymbol==symbol].instrument_token.values[0] except: return -1 # Below is only for 5 minutes interveral def fetchOHLCExtended(ticker,from_date,to_date,interval): """extracts historical data and outputs in the form of dataframe inception date string format - dd-mm-yyyy""" instrument = instrumentLookup(instrument_df,ticker) from_date = dt.datetime.strptime(from_date, "%d-%m-%Y") to_date = dt.datetime.strptime(to_date, "%d-%m-%Y") data = pd.DataFrame(columns=['date', 'open', 'high', 'low', 'close', 'volume']) while True: if (to_date.date() - from_date.date() ) <= dt.timedelta(100): data = data.append(pd.DataFrame(kite.historical_data(instrument,from_date,to_date,interval)),ignore_index=True) break else: to_date = from_date + dt.timedelta(100) data = data.append(pd.DataFrame(kite.historical_data(instrument,from_date,to_date,interval)),ignore_index=True) from_date = to_date data.set_index("date") return data from_date = "01-10-2020" to_date = "05-01-2021" # ohlc_TCS = fetchOHLCExtended("TCS",from_date, to_date, '5minute') ohlc_TCS.info() ohlc_TCS['date'] = pd.to_datetime(ohlc_TCS['date'], format = "%m/%d/%Y %H:%M:%S %z" ) ohlc_TCS.info() cerebro = bt.Cerebro() cerebro.broker.set_cash(100000) cerebro.broker.setcommission(commission=0.001) data = bt.feeds.PandasData(dataname=ohlc_TCS) cerebro.adddata(data) cerebro.addstrategy(macdStrategy) print('Starting Portfolio Value : %0.2f' % cerebro.broker.getvalue()) cerebro.run() cerebro.plot() print('Final Portfolio Value : %0.2f' % cerebro.broker.getvalue())
Below is the code for strategy, is slightly tweaked from strategy code from backtrader site.
import math import backtrader as bt from backtrader.indicators import EMA import argparse import datetime import os.path import time import sys import backtrader as bt import backtrader.feeds as btfeeds import backtrader.indicators as btind class macdStrategy(bt.Strategy): lines = ('macd', 'signal', 'histo',) params = (('period_me1', 12), ('period_me2', 26), ('period_signal', 9),) def log(self,txt, dt = None): dt = dt or self.datas[0].datatime.data(0) print('%s, %s' % (dt.isoformat(), txt)) def notify_order(self, order): if order.status in [order.Submitted, order.Accepted]: self.log('ORDER ACCEPTED/SUBMITTED', dt=order.created.dt) self.order = order return if order.status in [order.Expired]: self.log('BUY EXPIRED') elif order.status in [order.Completed]: if order.isbuy(): self.log( 'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) self.buyprice = order.executed.price self.buycomm = order.executed.comm elif order.issell(): self.log( 'SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm)) elif order.status in [order.Canceled, order.Margin, order.Rejected]: self.log('Order Canceled/Margin/Rejected') # Sentinel to None: new orders allowed self.order = None def __init__ (self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close # To keep track of pending orders and buy price/commission self.order = None self.buyprice = None self.buycomm = None me1 = EMA(self.data, period=self.p.period_me1) me2 = EMA(self.data, period=self.p.period_me2) self.lines.macd = me1 - me2 self.lines.signal = EMA(self.l.macd, period=self.p.period_signal) self.lines.histo = self.l.macd - self.l.signal self.crossover = btind.CrossOver(self.lines.macd, self.lines.signal) def next(self): self.log('Close, %.2f' % self.dataclose[0]) # Check if an order is pending ... if yes, we cannot send a 2nd one if self.order: return # Check if we are in the market if not self.position: if self.crossover > 0: self.log('BUY CREATE, %.2f' %self.dataclose[0]) self.order = self.buy() else: # Already in the market ... we might sell if self.crossover < 0: self.log('SELL CREATE, %.2f' %self.dataclose[0]) self.order = self.sell()
i have one more api code for kite connect, if above code is not enough, i can supply that one as well.
-
Not the code.. I mean the few lines of data that you are feeding to backtrader ( values of ohlc_TCS in your case ) like below
timestamp,open,high,low,close,volume,oi 2019-12-02 09:15:00,12140.00,12161.80,12127.95,12134.65,4277,440100 2019-12-02 09:16:00,12132.80,12138.35,12129.75,12138.35,1875,440100 2019-12-02 09:17:00,12138.80,12139.65,12137.55,12137.55,600,442350 2019-12-02 09:18:00,12137.40,12137.40,12129.00,12137.35,2550,442350 2019-12-02 09:19:00,12136.95,12142.75,12133.50,12142.75,2250,442350 2019-12-02 09:20:00,12143.00,12150.00,12142.90,12146.05,2475,442800 2019-12-02 09:21:00,12145.05,12148.40,12141.15,12141.15,900,442800 2019-12-02 09:22:00,12141.05,12149.30,12138.55,12145.50,1875,442800
-
@rajanprabu
i am sorry, here is the datadate open high low close volume 2020-10-01 09:15:00+05:30 2510 2524 2507 2513.15 231516 2020-10-01 09:20:00+05:30 2514.85 2517.95 2495.9 2502.6 122766 2020-10-01 09:25:00+05:30 2502.1 2504.85 2497 2503.95 132321 2020-10-01 09:30:00+05:30 2502.9 2504 2496 2499.05 45690 2020-10-01 09:35:00+05:30 2498.4 2503 2495.7 2500.05 53982 2020-10-01 09:40:00+05:30 2500.9 2509.8 2497.3 2507.5 61781 2020-10-01 09:45:00+05:30 2507.5 2511.95 2503.7 2504.95 60255 2020-10-01 09:50:00+05:30 2504.95 2507.2 2500 2502.4 39571 2020-10-01 09:55:00+05:30 2502.95 2502.95 2492.35 2495 72469 2020-10-01 10:00:00+05:30 2495 2500 2494 2498 33873 2020-10-01 10:05:00+05:30 2498.2 2503 2496 2501.8 36952 2020-10-01 10:10:00+05:30 2501.8 2503.8 2498 2500.9 36154 2020-10-01 10:15:00+05:30 2500.9 2506 2500 2505.1 46938 2020-10-01 10:20:00+05:30 2505.85 2507.65 2502.1 2502.65 24609 2020-10-01 10:25:00+05:30 2503.75 2504.4 2499.15 2503.5 25443 2020-10-01 10:30:00+05:30 2503 2504 2501 2503.15 11733 2020-10-01 10:35:00+05:30 2503.1 2505.9 2501.55 2502.5 17242 2020-10-01 10:40:00+05:30 2502.5 2505.15 2501.65 2501.9 19095 2020-10-01 10:45:00+05:30 2501.9 2505 2499 2504.7 32404 2020-10-01 10:50:00+05:30 2504.85 2505.2 2501.4 2502.4 12070
just for additional info, here is the screen shot
!
-
BT processes them using
datetime.datetime.strptime
so it can't take the additional +5:30. One had to process the date time format before..df = pd.read_csv(data_file, header=0, sep='\t', parse_dates=True) df = df.iloc[:, 0:6] df.columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume' ] df['timestamp'] = pd.to_datetime(df['timestamp']).dt.tz_convert(None) df.set_index('timestamp', inplace=True) data = bt.feeds.PandasData( dataname=df, timeframe=bt.TimeFrame.Minutes, compression=5, tz='Asia/Kolkata', )
-
i removed timezone information by below code.
ohlc_TCS['date'] = pd.to_datetime(ohlc_TCS['date'], format = "%Y/%m/%d %H:%M:%S" ) ohlc_TCS.info() ohlc_TCS['date'] = ohlc_TCS['date'].astype(str).str[:-6] ohlc_TCS.info() cerebro = bt.Cerebro() cerebro.broker.set_cash(100000) cerebro.broker.setcommission(commission=0.001) data = bt.feeds.PandasData( dataname=ohlc_TCS, ) cerebro.adddata(data) cerebro.addstrategy(macdStrategy)
but after running this, i am getting attribute error, may i know where i making mistake
print('Starting Portfolio Value : %0.2f' % cerebro.broker.getvalue()) cerebro.run() # cerebro.plot() print('Final Portfolio Value : %0.2f' % cerebro.broker.getvalue())
AttributeError: 'int' object has no attribute 'to_pydatetime'
-
My solution was tested on what you posted
date open high low close volume 2020-10-01 09:15:00+05:30 2510 2524 2507 2513.15 231516 2020-10-01 09:20:00+05:30 2514.85 2517.95 2495.9 2502.6 122766 2020-10-01 09:25:00+05:30 2502.1 2504.85 2497 2503.95 132321 2020-10-01 09:30:00+05:30 2502.9 2504 2496 2499.05 45690 2020-10-01 09:35:00+05:30 2498.4 2503 2495.7 2500.05 53982 2020-10-01 09:40:00+05:30 2500.9 2509.8 2497.3 2507.5 61781 2020-10-01 09:45:00+05:30 2507.5 2511.95 2503.7 2504.95 60255
Now in the screenshot have an index column.. BT expects Date to be on the index column by default unless specifically stated.
Just make date as your dataframe indexohlc_TCS['date'] = pd.to_datetime(ohlc_TCS['date'], format = "%Y/%m/%d %H:%M:%S" ) ohlc_TCS.info() ohlc_TCS['date'] = ohlc_TCS['date'].astype(str).str[:-6] ohlc_TCS.info() ### Just add this line ohlc_TCS.set_index('date', inplace=True) cerebro = bt.Cerebro() cerebro.broker.set_cash(100000) cerebro.broker.setcommission(commission=0.001) data = bt.feeds.PandasData( dataname=ohlc_TCS, ) cerebro.adddata(data) cerebro.addstrategy(macdStrategy)
-
@rajanprabu
i used your solution, i am getting below error message, i am missing some thing ?ohlc_TCS['date'] = pd.to_datetime(ohlc_TCS['date'], format = "%Y/%m/%d %H:%M:%S" ) ohlc_TCS.info() ohlc_TCS['date'] = ohlc_TCS['date'].astype(str).str[:-6] ohlc_TCS.info() ### Just add this line ohlc_TCS.set_index('date', inplace=True) cerebro = bt.Cerebro() cerebro.broker.set_cash(100000) cerebro.broker.setcommission(commission=0.001) data = bt.feeds.PandasData( dataname=ohlc_TCS, ) cerebro.adddata(data) cerebro.addstrategy(macdStrategy)
TypeError: super(type, obj): obj must be an instance or subtype of type
-
ohlc_TCS['date'] = pd.to_datetime(ohlc_TCS['date'], format = "%Y/%m/%d %H:%M:%S" ) ohlc_TCS.info() ohlc_TCS['date'] = ohlc_TCS['date'].astype(str).str[:-6] ohlc_TCS.info() ### Just add this line ohlc_TCS.set_index('date', inplace=True) ohlc_TCS = ohlc_TCS.iloc[:, 0:6] ohlc_TCS.columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume' ] cerebro = bt.Cerebro() cerebro.broker.set_cash(100000) cerebro.broker.setcommission(commission=0.001) data = bt.feeds.PandasData( dataname=ohlc_TCS, ) cerebro.adddata(data) cerebro.addstrategy(macdStrategy)
Please give it a try with this and let me know.. It really hard to bug fix without sample data at hand. If this doesn't work please upload your some lines of your data. Not a screenshot.
-
Hope you can use below data, thanks in advance for help
date open high low close volume 0 2020-10-01 09:15:00+05:30 2510 2524 2507 2513.15 231516 1 2020-10-01 09:20:00+05:30 2514.85 2517.95 2495.9 2502.6 122766 2 2020-10-01 09:25:00+05:30 2502.1 2504.85 2497 2503.95 132321 3 2020-10-01 09:30:00+05:30 2502.9 2504 2496 2499.05 45690 4 2020-10-01 09:35:00+05:30 2498.4 2503 2495.7 2500.05 53982 5 2020-10-01 09:40:00+05:30 2500.9 2509.8 2497.3 2507.5 61781 6 2020-10-01 09:45:00+05:30 2507.5 2511.95 2503.7 2504.95 60255 7 2020-10-01 09:50:00+05:30 2504.95 2507.2 2500 2502.4 39571 8 2020-10-01 09:55:00+05:30 2502.95 2502.95 2492.35 2495 72469 9 2020-10-01 10:00:00+05:30 2495 2500 2494 2498 33873 10 2020-10-01 10:05:00+05:30 2498.2 2503 2496 2501.8 36952 11 2020-10-01 10:10:00+05:30 2501.8 2503.8 2498 2500.9 36154 12 2020-10-01 10:15:00+05:30 2500.9 2506 2500 2505.1 46938 13 2020-10-01 10:20:00+05:30 2505.85 2507.65 2502.1 2502.65 24609 14 2020-10-01 10:25:00+05:30 2503.75 2504.4 2499.15 2503.5 25443 15 2020-10-01 10:30:00+05:30 2503 2504 2501 2503.15 11733 16 2020-10-01 10:35:00+05:30 2503.1 2505.9 2501.55 2502.5 17242 17 2020-10-01 10:40:00+05:30 2502.5 2505.15 2501.65 2501.9 19095 18 2020-10-01 10:45:00+05:30 2501.9 2505 2499 2504.7 32404 19 2020-10-01 10:50:00+05:30 2504.85 2505.2 2501.4 2502.4 12070 20 2020-10-01 10:55:00+05:30 2504.1 2504.15 2502 2502.4 8106 21 2020-10-01 11:00:00+05:30 2502.4 2503.95 2500 2500.55 16986 22 2020-10-01 11:05:00+05:30 2501 2502.8 2499.75 2501.6 19452 23 2020-10-01 11:10:00+05:30 2501.6 2505 2500.1 2504.55 22629 24 2020-10-01 11:15:00+05:30 2504.55 2509 2504 2504.85 44211 25 2020-10-01 11:20:00+05:30 2504.85 2508 2504.1 2507.95 19798 26 2020-10-01 11:25:00+05:30 2507.95 2507.95 2505 2506.95 16231 27 2020-10-01 11:30:00+05:30 2506.5 2511 2506.5 2509.9 57648 28 2020-10-01 11:35:00+05:30 2510 2510 2506 2507.2 32086 29 2020-10-01 11:40:00+05:30 2507 2507.4 2502.3 2505.25 36883 30 2020-10-01 11:45:00+05:30 2505.9 2507 2502.8 2507 16419 31 2020-10-01 11:50:00+05:30 2507 2509 2506.45 2507.5 13789 32 2020-10-01 11:55:00+05:30 2508.6 2508.95 2505.9 2507 11581 33 2020-10-01 12:00:00+05:30 2506.8 2508.85 2506 2508.15 9224 34 2020-10-01 12:05:00+05:30 2508.8 2510 2507.75 2508.3 14697 35 2020-10-01 12:10:00+05:30 2508.85 2509 2500.6 2500.6 19791 36 2020-10-01 12:15:00+05:30 2500.6 2504.5 2500.6 2501.6 18619 37 2020-10-01 12:20:00+05:30 2501.6 2501.85 2497 2499 51028 38 2020-10-01 12:25:00+05:30 2499.1 2500 2496 2498 29806 39 2020-10-01 12:30:00+05:30 2498 2499 2494.05 2498.25 26769 40 2020-10-01 12:35:00+05:30 2498.25 2502.65 2498.25 2500.5 12196 41 2020-10-01 12:40:00+05:30 2500.5 2502.85 2500 2501.15 18210 42 2020-10-01 12:45:00+05:30 2501.15 2502 2499.6 2501.75 8930 43 2020-10-01 12:50:00+05:30 2501.75 2503.55 2500 2501.25 11219 44 2020-10-01 12:55:00+05:30 2501.25 2502.2 2497 2497.5 14306 45 2020-10-01 13:00:00+05:30 2497.65 2501 2496.25 2499.15 11898 46 2020-10-01 13:05:00+05:30 2499.1 2502.2 2498.2 2500.5 10122 47 2020-10-01 13:10:00+05:30 2500.5 2504.9 2500.15 2503.15 17223 48 2020-10-01 13:15:00+05:30 2502.55 2505 2502 2502.2 14267 49 2020-10-01 13:20:00+05:30 2502.2 2503 2500.05 2500.6 12051 50 2020-10-01 13:25:00+05:30 2500.6 2503.9 2500.15 2500.25 8655
-
Since your tabs are not in order.. used delim_whitespace=True this time.
data_file = 'example given above' df = pd.read_csv(data_file, header=0, delim_whitespace=True, parse_dates=True) df['date'] = pd.to_datetime(df['date']).dt.tz_convert(None) df.set_index('date', inplace=True) data = bt.feeds.PandasData( dataname=df, timeframe=bt.TimeFrame.Minutes, compression=5, tz='Asia/Kolkata', )
I have tested this and this works fine.. Hope this helps.
-
@rajanprabu
the above code didn't work, i am trying in different ways, thanks for trying -
@rajesh
i am using following csv filedate open high low close volume 01-10-2020 03:45 2510 2524 2507 2513.15 231516 01-10-2020 03:50 2514.85 2517.95 2495.9 2502.6 122766 01-10-2020 03:55 2502.1 2504.85 2497 2503.95 132321 01-10-2020 04:00 2502.9 2504 2496 2499.05 45690 01-10-2020 04:05 2498.4 2503 2495.7 2500.05 53982 01-10-2020 04:10 2500.9 2509.8 2497.3 2507.5 61781 01-10-2020 04:15 2507.5 2511.95 2503.7 2504.95 60255 01-10-2020 04:20 2504.95 2507.2 2500 2502.4 39571 01-10-2020 04:25 2502.95 2502.95 2492.35 2495 72469 01-10-2020 04:30 2495 2500 2494 2498 33873 01-10-2020 04:35 2498.2 2503 2496 2501.8 36952 01-10-2020 04:40 2501.8 2503.8 2498 2500.9 36154 01-10-2020 04:45 2500.9 2506 2500 2505.1 46938 01-10-2020 04:50 2505.85 2507.65 2502.1 2502.65 24609 01-10-2020 04:55 2503.75 2504.4 2499.15 2503.5 25443 01-10-2020 05:00 2503 2504 2501 2503.15 11733 01-10-2020 05:05 2503.1 2505.9 2501.55 2502.5 17242 01-10-2020 05:10 2502.5 2505.15 2501.65 2501.9 19095 01-10-2020 05:15 2501.9 2505 2499 2504.7 32404 01-10-2020 05:20 2504.85 2505.2 2501.4 2502.4 12070 01-10-2020 05:25 2504.1 2504.15 2502 2502.4 8106
i am using below code to run strategy.
data_file = '...\\datafile.csv' df = pd.read_csv(data_file, header=0) df["date"] = pd.to_datetime(df["date"]).dt.tz_convert(None) df.drop(df.iloc[ :, 0:1,], inplace = True, axis = 1) df.set_index("date", inplace = True) df.to_csv("datafile_clean.csv") # df.iloc[0:3, :] # df.to_csv('tcs4.csv') cdata = btfeeds.GenericCSVData( dataname="datafile_clean.csv", fromdate = dt.datetime(2020, 10,1), dtformat="%Y-%m-%d %H:%M", compression = 1, datatime = 0 , open=1, high=2, low=3, close=4, volume=5, ) cerebro = bt.Cerebro() cerebro.adddata(cdata) cerebro.addstrategy(macdStrategy) print('Starting Portfolio Value : %0.2f' % cerebro.broker.getvalue()) cerebro.run() # cerebro.plot() print('Final Portfolio Value : %0.2f' % cerebro.broker.getvalue())
and i am getting below error:
ValueError: unconverted data remains: :00
from the past few days i am trying to resolve this, can any one help on this
-
@rajesh
i figured out the issue and solved the issue, now i got new error, can some one look into it.df = pd.read_csv(data_file, header=0) df["date"] = pd.to_datetime(df["date"]).dt.tz_convert(None) df.drop(df.iloc[ :, 0:1,], inplace = True, axis = 1) df.set_index("date", inplace = True) df.to_csv("datafile_clean.csv") # df.iloc[0:3, :] # df.to_csv('tcs4.csv') cdata = btfeeds.GenericCSVData( dataname="datafile_clean.csv", fromdate = dt.datetime(2020, 10,1), dtformat="%Y-%m-%d %H:%M:%S", datatime = 0 , open=1, high=2, low=3, close=4, volume=5, reverse = True ) if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro() # Add a strategy cerebro.adddata(cdata) cerebro.addstrategy(macdStrategy) print('Starting Portfolio Value : %0.2f' % cerebro.broker.getvalue()) cerebro.run() # cerebro.plot() print('Final Portfolio Value : %0.2f' % cerebro.broker.getvalue())
TypeError: super(type, obj): obj must be an instance or subtype of type