Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. Xu Liu
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 9
    • Best 3
    • Groups 0

    Xu Liu

    @Xu Liu

    3
    Reputation
    17
    Profile views
    9
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    Xu Liu Unfollow Follow

    Best posts made by Xu Liu

    • RE: Run A classic Simple Moving Average Crossover strategy Happen OverflowError

      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.

      posted in General Code/Help
      Xu Liu
      Xu Liu
    • RE: Cryptowatch Data Feed

      example:

      import time
      
      import cryptowatch as cw
      
      cw.api_key = "your public key"
      
      # Return market candlestick info (open, high, low, close, volume) on some timeframes
      list = cw.markets.get("BINANCE:BTCUSDT", ohlc=True, periods=["15m"])
      
      print(list)
      print(len(list.of_15m))
      it = iter(list.of_15m)
      for i in it:
          i[0] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(i[0]))
          print(i)
      
      ['2020-11-04 21:30:00', 13803.38, 13860.0, 13786.87, 13834.74, 728.051491, 10066510.70990301]
      ['2020-11-04 21:45:00', 13834.73, 13895.0, 13823.83, 13858.36, 880.441081, 12210240.06888313]
      ['2020-11-04 22:00:00', 13858.36, 13885.0, 13832.5, 13835.13, 631.065418, 8745946.00082795]
      
      posted in General Code/Help
      Xu Liu
      Xu Liu
    • RE: Cryptowatch Data Feed

      https://github.com/cryptowatch/cw-sdk-python
      It is very clear here, you can copy the code to familiarize yourself with the usage

      posted in General Code/Help
      Xu Liu
      Xu Liu

    Latest posts made by Xu Liu

    • RE: Live Data Feed

      @Xu-Liu emmm, ‘return None return’ None should be changed ‘continue’

      posted in General Code/Help
      Xu Liu
      Xu Liu
    • RE: Live Data Feed

      I can provide an example of myself

      import _thread
      from datetime import datetime
      import time
      import backtrader as bt
      from backtrader import date2num
      from backtrader.feeds import DataBase
      
      
      class LiveDataStream(DataBase):
      
          def __init__(self):
              self.isOpen = True
              self.i = 0
              _thread.start_new_thread(self._timerstart, (1,))
      
          def start(self):
              super(LiveDataStream, self).start()
      
          def _timerstart(self, n):
              while True:
                  if self.isOpen == False:
                      self.isOpen = True
                  time.sleep(n)
      
          def _load(self):
              if self.isOpen == True:
                  # print(self.i)
                  self.i += 1
                  self.lines.datetime[0] = date2num(datetime.now())
                  # Get the rest of the unpacked data
                  self.lines.open[0] = 100 + self.i
                  self.lines.high[0] = 120 + self.i
                  self.lines.low[0] = 80 + self.i
                  self.lines.close[0] = 90 + self.i
                  self.lines.volume[0] = 2342 + self.i
                  self.lines.openinterest[0] = 443 + self.i
                  self.isOpen = False
                  return True
              else:
                  return None
      
          def islive(self):
              '''Returns ``True`` to notify ``Cerebro`` that preloading and runonce
              should be deactivated'''
              return True
      
          def stop(self):
              '''Stops and tells the store to stop'''
              super().stop()
      
      
      class MyStrategy(bt.Strategy):
          def __init__(self):
              pass
      
          def next(self):
              print('{} -- {} -- {}'.format(self.data.open[0], self.data.high[0], self.data.close[0]))
      
          def notify_data(self, data, status, *args, **kwargs):
              if status == data.LIVE:
                  print("live data")
              elif status == data.DELAYED:
                  print("DELAYED data")
      
      
      cerebro = bt.Cerebro()
      cerebro.addstrategy(MyStrategy)
      cerebro.adddata(LiveDataStream())
      cerebro.run()
      cerebro.plot()
      print("over")
      
      
      posted in General Code/Help
      Xu Liu
      Xu Liu
    • RE: Cryptowatch Data Feed

      https://github.com/cryptowatch/cw-sdk-python
      It is very clear here, you can copy the code to familiarize yourself with the usage

      posted in General Code/Help
      Xu Liu
      Xu Liu
    • RE: Cryptowatch Data Feed

      example:

      import time
      
      import cryptowatch as cw
      
      cw.api_key = "your public key"
      
      # Return market candlestick info (open, high, low, close, volume) on some timeframes
      list = cw.markets.get("BINANCE:BTCUSDT", ohlc=True, periods=["15m"])
      
      print(list)
      print(len(list.of_15m))
      it = iter(list.of_15m)
      for i in it:
          i[0] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(i[0]))
          print(i)
      
      ['2020-11-04 21:30:00', 13803.38, 13860.0, 13786.87, 13834.74, 728.051491, 10066510.70990301]
      ['2020-11-04 21:45:00', 13834.73, 13895.0, 13823.83, 13858.36, 880.441081, 12210240.06888313]
      ['2020-11-04 22:00:00', 13858.36, 13885.0, 13832.5, 13835.13, 631.065418, 8745946.00082795]
      
      posted in General Code/Help
      Xu Liu
      Xu Liu
    • RE: Run A classic Simple Moving Average Crossover strategy Happen OverflowError

      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.

      posted in General Code/Help
      Xu Liu
      Xu Liu
    • RE: Run A classic Simple Moving Average Crossover strategy Happen OverflowError

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

      posted in General Code/Help
      Xu Liu
      Xu Liu
    • RE: Run A classic Simple Moving Average Crossover strategy Happen OverflowError

      @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

      posted in General Code/Help
      Xu Liu
      Xu Liu
    • RE: extending data feed ==> list index out of range issue

      I have the same problem,My solution is to get more columns out。

      posted in General Code/Help
      Xu Liu
      Xu Liu
    • 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
      
      posted in General Code/Help
      Xu Liu
      Xu Liu