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 create a custom indicator based on other libraries

    Indicators/Strategies/Analyzers
    2
    6
    298
    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.
    • P
      ppsev last edited by

      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 KalmanFilter class from the pykalman library. I created the a class named Kalman that inherits from bt.Indicator and KalmanFilter and 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.l at the end of the code. Also, in the tutorial, what is the self.p?

      The code goes as follows:

      class Kalman(bt.Indicator,KalmanFilter):
           lines = ('Kalman',)
      
           def __init__(self,transition_matrices = [1],
                       observation_matrices = [1],
                       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 = [1],
                                    observation_matrices = [1],
                                    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)
      
      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        @ppsev said in How to create a custom indicator based on other libraries:

        pykalman

        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!

        RunBacktest.com

        P 1 Reply Last reply Reply Quote 1
        • P
          ppsev @run-out last edited by

          @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.

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

            @ppsev This thing you will need to focus on is how to build custom indicators. Search the forum and interwebs. Start with this from the docs.

            And try out this article. Written by backtraders creater Daniel Rodriquez.

            https://www.backtrader.com/blog/posts/2015-07-18-developing-an-indicator/developing-an-indicator/

            RunBacktest.com

            P 1 Reply Last reply Reply Quote 2
            • P
              ppsev @run-out last edited by

              @run-out Thank you :). In those blog posts was an implementation of Kalman :)

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

                @ppsev I wish I could say I planned that!

                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