Skip to content

Instantly share code, notes, and snippets.

@mjbommar
Created October 29, 2010 11:24
Show Gist options
  • Save mjbommar/653361 to your computer and use it in GitHub Desktop.
Save mjbommar/653361 to your computer and use it in GitHub Desktop.
Update Permanent Open Market Operation data directly from NYFRB
import csv
import datetime
import dateutil.parser
import lxml, lxml.etree
import urllib2
# Setup date-related variables
today = datetime.date.today()
todayString = today.strftime('%m/%d/%Y')
rangeString = '20050825_{0}'.format(today.strftime('%Y%m%d'))
baseURL = 'http://www.newyorkfed.org/markets/pomo/xml/v3_0/pomoXML.cfm?SHOWMORE=TRUE'
dateQueryString = '&date1=08/25/2005&date2={0}'.format(todayString)
# Retrieve and store the POMO data from the NYFRB
xmlFileName = "data/pomo_{0}.xml".format(rangeString)
xmlOut = open(xmlFileName, 'w')
xmlOut.write(urllib2.urlopen(baseURL + dateQueryString).read())
xmlOut.close()
# Parse the XML data, store rows of the data, and then write to CSV
xmlRoot = lxml.etree.parse(xmlFileName).getroot()
xmlData = xmlRoot.getchildren()[1]
pomoData = []
for xmlGroup in xmlData:
operationDate = dateutil.parser.parse(xmlGroup.attrib['operationDate'])
operationDirection = xmlGroup.attrib['operationDirection']
operationType = xmlGroup.attrib['operationType']
releaseTime = xmlGroup.attrib['releaseTime']
closeTime = xmlGroup.attrib['closeTime']
if operationType != 'AGENCY':
maturityRangeStart = dateutil.parser.parse(xmlGroup.attrib['maturityRangeStart'])
maturityRangeEnd = dateutil.parser.parse(xmlGroup.attrib['maturityRangeEnd'])
else:
maturityRangeStart = None
maturityRangeEnd = None
numIssues = len(list(xmlGroup)) - 2
totalAccepted = float(list(xmlGroup)[-2].getchildren()[0].attrib['value'])
totalSubmitted = float(list(xmlGroup)[-1].getchildren()[0].attrib['value'])
pomoData.append((operationDate, operationDirection, operationType, releaseTime, closeTime, maturityRangeStart, maturityRangeEnd, numIssues, totalAccepted, totalSubmitted))
w = csv.writer(open("data/pomo_{0}.csv".format(rangeString), 'w'))
w.writerows(pomoData)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment