Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Momentum Indicator using Linear regression help

Hi,

I'm new to python and trying to code a squeeze momentum indicator. When I try to use the linear regression function it throws an error.

This is the formula I'm trying to emulate into python. I've commented out the code that is erroring so I can attach the backtest.

Forumla I want to use:

val = linreg(source  -  avg(avg(highest(high, lengthKC), lowest(low, lengthKC)),sma(close,lengthKC)),  
            lengthKC,0)  

My version:

val2 =  stats.linregress(hist['close'][-1] - ((((hist['high'].max() + hist['low'].min()) / 2) + hist['close'].mean() ) / 2), lengthKC)  

I'm sure I'm just not understanding what im doing.

Thanks
-Danny

1 response

Hi Danny,

Most newcomers don't realize that you can actually debug your code execution. Just double-click on the line number and you can evaluate expressions in debug mode:

when you do:

hist['close'][-1] - ((((hist['high'].max() + hist['low'].min()) / 2) + hist['close'].mean() ) / 2)
-->float64: 3.128775
your expression is a float whereas linegress expects an array like structure:

scipy.stats.linregress(x, y=None)

Calculate a linear least-squares regression for two sets of measurements.
Parameters:
x, y : array_like
Two sets of measurements. Both arrays should have the same length. If only x is given (and y=None), then it must be a two-dimensional array where one dimension has length 2. The two sets of measurements are then found by splitting the array along the length-2 dimension.