Getting AttributeError: 'GenericCSV' object has no attribute 'setenvironment' error
Burak Aktas last edited by Burak Aktas
I am trying to run a very simple backtesting strategy, however, I am getting
AttributeError: 'GenericCSV' object has no attribute 'setenvironment'error while trying to load the data into cerebro. It basically happens in
cerebro.adddataand I am using
GenericCSVdata format by reading from a .csv file.
Traceback (most recent call last): File "backtesting.py", line 40, in <module> cerebro.adddata(data) File "/Users/buraktas/.virtualenvs/alga-trader/lib/python3.7/site-packages/backtrader/cerebro.py", line 758, in adddata data.setenvironment(self) AttributeError: 'GenericCSV' object has no attribute 'setenvironment'
Here is my example code below;
from datetime import datetime import backtrader as bt class TestStrategy(bt.Strategy): def __init__(self): print('FOOBAR') class OHLCData(bt.feeds.GenericCSV): params = ( ('dtformat', '%Y-%m-%d %H:%M:%S'), ('datetime', 0), ('time', -1), ('open', 1), ('high', 2), ('low', 3), ('close', 4), ('volume', 5), ('openinterest', -1), ('timeframe', bt.TimeFrame.Minutes), ('compression', 60), ) if __name__ == '__main__': cerebro = bt.Cerebro() cerebro.addstrategy(TestStrategy) cerebro.broker.setcommission(commission=0.001) data = OHLCData(dataname='/Users/buraktas/algotrader/algotrader/backtesting/test_data.csv') cerebro.adddata(data) cerebro.broker.setcash(1000.0) print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.run() print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.plot(style='bar')
And here is the .csv file I got
datetime,open,high,low,close,volume 2017-07-15 13:00:00,183.33,189.0,181.34,181.54,9797.22 2017-07-15 14:00:00,181.52,184.0,178.0,180.26,24246.76 2017-07-15 15:00:00,180.26,180.27,176.35,178.82,22395.28 2017-07-15 16:00:00,178.82,182.0,177.11,180.52,11845.78 2017-07-15 17:00:00,180.52,184.0,178.2,182.49,11625.9 2017-07-15 18:00:00,182.49,183.2,179.05,179.32,6882.71 2017-07-15 19:00:00,179.32,181.2,179.28,181.2,8096.29 2017-07-15 20:00:00,181.19,181.21,175.51,175.51,14454.75 2017-07-15 21:00:00,175.51,178.12,175.05,176.18,10333.21 2017-07-15 22:00:00,176.17,177.05,173.5,174.48,15422.67 2017-07-15 23:00:00,174.49,176.0,167.0,167.72,32336.96 2017-07-16 00:00:00,168.22,172.0,165.24,165.64,27662.9 2017-07-16 01:00:00,165.67,171.1,164.0,166.86,24991.78 2017-07-16 02:00:00,166.86,172.3,166.85,169.78,11534.34 2017-07-16 03:00:00,169.77,171.21,166.41,167.03,8431.53 2017-07-16 04:00:00,166.87,168.16,165.05,165.54,6999.98 2017-07-16 05:00:00,165.33,165.47,160.53,160.79,13150.77 2017-07-16 06:00:00,160.64,164.0,154.42,160.66,34607.19 2017-07-16 07:00:00,160.97,162.16,155.0,156.79,11609.98 2017-07-16 08:00:00,156.78,157.55,154.5,154.53,7494.6 2017-07-16 09:00:00,154.53,154.63,151.17,152.42,16870.68 2017-07-16 10:00:00,152.45,152.59,145.0,145.12,21192.75 2017-07-16 11:00:00,145.28,147.54,137.0,137.99,38179.05 2017-07-16 12:00:00,137.99,141.7,132.54,134.32,37655.74 2017-07-16 13:00:00,134.26,155.5,130.26,153.0,77604.66 2017-07-16 14:00:00,152.12,152.57,140.34,146.93,38863.56 2017-07-16 15:00:00,146.99,154.56,146.99,152.07,31752.34 2017-07-16 16:00:00,152.07,163.88,151.01,162.49,34226.85 2017-07-16 17:00:00,162.47,164.55,157.18,159.0,28391.9 2017-07-16 18:00:00,158.89,158.93,151.0,153.0,22441.2
I am not a python expert, however, I kinda debugged and tried to see what's going on internally. The thing is GenericCSV class itself doesn't have
setenvironmentmethod defined (it is inheriting from
AbstractDataBasein feeds.py file). So, if I copy paste
setenvironmentmethod into GenericCSV class then I start to get another error on next undefined method or variable. I appreciate for any kind of help. Thanks!
Let me link this to your other topic where you already mention this and which I couldn' yet come to. Which is here:
Understanding you are eager to crack the jackpot, algotrading requires that you work as an engineer ... paying attention to the little details. Do not take this as demoting, it is simply trying to point you in the right direction.
For example in this post. Error message
AttributeError: 'GenericCSV' object has no attribute 'setenvironment'
But the code says
data = OHLCData(dataname='/Users/buraktas/algotrader/algotrader/backtesting/test_data.csv') cerebro.adddata(data)
Diagnostic: You are NOT running the same code which is producing the error. This is paramount when trying to debug an error and also when reporting it. You have to keep a very tight control of what you execute and which output corresponds to which code (
gitand other version control systems do help) Mostly for yourself, because you will end up reporting the errors to yourself (as we all do)
In any case ... that's not your problem. Let's see the documentation where
GenericCSVDatais presented (you should have already seen the error here) and the reference.
Let's even quote the usage in the code in the 2nd link
... data = btfeeds.GenericCSVData( dataname='mydata.csv', fromdate=datetime.datetime(2000, 1, 1), todate=datetime.datetime(2000, 12, 31), nullvalue=0.0, dtformat=('%Y-%m-%d'), datetime=0, high=1, low=2, open=3, close=4, volume=5, openinterest=-1 ) ...
Now, let's quote your code
class OHLCData(bt.feeds.GenericCSV): params = ( ('dtformat', '%Y-%m-%d %H:%M:%S'), ('datetime', 0), ('time', -1), ('open', 1), ('high', 2), ('low', 3), ('close', 4), ('volume', 5), ('openinterest', -1), ('timeframe', bt.TimeFrame.Minutes), ('compression', 60), )
If you haven't noticed it yet ... you are using an internal name GenericCSV and NOT THE REAL NAME which is in the documentation and samples:
I am not a python expert, however, I kinda debugged and tried to see what's going on internally
It's not about being a Python expert and doing deep debugging. It's about paying attention to the small details. It may not be the most important thing when loading a data feed ... but the discipline it brings will help in all other areas which you will have to tackle with algotrading.
Burak Aktas last edited by
Thanks @backtrader I used
GenericCSVDataand successfully loaded data into cerebro. I was changing the code a lot due to trying loading data phase that is I the error log and original code was pointing different classes.