Navigation

    Backtrader Community

    • Register
    • 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/

    How to convert pandas dataframe to line object?

    Indicators/Strategies/Analyzers
    2
    8
    362
    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.
    • T
      Tony 0 last edited by

      Hi all,
      I'm trying to port an indicator implemented by the FinTa library. The indicator receives as input the pandas dataframe in the format ohlc and returns two variables per each candle that analyzes in this format:

                                    WT1.       WT2.
      

      datetime
      2019-08-27 04:00:00+00:00 NaN NaN
      2019-08-27 05:00:00+00:00 116.666667 NaN
      2019-08-27 06:00:00+00:00 81.045764 NaN
      2019-08-27 07:00:00+00:00 23.298112 NaN
      2019-08-27 08:00:00+00:00 43.091340 66.025471
      ... ... ...

      This is the code I have used to integrate this indicator:

      class WTO(bt.Indicator):
      lines = (('wt1'), ('wt2'),)

      def __init__(self):
          self.addminperiod(10)
          ohlc = self.data._dataname[['open', 'high', 'low', 'close']]
          
          wto = ft.TA.WTO(ohlc, channel_lenght = 7,
                          average_lenght = 21)
          print(wto)
          # print(wto['WT1.'].to_list())
          # print(wto['WT2.'].to_list())
          self.lines.wt1 = wto['WT1.'].values
          self.lines.wt2 = wto['WT2.'].values
      

      The interpreter gives me the following error:

      Traceback (most recent call last):
      File "backtrade2.py", line 101, in <module>
      cerebro.run()
      File "/home/antonino/.local/lib/python3.6/site-packages/backtrader/cerebro.py", line 1127, in run
      runstrat = self.runstrategies(iterstrat)
      File "/home/antonino/.local/lib/python3.6/site-packages/backtrader/cerebro.py", line 1217, in runstrategies
      strat = stratcls(*sargs, **skwargs)
      File "/home/antonino/.local/lib/python3.6/site-packages/backtrader/metabase.py", line 88, in call
      _obj, args, kwargs = cls.doinit(_obj, *args, **kwargs)
      File "/home/antonino/.local/lib/python3.6/site-packages/backtrader/metabase.py", line 78, in doinit
      _obj.init(*args, **kwargs)
      File "backtrade2.py", line 27, in init
      self.my_wto = WTO(self.datas[0])
      File "/home/antonino/.local/lib/python3.6/site-packages/backtrader/indicator.py", line 53, in call
      return super(MetaIndicator, cls).call(*args, **kwargs)
      File "/home/antonino/.local/lib/python3.6/site-packages/backtrader/metabase.py", line 88, in call
      _obj, args, kwargs = cls.doinit(_obj, *args, **kwargs)
      File "/home/antonino/.local/lib/python3.6/site-packages/backtrader/metabase.py", line 78, in doinit
      _obj.init(*args, **kwargs)
      File "backtrade2.py", line 22, in init
      self.lines.wt1 = wto['WT1.'].values
      File "/home/antonino/.local/lib/python3.6/site-packages/backtrader/lineseries.py", line 79, in set
      value = value(0)
      TypeError: 'numpy.ndarray' object is not callable

      The error is clearly caused by the fact that backtrader expects data in a different format. It should be a line object. Is there any good way to convert my data in that format. I can work around this issue by calculating the indicator outside backtrader and then feed it as input data, but this sounds like a dirty workaround.

      Thanks for your help.

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

        @tony-0 I hope I'm following you correctly, but if so, you should preprocess your pandas ohlc and the indicators before loading data into your backtest. You can load a pandas dataframe using this and this

        RunBacktest.com

        T 1 Reply Last reply Reply Quote 0
        • T
          Tony 0 @run-out last edited by

          @run-out That is definitely an option, but I would like to integrate the Wave Trend Oscillator indicator from the FinTa (https://pypi.org/project/finta/) library directly within my strategy. Is that possible?

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

            @tony-0 I think a good option then would be to create a custom indicator. I have used pandas inside custom indicators to so some heavy lifting. Have a look here and see if this helps.

            RunBacktest.com

            T 1 Reply Last reply Reply Quote 0
            • T
              Tony 0 @run-out last edited by

              @run-out I'm trying to create it as a custom indicator but I don't know how to convert the FinTa indicator's output in a line object.
              This is my code

              class WTO(bt.Indicator):
                  lines = (('wt1'), ('wt2'),)
                  
                  def __init__(self):
                      self.addminperiod(10)
                      ohlc = self.data._dataname[['open', 'high', 'low', 'close']]
                      
                      wto = ft.TA.WTO(ohlc, channel_lenght = 7,
                                      average_lenght = 21)
                      print(wto)
                      self.lines.wt1 = wto['WT1.'].values
                      self.lines.wt2 = wto['WT2.'].values
              
              run-out 1 Reply Last reply Reply Quote 0
              • run-out
                run-out @Tony 0 last edited by

                @tony-0 Have you checked to see if you are getting values here:

                self.lines.wt1 = wto['WT1.'].values
                self.lines.wt2 = wto['WT2.'].values
                

                Assuming you are, you should then be getting lines for your indicator. However, I think you have to take out the braces in your lines definition :

                lines = ('wt1', 'wt2',)
                

                Let us know if that works, I'm interested to know.

                RunBacktest.com

                T 1 Reply Last reply Reply Quote 0
                • T
                  Tony 0 @run-out last edited by

                  @run-out
                  Yes, if I print those to variables I get:

                  [         nan 116.66666667  81.04576412 ...  -3.12979911   6.40985569
                    13.60683347]
                  [         nan          nan          nan ... -16.15140739  -7.0198421
                     1.50132138]
                  

                  I tried to remove the braces but that didn't change. I still get the same error.

                  Traceback (most recent call last):
                    File "backtrade2.py", line 126, in <module>
                      cerebro.run()
                    File "/home/antonino/.local/lib/python3.6/site-packages/backtrader/cerebro.py", line 1127, in run
                      runstrat = self.runstrategies(iterstrat)
                    File "/home/antonino/.local/lib/python3.6/site-packages/backtrader/cerebro.py", line 1217, in runstrategies
                      strat = stratcls(*sargs, **skwargs)
                    File "/home/antonino/.local/lib/python3.6/site-packages/backtrader/metabase.py", line 88, in __call__
                      _obj, args, kwargs = cls.doinit(_obj, *args, **kwargs)
                    File "/home/antonino/.local/lib/python3.6/site-packages/backtrader/metabase.py", line 78, in doinit
                      _obj.__init__(*args, **kwargs)
                    File "backtrade2.py", line 41, in __init__
                      self.my_wto = WTO(self.datas[0])
                    File "/home/antonino/.local/lib/python3.6/site-packages/backtrader/indicator.py", line 53, in __call__
                      return super(MetaIndicator, cls).__call__(*args, **kwargs)
                    File "/home/antonino/.local/lib/python3.6/site-packages/backtrader/metabase.py", line 88, in __call__
                      _obj, args, kwargs = cls.doinit(_obj, *args, **kwargs)
                    File "/home/antonino/.local/lib/python3.6/site-packages/backtrader/metabase.py", line 78, in doinit
                      _obj.__init__(*args, **kwargs)
                    File "backtrade2.py", line 36, in __init__
                      self.lines.wt1 = wto['WT1.'].values 
                    File "/home/antonino/.local/lib/python3.6/site-packages/backtrader/lineseries.py", line 79, in __set__
                      value = value(0)
                  TypeError: 'numpy.ndarray' object is not callable
                  
                  run-out 1 Reply Last reply Reply Quote 0
                  • run-out
                    run-out @Tony 0 last edited by

                    @tony-0 Do you mind if I ask why you don't want to pre-process this?

                    RunBacktest.com

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