8
votes

I'm using python and its framework flask to build a frontEnd backEnd project. The project needs stock data. I used Yahoo's Api before it stopped working and now I'm using Alpha Vantage API. It's working pretty well but I'm having difficulties with stock market Indices like Nasdaq, Dow Jones.. with yahoo I was using their tickers(like symboles) (^IXIC, ^DJI...) but it doesn't seem to work with alpha vantage. Has anyone worked with alpha vantage?

example of url to get data for Microsoft:
https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&outputsize=full&apikey=CN3J

Python code:

@app.route('/pfa/medaf/IndAct', methods = ['POST'])
def donnee():
Action1 = request.form['code1']
Action2 = request.form['code2']
Indice = request.form['Ind']

url="https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol="
urlInd=url+Indice+"&apikey=CN3J"
urlAct1=url+Action1+"&apikey=CN3J"
urlAct2=url+Action2+"&apikey=CN3J"

respInd = urlopen(urlInd)
dataInd = json.loads(respInd.read().decode(respInd.info().get_param('charset') or 'utf-8'))

coursIndice=[]
listInd=[]
for elt in dataInd['Time Series (Daily)'].keys():
    listInd.append(elt)
listInd.sort(reverse=True)
for e in listInd:
    coursIndice.append(float(dataInd['Time Series (Daily)'][e]['4. close']))

lenIndice = len(coursIndice)

rentabIndice=[]
for j in range(lenIndice-1):
    rentabIndice.append(100*(coursIndice[j+1]/coursIndice[j] -1 ))

moyenneMarche=sum(rentabIndice)/len(rentabIndice)

HTML code:

<section class="cols pad_left1">
    <form action = "http://localhost:5000/pfa/medaf/IndAct" method = "post">
    Tickers:
    <input type = "text" name = "code1" placeholder="Ticker here"><br>
    <input type = "text" name = "code2" placeholder="Ticker here"><br><br>
    Indice:<br>
    <select name="Ind" size="1" >
    <option   value="^IXIC" > NASDAQ Composite    </option>
    <option   value="^FCHI" > CAC40    </option>
    <option   value="^DJI" > Dow Jones</option>
    </select><br><br>
    <input type = "submit" value = "submit" />
    </form>
</section>
3

3 Answers

11
votes

I have a python library for alphavantage (MIT licensed) https://github.com/RomelTorres/alpha_vantage you can have a look at it. I have shared some examples there on how to work with the library.

3
votes

I was able to get the data for the indices using the example URL in your question and my key with the following changes:

Use IXIC instead of ^IXIC. Use DJI instead of ^DJI. Use FCHI instead of FCHI.

e.g. https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=FCHI&outputsize=full&apikey=

Basically, just remove the carat(^) prefix from the symbol.

-1
votes

You can connect to the time series by importing it

import pandas as pd
from alpha_vantage.timeseries import TimeSeries
import time
ts = TimeSeries (key=api_key, output_format = "pandas")
daily_results = ts.get_daily_adjusted(symbol="MSFT")
print(daily_results)

Or for the other stuff like balance sheet ect

 base_url = 'https://www.alphavantage.co/query?'
 params = {'function': 'INCOME_STATEMENT',
     'symbol': stock_ticker,
     'apikey': keys}
 response_data_income = requests.get(base_url, params=params)

 data_income_annual_last_fiscalDateEnding = 
 response_data_income.json()['annualReports'][0]['fiscalDateEnding']