Created
March 6, 2017 05:22
-
-
Save billmei/397467c717a3f4dd9d42a2d4cc3cab05 to your computer and use it in GitHub Desktop.
Send yourself an email if the Federal Reserve interest rate rises above a trigger value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Monitor the Federal Reserve Interest rates and email | |
an alert if the risk-free rate exceeds a certain percentage | |
""" | |
from bs4 import BeautifulSoup | |
import requests | |
import os | |
# This is a percent value | |
THRESHOLD = float(os.environ.get('INTEREST_RATE_THRESHOLD')) | |
def scrape_xml(url): | |
response = requests.get(url) | |
if response.status_code < 200 or response.status_code >= 400: | |
exit(1) | |
return response.text | |
def notify_email(term, rate=0.0): | |
MAKER_SECRET_KEY = os.environ.get('IFTTT_SECRET_KEY') | |
IFTTT_URL = 'https://maker.ifttt.com/trigger/{event}/with/key/{secret_key}?value1={threshold}&value2={term}&value3={rate}'.format( | |
event='fed_funds_rate_alert', | |
secret_key=MAKER_SECRET_KEY, | |
threshold=str(THRESHOLD), | |
term=term, | |
rate=str(rate)) | |
requests.get(IFTTT_URL) | |
def num_obs(num_observations): | |
return '&lastObs=' + str(num_observations) | |
def main(): | |
"""Look at the 3-month and 1-year Treasury Bill""" | |
FEDERAL_RESERVE_URL = 'http://www.federalreserve.gov/datadownload/Output.aspx' | |
SERIES = '?rel=H15&series=35c19fa888ceba3f5a29a2b21eff936f' | |
OPTIONS = '&from=&to=&filetype=sdmx&label=include&layout=seriescolumn' | |
result = scrape_xml(FEDERAL_RESERVE_URL + SERIES + num_obs(1) + OPTIONS) | |
soup = BeautifulSoup(result, 'lxml') | |
rates = [float(i['obs_value']) for i in soup.findAll('frb:obs')] | |
terms = [i['maturity'] for i in soup.findAll('kf:series')] | |
TERM_FULLTEXT = { | |
'M3' : '3-Month', | |
'Y1' : '1-Year', | |
} | |
for i, rate in enumerate(rates): | |
if rate > THRESHOLD: | |
notify_email(TERM_FULLTEXT[terms[i]], rate) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment