0

I've been working on the quandl API recently and I've been stuck on an issue for a while.

My question is how to create a method on the difference between One date and the date before for a stock index, Data seems to come out as an array as an example: [[u'2015-04-30', 17840.52]] for the Dow Jones Industrial Average. I'd like to also create a way to get the change from one day away from the latest one. Say getting Friday's stock and the change between that and the day before.

My code:

def fetchData(apikey, url):
    '''Returns JSON data of the Dow Jones Average.'''
    parameters = {'rows' : 1, 'auth_token' : apikey}
    req = requests.get(url, params=parameters)
    data = json.loads(req.content)
    parsedData = []
    stockData = {}
    for datum in data:
        if data['code'] == 'COMP':
            stockData['name'] = data['name']
            stockData['description'] = '''The NASDAQ Composite Index measures all 
            NASDAQ domestic and international based common type stocks listed on The NASDAQ Stock Market.'''
            stockData['data'] = data['data']
            stockData['code'] = data['code']
        else:
            stockData['name'] = data['name']
            stockData['description'] = data['description']
            stockData['data'] = data['data']
            stockData['code'] = data['code']
    parsedData.append(stockData)
    return parsedData

I've attempted to just tack on [1] on data to get just the current day but both the issue of getting the day before has kinda stumped me.

jrouquie
  • 4,315
  • 4
  • 27
  • 43
Marorin
  • 167
  • 3
  • 12
  • Something's weird in your code. Each time you loop, you overwrite the previous entries of your variable stockData. In the end you will only have the last record of "data" parsed into stockData. If you want to keep all records, you have to create a new stockData instance each time you loop. – Joe May 02 '15 at 13:52
  • Yes. That's the point. I want the last record. That's how it is so far. But the last record of "data" is an array. I want to make another method to get the change between one day and the day before. – Marorin May 02 '15 at 13:59
  • When you have a list, you can access the last record like this: last_record = record_list[-1] instead of looping. http://stackoverflow.com/questions/509211/explain-pythons-slice-notation – Joe May 02 '15 at 14:06
  • For the rest of the question, you'll have to show us the content of "data" to help understanding the problem. – Joe May 02 '15 at 14:08
  • I did indeed show you the content of data: : [[u'2015-04-30', 17840.52]] It's like that for the entirety of the JSON. – Marorin May 02 '15 at 16:13
  • You are accessing the data object with keys, implying that data is a map; however you are only showing an example array. Having the first 10 lines and the last 5 lines of the raw json would help us help you. As a tip, you will have to parse the date using the datetime.date object https://docs.python.org/2/library/datetime.html#date-objects . You can then compare them using datetime.timedelta objects http://www.pythonforbeginners.com/basics/python-datetime-timedelta/ – Joe May 02 '15 at 18:17

0 Answers0