python - How to reverse engineer exponential moving average given the last known ema -


i've been trying reverse enginner series of ema given last known ema. should able done simple arithmetic manipulation of exponential moving average equation :

multiplier= (2 / (time periods + 1) ) ema = {close - ema(previous day)} x multiplier + ema(previous day) 

which gives me:

ema(previous day) = (ema(today) - close * multiplier)/(1 - multiplier) 

hence, following function defined follows:

def ema(df, last_ma ,period):      ema = [last_ma]     k = 2/(period+1)      in range(len(df)-1,0,-1):          prev_ema = (last_ma - df['close'][i] * k)/(1-k)         print(last_ma, df['close'][i], prev_ema)         last_ma = prev_ema          ema.append(prev_ema)      ema.reverse()  return ema 

the problem values appended list ema keeps getting larger , larger, went infinity. there nothing wrong data set, guessing wrong logic of code, can't figure out why. appreciated. thanks.

the link below data wish work on, last known ema number using -23628.2 , 200-period exponential moving average.

https://drive.google.com/open?id=0byc-aswzi5ofd1zwvegxnui5xzg

after doing research, guess due floating point precision error when performing such calculations on large array of data points.

the fundamental problem math unstable.

when going forward, each step multiplies previous ema multiplier between 0 , 1. means influence of given close dies off exponentially (thus name exponential moving average).

however, when going backwards, each step divides following ema multiplier. means influence of given data point increases exponentially, rather dying out.

if math perfect, , got numbers right, wouldn't matter. however, floating point errors also magnified exponentially, repeatedly amplified noise outgrow signal.

there's no simple way fix this. obvious thing try try going forwards instead of backwards, it's not clear if that's compatible goal.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -