How to download stock market data with Python.

How to download stock market data with Python.

Data is the key to developing investing tools in Python. Lucky for us a company called AlphaAdvantage provides a free API which we can use in your Python development. You can test your programs on the MSFT ticker without a key but if you want to test other tickers you’ll need a key.

import pandas as pd
import sys

api_key = 'demo'
ticker = sys.argv[1]   

api_url_daily = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=' + ticker + '&apikey=' + api_key + '&datatype=csv'
data = pd.read_csv(api_url_daily)


for i in range(0,len(data)):
    timestamp = data['timestamp'][i]
    close = float(data['close'][i])
    high = float(data['high'][i])
    low = float(data['low'][i])
    open = float(data['open'][i])
    volume = int(data['volume'][i])
    print(timestamp,close,high,low,open,volume)

Leave a Reply