AlphaVantage API Stock Market Indices

心不动则不痛 提交于 2019-12-10 18:38:56

问题


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>

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/44742003/alphavantage-api-stock-market-indices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!