Fix Gamma and cap the number of simulations at 10M
This commit is contained in:
15
models.go
15
models.go
@@ -47,6 +47,7 @@ func (opt *Option) PriceMonteCarlo() {
|
|||||||
opt.Levels = make([]float64, opt.Sims)
|
opt.Levels = make([]float64, opt.Sims)
|
||||||
|
|
||||||
tte := opt.ExpiryDate.Sub(opt.ValueDate).Hours() / (24 * 365)
|
tte := opt.ExpiryDate.Sub(opt.ValueDate).Hours() / (24 * 365)
|
||||||
|
rand.Seed(time.Now().UTC().UnixNano())
|
||||||
|
|
||||||
for i = 0; i < opt.Sims; i++ {
|
for i = 0; i < opt.Sims; i++ {
|
||||||
randNum := rand.NormFloat64()
|
randNum := rand.NormFloat64()
|
||||||
@@ -60,11 +61,11 @@ func (opt *Option) PriceMonteCarlo() {
|
|||||||
|
|
||||||
// Gamma -- TODO: Doesn't look right
|
// Gamma -- TODO: Doesn't look right
|
||||||
level = gbmSimulation(opt.Spot+0.0001, opt.Rfr, opt.Vol, tte, randNum)
|
level = gbmSimulation(opt.Spot+0.0001, opt.Rfr, opt.Vol, tte, randNum)
|
||||||
opt.Gamma += math.Max((level - opt.Strike) * float64(opt.OptType), 0)
|
opt.Gamma += math.Max((level-opt.Strike)*float64(opt.OptType), 0)
|
||||||
level += gbmSimulation(opt.Spot-0.0001, opt.Rfr, opt.Vol, tte, randNum)
|
level = gbmSimulation(opt.Spot-0.0001, opt.Rfr, opt.Vol, tte, randNum)
|
||||||
opt.Gamma += math.Max((level - opt.Strike) * float64(opt.OptType), 0)
|
opt.Gamma += math.Max((level-opt.Strike)*float64(opt.OptType), 0)
|
||||||
level -= 2 * gbmSimulation(opt.Spot, opt.Rfr, opt.Vol, tte, randNum)
|
level = gbmSimulation(opt.Spot, opt.Rfr, opt.Vol, tte, randNum)
|
||||||
opt.Gamma += math.Max((level - opt.Strike) * float64(opt.OptType), 0)
|
opt.Gamma -= 2 * math.Max((level-opt.Strike)*float64(opt.OptType), 0)
|
||||||
|
|
||||||
// Vega
|
// Vega
|
||||||
level = gbmSimulation(opt.Spot, opt.Rfr, opt.Vol+0.0001, tte, randNum)
|
level = gbmSimulation(opt.Spot, opt.Rfr, opt.Vol+0.0001, tte, randNum)
|
||||||
@@ -82,10 +83,10 @@ func (opt *Option) PriceMonteCarlo() {
|
|||||||
df := math.Exp(-opt.Rfr * tte)
|
df := math.Exp(-opt.Rfr * tte)
|
||||||
opt.FV = opt.FV / float64(opt.Sims) * df
|
opt.FV = opt.FV / float64(opt.Sims) * df
|
||||||
opt.Delta = (opt.Delta/float64(opt.Sims)*df - opt.FV) / 0.0001
|
opt.Delta = (opt.Delta/float64(opt.Sims)*df - opt.FV) / 0.0001
|
||||||
opt.Gamma = (opt.Gamma/float64(opt.Sims)*df - opt.FV) / 10000
|
opt.Gamma = opt.Gamma / float64(opt.Sims) * df / 1e-8
|
||||||
opt.Vega = (opt.Vega/float64(opt.Sims)*df - opt.FV) / 0.01
|
opt.Vega = (opt.Vega/float64(opt.Sims)*df - opt.FV) / 0.01
|
||||||
opt.Theta = (opt.Theta/float64(opt.Sims)*math.Exp(-opt.Rfr*(tte-1./365)) -
|
opt.Theta = (opt.Theta/float64(opt.Sims)*math.Exp(-opt.Rfr*(tte-1./365)) -
|
||||||
opt.FV) / -(1. / 365)
|
opt.FV) / (1. / 365)
|
||||||
opt.Rho = (opt.Rho/float64(opt.Sims)*math.Exp(-(opt.Rfr+0.01)*tte) - opt.FV)
|
opt.Rho = (opt.Rho/float64(opt.Sims)*math.Exp(-(opt.Rfr+0.01)*tte) - opt.FV)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
6
serve.go
6
serve.go
@@ -21,6 +21,12 @@ func index(w http.ResponseWriter, r *http.Request) {
|
|||||||
json.Unmarshal(body, &bsOpt)
|
json.Unmarshal(body, &bsOpt)
|
||||||
json.Unmarshal(body, &mcOpt)
|
json.Unmarshal(body, &mcOpt)
|
||||||
|
|
||||||
|
// Cap the number of simulations at 10M
|
||||||
|
if bsOpt.Sims > 1e7 || mcOpt.Sims > 1e7 {
|
||||||
|
bsOpt.Sims = 1e7
|
||||||
|
mcOpt.Sims = 1e7
|
||||||
|
}
|
||||||
|
|
||||||
mcOpt.PriceMonteCarlo()
|
mcOpt.PriceMonteCarlo()
|
||||||
bsOpt.PriceClosedForm()
|
bsOpt.PriceClosedForm()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user