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/

    Adding new lines to data feed while downsampling on data with price bars

    General Code/Help
    2
    5
    111
    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.
    • A
      Akul Kanojia last edited by

      Hi all,
      I downsampled data feed using filters to make price bars. Now there is some information that I want to retain with the new data feed for eg, total qty traded within the price bar, which I did on by updating a column names Qty.

      But let's say I want to retain additional information eg timeDelta, priceDelta, etc., I would require additional columns in the data itself.

      How to add such columns while applying a filter to a data feed?

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @Akul Kanojia last edited by

        @Akul-Kanojia said in Adding new lines to data feed while downsampling on data with price bars:

        How to add such columns while applying a filter to a data feed?

        You said you have used filters, but which?

        If your filter is custom you will have to copy the data from the input column to the output column.

        A 1 Reply Last reply Reply Quote 0
        • A
          Akul Kanojia last edited by

          Yes, I have a custom filter, I'm trying to aggregate data in price bars. In the small illustration below, I'm trying to make a new line 'priceDiff'. The line 'Ltp' is already defined in a custom feed deriving GenericCSVData class.

          #!/usr/bin/env python3
          # -*- coding: utf-8 -*-
          """
          Created on Wed Dec 11 06:41:45 2019
          
          @author: akul
          """
          #!/usr/bin/env python
          # -*- coding: utf-8; py-indent-offset:4 -*-
          ###############################################################################
          #
          # Copyright (C) 2015, 2016, 2017 Daniel Rodriguez
          #
          # This program is free software: you can redistribute it and/or modify
          # it under the terms of the GNU General Public License as published by
          # the Free Software Foundation, either version 3 of the License, or
          # (at your option) any later version.
          #
          # This program is distributed in the hope that it will be useful,
          # but WITHOUT ANY WARRANTY; without even the implied warranty of
          # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
          # GNU General Public License for more details.
          #
          # You should have received a copy of the GNU General Public License
          # along with this program.  If not, see <http://www.gnu.org/licenses/>.
          #
          ###############################################################################
          from __future__ import (absolute_import, division, print_function,
                                  unicode_literals)
          
          
          from . import Filter
          
          
          __all__ = ['RangeBarFilter']
          
          
          class RangeBarFilter(Filter):
              '''Modify the data stream to create rangebars of price change similar to Renko
          
              Params:
          
                - ``size`` (default: *None*) The size to consider for each brick
          
                - ``autosize`` (default: *20.0*) If *size* is *None*, this will be used
                  to autocalculate the size of the bricks (simply dividing the current
                  price by the given value)
          
                - ``align`` (default: *1.0*) Factor use to align the price boundaries of
                  the bricks. If the price is for example *3563.25* and *align* is
                  *10.0*, the resulting aligned price will be *3560*. The calculation:
          
                    - 3563.25 / 10.0 = 356.325
                    - round it and remove the decimals -> 356
                    - 356 * 10.0 -> 3560
          
              See:
                - http://stockcharts.com/school/doku.php?id=chart_school:chart_analysis:renko
          
              '''
          
              params = (
                  ('size', None),
                  ('autosize', 250.0),
                  ('align', 1.0),
              )
          
              def nextstart(self, data):
                  o = data.lines.Ltp[0]
                  o = round(o / self.p.align, 0) * self.p.align  # aligned
                  self._size = self.p.size or float(o // self.p.autosize)
                  self._top = int(o) + self._size
                  self._bot = int(o) - self._size
                  self.initialPrice = data.lines.Ltp[0]
                  
              def next(self, data):
                  
                  if (data.lines.Ltp[0] > self._top) or (data.lines.Ltp[0] < self._bot):
                      data.lines.priceDiff[0] = data.lines.Ltp[0] - self.initialPrice
                      self.initialPrice = None
                      o = data.lines.Ltp[0]
                      o = round(o / self.p.align, 0) * self.p.align  # aligned
                      self._size = self.p.size or float(o // self.p.autosize)
                      self._top = int(o) + self._size
                      self._bot = int(o) - self._size
                      return False  # length of data stream is unaltered
          
                  if(not self.initialPrice):
                     self.initialPrice  = data.lines.Ltp[0]
                  data.backwards()
                  return True  # length of stream was changed, get new bar
           
          

          The error I get it something like this,

          Traceback (most recent call last):
            File "testpandas.py", line 52, in <module>
              cerebro.run()
            File "/home/akul/Documents/backtrader/cerebro.py", line 1127, in run
              runstrat = self.runstrategies(iterstrat)
            File "/home/akul/Documents/backtrader/cerebro.py", line 1212, in runstrategies
              data.preload()
            File "/home/akul/Documents/backtrader/feed.py", line 688, in preload
              while self.load():
            File "/home/akul/Documents/backtrader/feed.py", line 523, in load
              retff = ff(self, *fargs, **fkwargs)
            File "/home/akul/Documents/backtrader/flt.py", line 48, in __call__
              self.next(data)
            File "/home/akul/Documents/backtrader/filters/rangeBarFilter.py", line 94, in next
              data.lines.priceDiff[0] = data.lines.Ltp[0] - self.initialPrice
          AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'priceDiff'
          
          
          1 Reply Last reply Reply Quote 0
          • A
            Akul Kanojia @backtrader last edited by

            @backtrader Please look at the above illustration. Thanks in advance.

            1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators last edited by

              Let me tell you what I have done

              1. See what the error is ...

              @Akul-Kanojia said in Adding new lines to data feed while downsampling on data with price bars:

              File "/home/akul/Documents/backtrader/filters/rangeBarFilter.py", line 94, in next

                  data.lines.priceDiff[0] = data.lines.Ltp[0] - self.initialPrice
              AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'priceDiff'
              

              2. Read what your intention is

              @Akul-Kanojia said in Adding new lines to data feed while downsampling on data with price bars:

              I'm trying to make a new line 'priceDiff'. The line 'Ltp' is already defined in a custom feed deriving GenericCSVData class.

              and I have to read that because the custom data feed is not there (probably the key to the problem)

              3. Conclude that you haven't defined a line named priceDiff in your custom data feed. Unfortunately, lines are not created out of thin air.

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
              $(document).ready(function () { app.coldLoad(); }); }