anandanand84 / technicalindicators

A javascript technical indicators written in typescript with pattern recognition right in the browser
MIT License
2.14k stars 557 forks source link

Wrong value using nextValue ? #194

Closed c0nf1ck closed 4 years ago

c0nf1ck commented 4 years ago

After using this lib for hours, i'm seeing some divergence in the values calculated using nextValue.

let bbands = indicators.BollingerBands(candlestick.close, 120, 2, symbol, close)
let bbResult = []

const BollingerBands = function (C, period, stdDev, symbol, next) {
    if (!bbResult[symbol]) {
        bbResult[symbol] = new bb({ values: C, period: period, stdDev: stdDev })

        let result = bbResult[symbol].getResult()

        return result[result.length - 1]
    } else return bbResult[symbol].nextValue(next)
}

Maybe i'm doing something wrong? For example

LTCBTC (Binance) - At: 2019-11-21T05:25:10-03:00
BB > Upper: 0.0068462318 Lower: 0.0067572348

Values from tradingview: Upper: 0.006700 Lower: 0.006660
XRPBTC (Binance) - At: 2019-11-21T05:44:05-03:00
BB > Upper: 0.000030946226 Lower: 0.000030701274

Values from tradingview: Upper: 0.00003074 Lower: 0.00003064
anandanand84 commented 4 years ago

Do you mean to say using getResult and nextValues have different results.

Take a look at the the test case here https://github.com/anandanand84/technicalindicators/blob/master/test/volatility/BollingerBands.js

c0nf1ck commented 4 years ago

Hey @anandanand84 thanks for the fast reply.

Yeah i know, i'm using getResult only when there's no value inside the array of that particular symbol. But after some time, the values are wrong as u can see.. I don't know why

anandanand84 commented 4 years ago

how do you compare the values between tradingview and this, because all you are getting is the last value of the get result in here. It may have multiple values based on the size of C and period. So you may be comparing the wrong values.

For example you are comparing fifth value of your result with first value of tradingview, because you are ignoring the first four values of the result.

c0nf1ck commented 4 years ago

how do you compare the values between tradingview and this, because all you are getting is the last value of the get result in here.

I'm using only the last value and comparing based on time, for example at 2019-11-21T05:25:10-03:00 i get this from the lib: Upper: 0.0068462318 Lower: 0.0067572348

on tradingview the value for the same moment is: Upper: 0.006700 Lower: 0.006660

But i think i found the error, im not saving the 'nextValue' back in the array. Changed my method to this:

let bbResult = [], bbResults = []

const BollingerBands = function (C, period, stdDev, symbol, next) {
    if (!bbResult[symbol]) {
        bbResult[symbol] = new bb({ values: C, period: period, stdDev: stdDev })

        bbResults = bbResult[symbol].getResult()
    } else bbResults.push(bbResult[symbol].nextValue(next))

    return bbResults[bbResults.length - 1]
}

I'll see if its correct after some time.

Thanks :)

c0nf1ck commented 4 years ago

Probably my error, as described above.

Thanks @anandanand84