Skip to content

Converting linreg function from pinescript to Python?

I am trying to convert a TradingView indicator into Python (also using pandas to store its result).

This is the indicator public code I want to convert into a python indicator:

And I am stuck creating that pine script linereg default function.

This is the fragment of the pinescript indicator I have troubles with:

lrc = linreg(src, length, 0)
lrc1 = linreg(src,length,1)
lrs = (lrc-lrc1)
TSF = linreg(src, length, 0)+lrs

This is its documentation:

Linear regression curve. A line that best fits the prices specified over a user-defined time period. It is calculated using the least squares method. The result of this function is calculated using the formula: linreg = intercept + slope * (length – 1 – offset), where length is the y argument, offset is the z argument, intercept and slope are the values calculated with the least squares method on source series (x argument). linreg(source, length, offset) → series[float]


I have found this mql4 code and tried to follow it step by step in order to convert it and finally to create a function linreg in Python in order to use it further for building that pine script indicator:

And this is my code so far:

# calculate linear regression:
barsToCount = 14
# sumy+=Close[i];
df['sumy'] = df['Close'].rolling(window=barsToCount).mean()

# sumxy+=Close[i]*i;
tmp = []
sumxy_lst = []
for window in df['Close'].rolling(window=barsToCount):
    for index in range(len(window)):
        tmp.append(window[index] * index)
    del tmp[:]
df.loc[:,'sumxy'] = sumxy_lst
# sumx+=i;
sumx = 0
for i in range(barsToCount):
    sumx += i
# sumx2+=i*i;
sumx2 = 0
for i in range(barsToCount):
    sumx2 += i * i
# c=sumx2*barsToCount-sumx*sumx;
c = sumx2*barsToCount - sumx*sumx
# Line equation:
# b=(sumxy*barsToCount-sumx*sumy)/c;
df['b'] = ((df['sumxy']*barsToCount)-(sumx*df['sumy']))/c
# a=(sumy-sumx*b)/barsToCount;
df['a'] = (df['sumy']-sumx*df['b'])/barsToCount
# Linear regression line in buffer:
df['LR_line'] = 0.0
for x in range(barsToCount):
    # LR_line[x]=a+b*x;
    df['LR_line'].iloc[x] = df['a'].iloc[x] + df['b'].iloc[x] * x
    # print(x, df['a'].iloc[x], df['b'].iloc[x], df['b'].iloc[x]*x)

It doesn’t work.

Any idea how to create a similar pine script linereg function into python, please?

Thank you in advance!



I used talib to calculate the slope and intercept on the closing prices, then realised talib offers the full calc also. The result looks to be same as TradingView (just eyeballing).

Did the following in jupyterlab:

import pandas as pd
import numpy as np
import talib as tl
from pandas_datareader import data
%run "../../"

asset = data.DataReader('^AXJO', 'yahoo', start='1/1/2015')

n = 270
 .assign(linreg = tl.LINEARREG(asset.Close, n))
 [['Close', 'linreg']]

4 People found this is helpful