Adding new lines to data feed while downsampling on data with price bars
-
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?
-
@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.
-
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'
-
@backtrader Please look at the above illustration. Thanks in advance.
-
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.