-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhistory_manipulation.R
145 lines (102 loc) · 4.46 KB
/
history_manipulation.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# + ------------------------------------------------------- +
# | Draw volatility curves at the end of a selected day |
# + ------------------------------------------------------- +
require(dplyr)
require(tidyr)
require(ggplot2)
# +------------------------------------+
# | Prepare data
# +------------------------------------+
# TODO: Add RVI or RTSVX level
# rts.data = read.csv('RTSI.txt') %>% select(c(3, 8)) %>% mutate(Dates = as.Date(as.character(X.DATE.), format='%Y%m%d')) %>% select(c(3, 2))
# names(rts.data) = c('Dates', 'Close')
# save(rts.data, file = 'rtsi.RData')
load('rtsi.RData')
# smile.data = read.csv(file = 'ri_smile.csv', sep = ';', header=T, dec=',') %>% select(c(-1))
# smile.data$tms = as.Date(substr(as.character(smile.data$tms), 0, 10))
# save(smile.data, file = 'smile.RData')
load('smile.RData')
dates.rng = c(min(smile.data$tms), max(smile.data$tms))
# +------------------------------------+
# | For each option series calc smile
# | External variables used: all.data
# +------------------------------------+
CalcSmilesSeries = function(strikeRng = 0.2,
<<<<<<< HEAD
smileDate = as.Date('2015-04-30'),
nearest = 10){
### Find coefs for inputed date
vx.at.date = smile.data %>% filter(tms == smileDate) %>% top_n(nearest, 1/t)
=======
smileDate = as.Date('2010-09-06'),
nearest = 0){
options(warn=-1)
### Find coefs for intuted date
vx.at.date = smile.data %>% filter(tms == smileDate) %>% arrange(t)
if(nearest > 0){
vx.at.date = vx.at.date[order(vx.at.date$t),]
vx.at.date = vx.at.date[1:nearest, ]
}
>>>>>>> origin/master
### Make strikes range, include futures values
rng = strikeRng
strikes = seq( min(vx.at.date$fut_price)*(1-rng), max(vx.at.date$fut_price)*(1+rng), length.out = 50 )
strikes = sort(c(strikes, vx.at.date$fut_price))
### Calc smile for every exp.date, strike value
smiles = lapply( c(1:nrow(vx.at.date)), function(x){
x.row = x
sapply(strikes, function(x){
strike = x
fut = vx.at.date[x.row, 'fut_price', drop=T]
tdays = vx.at.date[x.row, 't', drop=T] * 250
coef.vector = as.vector(vx.at.date[x.row, c('s', 'a', 'b', 'c', 'd', 'e')])
vxSmile(strike, fut, tdays, coef.vector, method = 2)
})
})
### Arrange data for ggplot
names(smiles) = as.vector(vx.at.date$small_name)
smiles = gather(data = as.data.frame(c(list(strike = strikes), smiles)), key=strike )
names(smiles) = c('Strike', 'BaseFutures', 'IV')
<<<<<<< HEAD
smiles$BaseFutures = as.character(smiles$BaseFutures)
fut.days = vx.at.date %>%
select(small_name, t) %>%
mutate( tdays = (round(t * 250, 0)) )
fut.days$small_name = as.character(fut.days$small_name)
#%>%
smiles = dplyr::left_join(smiles, fut.days, by = c('BaseFutures' = 'small_name'))
smiles$tdays = as.character(smiles$tdays)
=======
try({smiles = vx.at.date %>% select(small_name, t) %>% mutate(tdays = as.factor(round(t * 250, 0))) %>%
left_join(smiles, by = c('small_name' = 'BaseFutures')) %>% arrange(t)
})
>>>>>>> origin/master
return(smiles)
}
<<<<<<< HEAD
=======
#CalcSmilesSeries()
>>>>>>> origin/master
# +------------------------------------+
# | IV smile functions
# +------------------------------------+
vxSmile = function(strike, fut, tdays, coef.vector=NULL, method=2)
{
s = try(as.numeric(coef.vector[['s']]), silent = T)
a = try(as.numeric(coef.vector[['a']]), silent = T)
b = try(as.numeric(coef.vector[['b']]), silent = T)
c = try(as.numeric(coef.vector[['c']]), silent = T)
d = try(as.numeric(coef.vector[['d']]), silent = T)
e = try(as.numeric(coef.vector[['e']]), silent = T)
f = try(as.numeric(coef.vector[['f']]), silent = T)
g = try(as.numeric(coef.vector[['g']]), silent = T)
try({
if(method==1)
vxs=a + b*(1 - e ^ ( (-1)*c*( 1/(tdays/365)^0.5 * log(strike / fut)-s )^2 )) + d * atan(e * (1 / (tdays / 365) ^ 0.5 * log(strike / fut) - s)) / e
if(method==2)
vxs = a + b*(1 - exp(-c * (1 / (tdays / 365) ^ 0.5 * log(strike / fut) - s) ^ 2)) + d * atan(e * (1 / (tdays / 365) ^ 0.5 * log(strike / fut) - s)) / e
if(method==3)
vxs = a + b*strike + c*strike^2 + d*strike^3 + e*strike^4 + f*strike^5 + g*strike^6
}, silent=T)
return(as.numeric(vxs))
}