Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    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

    General Code/Help
    3
    7
    1453
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Xu Liu
      Xu Liu last edited by

      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
      
      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        Could you provide the code you used and and example of your date please?

        RunBacktest.com

        Xu Liu 1 Reply Last reply Reply Quote 0
        • Xu Liu
          Xu Liu @run-out last edited by

          @run-out

          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

          1 Reply Last reply Reply Quote 0
          • R
            rajanprabu last edited by

            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) 
            
            Xu Liu 1 Reply Last reply Reply Quote 0
            • Xu Liu
              Xu Liu @rajanprabu last edited by

              @rajanprabu It's still going to go wrong,I don’t know if it’s because of too much data

              1 Reply Last reply Reply Quote 0
              • R
                rajanprabu last edited by

                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)
                
                1 Reply Last reply Reply Quote 0
                • Xu Liu
                  Xu Liu last edited by

                  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.

                  1 Reply Last reply Reply Quote 1
                  • 1 / 1
                  • First post
                    Last post
                  Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors