-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
437 lines (355 loc) · 14.3 KB
/
server.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# This is the server logic for a Shiny web application.
#server.R for fixedIncome part of GARPFRM package
#Loading the required library files
library(xlsx)
library(shiny)
library(xts)
source("discountFactorArbitrage.R")
source("riskMetricsAndHedges.R")
#Modified the function bondFullPrice already present in bondFullPrice
bondFullPrice<-function(bond, yield, cashFlowPd, t0, t1, currentDate){
compoundPd = bond$m
face = bond$face
couponRate = bond$couponRate
d1 = as.numeric(t1-currentDate)
d2 = as.numeric(t1-t0)
tmp = 0
if (cashFlowPd > 1)
{
for(k in 1:(cashFlowPd-1)){
tmp = tmp + ((couponRate / compoundPd * face) / ((1 + yield/compoundPd)^k))
}
}
# Calculate dirty price based on partial periods formula
dirtyP = (1 / ((1 + yield / compoundPd)^(d1/d2))) * (couponRate / compoundPd * face + tmp + face / ((1 + yield/compoundPd)^(cashFlowPd-1)))
# Calculate accruedInterest
aiDays = as.numeric(currentDate-t0)
couponDays = as.numeric(t1-t0)
ai = couponRate / compoundPd * face * aiDays / couponDays
cleanP = dirtyP - ai
return(list(dirty=dirtyP, clean=cleanP, accruedInterest=ai))
}
#Reading Inbuilt Data Files
bond.data <- readRDS("data/bond_Data.rds")
tnotes <-readRDS("data/tnotes.rds")
###Main Function####
#We can write all the functionalities of the application under the shinyServer function
shinyServer(function(input, output) {
#Function to calculate the discount factor
output$discount.factor<-renderPrint({
# Take a dependency on input$discount.factor_button
if(input$discount.factor_button == 0)
return("Input paramerters are initialized with default values. Please Press the Run Button to Start the Program")
# Use isolate() to avoid dependency on other inputs
isolate({
#Reading Values once the Run button is clicked
bondprice<-as.numeric(unlist(strsplit(input$bondprice,",")))
cr<-as.numeric(unlist(strsplit(input$cr,",")))
ttm<-as.numeric(unlist(strsplit(input$ttm,",")))
#Checking if all the inputs are numeric
if(all(is.na(c(cr,bondprice,ttm))==FALSE)==FALSE)
return("All the inputs must be numeric")
#Checking if the coupon rate is less than 100%
if(!all(cr<100))
return("Please enter a coupon rate less than 100%")
interval<-max(ttm)/length(ttm)
#Checking if the bonds have equally spaced maturity
if(!all(diff(ttm) == interval))
return("Bonds must have equally spaced maturity dates")
#Checking if the Arguments entered are all of equal length
if(length(cr)!=length(bondprice)){
return("Arguments must be of equal length") } else if(length(cr)!=length(ttm)){
return("Arguments must be of equal length")}
cr <-cr/100
#Generating the cash flow matrix
cashflow<-matrix(0,nrow=length(ttm),ncol=length(ttm))
for(i in 1:nrow(cashflow))
{
for(j in 1:ncol(cashflow))
{
if(ttm[i]-j*interval==0)
{
cashflow[i,j]= 100 * (1+ cr[i]/2)
break
}
else
cashflow[i,j]=100*cr[i]/2
}
}
bondprice<-matrix(bondprice,ncol=1)
#Getting the discount curve using Function defined in GARPFRM
DF<-discountFactor(bondprice,cashflow)
#Getting the spot rates curve using Function defined in GARPFRM
spotrates <- spotForwardRates(ttm,DF)
#Returning the output as List
list(BondPrice=round(bondprice,input$digits),CashFlow=round(cashflow,input$digits),
DiscountFactor=round(DF,input$digits),"Spot Rates"=round(spotrates,input$digits))
})
}
)
##Function to Calculate the full bond price####
output$bond.price<-renderPrint({
# Take a dependency on input$price_button
if (input$bond.price_button == 0)
return("Input paramerters are initialized with default values.Please Press the Run Button to Start the Program")
# Use isolate() to avoid dependency on other inputs
isolate({
#Reading input values
#next coupon date
t1<-input$t1
#previous coupon date
t0<-input$t0
#Maturity Date
tm<-input$tm
#Current Date
tn<-input$tn
#Coupon Rate
cr<-input$bcr
#Yield
y<-input$yield
#Checking if Yield and Coupon rate is greated than 100%
if(!all(c(cr,y)<100))
return("Please enter a percentage less than 100")
cr<-cr/100
y<-y/100
#Calculating the cash flow periods
n <- as.numeric(round((tm-t0)/365*2))
bond = bondSpec(face=100, m=2, couponRate = cr)
#Getting the Full Bond Price using function defined in GARPFRM
bp.tn<-bondFullPrice(bond, y, n, t0, t1, tn)
list(dirty=round(bp.tn$dirty,input$digits),clean=round(bp.tn$clean,input$digits),
accruedInterest=round(bp.tn$accruedInterest,input$digits))
})
}
)
#Function to Plot the Bond Price at various times with a constant discout curve####
output$bond.price_plot<-renderPlot({
# Take a dependency on input$goButton
if(input$bond.price_button == 0)
return("")
# Use isolate() to avoid dependency on other inputs
isolate({
#Reading input values
#next coupon date
t1<-input$t1
#previous coupon date
t0<-input$t0
#Maturity Date
tm<-input$tm
#Current Date
tn<-input$tn
#Reading Bond Coupon Rate and Yield
cr<-input$bcr
y<-input$yield
#Checking if Yield and Coupon rate is greated than 100%
if(!all(c(cr,y)<100))
return("Please enter a percentage less than 100")
cr<-cr/100
y<-y/100
#Calculating the cash flow periods
n <- as.numeric(round((tm-t0)/365*2))
bond = bondSpec(face=100, m=2, couponRate = cr)
t1.add<- seq(t1,length=2, by = "6 months")[2]
#Getting the Full Bond Price at different times using function defined in GARPFRM
bp.tn<-bondFullPrice(bond, y, n, t0, t1, tn)
bp.t0<-bondFullPrice(bond, y, n, t0, t1, t0)
bp.t1<-bondFullPrice(bond, y, n, t0, t1, t1)
bp.t1.clean<-bondFullPrice(bond, y, n-1, t1, t1.add, t1)
bp.t1.new<-bondFullPrice(bond, y, n-1, t1, t1.add, t1.add)
#Merging all the prices
price<-cbind(bp.t0,bp.tn,bp.t1,bp.t1.clean,bp.t1.new)
#Getting the clean and dirty prices from the price
dirtyp <- price[1,]
cleanp <- price[2,]
#According to the time to maturity, changing the date vector
if ( t1.add > tm)
{date <- c(t0,tn,t1)} else
{date<-c(t0,tn,t1,t1,t1.add)}
#Changing the y-axis limits
ymin<- min(as.numeric(dirtyp[1:length(date)]),as.numeric(cleanp[1:length(date)]))
ymax<- max(as.numeric(dirtyp[1:length(date)]),as.numeric(cleanp[1:length(date)]))
#Plotting the data
plot(x=date,y=dirtyp[1:length(date)],type="b",xaxt="n", xlab='Settlement Date', ylab="Price"
,ylim = c(ymin, ymax), col= 3, lty = 1, main = "Plot Showing Variation in Price with Constant Discount Curve")
axis(side=1, at=date, labels=format(as.Date(date), '%Y-%m-%d'))
lines(as.Date(date),cleanp[1:length(date)],type="l",lty=2, col = 4)
legend("bottomleft",c("Dirty Price", "Flat Price"),lty=c(1,2),col=c(3,4), bty="n")
})
}
)
###Function to calculate and display Bond Parameters####
output$bond.parameters<-renderPrint({
# Take a dependency on input$goButton
if(input$present.value_button == 0)
return("Input paramerters are initialized with default values. Please Press the Run Button to Start the Program")
# Use isolate() to avoid dependency on other inputs
isolate({
#Reading years to maturity
t <- input$t
#Reading the discount factor
df<-as.numeric(unlist(strsplit(input$df,",")))
#Reading the bond parameters coupon rate
cr<-input$pcr
#Creating a time sequnce for cash flows until maturity
time<-seq(from=0.5,to=t,by=0.5)
#Checking if only numeric values are entered for discount curve
if(all(is.na(df)==FALSE)==FALSE)
return("All the inputs must be numeric")
#Checking if the coupon rate is less than 100%
if(cr>100)
return("Coupon Rate must be less than 100%")
#Checking if the length of discount curve is greater than sequence of cashflows
if(length(df)<length(time))
return("Discount Curve Should be longer than time to maturiy")
df<-df[1:length(time)]
cr<-cr/100
#Calculating the bondparametneres using the functions defined in GARPFRM R package
bond = bondSpec(time,face=100, m=2, couponRate = cr)
price = bondPrice(bond,df)
ytm = bondYTM(bond,df)
duration=bondDuration(bond,df)
convexity=bondConvexity(bond,df)
mduration = duration/(1+ytm/2)
#Giving output as a list
list(BondPrice=round(price,input$digits),YTM=round(ytm,input$digits),
MacaulayDuration=round(duration,input$digits),ModifiedDuration=round(mduration,input$digits)
,BondConvexity=round(convexity,input$digits))
})
}
)
###Function to get the data for spot rates and discount curve rates####
getDFSpotrate<-reactive(
{
#Taking the dependencies
input$spot.rate_button
isolate({
#Validating the uploaded dataset
if(input$userip == 'Upload a Dataset')
{
if(input$filetype == "Excel"){
format<-unlist(strsplit(input$file.excel$name,'[.]'))[2]
if(format == 'xlsx' || format == 'xlsm'){
dat<- read.xlsx(file=input$file.excel$datapath, sheetName=input$shname,
header=input$header, as.is=TRUE)
} else {
return ("Incorrect File Format. Please Upload an Excel File.")
}
} else if (input$filetype=="CSV"){
format<-unlist(strsplit(input$file.csv$name,'[.]'))[2]
if(format == 'csv'){
dat <- read.table(file=input$file.csv$datapath, sep=",", header=input$header, as.is=TRUE)
} else {
return ("Incorrect File Format. Please Upload a CSV File.")
}
} else if (input$filetype == "Text"){
format<-unlist(strsplit(input$file.txt$name,'[.]'))[2]
if(format == 'txt'){
dat <- read.table(file=input$file.txt$datapath, sep=input$sep, header=input$header, as.is=TRUE)
} else {
return ("Incorrect File Format. Please Upload a Text File.")
}
} else if (input$filetype=="RData"){
format<-unlist(strsplit(input$file.rdata$name,'[.]'))[2]
if(format == 'RData'){
load(input$file.rdata$datapath,newEnv <- new.env())
dat<-get(unlist(ls(newEnv)),envir=newEnv)
} else {
return ("Incorrect File Format. Please Upload a RData File.")
}
}
#Taking the column names
dat.names<- colnames(dat)
#Checking if the header names are present in the files
if(!is.element("IssueDate",dat.names) || !is.element("MaturityDate",dat.names)
|| !is.element("Coupon",dat.names) || !is.element("Ask",dat.names)
|| !is.element("Bid",dat.names) )
return ("Please Check the header names In the file.")
#Checking the maturity date format
if(any(is.na(as.Date(as.character(dat[,"MaturityDate"])))))
return ("Maturity Date Column is not properly formatted")
dat[,"MaturityDate"]<-as.Date(dat[,"MaturityDate"])
step.size = as.numeric(round(diff(dat[,"MaturityDate"])/365,1))[1]
#Checking the length bewtween maturity date
if (!all(as.numeric(round(diff(dat[,"MaturityDate"])/365,1))==step.size )){
return ("Maturity Dates must be equall spaced")}
} else{
switch(input$dataset,"T-Notes & Bonds" = dat<-bond.data,
"T-Notes" = dat<-tnotes)
}
#Taking the number of rows of the data and generating the cash flow matrix
n = nrow(dat)
CF = matrix(0, nrow = n, ncol = n)
for(i in 1:n)
{
CF[i, 1:i] = dat[i,"Coupon"]/2
}
diag(CF) = rep(100, n) + diag(CF)
#Extracting th discount factor
DF<-discountFactor(as.matrix((dat[,"Bid"]+dat["Ask"])/2),CF)
#Merging a discount factor of 1 to to account for discount factor at present time
DF <- c(1,DF)
step.size = as.numeric(round(diff(dat[,"MaturityDate"])/365,1))[1]
time = seq(from=0,by=step.size,length=n+1)
#Calculating the spot and forward rates
rates = spotForwardRates(time,DF)
#Giving output as a list
data<-list(dat=dat,DF=DF,rates=rates,time=time)
})
})
####Function to display spot rates and forward rates####
output$spot.rate<-renderPrint({
# Take a dependency on input$spot.rate_button
if(input$spot.rate_button == 0)
return("Please select Initial dataset or upload your own dataset. Then Press the Run Button to Start the Program")
# Use isolate() to avoid dependency on other inputs
isolate({
#Getting the values from the function
data<-getDFSpotrate()
#If the return is a list then it will be displayed
#otherwise error message will be displayed
if(is.list(data)){
list(DsicountFactor= round(as.vector(data$DF),input$digits)
,SpotRates=round(data$rates[,1],input$digits),ForwardRates=round(data$rates[,2],input$digits))
} else {
data
}
})
}
)
##Function to plot the spot rate and the discount curve####
output$spot.rate_plot<-renderPlot({
# Take a dependency on input$spot.rate_button
if (input$spot.rate_button == 0)
return("")
# Use isolate() to avoid dependency on other inputs
isolate({
#Getting date to plot the spot rate and discount curve
data<-getDFSpotrate()
if(is.list(data))
{
#Plotting spot rate and discount curve
par(mfrow=c(1,2))
plot(data$time,data$rates[,1],type = "b",xlab = "Maturity (In Years)",ylab="Rate", main = "Spot Rates")
plot(data$time,data$DF,type = "b",xlab = "Maturity (In Years)",ylab="Rate", main = "Discount Factors")
par(mfrow=c(1,1))
}
})
}
)
#Function to Print the table of DataSet
output$data.table<-renderDataTable({
# Take a dependency on input$spot.rate_button
if(input$spot.rate_button == 0)
return("")
# Use isolate() to avoid dependency on other inputs
isolate({
#Getting date to plot the spot rate and discount curve
data<-getDFSpotrate()
if(is.list(data))
{
data$dat
}
})
}, options = list(lengthMenu = c(10,15,20,25), pageLength = 10)
)
})#shinyserver