Sunday, 21 July 2019

Fbasic option

Attaching package: ‘fBasics’

The following object is masked from ‘package:TTR’:

    volatility

Warning messages:
1: package ‘fOptions’ was built under R version 3.5.1
2: package ‘timeDate’ was built under R version 3.5.1
3: package ‘timeSeries’ was built under R version 3.5.1
4: package ‘fBasics’ was built under R version 3.5.1
> library(fOptions)
> library("fExoticOptions", lib.loc="~/R/win-library/3.5")
Warning message:
package ‘fExoticOptions’ was built under R version 3.5.1
> library(fExoticOptions)
> a <- GBSOption("c", 100, 100, 1, 0.02, -0.02, 0.3, title = NULL,
+                description = NULL)
> (z <- a@price)
[1] 10.62678
> a <- GeometricAverageRateOption("c", 100, 100, 1, 0.02, -0.02, 0.3,
+                                 title = NULL, description = NULL)
> (z <- a@price)
[1] 5.889822
> vanilla <- GBSOption(TypeFlag = "c", S = 100, X = 90, Time = 1,
+                      r = 0.02, b = -0.02, sigma = 0.3)
> KO <- sapply(100:300, FUN = StandardBarrierOption, TypeFlag = "cuo",
+              S = 100, X = 90, K = 0, Time = 1, r = 0.02, b = -0.02, sigma = 0.30)
Warning messages:
1: In if (X >= H) { :
  the condition has length > 1 and only the first element will be used
2: In if (X < H) { :
  the condition has length > 1 and only the first element will be used
> plot(KO[[1]]@price, type = "l",
+      xlab = "barrier distance from spot",
+      ylab = "price of option",
+      main = "Price of KO converges to plain vanilla")
> abline(h = vanilla@price, col = "red")
> library("plot3D", lib.loc="~/R/win-library/3.5")
Warning message:
package ‘plot3D’ was built under R version 3.5.1
> library("scatterplot3d", lib.loc="~/R/win-library/3.5")
> install.packages('plot3D')
Error in install.packages : Updating loaded packages
> BS_surface <- function(S, Time, FUN, ...) {
+     require(plot3D)
+     n <- length(S)
+     k <- length(Time)
+     m <- matrix(0, n, k)
+     for (i in 1:n){
+         for (j in 1:k){
+             l <- list(S = S[i], Time = Time[j], ...)
+             m[i,j] <- max(do.call(FUN, l)@price, 0)
+         }
+     }
+     persp3D(z = m, xlab = "underlying", ylab = "Remaining time",
+             zlab = "option price", phi = 30, theta = 20, bty = "b2")
+ }
> BS_surface(seq(1, 200,length = 200), seq(0, 2, length = 200),
+            GBSOption, TypeFlag = "c", X = 90, r = 0.02, b = 0, sigma = 0.3)
> BS_surface(seq(1,200,length = 200), seq(0, 2, length = 200),
+            StandardBarrierOption, TypeFlag = "cuo", H = 130, X = 90, K = 0,
+            r = 0.02, b = -0.02, sigma = 0.30)
> BS_surface(seq(1,200,length = 200), seq(0, 2, length = 200),
+            StandardBarrierOption, TypeFlag = "cuo", H = 130, X = 90, K = 0,
+            r = 0.02, b = -0.02, sigma = 0.30)
> GetGreeks <- function(FUN, arg, epsilon,...) {
+     all_args1 <- all_args2 <- list(...)
+     all_args1[[arg]] <- as.numeric(all_args1[[arg]] + epsilon)
+     all_args2[[arg]] <- as.numeric(all_args2[[arg]] - epsilon)
+     (do.call(FUN, all_args1)@price -
+             do.call(FUN, all_args2)@price) / (2 * epsilon)
+ }
> x <- seq(10, 200, length = 200)
> delta <- vega <- theta <- rho <- rep(0, 200)
> for(i in 1:200){
+     delta[i] <- GetGreeks(FUN = FloatingStrikeLookbackOption,
+                           arg = 2, epsilon = 0.01, "p", x[i], 100, 1, 0.02, -0.02, 0.2)
+     vega[i] <- GetGreeks(FUN = FloatingStrikeLookbackOption,
+                          arg = 7, epsilon = 0.0005, "p", x[i], 100, 1, 0.02, -0.02,
+                          0.2)
+     theta[i] <- GetGreeks(FUN = FloatingStrikeLookbackOption,
+                           arg = 4, epsilon = 1/365, "p", x[i], 100, 1, 0.02, -0.02,
+                           0.2)
+     rho[i] <- GetGreeks(FUN = FloatingStrikeLookbackOption,
+                         arg = 5, epsilon = 0.0001, "p", x[i], 100, 1, 0.02, -0.02, 0.2)
+ }
> par(mfrow = c(2, 2))
> plot(x, delta, type = "l", xlab = "S", ylab = "", main = "Delta")
> plot(x, vega, type = "l", xlab = "S", ylab = "", main = "Vega")
> plot(x, theta, type = "l", xlab = "S", ylab = "", main = "Theta")
> plot(x, rho, type = "l", xlab = "S", ylab = "", main = "Rho")
https://mathclasstutor.blogspot.com

Tuesday, 16 July 2019

Permutation Mathematics

 Permutation Mathematics

library(igraph)
> g <- sample_gnm(20, 50)
> g2 <- permute(g, sample(vcount(g)))
> graph.isomorphic(g, g2)
[1] TRUE
> g$name <- "Random graph, Gnm, 20, 50"
> V(g)$name <- letters[1:vcount(g)]
> E(g)$weight <- sample(1:5, ecount(g), replace=TRUE)
> g2 <- permute(g, sample(vcount(g)))
> graph.isomorphic(g, g2)
[1] TRUE
> g2$name
[1] "Random graph, Gnm, 20, 50"
> V(g2)$name
 [1] "m" "q" "d" "c" "e" "t" "b" "p" "h"
[10] "n" "r" "i" "s" "k" "l" "o" "a" "j"
[19] "f" "g"
> E(g2)$weight
 [1] 5 2 4 1 4 1 5 4 3 1 5 3 5 1 3 2 4 5
[19] 3 1 2 5 1 2 4 4 1 4 5 1 1 4 2 2 3 5
[37] 1 4 2 3 5 5 5 1 3 3 3 5 3 3
> all(sort(E(g2)$weight) == sort(E(g)$weight))
[1] TRUE
> plot(g2)
plot-neural-network

> plot(g)

Tuesday, 9 July 2019

Time Series Analysis TITAN.NS

Time Series Analysis

chartSeries(TITAN,multi.col=TRUE,theme="white")
> addADX()
> addRSI()
> addMACD()

> TITAN_return <-
+     log(TITAN$TITAN.NS.Close/TITAN$TITAN.NS.Open)
> qqnorm(TITAN_return, main = "Normal Q-Q Plot of TITAN daily log return",
+        xlab = "Theoretical Quantiles",
+        ylab = "Sample Quantiles", plot.it = TRUE, datax = FALSE
+ )
> qqline(TITAN_return, col="red")
> qqnorm(TITAN_return, main = "Normal Q-Q Plot of TITAN daily log return",
+ xlab = "Theoretical Quantiles",ylab = "Sample Quantiles", plot.it = TRUE, datax = FALSE )
> ret <- dailyReturn(Cl(TITAN), type='log')
Warning message:
In to_period(xx, period = on.opts[[period]], ...) :
  missing values removed from data
> par(mfrow=c(2,2))
> acf(ret, main="Return ACF");
> pacf(ret, main="Return PACF");
> acf(ret^2, main="Squared return ACF");
> pacf(ret^2, main="Squared return PACF")


> par(mfrow=c(1,1))
> m=mean(ret);s=sd(ret);
> par(mfrow=c(1,2))
> hist(ret, nclass=40, freq=FALSE, main='Return histogram');curve(dnorm(x,
+                                                                       mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")





> plot(density(ret), main='Return empirical distribution');curve(dnorm(x,
+                                                                      mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
> par(mfrow=c(1,1))
> kurtosis(ret)
[1] 6.615807
attr(,"method")
[1] "excess"
> plot(density(ret), main='Return EDF - upper tail', xlim = c(0.1, 0.2),
+      ylim=c(0,2));
> curve(dnorm(x, mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
> plot(density(ret), xlim=c(-5*s,5*s),log='y', main='Density on log-scale')
> curve(dnorm(x, mean=m,sd=s), from=-5*s, to=5*s, log="y", add=TRUE,
+       col="red")
> qqnorm(ret);qqline(ret)
> library("rugarch", lib.loc="~/R/win-library/3.5")
garch11.spec = ugarchspec(variance.model = list(model="sGARCH",
+                                                 garchOrder=c(1,1)), mean.model = list(armaOrder=c(0,0)))
> titan.garch11.fit = ugarchfit(spec=garch11.spec, data=ret)
> coef(titan.garch11.fit)
          mu        omega       alpha1
0.0015069015 0.0001130584 0.1180053516
       beta1
0.6339646076
> ni.garch11 <- newsimpact(titan.garch11.fit)
> plot(ni.garch11$zx, ni.garch11$zy, type="l", lwd=2, col="blue",
+      main="GARCH(1,1) - News Impact", ylab=ni.garch11$yexpr, xlab=ni.
+      garch11$xexpr)
Error: unexpected symbol in:
"     main="GARCH(1,1) - News Impact", ylab=ni.garch11$yexpr, xlab=ni.
     garch11"
> plot(ni.garch11$zx, ni.garch11$zy, type="l", lwd=2, col="blue",main="GARCH(1,1) - News Impact", ylab=ni.garch11$yexpr, xlab=ni.garch11$xexpr)

Sunday, 7 July 2019

How to analyse time series data NIFTY50

 How to analyze data( NIFTY50)

nifty,bank nifty,bank nifty analysis,nifty analysis,bank nifty strategy,nifty tomorrow,bank nifty today,bank nifty future,nifty 50,nifty options trading strategies,nifty bank,nifty future,nifty this week,bank nifty live,nifty tommorow,nifty strategy,bank nifty studio,nifty prediction,bank nifty option,bank nifty prediction for tomorrow,nifty 50 companies,bank nifty options,bank nifty live chart


https://www.mathclasstutor.online
getSymbols("^NSEI", from="2004-01-01", to=Sys.Date())
chartSeries(Cl(NSEI))
> ret <- dailyReturn(Cl(NSEI), type='log')
Warning message:
In to_period(xx, period = on.opts[[period]], ...) :
  missing values removed from data
> par(mfrow=c(2,2))
> acf(ret, main="Return ACF");
> pacf(ret, main="Return PACF");
> acf(ret^2, main="Squared return ACF");
> pacf(ret^2, main="Squared return PACF")
> par(mfrow=c(1,1))
> m=mean(ret);s=sd(ret);
> par(mfrow=c(1,2))
> hist(ret, nclass=40, freq=FALSE, main='Return histogram');curve(dnorm(x,
+          mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
> plot(density(ret), main='Return empirical distribution');curve(dnorm(x,
+         mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
> par(mfrow=c(1,1))
https://www.mathclasstutor.online

> kurtosis(ret)
[1] 12.6355
attr(,"method")
[1] "excess"
> plot(density(ret), main='Return EDF - upper tail', xlim = c(0.1, 0.2),
+      ylim=c(0,2));
> curve(dnorm(x, mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
> plot(density(ret), xlim=c(-5*s,5*s),log='y', main='Density on log-scale')
Warning message:
In xy.coords(x, y, xlabel, ylabel, log) :
  75 y values <= 0 omitted from logarithmic plot
> curve(dnorm(x, mean=m,sd=s), from=-5*s, to=5*s, log="y", add=TRUE,
+       col="red")
https://www.mathclasstutor.online

https://www.mathclasstutor.online

https://www.mathclasstutor.online

> qqnorm(ret);qqline(ret);

Saturday, 6 July 2019

Symbols("SNP", from="2004-01-01", to=Sys.Date())

GetSymbols("SNP", from="2004-01-01", to=Sys.Date())

[1] "SNP"
>chartSeries(Cl(SNP))
Hit <Return> to see next plot:
> ret <- dailyReturn(Cl(SNP), type='log')
> par(mfrow=c(2,2))
> acf(ret, main="Return ACF");
Hit <Return> to see next plot:
> pacf(ret, main="Return PACF");
> acf(ret^2, main="Squared return ACF");
> pacf(ret^2, main="Squared return PACF")
> par(mfrow=c(1,1))

Intersection of subring

How to prove the intersection of two subrings is a subring? Let S1 and S2  be two subrings of a ring R. Then S1∩S2 is not empty since ...