Momentum indicator, arrays and linregress
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!
Jérémy A. last edited by
Hi @wykazox Hi all
Did you get an answer / did you find a way to solve your issue ? Still trying to develop a custom indicator that would include linear regression
@Jérémy-A, a lot if the stuff that the original poster did in init() need to be done in next(). There is a very good blog article that describes how to implement a linregress indicator in next() and how to implement the same indicator in init(). https://www.backtrader.com/blog/2019-05-20-momentum-strategy/momentum-strategy/