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!