From 6b566992809003ba337999a622ddb4e0a4614df3 Mon Sep 17 00:00:00 2001 From: Kevin Date: Sun, 17 Sep 2017 22:42:34 -0400 Subject: [PATCH] Fix Gamma and cap the number of simulations at 10M --- models.go | 15 ++++++++------- serve.go | 6 ++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/models.go b/models.go index 42b6924..5ee0992 100644 --- a/models.go +++ b/models.go @@ -47,6 +47,7 @@ func (opt *Option) PriceMonteCarlo() { opt.Levels = make([]float64, opt.Sims) tte := opt.ExpiryDate.Sub(opt.ValueDate).Hours() / (24 * 365) + rand.Seed(time.Now().UTC().UnixNano()) for i = 0; i < opt.Sims; i++ { randNum := rand.NormFloat64() @@ -60,11 +61,11 @@ func (opt *Option) PriceMonteCarlo() { // Gamma -- TODO: Doesn't look right level = gbmSimulation(opt.Spot+0.0001, opt.Rfr, opt.Vol, tte, randNum) - opt.Gamma += math.Max((level - opt.Strike) * float64(opt.OptType), 0) - level += gbmSimulation(opt.Spot-0.0001, opt.Rfr, opt.Vol, tte, randNum) - opt.Gamma += math.Max((level - opt.Strike) * float64(opt.OptType), 0) - level -= 2 * gbmSimulation(opt.Spot, 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) + opt.Gamma += math.Max((level-opt.Strike)*float64(opt.OptType), 0) + level = gbmSimulation(opt.Spot, opt.Rfr, opt.Vol, tte, randNum) + opt.Gamma -= 2 * math.Max((level-opt.Strike)*float64(opt.OptType), 0) // Vega 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) opt.FV = opt.FV / float64(opt.Sims) * df 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.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) } diff --git a/serve.go b/serve.go index f922c27..e9e1cc2 100644 --- a/serve.go +++ b/serve.go @@ -21,6 +21,12 @@ func index(w http.ResponseWriter, r *http.Request) { json.Unmarshal(body, &bsOpt) 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() bsOpt.PriceClosedForm()