I wrote a python script which uses Selenium to scrape the predictions for Fed rate movements from the CME FedWatch tool.
www.cmegroup.com/trading/interest-rates/countdown-to-fomc.html#resources The tool works by converting the price of a 30 day Fed Fund future into an implied probability of a given range of yields. The CME website embeds the output in a something called an 'iframe', which I had never heard of before, and the iframe then contains a dashboard powered by something called Quikstrike. It took me a while to figure out how to switch focus to the iframe, as you can't simply reference elements inside the iframe without first switching focus. The script below may not look too complicated, but believe me, it took a while to write.
Old Federal Reserve building Philadelphia, Source: https://commons.wikimedia.org/wiki/User:Beyond_My_Ken
In [ ]:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import pandas as pd
In [ ]:
driver = webdriver.Chrome(executable_path='C:/temp/chromedriver.exe')
driver.set_page_load_timeout(10)
driver.get("https://www.cmegroup.com/trading/interest-rates/countdown-to-fomc.html")
time.sleep(2)
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
driver.find_element_by_link_text('Probabilities').click()
time.sleep(2)
xpath = "//*[@id='MainContent_pnlContainer']/div[3]/div/div/table[3]/tbody"
table = driver.find_element_by_xpath(xpath)
tabletext = table.text
rows = table.find_elements(By.TAG_NAME, "tr") # get all of the rows in the table
i = 0
MeetingDates = []
DaysToMeeting = []
Easeprob = []
NoChangeprob = []
Hikeprob = []
for row in rows:
if i > 1:
# Get the columns (all the column 2)
col = row.find_elements(By.TAG_NAME, "td")
MeetingDates.append(col[0].text)
DaysToMeeting.append(col[1].text)
Easeprob.append(col[2].text)
NoChangeprob.append(col[3].text)
Hikeprob.append(col[4].text)
i = i +1
CMEdf = pd.DataFrame(
{'MeetingDate': MeetingDates,
'DaysToMeeting': DaysToMeeting,
'Easeprob': Easeprob,
'NoChangeprob': NoChangeprob,
'Hikeprob': Hikeprob,
})
CMEdf.to_csv(path + "\CMEFedWatch.csv")
driver.close()
|
AuthorI work as an actuary and underwriter at a global reinsurer in London. Categories
All
Archives
April 2024
|
Thank you for this snippet. I was having a hard time understanding how to do it. You have my full gratitude
Leave a Reply.