Backtrader Community

    • 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/

    Own indicator: "if function" with min max is very slow

    Indicators/Strategies/Analyzers
    momentum indicator if function
    2
    3
    296
    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.
    • J
      Jonny8 last edited by

      Hi everyone,

      I am having a hard time creating / adapting an indicator.
      I made it in the most slow way and I wonder whether there is a better solution.

      The idea is to have an if positive RoC the 1 else 0.

      class Momentumplus_org(bt.Indicator):
          lines = ('trend',)
          params = (('period', 190), ('rperiod', 30))
          
          def __init__(self):
              self.addminperiod(self.params.period)
              self.roc = bt.ind.ROC(self.data, period=self.p.rperiod)
      
          def next(self):
              returns = np.log(self.data.get(size=self.p.period))
              x = np.arange(len(returns))
              slope, _, rvalue, _, _ = linregress(x, returns)
              annualized = (1 + slope) ** 252
              test = math.ceil(self.roc[0])
              rate = bt.Min(1, bt.Max(0, test))
              self.lines.trend[0] = annualized * (rvalue ** 2) * rate
      

      This takes a hell of a time to get calculated. I tried to get the test and rate into the init ...

      I also tried to keep the original design:

      def momentum_func(the_array):
          r = np.log(the_array)
          slope, _, rvalue, _, _ = linregress(np.arange(len(r)), r)
          annualized = (1 + slope) ** 252
          return annualized * (rvalue ** 2)
      
      
      class Momentum(bt.ind.OperationN):
          lines = ('trend',)
          params = dict(period=50)
          func = momentum_func
      

      But I couldn't make it work with two parameters...

      Are there better ways than math.ceil / min / max?

      Thank you very much in advance!

      Best

      1 Reply Last reply Reply Quote 0
      • C
        crunchypickle last edited by

        @Jonny8 said in Own indicator: "if function" with min max is very slow:

        test

        are you sure it is slow where you said it was? Have you profiled the code?

        J 1 Reply Last reply Reply Quote 1
        • J
          Jonny8 @crunchypickle last edited by

          @crunchypickle

          Good call.

          I used cProfile to do it. https://towardsdatascience.com/how-to-profile-your-code-in-python-e70c834fad89

          There is a execution difference of 20% but it does not seem to come from min max or ceil, I assume it is due to the roc calculation in the init.

          Thank you chrunchypickle!

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors