2

I'm trying to port a strategy from tradingview (pinescript) to a framework I built in nodejs. My strategy uses breakouts on the Bollinger bands. in my pine script I've implemented the BBs with standard deviation ( basically copying the build-in indicator formula) as follows:

length = input(200, "BB Len", minval=1)
mult = input(1.5, "BB STD", minval=0.001, maxval=50, type=input.float)


basis = sma(src, length)
dev = mult * stdev(src, length)

upper1 = basis + dev
lower1 = basis - dev

Based on this a problem arose because of two reasons:

First reason.

As I can see in the explain of the STDDEV function in talib, there are three parameters instead of the two you get in the pine script version:

  1. inReal: the data serie
  2. optInNbDev: Deviations, with a default value of 1
  3. optInTimePeriod: Time Period, with a default value of 5 ( pine script does not have this )

Second reason.

As I noticed in the pine script reference in tradingview, it says that the stdev function is "a biased estimation of standard deviation.". I couldn't find any specific on this topic in the talib's doc, but I am assuming that they are applying the non-biased formula, based on some comments I found googleing here and there.

Based on this, do you think there would be a way to precisely replicate the tradingview behaviour using ta-lib?

holographix
  • 2,497
  • 2
  • 32
  • 46

2 Answers2

2

To remove all doubts, you can write your own function to calculate the standard deviation. I also had the experience of transferring to another platform and I needed a formula for calculating the standard deviation. Here is the result of my experiments.

//@version=4
study("Standard Deviation",shorttitle="Stdev")
length = input(14)

a = sma(pow(close,2),length)
b = pow(sum(close,length),2)/pow(length,2)
c = sqrt(a - b)

plot(c,title="Stdev",color=color.red,transp=0,linewidth=5)

plot(stdev(close,length),color=color.blue)

The values of the standard deviation of the built-in function are identical to those calculated independently.

AnyDozer
  • 2,236
  • 2
  • 5
  • 22
2

The advantage of ta-lib as opensource is that you can look at sources and see what exactly it does. The stdev function sources are here and it refers to TA_VAR() function, which sources are here.

According to my understanding TA-Lib's stddev is a rolling biased stddev of period optInTimePeriod that is multiplied by optInNbDev. So this function results in an array of stdevs made for each input array value considering subarray with length of optInTimePeriod.

There is no pine's stddev sources to compare, but it looks like you shall use length as optInTimePeriod and mult as optInNbDev (or leave it 1 and multiply values later). And expect the same results.

truf
  • 2,843
  • 26
  • 39
  • 1
    Turn out that the BB function in ta-lib is also biased, so it is pretty similar to the pine script counterpart, that being said, I made a few tests and the difference between the two in negligible, like 1-2 micro pips. – holographix Jan 28 '21 at 09:00