Aug-03-2019, 03:59 PM
Hello Everyone!
I need your help with the following problem. I have a list of dictionaries and try to access the value of each dictionary in the list that are then passed to a function, e.i. ExpMovingAverage, which in turn is supposed to return the calculated value (based on the function formula) and will associate that returned value with its corresponding key. For example, for AAPL would get its current and open prices and stores them in val, thus if ExpMovingAverage(val, 3)= 1.245.. then result should be{'AAPL',1.245..} and do the same for other dictionaries in my list.
This is the error message i get:
I need your help with the following problem. I have a list of dictionaries and try to access the value of each dictionary in the list that are then passed to a function, e.i. ExpMovingAverage, which in turn is supposed to return the calculated value (based on the function formula) and will associate that returned value with its corresponding key. For example, for AAPL would get its current and open prices and stores them in val, thus if ExpMovingAverage(val, 3)= 1.245.. then result should be{'AAPL',1.245..} and do the same for other dictionaries in my list.
This is the error message i get:
Error:Traceback (most recent call last):
File "C:/Users/.../Desktop/multiEMAs.py", line 35, in <module>
ema_val = ExpMovingAverage(val, 3)
NameError: name 'val' is not defined
This is my scrip: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 |
stocks = [ 'AAPL' , 'MSFT' , 'TSLA' ] def stockdata(): yahoo_financials = YahooFinancials(stocks) price = yahoo_financials.get_current_price() Open = yahoo_financials.get_open_price() return price, Open get_curprices = lambda :stockdata()[ 0 ] #getting dataset of current prices into a dictionary dict_curprices = {} dict_curprices.update(get_curprices()) get_openprices = lambda :stockdata()[ 1 ] #getting dataset of open prices into a dictionary dict_openprices = {} dict_openprices.update(get_openprices()) def ExpMovingAverage(values, window): dataList = [] #attempting to access only the value part of each above dictionary, e.g. get_curprices, get_openprices,... dataList = [get_curprices, get_openprices] for dic in dataList: for val in dic.values(): weights = np.exp(np.linspace( - 1. , 0. , window)) weights / = weights. sum () a = np.convolve(values, weights, mode = 'full' )[: len (values)] a[:window] = a[window] return a if __name__ = = "__main__" : ema_val = ExpMovingAverage(val, 3 ) print (ema_val, dic.key()) |