Skip to content

Instantly share code, notes, and snippets.

@jordan-chalupka
Created October 4, 2019 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordan-chalupka/b0bd2268b7dfeba7656a450e58652365 to your computer and use it in GitHub Desktop.
Save jordan-chalupka/b0bd2268b7dfeba7656a450e58652365 to your computer and use it in GitHub Desktop.
lambda function to get stocks from AlphaVantage and push the data to DataDog
import os
from datadog import datadog_lambda_wrapper, lambda_metric
from botocore.vendored import requests
def get_stock_quote(symbol):
API_KEY = os.environs['API_KEY']
response = requests.get(f'https://www.alphavantage.co/query?apikey={API_KEY}&function=GLOBAL_QUOTE&symbol={symbol}')
return response.json()['Global Quote']
def track_metric(symbol, title, value):
lambda_metric(f'{symbol}.stock.{title}', value)
def send_metrics(symbol, quote):
track_metric(symbol, 'open', quote['02. open'])
track_metric(symbol, 'high', quote['03. high'])
track_metric(symbol, 'low', quote['04. low'])
track_metric(symbol, 'price', quote['05. price'])
track_metric(symbol, 'volume', quote['06. volume'])
track_metric(symbol, 'previous_close', quote['08. previous close'])
track_metric(symbol, 'change', quote['09. change'])
track_metric(symbol, 'change_percent', quote['10. change percent'][:-1])
@datadog_lambda_wrapper
def lambda_handler(event, context):
for symbol in event['symbols']:
quote = get_stock_quote(symbol)
send_metrics(symbol, quote)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment