-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalphavantage_script.py
54 lines (46 loc) · 2.05 KB
/
alphavantage_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
extracting data using alpha vantage
@author: Mayank Rasu (http://rasuquant.com/wp/)
"""
# importing libraries
from alpha_vantage.timeseries import TimeSeries
import pandas as pd
import time
key_path = "D:\\Udemy\\Quantitative Investing Using Python\\1_Getting Data\\AlphaVantage\\key.txt"
# extracting data for a single ticker
ts = TimeSeries(key=open(key_path,'r').read(), output_format='pandas')
data = ts.get_daily(symbol='EURUSD', outputsize='full')[0]
data.columns = ["open","high","low","close","volume"]
data = data.iloc[::-1] # to get around the problem of alpha vantage providing data in reverse order
# extracting stock data (historical close price) for multiple stocks - getting around the 5 API call per minute limit
all_tickers = ["AAPL","MSFT","CSCO","AMZN","GOOG",
"FB","BA","MMM","XOM","NKE","INTC"]
close_prices = pd.DataFrame()
api_call_count = 1
ts = TimeSeries(key=open(key_path,'r').read(), output_format='pandas')
start_time = time.time()
for ticker in all_tickers:
data = ts.get_intraday(symbol=ticker,interval='1min', outputsize='compact')[0]
api_call_count+=1
data.columns = ["open","high","low","close","volume"]
data = data.iloc[::-1]
close_prices[ticker] = data["close"]
if api_call_count==5:
api_call_count = 1
time.sleep(60 - ((time.time() - start_time) % 60.0))
# extracting ohlcv data for multiple stocks - getting around the 5 API call per minute limit
all_tickers = ["AAPL","MSFT","CSCO","AMZN","GOOG",
"FB","BA","MMM","XOM","NKE","INTC"]
ohlv_dict = {}
api_call_count = 1
ts = TimeSeries(key=open(key_path,'r').read(), output_format='pandas')
start_time = time.time()
for ticker in all_tickers:
data = ts.get_intraday(symbol=ticker,interval='1min', outputsize='compact')[0]
api_call_count+=1
data.columns = ["open","high","low","close","volume"]
data = data.iloc[::-1]
ohlv_dict[ticker] = data
if api_call_count==5:
api_call_count = 1
time.sleep(60 - ((time.time() - start_time) % 60.0))