2

To retrieve data from Alpha Vantage :

from alpha_vantage.timeseries 
import TimeSeries 
import matplotlib.pyplot as plt 
import sys

def stockchart(symbol):
    ts = TimeSeries(key='1ORS1XLM1YK1GK9Y', output_format='pandas')
    data, meta_data = ts.get_intraday(symbol=symbol, interval='1min', outputsize='full')
    print (data)
    data['close'].plot()
    plt.title('Stock chart')
    plt.show()

symbol=input("Enter symbol name:") stockchart(symbol)

My question is if there is a way to specify start and end dates for the data. On the website they have mentioned on the limits to number data points but they have not mentioned if start and end dates could be used in the code and still not exceed the number of data points.

  • 2
    Please be careful about setting API keys in public! An easy way to avoid this is to set ALPHAVANTAGE_API_KEY to an environment variable, check this out for more information: https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html – Patrick Collins Nov 29 '19 at 16:13

1 Answers1

0

One API call to Alpha Vantage counts as just one API call. There are no limits to the number of data points from an API call.

To change the date range in python, run something like the following:

data_date_changed = data[:'2019-11-29']

This will give you everything from 2019-11-29 to the present time. The full code being:

from alpha_vantage.timeseries import TimeSeries 
import matplotlib.pyplot as plt 
import sys

def stockchart(symbol):
    ts = TimeSeries(key='ABCDEFG', output_format='pandas')
    data, meta_data = ts.get_intraday(symbol=symbol, interval='1min', outputsize='full')
    data_date_changed = data[:'2019-11-29']
    data_date_changed['4. close'].plot()
    print(data_date_changed)
    plt.title('Stock chart')
    plt.show()

symbol=input("Enter symbol name:") 
stockchart(symbol)
Patrick Collins
  • 5,621
  • 3
  • 26
  • 64
  • Doesn't go past 100 data points. Try going back at-least 2 years! – Jeereddy Feb 16 '20 at 17:53
  • Correct, the intraday function doesn't go back to the tickers creation date. – Patrick Collins Feb 17 '20 at 01:14
  • `get_intraday` only returns the current day's data, whereas it seems this answer suggests you can go back further. even then though, the syntax for the slice seems incorrect since you'd end at the date, rather than from the date onwards. – TankorSmash Aug 08 '20 at 16:19
  • It looks like the API has been updated to not go as far back. When answered, you could go back multiple days. Perhaps suggest an edit? – Patrick Collins Aug 10 '20 at 13:00