0

Pycharm, Mac Catalina, Python 3.8 So I retrieved this json file with historical data stocks, i tried

dictionary = get_jsonparsed_data('AAPL').values()

#Because the method returns a dictionary with a key that is the name of the stock and then the values is a list of dictionaries that are separated by dates.

    dictionary = []
for key, value in get_jsonparsed_data('AAPL').items():
    dictionary.append(value)

The output is something exactly like this but with dates dating back ten years if i print it.

[{'date': '2020-12-22', 'open': 131.610001, 'high': 134.404999, 'low': 129.654999, 'close': 131.880005, 'adjClose': 131.880005, 'volume': 165746075.0, 'unadjustedVolume': 165746075.0, 'change': 0.27, 'changePercent': 0.205, 'vwap': 131.98, 'label': 'December 22, 20', 'changeOverTime': 0.00205}, {'date': '2020-12-21', 'open': 125.019997, 'high': 128.309998, 'low': 123.449997, 'close': 128.229996, 'adjClose': 128.229996, 'volume': 120093700.0, 'unadjustedVolume': 120093700.0, 'change': 3.21, 'changePercent': 2.568, 'vwap': 126.66333, 'label': 'December 21, 20', 'changeOverTime': 0.02568}, {'date': '2020-12-18', 'open': 128.960007, 'high': 129.100006, 'low': 126.120003, 'close': 126.660004, 'adjClose': 126.660004, 'volume': 192541500.0, 'unadjustedVolume': 192541500.0, 'change': -2.3, 'changePercent': -1.783, 'vwap': 127.29334, 'label': 'December 18, 20', 'changeOverTime': -0.01783}]

so then i tried

dates = [date['date'] for date in dictionary[1]]
print(dates)

prices = [price['open'] for price in dictionary[1]]
print(prices)

the output

Dates

['2020-12-22', '2020-12-21', '2020-12-18', '2020-12-17', '2020-12-16', '2020-12-15', '2020-12-14', '2020-12-11', '2020-12-10', '2020-12-09', '2020-12-08', '2020-12-07', '2020-12-04', '2020-12-03', '2020-12-02', '2020-12-01', '2020-11-30', '2020-11-27', '2020-11-25', '2020-11-24', '2020-11-23', '2020-11-20', '2020-11-19', '2020-11-18', '2020-11-17', '2020-11-16', '2020-11-13', '2020-11-12', '2020-11-11', '2020-11-10', '2020-11-09', '2020-11-06', '2020-11-05', '2020-11-04', '2020-11-03', '2020-11-02', '2020-10-30', '2020-10-29']

Price

[131.610001, 125.019997, 128.960007, 128.899994, 127.410004, 124.339996, 122.599998, 122.43, 120.5, 124.529999, 124.370003, 122.309998, 122.599998, 123.519997, 122.019997, 121.010002, 116.970001, 116.57, 115.550003, 113.910004, 117.18, 118.639999, 117.589996, 118.610001, 119.550003, 118.919998, 119.440002, 119.620003, 117.190002, 115.550003, 120.5, 118.32, 117.949997, 114.139999, 109.660004, 109.110001, 111.059998, 112.370003, 115.050003, 115.489998, 114.010002, 116.389999, 117.449997, 116.669998, 116.199997, 119.959999]

These are the ouputs but they go all the way back ten years it would be to long to put it on here, but I only want 30 days worth of both values each value is a date. I want only 30 days to plot them.

  • Please post a sample input with expected output. – Mayank Porwal Dec 23 '20 at 05:57
  • So you want first 30 entries for date? – Harsh Dec 23 '20 at 06:09
  • You could sort the dictionaries by date, then choose the first 30 days. Can you have the same date multiple times ? Do you just want to choose `CurrentDate - EarliestDate <= 30 days` ? – steveb Dec 23 '20 at 06:23
  • yes the first 30 entries for dates and price –  Dec 23 '20 at 06:23
  • The dictionary is already sorted by date, so im wondering how can i get the first 30 dates. –  Dec 23 '20 at 06:27
  • @steveb I forgot to @. –  Dec 23 '20 at 06:43
  • 1
    @EGC I think at least one of the answers already addresses your question. In the example of your question, the dictionaries are in reverse sorted order, I think you want them sorted earliest to latest. When they are sorted, can't you just grab the first thirty records indexing into the array `[:30]` ? One of my original questions was, can a date appear more than once ? If so then you will need to look for dates where `CurrentDate - EarliestDate <= 30 days` in the sorted list. – steveb Dec 23 '20 at 16:11

2 Answers2

0

Since these are now a list I just used the slice function

dates = [date['date'] for date in dictionary[1]]
date_of_stock = date[0:30]

i did the same for the price

  • How does this actually work, you are indexing into the first element by using `dictionary[1]` (which is one element of your list, not the list). Unless I am missing something, this is not correct. It also doesn't sort the original list in increasing order; your original list is sorted in decreasing order. – steveb Dec 23 '20 at 16:27
  • @steveb so the json file gives me a dictionary with a key of the companies symbol and the value of a list of dictionaries. Which is the proper information for different days. So I append the value into dictionary [] should've called it list_of_dictionaries, which is just a list of dictionaries at subscript one. Then grab all the dates from all the dictionaries in that list, but only actually use the first 30. The order I wanted was most recent to 30 days ago. Which luckily is already in that order. –  Dec 24 '20 at 11:13
0

You can create sort the dictionary if not sorted already

dictionary = [{'date': '2020-12-22', 'open': 131.610001, 'high': 134.404999, 'low': 129.654999, 'close': 131.880005, 'adjClose': 131.880005, 'volume': 165746075.0, 'unadjustedVolume': 165746075.0, 'change': 0.27, 'changePercent': 0.205, 'vwap': 131.98, 'label': 'December 22, 20', 'changeOverTime': 0.00205}, {'date': '2020-12-21', 'open': 125.019997, 'high': 128.309998, 'low': 123.449997, 'close': 128.229996, 'adjClose': 128.229996, 'volume': 120093700.0, 'unadjustedVolume': 120093700.0, 'change': 3.21, 'changePercent': 2.568, 'vwap': 126.66333, 'label': 'December 21, 20', 'changeOverTime': 0.02568}, {'date': '2020-12-18', 'open': 128.960007, 'high': 129.100006, 'low': 126.120003, 'close': 126.660004, 'adjClose': 126.660004, 'volume': 192541500.0, 'unadjustedVolume': 192541500.0, 'change': -2.3, 'changePercent': -1.783, 'vwap': 127.29334, 'label': 'December 18, 20', 'changeOverTime': -0.01783}]

dictionary_sorted = sorted(dictionary,key=lambda x:x['date'])

#### Once the inputs are sorted, you can slice the required number of indexes

dictionary_sorted = dictionary_sorted[0:30]

dates_price = [[value['date'],value['open']] for value in dictionary_sorted]

dates_price would the final list that will contain both dates and prices

Vaebhav
  • 4,672
  • 1
  • 13
  • 33