Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. RamSurya
    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 5
    • Best 1
    • Controversial 0
    • Groups 0

    RamSurya

    @RamSurya

    1
    Reputation
    2
    Profile views
    5
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    RamSurya Unfollow Follow

    Best posts made by RamSurya

    • RE: Printing Stock name in the log

      Hello , I am also fairly new to backtrader , However without giving the data name to the cerebro engine while adding the data we would not get the data name I would recommend to use the name parameter while adding the data
      3e8c8a57-879a-44b1-907c-18ad43008af6-image.png

      posted in General Code/Help
      RamSurya
      RamSurya

    Latest posts made by RamSurya

    • RE: Printing Stock name in the log

      @kk That is fine manh.

      posted in General Code/Help
      RamSurya
      RamSurya
    • RE: Printing Stock name in the log

      Happy to help!!

      posted in General Code/Help
      RamSurya
      RamSurya
    • RE: Array index Out of range for Multitimeframe sampling mainly on compression more than 50

      I have resolved the issue sorry for the trouble !!.

      posted in Indicators/Strategies/Analyzers
      RamSurya
      RamSurya
    • RE: Printing Stock name in the log

      Hello , I am also fairly new to backtrader , However without giving the data name to the cerebro engine while adding the data we would not get the data name I would recommend to use the name parameter while adding the data
      3e8c8a57-879a-44b1-907c-18ad43008af6-image.png

      posted in General Code/Help
      RamSurya
      RamSurya
    • Array index Out of range for Multitimeframe sampling mainly on compression more than 50

      The following code listed below works perfectly fine when working on resampling till compression value 50 however after 50 I am getting a strange error. Like Array index out of range even after using the Data coupling concept... Kindly help me out with the BUG. After referring through the Main code and strategy code. The same code works well on every time frame less than 50 mins such as 30 mins, 40 mins, etc. Moreover, as I am new to this platform I would like to work with people who are working on crypto, I would love to connect and work together.

      "ERROR":

      Traceback (most recent call last):
        File ".\resampler_main.py", line 102, in <module>
          back = cerebro.run()
        File "C:\Users\ramsu\Anaconda3\envs\crypto\lib\site-packages\backtrader\cerebro.py", line 1127, in run
          runstrat = self.runstrategies(iterstrat)
        File "C:\Users\ramsu\Anaconda3\envs\crypto\lib\site-packages\backtrader\cerebro.py", line 1298, in runstrategies
          self._runnext(runstrats)
        File "C:\Users\ramsu\Anaconda3\envs\crypto\lib\site-packages\backtrader\cerebro.py", line 1630, in _runnext
          strat._next()
        File "C:\Users\ramsu\Anaconda3\envs\crypto\lib\site-packages\backtrader\strategy.py", line 347, in _next
          super(Strategy, self)._next()
        File "C:\Users\ramsu\Anaconda3\envs\crypto\lib\site-packages\backtrader\lineiterator.py", line 263, in _next
          indicator._next()
        File "C:\Users\ramsu\Anaconda3\envs\crypto\lib\site-packages\backtrader\lineiterator.py", line 263, in _next
          indicator._next()
        File "C:\Users\ramsu\Anaconda3\envs\crypto\lib\site-packages\backtrader\lineiterator.py", line 263, in _next
          indicator._next()
        File "C:\Users\ramsu\Anaconda3\envs\crypto\lib\site-packages\backtrader\lineiterator.py", line 282, in _next
          self.nextstart()  # only called for the 1st value
        File "C:\Users\ramsu\Anaconda3\envs\crypto\lib\site-packages\backtrader\indicators\crossover.py", line 41, in nextstart
          self.l.nzd[0] = self.data0[0] - self.data1[0]  # seed value
        File "C:\Users\ramsu\Anaconda3\envs\crypto\lib\site-packages\backtrader\lineseries.py", line 467, in __getitem__
          return self.lines[0][key]
        File "C:\Users\ramsu\Anaconda3\envs\crypto\lib\site-packages\backtrader\linebuffer.py", line 163, in __getitem__
          return self.array[self.idx + ago]
      IndexError: array index out of range
      

      Following is the main code to run the cerebro:

      cerebro = bt.Cerebro()
      
      # adding data for a list of stocks
      for instrument in cryptos:
          df = pd.read_csv(rf"C:\Users\ramsu\ltphd\data\Crypto_data\Backtesting\\{instrument}.csv",
                           index_col=0, parse_dates=True)
          df = df[-10000:]
          # adding the data into the pandas frame
          data = bt.feeds.PandasData(
              dataname=df, timeframe=bt.TimeFrame.Minutes, compression=1)
          # Adding the data
          cerebro.adddata(data, name=instrument)
          # resampling the data
          cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes,
                               compression=60, rightedge=False)
      
      cerebro.addstrategy(long)
      
      cerebro.broker.setcash(100000.0)
      
      cerebro.addsizer(bt.sizers.PercentSizer, percents=5)
      cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name="sharpe")
      cerebro.addanalyzer(bt.analyzers.Returns,     _name="returns")
      cerebro.addanalyzer(bt.analyzers.Transactions, _name="trans")
      cerebro.addwriter(bt.WriterFile, csv=True,
                        out=os.path.realpath('log_file'), rounding=2)
      
      back = cerebro.run()
      
      print(cerebro.broker.getvalue())
      

      Listed below is the simple long strategy test code.

      class long(bt.Strategy):
      
          def __init__(self):
      
              self.crossover = []
              self.fast_emas = []
              self.slow_emas = []
              for index in range(0, len(cryptos)+len(cryptos), 2):
                  self.fast_ema = bt.ind.EMA(self.datas[index].close, period=5)
                  self.slow_ema = bt.ind.EMA(self.datas[index+1].close, period=20)
      
                  self.crossover.append(bt.ind.CrossOver(
                      self.fast_ema, self.slow_ema()))
                  self.fast_emas.append(self.fast_ema)
                  self.slow_emas.append(self.slow_ema())
      
          def next(self):
              for index, value in enumerate(list(range(0, len(cryptos)+len(cryptos), 2))):
                  # print(self.crossover[index])
                  # print(index)
                  if not self.getposition(self.datas[value]).size:
                      # print(self.getposition(self.datas[value]).size)
                      if self.crossover[index] > 0:
                          print(f"Higher time is {self.datas[value+1].datetime.datetime(0)} slowMA is {self.slow_emas[index][0] }\
                          Timeframe is {self.datas[value].datetime.datetime(0)}  LONGMA is {self.fast_emas[index][0]} \
                          Buying at{self.datas[value]._name} instrument at", self.datas[value].close[0])
                          self.buy(data=self.datas[value])
                  elif self.crossover[index] < 0:
                      self.close(data=self.datas[value])
                      print(
                          f"Higher time is {self.datas[value+1].datetime.datetime(0)} slowMA is {self.slow_emas[index][0]} \
                          Timeframe is {self.datas[value].datetime.datetime(0)} LONGMA is {self.fast_emas[index][0]} \
                          Buying at{self.datas[value]._name} instrument at", self.datas[value].close[0])
      
      posted in Indicators/Strategies/Analyzers
      RamSurya
      RamSurya