How to create a custom indicator based on other libraries
Hello! I'm new in backtrader and I'm learning how to create custom indicators and strategies to test them. I would like add a custom indicator based on
KalmanFilterclass from the
pykalmanlibrary. I created the a class named Kalman that inherits from
KalmanFilterand then added to cerebro using the addindicator function, but when I use the run function, it always gives me an error, saying that some arguments are missing or a
TypeError: '>' not supported between instances of 'tuple' and 'float'. Which is the correct way to add an indicator like this and how should be incorporated to a trading strategy? I tried to follow some tutorials on backtrader's website, thats why there is a
self.lat the end of the code. Also, in the tutorial, what is the
The code goes as follows:
class Kalman(bt.Indicator,KalmanFilter): lines = ('Kalman',) def __init__(self,transition_matrices = , observation_matrices = , initial_state_mean = 0, initial_state_covariance = 1, observation_covariance=1, transition_covariance=.0001, n_dim_state=None, n_dim_obs=None): KalmanFilter.__init__(self,transition_matrices = , observation_matrices = , initial_state_mean = 0, initial_state_covariance = 1, observation_covariance=1, transition_covariance=.0001, n_dim_state=None, n_dim_obs=None) _filter = self.filter(self.data.close) self.l.kfilter = bt.Cmp(_filter,self.data.close)
What you are trying to do is not trivial. If you are new to backtrader, I would definitely not start with this. Work on some of the standard code in the docs first. Good luck!
@run-out I know that what I'm trying to achieve is not trivial, but I am currently using a strategy that uses KalmanFilter. I did some backtesting on my own (I mean I coded my own backtester), but I would like to repeat the backtest using a better tool like backtrader, that not only enables me to test strategies, but optimize them too.
Also, I like the idea of having the same backtest engine to test my strategies and not having to (re)code all things again when doing new strategies.
And try out this article. Written by backtraders creater Daniel Rodriquez.
@run-out Thank you :). In those blog posts was an implementation of Kalman :)
@ppsev I wish I could say I planned that!