Fix exported struct fields

This commit is contained in:
Kevin
2017-09-15 00:09:54 -04:00
parent 47f64bc0ea
commit c41a1cafe2
2 changed files with 52 additions and 52 deletions

View File

@@ -25,24 +25,24 @@ func (t RFC8601Time) MarshalJSON() ([]byte, error) {
// Option struct that holds all the details of an option // Option struct that holds all the details of an option
// Option details // Option details
type Option struct { type Option struct {
optType int64 OptType int64
strike float64 Strike float64
expiryDate RFC8601Time ExpiryDate RFC8601Time
// Market data // Market data
valueDate RFC8601Time ValueDate RFC8601Time
spot float64 Spot float64
rfr float64 Rfr float64
vol float64 Vol float64
sims int64 Sims int64
// Results // Results
fv float64 FV float64
delta float64 Delta float64
vega float64 Vega float64
rho float64 Rho float64
gamma float64 Gamma float64
theta float64 Theta float64
} }
// Single simulation of Geometric Brownian Motion // Single simulation of Geometric Brownian Motion
@@ -62,46 +62,46 @@ func RunSimulations(opt *Option) {
var i int64 var i int64
var level float64 var level float64
levels := make([]float64, opt.sims) levels := make([]float64, opt.Sims)
tte := opt.expiryDate.Sub(opt.valueDate).Hours() / (24 * 365) tte := opt.ExpiryDate.Sub(opt.ValueDate).Hours() / (24 * 365)
for i = 0; i < opt.sims; i++ { for i = 0; i < opt.Sims; i++ {
randNum := rand.NormFloat64() randNum := rand.NormFloat64()
// Base // Base
levels[i] = gbmSimulation(opt.spot, opt.rfr, opt.vol, tte, randNum) levels[i] = gbmSimulation(opt.Spot, opt.Rfr, opt.Vol, tte, randNum)
opt.fv += math.Max((levels[i]-opt.strike)*float64(opt.optType), 0) opt.FV += math.Max((levels[i]-opt.Strike)*float64(opt.OptType), 0)
// Delta // Delta
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.delta += math.Max((level-opt.strike)*float64(opt.optType), 0) opt.Delta += math.Max((level-opt.Strike)*float64(opt.OptType), 0)
// 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)
level += gbmSimulation(opt.spot-0.0001, opt.rfr, opt.vol, tte, randNum) level += gbmSimulation(opt.Spot-0.0001, opt.Rfr, opt.Vol, tte, randNum)
level -= 2 * gbmSimulation(opt.spot, opt.rfr, opt.vol, tte, randNum) 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)
// 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)
opt.vega += math.Max((level-opt.strike)*float64(opt.optType), 0) opt.Vega += math.Max((level-opt.Strike)*float64(opt.OptType), 0)
// Theta // Theta
level = gbmSimulation(opt.spot, opt.rfr, opt.vol, tte-1./365, randNum) level = gbmSimulation(opt.Spot, opt.Rfr, opt.Vol, tte-1./365, randNum)
opt.theta += math.Max((level-opt.strike)*float64(opt.optType), 0) opt.Theta += math.Max((level-opt.Strike)*float64(opt.OptType), 0)
// Rho -- TODO: Doesn't look right // Rho -- TODO: Doesn't look right
level = gbmSimulation(opt.spot, opt.rfr+0.0001, opt.vol, tte, randNum) level = gbmSimulation(opt.Spot, opt.Rfr+0.0001, opt.Vol, tte, randNum)
opt.rho += math.Max((level-opt.strike)*float64(opt.optType), 0) opt.Rho += math.Max((level-opt.Strike)*float64(opt.OptType), 0)
} }
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 - opt.FV) / 10000
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.fv) / -(1. / 365) opt.Theta = (opt.Theta/float64(opt.Sims)*math.Exp(-opt.Rfr*(tte-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)
} }
/* /*
@@ -138,18 +138,18 @@ func main() {
`, `,
value.Format("2006-01-02"), value.Format("2006-01-02"),
"Call", "Call", "Call", "Call",
opt.spot, opt.spot, opt.Spot, opt.Spot,
expiry.Format("2006-01-02"), expiry.Format("2006-01-02"), expiry.Format("2006-01-02"), expiry.Format("2006-01-02"),
opt.strike, opt.strike, opt.Strike, opt.Strike,
opt.rfr*100, opt.rfr*100, opt.Rfr*100, opt.Rfr*100,
opt.vol*100, opt.vol*100, opt.Vol*100, opt.Vol*100,
opt.fv, opt.fv, opt.FV, opt.FV,
opt.delta, opt.delta, opt.Delta, opt.Delta,
opt.gamma, opt.gamma, opt.Gamma, opt.Gamma,
opt.vega, opt.vega, opt.Vega, opt.Vega,
opt.theta, opt.theta, opt.Theta, opt.Theta,
opt.rho, opt.rho, opt.Rho, opt.Rho,
opt.sims) opt.Sims)
} }
*/ */

View File

@@ -19,9 +19,9 @@ func formatRequest(r *http.Request) {
fmt.Printf("Error: %s\n", err) fmt.Printf("Error: %s\n", err)
} }
fmt.Println("Option type:", opt.optType) fmt.Println("Option type:", opt.OptType)
fmt.Println("Strike:", opt.strike) fmt.Println("Strike:", opt.Strike)
fmt.Println("Expiry date:", opt.expiryDate.Format("2006-01-02")) fmt.Println("Expiry date:", opt.ExpiryDate.Format("2006-01-02"))
for k, v := range r.Header { for k, v := range r.Header {
fmt.Printf("%v: %v\n", k, v) fmt.Printf("%v: %v\n", k, v)