Example 2
The below scripts use the functions getFeasibleOptionFlags and getMidPriceQReg to clean the list of options given in spoptions.txt and return the Q distribution.
In the following script:
sp_nprepresents the spot prices in 4 different states of the worldbid_nprepresents the bid prices of different optionsask_nprepresents the ask prices of different optionsstrike_nprepresents the strike prices of different optionspFlag_npindicates whether an option si a call or a put optionfwis the forward price
import dbbpy
import numpy as np
fw_data = np.loadtxt("forward1.txt", delimiter='\t')
data = np.loadtxt("spopt1.txt", delimiter='\t')
strike_list = data[:, 0]
pFlag_list = data[:, 1]
bid_list = data[:, 2]
ask_list = data[:, 3]
min_strike = int(np.min(strike_list))
max_strike = int(np.max(strike_list))
sp = list(range(0, min_strike, 10)) + list(range(min_strike, max_strike, 5)) + list(range(max_strike, 4440, 10))
sp_np = np.array(sp)
bid_np = np.array(bid_list)
ask_np = np.array(ask_list)
strike_np = np.array(strike_list)
pFlag_np = np.array(pFlag_list, dtype=bool)
fw = fw_data[0]
feasible_options = dbbpy.getFeasibleOptionFlags(sp_np, bid_np, ask_np, strike_np, pFlag_np, fw, fw-0.02, fw+0.02)
q = dbbpy.getMidPriceQReg(sp_np, bid_np[feasible_options], ask_np[feasible_options], strike_np[feasible_options], pFlag_np[feasible_options], fw, fw-0.02, fw+0.02)
print(q)library(rdbb)
fw_data <- fread("forward1.txt", header = FALSE)
data <- fread("spopt1.txt", header = FALSE)
strike_list <- data$V1
pFlag_list <- data$V2
bid_list <- data$V3
ask_list <- data$V4
min_strike <- min(strike_list)
max_strike <- max(strike_list)
sp <- c(seq(0, min_strike, by = 10), seq(min_strike, max_strike, by = 5), seq(max_strike, 4440, by = 10))
sp_np <- as.numeric(sp)
bid_np <- as.numeric(bid_list)
ask_np <- as.numeric(ask_list)
strike_np <- as.numeric(strike_list)
pFlag_np <- as.logical(pFlag_list)
fw <- fw_data$V1[1]
feasible_options <- getFeasibleOptionFlags(sp_np, bid_np, ask_np, strike_np, pFlag_np, fw, fw - 0.02, fw + 0.02)
q <- getMidPriceQ(sp_np, bid_np[feasible_options], ask_np[feasible_options], strike_np[feasible_options], pFlag_np[feasible_options], fw, fw - 0.02, fw + 0.02)Last updated