如何解决解析雅虎金融期权链 R
下午好,
我正在尝试在 R 中手动解析来自 Yahoo Finance 的期权链。我知道 quantmod()
方法,但使用此包存在不足。我找到了一种使用 rvest
解析 HTML 的方法,但是我在格式化解析时遇到问题。
在最后几行代码中,我注意到每 11 行就有一个期权合约的所有信息。但是我不知道如何将这些数据放入表格中。我读过 html_table
但不知道如何使用它。我的管道方法给了我一个错误(我认为是因为没有 .calls
节点)但我想知道如何有效地管理我的 HTML 解析给出的信息。如果我无法搜索 Call 和 Puts,至少我知道每 11 行我有完整的合约详细信息。
任何帮助或协助将不胜感激。谢谢!
library(quantmod)
library(forecast)
library(IntroCompFinR)
library(rvest)
library(textreadr)
library(magrittr)
library(dplyr)
library(reshape2)
library(plotly)
library(akima)
###############################################################################################
#Define Black-Scholes Formula:
Black_Scholes <- function(S,X,sigma,r,q,ttm,type){
#S = stock price
#X = strike price
#sigma = volatility
#r = risk free interest rate
#q = dividend yield
#ttm = time to maturity in days
#type = option type
b <- r - q
t <- ttm/365.25
d1 <- (log(S / X) + (b + sigma ^ 2 / 2) * t) / (sigma * sqrt(t))
d2 <- d1 - sigma * sqrt(t)
if(type == "call"){
price <- S * exp((b - r) * t) * pnorm(d1) - X * exp(-r * t) * pnorm(d2)
}else if (type == "put"){
price <- (X * exp(-r * t) * pnorm(-d2) - S * exp((b - r) * t) * pnorm(-d1))
}
return(price)
}
###############################################################################################
#Inverse pricing model used to obtain Implied Volatility (IV) has no closed-form solution.
#Need to use an optimizer to calculate IV.
#Implement an objective function then use the optimize function to get IV.
Volatility_Optimizer <- function(sigma,price,S,K,type){
abs(price - Black_Scholes(S,type))
}
#optimize(Volatility_Optimizer,interval = c(0,2),price = price,S = S,K = K,r = r,q = q,ttm = ttm,type = type)
################################################################################################
#Input an Example Option Contract: Apple Inc. (NYSE:APPL)
price = 3.55 #Price of Option Contract
S = 119.02 #Current Price of Underlying Asset
K = 125 #Strike Price indicated by Option Contract
r = 0.0011 #Risk-free rate,Treasury 3M Bill Rate,1.1% on October 16,2020
q = 0 #Dividend Yield
ttm = 28 #Time to maturity (in days)
type = "call" #Type of Option
optimize(Volatility_Optimizer,type = type)
#$minimum
#[1] 0.4510778
#Implied Volatility = 45.11% vs. 45.12% listed on Yahoo Finance
#$objective
#[1] 4.005748e-05
##################################################################################################
#Underlying Asset info and last Trade date
symbol <- "AAPL"
priceInfo <- getQuote(symbol)
lastPrice <- priceInfo$Last
date <- as.Date(priceInfo$`Trade Time`)
#Apply moneyness boundaries and time to maturity (ttm)
moneynessBoundaries <- c(0.85,1.15) #or: (0.95,1.05) etc
ttmBoundaries <- c(7,183) #one week to 26 weeks (6M)
##########################################################
#Scrape Yahoo Finance site
baseUrl <- paste0("https://finance.yahoo.com/quote/",symbol,"/options")
baseHTML <- read_html(baseUrl)
baseHTML[69:85] #All expiration dates from HTML parsing
#[1] "march 12,2021" "march 19,2021" "march 26,2021" "April 1,2021"
#[5] "April 9,2021" "April 16,2021" "April 23,2021" "May 21,2021"
#[9] "June 18,2021" "July 16,2021" "September 17,2021" "October 15,2021"
#[13] "January 21,2022" "June 17,2022" "September 16,2022" "January 20,2023"
#[17] "march 17,2023"
#Pull Expiries from Options and Convert to TTM (time to maturity,ttm)
expiriesUNIX <- baseHTML[69:85]
expiries <- as.Date(expiriesUNIX,format = "%b %d,%Y")
timetoMats <- as.numeric(expiries - date)
#[1] 7 14 21 27 35 42 49 77 105 133 196 224 322 469 560 686 742
#Above we have the number of days until out Option contracts Expire
##############################################################################
#expiriesUNIX <- baseHTML %>% html_nodes("option") %>% html_attr("value")
#expiries <- as.Date((baseHTML %>% html_nodes("option") %>% html_text()),%Y")
#timetoMats <- as.numeric(expiries - date)
#############################################################################
#Select applicable expiries for our range of TTM Boundaries: 7 days to 180 days (6M)
sel <- timetoMats >= ttmBoundaries[1] & timetoMats <= ttmBoundaries[2]
expiriesUNIX <- expiriesUNIX[sel]
expiries <- expiries[sel]
timetoMats <- timetoMats[sel]
#URL codes for specific expirations dates
expiriesUNIX_code <- c(1615507200,1616112000,1616716800,1617235200,1617926400,1618531200,1619136000,1621555200,1623974400,1626393600)
#Loop over the Expiries to get Calls and Puts
calls <- NULL
puts <- NULL
#####################################################################
#Example Parsing of Option Contracts for a Given Expiration
expiryUrl <- paste0(baseUrl,"?date=",expiriesUNIX_code[10])
# https://finance.yahoo.com/quote/AAPL/options?date=1626393600
expiryHTML <- read_html(expiryUrl)
#Every 11 lines we see the information for one Call contract
#[105] "AAPL210716C00022500"
#[106] "2021-02-23 3:51PM EST"
#[107] "22.50"
#[108] "103.50"
#[109] "98.50"
#[110] "99.45"
#[111] "0.00"
#[112] "-"
#[113] "2"
#[114] "3"
#[115] "109.38%"
#[116] "AAPL210716C00025000"
tmpCalls <- expiryHTML %>% html_nodes(".calls") %>% html_table()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。