I'm building some sort of Stock Market Web App using django framework. I'm getting the data from Alpha Vantage API and I got stuck when parsing the data I need.
1 - I can successfully call the API but I always get an error when trying to get the data I need see the code I'm using on views.py
:
def home(request):
import requests
import json
import pandas as pd
from alpha_vantage.timeseries import TimeSeries
url = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=B3SA3.SA&outputsize=compact&apikey=XXX"
api_request = requests.post("GET", url)
try:
api = api_request.content.json()
except Exception as e:
api="Erro, tente novamente"
return render(request,'home.html', {'api': api})
On home.html
I'm using this code to whether show the info or an error:
{% if api %}
{% if api == "Erro, tente novamente."%}
Houve um problema com a busca da ação, tente novamente.
{% else %}
{% for key,value in api.items %}
{{key}}: {{value}}<br/>
{%endfor%}
{% endif %}
{% endif %}
With this code, I get the following and as you can see there are two separate dictionaries Meta Data and Time Series (Daily):
{'Meta Data': {'1. Information': 'Daily Time Series with Splits and Dividend Events', '2. Symbol': 'B3SA3.SA', '3. Last Refreshed': '2020-07-10', '4. Output Size': 'Compact', '5. Time Zone': 'US/Eastern'}, 'Time Series (Daily)': {'2020-07-10': {'1. open': '58.8000', '2. high': '59.9800', '3. low': '57.6000', '4. close': '59.9500', '5. adjusted close': '59.9500', '6. volume': '7989500', '7. dividend amount': '0.0000', '8. split coefficient': '1.0000'}, '2020-07-09': {'1. open': '60.9700', '2. high': '60.9700', '3. low': '58.4400', '4. close': '58.8900', '5. adjusted close': '58.8900', '6. volume': '13494000', '7. dividend amount': '0.0000', '8. split coefficient': '1.0000'}, '2020-07-08': {'1. open': '57.6100', '2. high': '60.8900', '3. low': '57.2300', '4. close': '60.6500', '5. adjusted close': '60.6500', '6. volume': '13847100', '7. dividend amount': '0.0000', '8. split coefficient': '1.0000'}, '2020-07-07': {'1. open': '56.5500', '2. high': '57.6000', '3. low': '56.2500', '4. close': '57.1700', '5. adjusted close': '57.1700', '6. volume': '9038800', '7. dividend amount': '0.0000', '8. split coefficient': '1.0000'}
I'm only trying to get the 'Time Series (Daily)' and parse it to a dataframe but I always get errors when trying to call the 'Time Series (Daily)' dictionary.
Do you guys have any clue of what I might be doing wrong? Thanks in advance guys!