New info on why EMA is slightly off
So this has been very frustrating, something so simple just off by a hair.
I tried this using an EMA with period 2, and then kept incrementing up. The EMA kept being correct. I was looking at the last candles out of around 300. When I went from 75 to 100 the number was very slightly off. 0.01. As I scanned down it got more a more off of the number from trading view. (also verified with yahoo finance).
I put it back to 4 and went to the very beginning and sure enough the ema on the first available candle is off and it gets better as you get further from the first candles.
2019-08-08T00:00:00, close: 16.91, ema: nan 2019-08-09T00:00:00, close: 17.97, ema: nan 2019-08-12T00:00:00, close: 21.09, ema: nan 2019-08-13T00:00:00, close: 17.52, ema: 18.37, real: 18.68 2019-08-14T00:00:00, close: 22.1, ema: 19.86, real: 20.05 2019-08-15T00:00:00, close: 21.18, ema: 20.39, real: 20.50 2019-08-16T00:00:00, close: 18.47, ema: 19.62, real: 19.69 2019-08-19T00:00:00, close: 16.88, ema: 18.53, real: 18.56 2019-08-20T00:00:00, close: 17.5, ema: 18.12, real: 18.14 2019-08-21T00:00:00, close: 15.8, ema: 17.19, real: 17.20 2019-08-22T00:00:00, close: 16.68, ema: 16.99, real: 16.99 # From here on it's correct
I know if I suggest that it might be a bug the user/creator backtrader will come for my throat :)
So it's probably something with the data somehow. So what the hell is going on here? Driving me crazy.
EMA is a recursive indicator and depends on the initial value. Maybe
btuses different initial value compare to trading view.
run-out last edited by
@kfue Also check if tradingview is using leftedge or rightedge for displaying data. This could also cause a small error like you mention.
rightedge (default: True)
Use the right edge of the time boundaries to set the time.
If False and compressing to 5 seconds the time of a resampled bar for seconds between hh:mm:00 and hh:mm:04 will be hh:mm:00 (the starting boundary
@ab_trader What initial value would be different? Wouldn't the initial value be 8/8 no matter what?
@run-out I don't think I would call this a small error. If I use a 100 EMA like I'm trying to it's 15.1 on 12/31/2019 compared to 14.35. That's a massive difference and will cause massively different results. If these four values are used to calculate the ema
2019-08-08T00:00:00, close: 16.91, ema: nan 2019-08-09T00:00:00, close: 17.97, ema: nan 2019-08-12T00:00:00, close: 21.09, ema: nan 2019-08-13T00:00:00, close: 17.52, ema: 18.37
Shouldn't it be the same from any source as long as the alpha is the same?