For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Run A classic Simple Moving Average Crossover strategy Happen OverflowError
-
While running the backtrader document "Hello Algotrading!" example, I try to use my own data, and the following error occurred。
Traceback (most recent call last): File "/Users/liuxuyang/PycharmProjects/pythonProject/MyTest.py", line 79, in <module> results = cerebro.run() File "/Users/liuxuyang/PycharmProjects/pythonProject/ven/lib/python3.7/site-packages/backtrader/cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "/Users/liuxuyang/PycharmProjects/pythonProject/ven/lib/python3.7/site-packages/backtrader/cerebro.py", line 1293, in runstrategies self._runonce(runstrats) File "/Users/liuxuyang/PycharmProjects/pythonProject/ven/lib/python3.7/site-packages/backtrader/cerebro.py", line 1652, in _runonce strat._once() File "/Users/liuxuyang/PycharmProjects/pythonProject/ven/lib/python3.7/site-packages/backtrader/lineiterator.py", line 297, in _once indicator._once() File "/Users/liuxuyang/PycharmProjects/pythonProject/ven/lib/python3.7/site-packages/backtrader/lineiterator.py", line 297, in _once indicator._once() File "/Users/liuxuyang/PycharmProjects/pythonProject/ven/lib/python3.7/site-packages/backtrader/lineiterator.py", line 318, in _once self.once(self._minperiod, self.buflen()) File "/Users/liuxuyang/PycharmProjects/pythonProject/ven/lib/python3.7/site-packages/backtrader/indicators/basicops.py", line 364, in once dst[i] = math.fsum(src[i - period + 1:i + 1]) / period OverflowError: intermediate overflow in fsum
-
Could you provide the code you used and and example of your date please?
-
from datetime import datetime import backtrader as bt # Create a subclass of Strategy to define the indicators and logic class SmaCross(bt.Strategy): # list of parameters which are configurable for the strategy params = dict( pfast=10, # period for the fast moving average pslow=30 # period for the slow moving average ) def __init__(self): sma1 = bt.ind.SMA(period=self.p.pfast) # fast moving average sma2 = bt.ind.SMA(period=self.p.pslow) # slow moving average self.crossover = bt.ind.CrossOver(sma1, sma2) # crossover signal def next(self): if not self.position: # not in the market if self.crossover > 0: # if fast crosses slow to the upside self.buy() # enter long elif self.crossover < 0: # in the market & cross to the downside self.close() # close long position def getData(): data = bt.feeds.GenericCSVData( dataname="/Users/liuxuyang/Desktop/btc_usd_1h_new.csv", dtformat='%Y-%m-%d %H:%M:%S', open=1, high=2, low=3, close=4, volume=5, fromdate=datetime(2017, 1, 1), todate=datetime(2020, 11, 4) ) return data cerebro = bt.Cerebro() # create a "Cerebro" engine instance cerebro.adddata(getData()) # Add the data feed cerebro.addstrategy(SmaCross) # Add the trading strategy cerebro.run() # run it all cerebro.plot() # and plot it with a single command
csv file linkl https://drive.google.com/file/d/15mtQP3wSVuG5OCUtasAjEMy-nWvBF1tx/view?usp=sharing
-
There is no need for getData() function. Just add data to the cerebro.
data = bt.feeds.GenericCSVData( dataname="/Users/liuxuyang/Desktop/btc_usd_1h_new.csv", dtformat='%Y-%m-%d %H:%M:%S', open=1, high=2, low=3, close=4, volume=5, fromdate=datetime(2017, 1, 1), todate=datetime(2020, 11, 4) ) cerebro.adddata(data)
-
@rajanprabu It's still going to go wrong,I don’t know if it’s because of too much data
-
I suggest you to check the data. Have you tried running for shorter time say for 1 year ?
fromdate=datetime(2017, 1, 1), todate=datetime(2017, 12, 31)
-
Oh, I found the problem-- it is the data issue, several lines of data around 2020/04/26 are missing. I removed these lines so the whole thing went through.