-
Notifications
You must be signed in to change notification settings - Fork 2
Retry and Cache Explained
The benefit of enabling caching, it will reduce the number of calls against your free account. At present your free account only allows 5 calls per minute.
The AlphavantageClient
uses an In-Memory Caching mechanism where your parameters (i.e. symbol, function, interval, format, etc) are used as a key into a dictionary. This is similar to how SQL uses the SQL statement to cache it's responses. If you have already requested data with those attributes then it will simply return it from the cache. You can enable this by calling client.use_simple_cache()
. The cache will continue to grow until you call the client.clear_cache()
(See full example below)
At present the Alpha Vantage API only allows 5 calls per min with a free account. This means you would need to build retry logic to maximize your calls. We have already done that for you! As a result, the client will calculate the time from the last call and pause up to a minute before making a subsequent call. You can enable this by calling client.should_retry_once()
.
Note the following two lines from the below response:
- cached response for PXD quote -
INFO:root:Found item in cache: {'success': True, 'limit_reached': False, 'status_code': 200, 'Global Quote': {'01. symbol': 'PXD', '02. open': '199.3500', '03. high': '202.9600', '04. low': '199.0100', '05. price': '201.3800', '06. volume': '1331532', '07. latest trading day': '2023-06-26', '08. previous close': '198.6600', '09. change': '2.7200', '10. change percent': '1.3692%'}, 'symbol': 'PXD'}
- client hit the free limit and needed to pause -
INFO:root:sleeping for 59.31067372301186 seconds
You will not have to worry as much about your free account limits within a single threaded context. If your situation requires multi-threading or multi-processing then create a queue of all your calls needed.
from alphavantage_api_client import AlphavantageClient, GlobalQuote
import logging
def sample_retry_when_limit_reached():
logging.basicConfig(level=logging.INFO)
client = AlphavantageClient().use_simple_cache().should_retry_once()
symbols = ["TSLA", "F", "C", "WFC", "ZIM", "PXD", "PXD", "POOL", "INTC", "INTU", "AAPL"] # more than 5 calls so should fail
for symbol in symbols:
event = {
"symbol": symbol
}
global_quote = client.get_global_quote(event)
if not global_quote.success:
raise ValueError(f"{global_quote.error_message}")
if global_quote.limit_reached:
raise ValueError(f"{global_quote.error_message}")
print(f"symbol: {global_quote.symbol}, Price: {global_quote.get_price()}, success {global_quote.success}")
client.clear_cache() # when you are done making calls, clear cache
if __name__ == "__main__":
sample_retry_when_limit_reached()
produces the following output:
INFO:root:{"method": "__init__", "action": "/home/xrgarcia/.alphavantage config file found"}
INFO:root:Found item in cache: None
symbol: TSLA, Price: 241.0500, success True
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "response_from_alphavantage", "status_code": 200, "data": "{\n \"Global Quote\": {\n \"01. symbol\": \"TSLA\",\n \"02. open\": \"250.0650\",\n \"03. high\": \"258.3700\",\n \"04. low\": \"240.7000\",\n \"05. price\": \"241.0500\",\n \"06. volume\": \"179990552\",\n \"07. latest trading day\": \"2023-06-26\",\n \"08. previous close\": \"256.6000\",\n \"09. change\": \"-15.5500\",\n \"10. change percent\": \"-6.0600%\"\n }\n}", "event": {"symbol": "TSLA", "function": "GLOBAL_QUOTE"}}
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "return_value", "data": {"success": true, "limit_reached": false, "status_code": 200, "Global Quote": {"01. symbol": "TSLA", "02. open": "250.0650", "03. high": "258.3700", "04. low": "240.7000", "05. price": "241.0500", "06. volume": "179990552", "07. latest trading day": "2023-06-26", "08. previous close": "256.6000", "09. change": "-15.5500", "10. change percent": "-6.0600%"}, "symbol": "TSLA"}, "event": {"symbol": "TSLA", "function": "GLOBAL_QUOTE"}}
INFO:root:Found item in cache: None
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "response_from_alphavantage", "status_code": 200, "data": "{\n \"Global Quote\": {\n \"01. symbol\": \"F\",\n \"02. open\": \"14.0100\",\n \"03. high\": \"14.2300\",\n \"04. low\": \"13.9900\",\n \"05. price\": \"14.1100\",\n \"06. volume\": \"44956274\",\n \"07. latest trading day\": \"2023-06-26\",\n \"08. previous close\": \"14.0200\",\n \"09. change\": \"0.0900\",\n \"10. change percent\": \"0.6419%\"\n }\n}", "event": {"symbol": "F", "function": "GLOBAL_QUOTE"}}
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "return_value", "data": {"success": true, "limit_reached": false, "status_code": 200, "Global Quote": {"01. symbol": "F", "02. open": "14.0100", "03. high": "14.2300", "04. low": "13.9900", "05. price": "14.1100", "06. volume": "44956274", "07. latest trading day": "2023-06-26", "08. previous close": "14.0200", "09. change": "0.0900", "10. change percent": "0.6419%"}, "symbol": "F"}, "event": {"symbol": "F", "function": "GLOBAL_QUOTE"}}
INFO:root:Found item in cache: None
symbol: F, Price: 14.1100, success True
symbol: C, Price: 46.2400, success True
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "response_from_alphavantage", "status_code": 200, "data": "{\n \"Global Quote\": {\n \"01. symbol\": \"C\",\n \"02. open\": \"46.1200\",\n \"03. high\": \"46.6900\",\n \"04. low\": \"46.0100\",\n \"05. price\": \"46.2400\",\n \"06. volume\": \"13253938\",\n \"07. latest trading day\": \"2023-06-26\",\n \"08. previous close\": \"46.0200\",\n \"09. change\": \"0.2200\",\n \"10. change percent\": \"0.4781%\"\n }\n}", "event": {"symbol": "C", "function": "GLOBAL_QUOTE"}}
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "return_value", "data": {"success": true, "limit_reached": false, "status_code": 200, "Global Quote": {"01. symbol": "C", "02. open": "46.1200", "03. high": "46.6900", "04. low": "46.0100", "05. price": "46.2400", "06. volume": "13253938", "07. latest trading day": "2023-06-26", "08. previous close": "46.0200", "09. change": "0.2200", "10. change percent": "0.4781%"}, "symbol": "C"}, "event": {"symbol": "C", "function": "GLOBAL_QUOTE"}}
INFO:root:Found item in cache: None
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "response_from_alphavantage", "status_code": 200, "data": "{\n \"Global Quote\": {\n \"01. symbol\": \"WFC\",\n \"02. open\": \"40.7800\",\n \"03. high\": \"41.2200\",\n \"04. low\": \"40.4550\",\n \"05. price\": \"40.5900\",\n \"06. volume\": \"13618534\",\n \"07. latest trading day\": \"2023-06-26\",\n \"08. previous close\": \"40.6100\",\n \"09. change\": \"-0.0200\",\n \"10. change percent\": \"-0.0492%\"\n }\n}", "event": {"symbol": "WFC", "function": "GLOBAL_QUOTE"}}
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "return_value", "data": {"success": true, "limit_reached": false, "status_code": 200, "Global Quote": {"01. symbol": "WFC", "02. open": "40.7800", "03. high": "41.2200", "04. low": "40.4550", "05. price": "40.5900", "06. volume": "13618534", "07. latest trading day": "2023-06-26", "08. previous close": "40.6100", "09. change": "-0.0200", "10. change percent": "-0.0492%"}, "symbol": "WFC"}, "event": {"symbol": "WFC", "function": "GLOBAL_QUOTE"}}
INFO:root:Found item in cache: None
symbol: WFC, Price: 40.5900, success True
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "response_from_alphavantage", "status_code": 200, "data": "{\n \"Global Quote\": {\n \"01. symbol\": \"ZIM\",\n \"02. open\": \"12.1000\",\n \"03. high\": \"12.2375\",\n \"04. low\": \"11.9450\",\n \"05. price\": \"12.0900\",\n \"06. volume\": \"2206381\",\n \"07. latest trading day\": \"2023-06-26\",\n \"08. previous close\": \"12.1800\",\n \"09. change\": \"-0.0900\",\n \"10. change percent\": \"-0.7389%\"\n }\n}", "event": {"symbol": "ZIM", "function": "GLOBAL_QUOTE"}}
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "return_value", "data": {"success": true, "limit_reached": false, "status_code": 200, "Global Quote": {"01. symbol": "ZIM", "02. open": "12.1000", "03. high": "12.2375", "04. low": "11.9450", "05. price": "12.0900", "06. volume": "2206381", "07. latest trading day": "2023-06-26", "08. previous close": "12.1800", "09. change": "-0.0900", "10. change percent": "-0.7389%"}, "symbol": "ZIM"}, "event": {"symbol": "ZIM", "function": "GLOBAL_QUOTE"}}
INFO:root:Found item in cache: None
symbol: ZIM, Price: 12.0900, success True
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "response_from_alphavantage", "status_code": 200, "data": "{\n \"Note\": \"Thank you for using Alpha Vantage! Our standard API call frequency is 5 calls per minute and 500 calls per day. Please visit https://www.alphavantage.co/premium/ if you would like to target a higher API call frequency.\"\n}", "event": {"symbol": "PXD", "function": "GLOBAL_QUOTE"}}
INFO:root:sleeping for 59.31067372301186 seconds
INFO:root:Found item in cache: None
symbol: PXD, Price: 201.3800, success True
symbol: PXD, Price: 201.3800, success True
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "response_from_alphavantage", "status_code": 200, "data": "{\n \"Global Quote\": {\n \"01. symbol\": \"PXD\",\n \"02. open\": \"199.3500\",\n \"03. high\": \"202.9600\",\n \"04. low\": \"199.0100\",\n \"05. price\": \"201.3800\",\n \"06. volume\": \"1331532\",\n \"07. latest trading day\": \"2023-06-26\",\n \"08. previous close\": \"198.6600\",\n \"09. change\": \"2.7200\",\n \"10. change percent\": \"1.3692%\"\n }\n}", "event": {"symbol": "PXD", "function": "GLOBAL_QUOTE"}}
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "return_value", "data": {"success": true, "limit_reached": false, "status_code": 200, "Global Quote": {"01. symbol": "PXD", "02. open": "199.3500", "03. high": "202.9600", "04. low": "199.0100", "05. price": "201.3800", "06. volume": "1331532", "07. latest trading day": "2023-06-26", "08. previous close": "198.6600", "09. change": "2.7200", "10. change percent": "1.3692%"}, "symbol": "PXD"}, "event": {"symbol": "PXD", "function": "GLOBAL_QUOTE"}}
INFO:root:Found item in cache: {'success': True, 'limit_reached': False, 'status_code': 200, 'Global Quote': {'01. symbol': 'PXD', '02. open': '199.3500', '03. high': '202.9600', '04. low': '199.0100', '05. price': '201.3800', '06. volume': '1331532', '07. latest trading day': '2023-06-26', '08. previous close': '198.6600', '09. change': '2.7200', '10. change percent': '1.3692%'}, 'symbol': 'PXD'}
INFO:root:Found item in cache: None
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "response_from_alphavantage", "status_code": 200, "data": "{\n \"Global Quote\": {\n \"01. symbol\": \"POOL\",\n \"02. open\": \"350.2500\",\n \"03. high\": \"356.7900\",\n \"04. low\": \"349.0100\",\n \"05. price\": \"354.1300\",\n \"06. volume\": \"292565\",\n \"07. latest trading day\": \"2023-06-26\",\n \"08. previous close\": \"352.3400\",\n \"09. change\": \"1.7900\",\n \"10. change percent\": \"0.5080%\"\n }\n}", "event": {"symbol": "POOL", "function": "GLOBAL_QUOTE"}}
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "return_value", "data": {"success": true, "limit_reached": false, "status_code": 200, "Global Quote": {"01. symbol": "POOL", "02. open": "350.2500", "03. high": "356.7900", "04. low": "349.0100", "05. price": "354.1300", "06. volume": "292565", "07. latest trading day": "2023-06-26", "08. previous close": "352.3400", "09. change": "1.7900", "10. change percent": "0.5080%"}, "symbol": "POOL"}, "event": {"symbol": "POOL", "function": "GLOBAL_QUOTE"}}
INFO:root:Found item in cache: None
symbol: POOL, Price: 354.1300, success True
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "response_from_alphavantage", "status_code": 200, "data": "{\n \"Global Quote\": {\n \"01. symbol\": \"INTC\",\n \"02. open\": \"33.1900\",\n \"03. high\": \"33.9900\",\n \"04. low\": \"33.0950\",\n \"05. price\": \"33.3400\",\n \"06. volume\": \"38952369\",\n \"07. latest trading day\": \"2023-06-26\",\n \"08. previous close\": \"33.0000\",\n \"09. change\": \"0.3400\",\n \"10. change percent\": \"1.0303%\"\n }\n}", "event": {"symbol": "INTC", "function": "GLOBAL_QUOTE"}}
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "return_value", "data": {"success": true, "limit_reached": false, "status_code": 200, "Global Quote": {"01. symbol": "INTC", "02. open": "33.1900", "03. high": "33.9900", "04. low": "33.0950", "05. price": "33.3400", "06. volume": "38952369", "07. latest trading day": "2023-06-26", "08. previous close": "33.0000", "09. change": "0.3400", "10. change percent": "1.0303%"}, "symbol": "INTC"}, "event": {"symbol": "INTC", "function": "GLOBAL_QUOTE"}}
INFO:root:Found item in cache: None
symbol: INTC, Price: 33.3400, success True
symbol: INTU, Price: 453.3000, success True
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "response_from_alphavantage", "status_code": 200, "data": "{\n \"Global Quote\": {\n \"01. symbol\": \"INTU\",\n \"02. open\": \"451.6600\",\n \"03. high\": \"462.4200\",\n \"04. low\": \"451.6600\",\n \"05. price\": \"453.3000\",\n \"06. volume\": \"1418201\",\n \"07. latest trading day\": \"2023-06-26\",\n \"08. previous close\": \"452.6900\",\n \"09. change\": \"0.6100\",\n \"10. change percent\": \"0.1348%\"\n }\n}", "event": {"symbol": "INTU", "function": "GLOBAL_QUOTE"}}
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "return_value", "data": {"success": true, "limit_reached": false, "status_code": 200, "Global Quote": {"01. symbol": "INTU", "02. open": "451.6600", "03. high": "462.4200", "04. low": "451.6600", "05. price": "453.3000", "06. volume": "1418201", "07. latest trading day": "2023-06-26", "08. previous close": "452.6900", "09. change": "0.6100", "10. change percent": "0.1348%"}, "symbol": "INTU"}, "event": {"symbol": "INTU", "function": "GLOBAL_QUOTE"}}
INFO:root:Found item in cache: None
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "response_from_alphavantage", "status_code": 200, "data": "{\n \"Global Quote\": {\n \"01. symbol\": \"AAPL\",\n \"02. open\": \"186.8300\",\n \"03. high\": \"188.0500\",\n \"04. low\": \"185.2300\",\n \"05. price\": \"185.2700\",\n \"06. volume\": \"48088661\",\n \"07. latest trading day\": \"2023-06-26\",\n \"08. previous close\": \"186.6800\",\n \"09. change\": \"-1.4100\",\n \"10. change percent\": \"-0.7553%\"\n }\n}", "event": {"symbol": "AAPL", "function": "GLOBAL_QUOTE"}}
INFO:root:{"method": "get_data_from_alpha_vantage", "action": "return_value", "data": {"success": true, "limit_reached": false, "status_code": 200, "Global Quote": {"01. symbol": "AAPL", "02. open": "186.8300", "03. high": "188.0500", "04. low": "185.2300", "05. price": "185.2700", "06. volume": "48088661", "07. latest trading day": "2023-06-26", "08. previous close": "186.6800", "09. change": "-1.4100", "10. change percent": "-0.7553%"}, "symbol": "AAPL"}, "event": {"symbol": "AAPL", "function": "GLOBAL_QUOTE"}}
symbol: AAPL, Price: 185.2700, success True
Process finished with exit code 0
Simple python wrapper around alpha vantage api, provide consistency across end points, debug easily and get the most out of your free account!