Created
June 7, 2022 20:48
-
-
Save Kazanskyi/3b4a7ab25a58a19a28fb9e4a307e9e7f to your computer and use it in GitHub Desktop.
10-Y bond historical daily data from yahoo.finance
This file contains 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
import time | |
import datetime | |
from datetime import date | |
from selenium import webdriver | |
from webdriver_manager.chrome import ChromeDriverManager | |
from selenium.webdriver.common.keys import Keys | |
import pandas as pd | |
import os | |
def download_tables(last_date = date.today(), historical_days = 1450): | |
''' | |
Downloads 10-Y bond historical daily data | |
''' | |
driver = webdriver.Chrome(ChromeDriverManager().install()) | |
driver.get('https://finance.yahoo.com/quote/%5ETNX/history?period1=1492732800&period2=1650326400&interval=1d&filter=history&frequency=1d&includeAdjustedClose=true') | |
#remove old file with data if it's available | |
filePath = 'C:\\Users\\oleg.kazanskyi\\Downloads\\^TNX.csv' | |
# Check whether the specified file is available and remove it | |
if os.path.exists(filePath): | |
os.remove(filePath) | |
print("We remove old csv file with metadata and replace with the newer one") | |
#Calculating current and historical periods | |
todays_year = last_date.year | |
todays_month = last_date.month | |
todays_day = last_date.day | |
historical_date = last_date-datetime.timedelta(days=historical_days) | |
historical_year = historical_date.year | |
historical_month = historical_date.month | |
historical_day = historical_date.day | |
#Searching an input field for dates | |
elem = driver.find_element_by_xpath('//*[@id="Col1-1-HistoricalDataTable-Proxy"]/section/div[1]/div[1]/div[1]/div/div/div/span') | |
elem.click() | |
time.sleep(3) | |
#Enter historical date | |
elem = driver.find_element_by_xpath('//*[@id="dropdown-menu"]/div/div[1]/input') | |
elem.send_keys(historical_year) | |
elem.send_keys(Keys.TAB) | |
elem.send_keys(historical_month) | |
elem.send_keys(historical_day) | |
time.sleep(3) | |
#Enter last date | |
elem = driver.find_element_by_xpath('//*[@id="dropdown-menu"]/div/div[2]/input') | |
elem.send_keys(todays_year) | |
elem.send_keys(Keys.TAB) | |
elem.send_keys(todays_month) | |
elem.send_keys(todays_day) | |
time.sleep(3) | |
#Press done after entering dates | |
elem = driver.find_element_by_xpath('//*[@id="dropdown-menu"]/div/div[3]/button[1]') | |
elem.click() | |
time.sleep(3) | |
#Press apply dates range | |
elem = driver.find_element_by_xpath('//*[@id="Col1-1-HistoricalDataTable-Proxy"]/section/div[1]/div[1]/button') | |
elem.click() | |
time.sleep(3) | |
#press download data | |
elem = driver.find_element_by_xpath('//*[@id="Col1-1-HistoricalDataTable-Proxy"]/section/div[1]/div[2]/span[2]/a/span') | |
elem.click() | |
time.sleep(3) | |
driver.close | |
data = pd.read_csv("C:\\Users\\oleg.kazanskyi\\Downloads\\^TNX.csv") | |
data.Date = pd.to_datetime(data.Date) | |
data.rename(columns = {"Date":"date", "Adj Close":"10Y_bonds"}, inplace = True) | |
data.set_index(["date"], inplace = True) | |
data = data["10Y_bonds"] | |
data.dropna(axis = 0, inplace = True) | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment