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/

    Momentum indicator, arrays and linregress

    Indicators/Strategies/Analyzers
    1
    1
    106
    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.
    • W
      wykazox last edited by

      Dear all,

      Still trying to recreate the momentum indicator as part of the Squeeze momentum indicator of LazyBear in TradingView.

      Made some huge progress, but I am getting stuck with the linregress function.
      The formula involves calculating averages involving the highest high, lowest low and close prices over a certain period, and then calculating the linear regression, via arrays.

      I think I need help with using arrays properly.

      Here is the code of the indicator itself.

      from __future__ import (absolute_import, division, print_function,
                              unicode_literals)
      import backtrader as bt
      import yfinance as yf
      import numpy as np
      from scipy.stats import linregress
      
      
      # Create momentum indicator
      class MomInd(bt.Indicator):
          lines = ('Mom',)
          plotlines = dict(Mom=dict(_method='bar', alpha=0.5, width=0.66))  # need to add colours
      
          params = (('period', 20),)
      
          plotinfo = dict(subplot=True)
      
          def _plotlabel(self):
              plabels = [self.p.period]
              return plabels
      
          def __init__(self):
              self.addminperiod(self.p.period)
      
              highest = bt.ind.Highest(self.data.high, period=self.p.period)
              lowest = bt.ind.Lowest(self.data.low, period=self.p.period)
              midline = (highest + lowest) / 2
              mavg = bt.ind.MovingAverageSimple(self.data.close, period=self.p.period)
              delta = self.data.close - ((midline + mavg) / 2)
              y = np.array(delta)
              x = np.array(self.p.period)
              slope, _, _, _, _ = linregress(x, y)
              self.lines.Mom = slope
      

      and the error message when running the code:

      ValueError: Inputs must not be empty.
      
      Process finished with exit code 1
      

      Thanks for your help!

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