In one of my previous tutorials, I explained you how to calculate Simple Moving Average. In this tutorial, I will explain how to calculate Exponential Moving Average.
Here also, I have used the same NIFTY data that I have used for calculating SMA in the previous tutorial.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
#calculating exponential moving average nifty_price=[22508.75, 22834.30, 22907.60, 23190.65, 23350.40, 23658.35, 23668.65, 23486.85, 23591.95, 23519.35, 23165.70, 23332.35, 23250.10, 22904.45, 22161.60, 22535.85, 22399.15, 22828.55, 23328.55, 23437.20, 23851.65, 24125.55, 24167.25, 24328.95, 24246.70, 24039.35, 24328.50, 24335.95, 24334.20, 24346.70, 24461.15, 24379.60] n=len(nifty_price) def sma(p): if len(nifty_price)>2*p: placeholder=[] i=0 while i<=p-1: placeholder.append(nifty_price[n-(2*p)+i]) if i==p-1: sma = sum(placeholder)/p return(sma) i=i+1 else: print("Length of the list is smaller than the period desired.") def ema(p): if len(nifty_price)>2*p: initial_ema=sma(p) placeholder=[] i=0 while i<=p-1: if i==0: smoothing_factor = 2/(p+1) ema = (nifty_price[n-p+i]*smoothing_factor)+(initial_ema*(1-smoothing_factor)) placeholder.append(ema) if i>0: smoothing_factor = 2/(p+1) ema = (nifty_price[n-p+i]*smoothing_factor)+(placeholder[-1]*(1-smoothing_factor)) placeholder.append(ema) if i==p-1: ema = placeholder[-1] print(ema) print(placeholder) i=i+1 else: print("Length of the list is smaller than the period desired.") ema(9) |
However, after calculations, I see that my data is not matching with the data of groww. I need to find out why.