anandanand84 / technicalindicators

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

sma\ema wrong result ? #158

Closed alexcrownjr closed 5 years ago

alexcrownjr commented 5 years ago

I tried to find days where SMA100 crossed up or above SMA200

` const ccxt = require('ccxt') const SMA = require('technicalindicators').SMA const timeStamp = require('unix-timestamp')

const binance = new ccxt.binance ({ 'enableRateLimit': true })

;(async ()=> { const ohlcv = await binance.fetchOHLCV ('RVN/BTC', '4h') //const series = ohlcv.slice (-200).map (x => x[4]) const sma200 = new SMA({period : 200, values : []}) const sma100 = new SMA({period : 100, values : []})

ohlcv.slice (-200).forEach(data => { var result200 = sma200.nextValue(data[4]); var result100 = sma200.nextValue(data[4]) if (parseFloat(result100) > parseFloat(result200)) //results.push(result) console.log(timeStamp.toDate(data[0]/1000)+" sma100="+result100+" sma200="+result200) }) //console.log(results); })() `

` console.log date sma100 sma200

Sun Dec 23 2018 12:00:00 GMT+0800 (PST) 0.000003877900000000002 0.000003877050000000002 Sun Dec 23 2018 16:00:00 GMT+0800 (PST) 0.000003880500000000004 0.0000038792000000000034 Sun Dec 23 2018 20:00:00 GMT+0800 (PST) 0.000003882500000000004 0.000003881500000000003 Mon Dec 24 2018 00:00:00 GMT+0800 (PST) 0.000003885100000000004 0.000003883800000000003 Mon Dec 24 2018 04:00:00 GMT+0800 (PST) 0.000003888800000000003 0.0000038869500000000035 Mon Dec 24 2018 08:00:00 GMT+0800 (PST) 0.0000038931000000000034 0.000003890950000000003 Mon Dec 24 2018 12:00:00 GMT+0800 (PST) 0.000003897100000000004 0.000003895100000000004 Mon Dec 24 2018 16:00:00 GMT+0800 (PST) 0.000003898200000000004 0.000003897650000000003 Mon Dec 24 2018 20:00:00 GMT+0800 (PST) 0.000003899200000000003 0.000003898700000000003 Tue Dec 25 2018 04:00:00 GMT+0800 (PST) 0.000003899600000000004 0.000003899200000000003 `

If you compare this with TradingView - it's totally wrong

anandanand84 commented 5 years ago

SMA and EMA are basic for many other indicators, we have already tested indicators like macd that is based of sma with tradingview and it is working fine. (see this https://github.com/anandanand84/technicalindicators/issues/44 for example) You may have to compare the input data you give between tradingview and your code as well.

instabot42 commented 5 years ago

This line in your example is wrong...

var result100 = sma200.nextValue(data[4])

You're always adding to the sma200 object.