For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Reading PANDA CVS
-
Hi there!
I am trying to run the following code:from __future__ import (absolute_import, division, print_function, unicode_literals) import argparse import backtrader as bt import backtrader.feeds as btfeeds import pandas as pd class firstStrategy(bt.Strategy): def __init__(self): self.rsi = bt.indicators.RSI_SMA(self.data.close, period=21) def next(self): if not self.position: if self.rsi < 40: self.buy(size=0.1) else: if self.rsi > 60: self.sell(size=0.1) def runstrat(): args = parse_args() # Create a cerebro entity cerebro = bt.Cerebro(stdstats=False) # Add a strategy cerebro.addstrategy(firstStrategy) # Get a pandas dataframe datapath = args.filepath # Simulate the header row isn't there if noheaders requested skiprows = 1 if args.noheaders else 0 header = None if args.noheaders else 0 dataframe = pd.read_csv(datapath, skiprows=skiprows, header=header, parse_dates=True, index_col=0) dataframe.index = pd.to_datetime(dataframe.index, unit='ms') if not args.noprint: print('--------------------------------------------------') print(dataframe) print('--------------------------------------------------') # Pass it to the backtrader datafeed and add it to the cerebro data = bt.feeds.PandasData(dataname=dataframe,timeframe=bt.TimeFrame.Minutes,openinterest=None) cerebro.adddata(data) # Run over everything cerebro.run() # Plot the result cerebro.plot(style='bar') def parse_args(): parser = argparse.ArgumentParser( description='Pandas test script') parser.add_argument('-nh','--noheaders', action='store_true', default=False, required=False, help='Do not use header rows') parser.add_argument('-np','--noprint', action='store_true', default=False, help='Print the dataframe') parser.add_argument('-fp','--filepath', type=str, default='binance-BTCUSDT-1m-2019-01-01.csv', required=False, help='File path (../../filename.csv') return parser.parse_args() if __name__ == '__main__': runstrat()
But I get these errors:
Traceback (most recent call last): File "loadcsv2.py", line 95, in <module> runstrat() File "loadcsv2.py", line 64, in runstrat cerebro.run() File "/anaconda3/envs/ccxt/lib/python3.7/site-packages/backtrader/cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "/anaconda3/envs/ccxt/lib/python3.7/site-packages/backtrader/cerebro.py", line 1293, in runstrategies self._runonce(runstrats) File "/anaconda3/envs/ccxt/lib/python3.7/site-packages/backtrader/cerebro.py", line 1652, in _runonce strat._once() File "/anaconda3/envs/ccxt/lib/python3.7/site-packages/backtrader/lineiterator.py", line 292, in _once indicator._once() File "/anaconda3/envs/ccxt/lib/python3.7/site-packages/backtrader/lineiterator.py", line 292, in _once indicator._once() File "/anaconda3/envs/ccxt/lib/python3.7/site-packages/backtrader/linebuffer.py", line 632, in _once self.once(self._minperiod, self.buflen()) File "/anaconda3/envs/ccxt/lib/python3.7/site-packages/backtrader/linebuffer.py", line 756, in once self._once_op(start, end) File "/anaconda3/envs/ccxt/lib/python3.7/site-packages/backtrader/linebuffer.py", line 773, in _once_op dst[i] = op(srca[i], srcb[i]) ZeroDivisionError: float division by zero
I suspect the issue is in this line:
data = bt.feeds.PandasData(dataname=dataframe,timeframe=bt.TimeFrame.Minutes,openinterest=None)
I've tried this:
data = bt.feeds.PandasData(dataname=dataframe,timeframe=1,openinterest=None)
and this:
data = bt.feeds.PandasData(dataname=dataframe)
.... but none of them solve the issue.
this is what my csv file looks like:
Timestamp Open High Low Close Volume 1546300800000 3701.23 3703.72 3701.09 3702.46 1.710.011 1546300860000 3702.44 3702.63 3695.66 3697.04 23.700.604 1546300920000 3699.42 3702.04 3696.08 3698.14 14.488.615 1546300980000 3697.49 3698.19 3695.97 3696.51 8.499.966 1546301040000 3697.2 3697.62 3695.0 3696.32 21.782.886 1546301100000 3696.3 3697.24 3695.01 3695.1 12.802.446 1546301160000 3695.16 3695.22 3690.24 3692.19 16.228.177 1546301220000 3690.71 3693.6 3689.88 3690.12 1.641.223 1546301280000 3691.23 3692.88 3691.23 3691.96 97.991
and this is the dataframe:
Open High Low Close Volume Timestamp 2019-01-01 00:00:00 3701.23 3703.72 3701.09 3702.46 17.100110 2019-01-01 00:01:00 3702.44 3702.63 3695.66 3697.04 23.700604 2019-01-01 00:02:00 3699.42 3702.04 3696.08 3698.14 14.488615 2019-01-01 00:03:00 3697.49 3698.19 3695.97 3696.51 8.499966 2019-01-01 00:04:00 3697.20 3697.62 3695.00 3696.32 21.782886
Thanks for any help you can provide!!!
-
The error has to do with division with zero, so I would suspect it has to do with the indicator. There is a parameter for RSI the safediv that you can set to True.
If you think that problem is with reading data, then I would suggest you check that bar(s) in next has the correct data
Hope this helps
-
Thanks @momentum!. You were right, I've activated the safediv and now it works!!
Many thanks!