How to convert pandas dataframe to line object?
-
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 callableThe 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 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 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 codeclass 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
-
@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.
-
@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
-
@tony-0 Do you mind if I ask why you don't want to pre-process this?