Calculte EMA Python

TheRenjithR
Hi Guys,

I am trying to calcualte CDMA for which I need EMA. I am trying below code but it gives me error.
'****************************************
Error'

closePrice = pd.DataFrame(adjustedClose, index=data.index)
AttributeError: 'dict' object has no attribute 'index'

'**************************************

import pdb
import datetime
import pandas as pd
from kiteconnect import KiteConnect
import numpy as np
import openpyxl
import xlsxwriter
import matplotlib.pyplot as plt

api_key = "***"
api_secret = "**"
# request_token = "**"
access_token = ""

kite = KiteConnect(api_key=api_key)
print("[*] Generate Access Token :", kite.login_url())
request_token = input("[*] Enter Your Request Token here: ")
data = kite.generate_session(request_token, api_secret=api_secret)
kite.set_access_token(data["access_token"])

trd_portfolio = {"HDFCBANK": {'token': 341249}}

token = 341249
to_date = datetime.datetime.now()
from_date = to_date - datetime.timedelta(days=100)
interval = "15minute"

records = kite.historical_data(token, from_date, to_date, interval)

df = pd.DataFrame(records)
# print(df['volume'])
adjustedClose = df['volume']

# df.to_csv(r'C:\Users\***\PycharmProjects\***\pandas.txt', header=True, index=None, sep='\t', mode='a')
# pdb.set_trace()
n = 20 #where n is the time period as described
smoothingConstant = 2/(n+1)
EMAprice = 0
EMAvalues = []
# declaring variables and finding mean
for price in adjustedClose:
if EMAprice == 0:
EMAprice = price
else:
EMAprice = (price - EMAprice) * smoothingConstant + EMAprice
EMAvalues.append(EMAprice)

closePrice = pd.DataFrame(adjustedClose, index=data.index)
closePrice['20dayEMA'] = EMAvalues
closePrice['adjustedPrice'] = adjustedClose

plotClosePrice = closePrice['adjustedPrice']
plot20dayEMA = closePrice['20dayEMA']
#visualise code
fig=plt.figure()
axis=fig.add_subplot(111,ylabel='Plot of google stocks exponential moving average')
plotClosePrice.plot(ax=axis, color='g', lw=2., legend=True)
plot20dayEMA.plot(ax=axis, color='b', lw=2., legend=True)
plt.show()
Sign In or Register to comment.