public override void calculate() { Utils.QL_REQUIRE(arguments_.exercise.type() == Exercise.Type.European, () => "not an European Option"); EuropeanExercise exercise = arguments_.exercise as EuropeanExercise; Utils.QL_REQUIRE(exercise != null, () => "not an European Option"); SpreadBasketPayoff spreadPayoff = arguments_.payoff as SpreadBasketPayoff; Utils.QL_REQUIRE(spreadPayoff != null, () => " spread payoff expected"); PlainVanillaPayoff payoff = spreadPayoff.basePayoff() as PlainVanillaPayoff; Utils.QL_REQUIRE(payoff != null, () => "non-plain payoff given"); double strike = payoff.strike(); double f1 = process1_.stateVariable().link.value(); double f2 = process2_.stateVariable().link.value(); // use atm vols double variance1 = process1_.blackVolatility().link.blackVariance(exercise.lastDate(), f1); double variance2 = process2_.blackVolatility().link.blackVariance(exercise.lastDate(), f2); double riskFreeDiscount = process1_.riskFreeRate().link.discount(exercise.lastDate()); Func <double, double> Square = x => x * x; double f = f1 / (f2 + strike); double v = Math.Sqrt(variance1 + variance2 * Square(f2 / (f2 + strike)) - 2 * rho_ * Math.Sqrt(variance1 * variance2) * (f2 / (f2 + strike))); BlackCalculator black = new BlackCalculator(new PlainVanillaPayoff(payoff.optionType(), 1.0), f, v, riskFreeDiscount); results_.value = (f2 + strike) * black.value(); }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(SpreadBasketPayoff obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }
public static string eqInstSpreadOptionMonteCarlo( [ExcelArgument(Description = "id of option to be constructed ")] string ObjectId, [ExcelArgument(Description = "Option type (Call/Put) ")] string optype, [ExcelArgument(Description = "Spot price leg 1")] double spot1, [ExcelArgument(Description = "Spot price leg 2")] double spot2, [ExcelArgument(Description = "Strike price ")] double stirkeprice, [ExcelArgument(Description = "Expiry Date ")] DateTime exdate, [ExcelArgument(Description = "Risk free rate ")] double riskfreerate, [ExcelArgument(Description = "Black-Scholes Vol for leg 1 ")] double vol1, [ExcelArgument(Description = "Black-Scholes Vol for leg 2 ")] double vol2, [ExcelArgument(Description = "correlation between leg 1 and leg 2 ")] double corr, [ExcelArgument(Description = "DayCounter ")] string daycounter, [ExcelArgument(Description = "Calendar ")] string calendar, [ExcelArgument(Description = "Pseudorandom (pr) or lowdiscrepancy (ld) ")] string traits, [ExcelArgument(Description = "trigger ")] object trigger) { if (ExcelUtil.CallFromWizard()) { return(""); } string callerAddress = ""; callerAddress = ExcelUtil.getActiveCellAddress(); try { if (exdate == DateTime.MinValue) { throw new Exception("Date must not be empty. "); } if (ExcelUtil.isNull(daycounter)) { daycounter = "ACTUAL365"; } if (ExcelUtil.isNull(calendar)) { calendar = "NYC"; } if (ExcelUtil.isNull(traits)) { traits = "pr"; } Option.Type optiontype; if (optype.ToUpper() == "CALL") { optiontype = Option.Type.Call; } else if (optype.ToUpper() == "PUT") { optiontype = Option.Type.Put; } else { throw new Exception("Unknow option type"); } EliteQuant.Calendar cal = EliteQuant.EQConverter.ConvertObject <EliteQuant.Calendar>(calendar); EliteQuant.DayCounter dc = EliteQuant.EQConverter.ConvertObject <EliteQuant.DayCounter>(daycounter); EliteQuant.Date maturitydate = EliteQuant.EQConverter.ConvertObject <EliteQuant.Date>(exdate); EliteQuant.Date today = EliteQuant.Settings.instance().getEvaluationDate(); EliteQuant.Date settlementdate = today; // T+2 if (maturitydate.serialNumber() <= today.serialNumber()) { throw new Exception("Option already expired."); } YieldTermStructureHandle rTSH = new YieldTermStructureHandle( new FlatForward(settlementdate, riskfreerate, dc)); BlackVolTermStructureHandle flatVolTSH1 = new BlackVolTermStructureHandle( new BlackConstantVol(settlementdate, cal, vol1, dc)); BlackVolTermStructureHandle flatVolTSH2 = new BlackVolTermStructureHandle( new BlackConstantVol(settlementdate, cal, vol2, dc)); Quote qh1 = new SimpleQuote(spot1); Quote qh2 = new SimpleQuote(spot2); QuoteHandle s1 = new QuoteHandle(qh1); QuoteHandle s2 = new QuoteHandle(qh2); BlackProcess p1 = new BlackProcess(s1, rTSH, flatVolTSH1); BlackProcess p2 = new BlackProcess(s2, rTSH, flatVolTSH2); StochasticProcessVector spv = new StochasticProcessVector(2); spv.Add(p1); spv.Add(p2); Matrix corrmtrx = new Matrix(2, 2); corrmtrx.set(0, 0, 1.0); corrmtrx.set(1, 1, 1.0); corrmtrx.set(0, 1, corr); corrmtrx.set(1, 0, corr); StochasticProcessArray spa = new StochasticProcessArray(spv, corrmtrx); PricingEngine engine = new MCEuropeanBasketEngine(spa, traits, 100, 1, false, true, 5000, 1e-6); Payoff payoff1 = new PlainVanillaPayoff(optiontype, stirkeprice); Payoff payoff2 = new SpreadBasketPayoff(payoff1); Exercise exercise = new EuropeanExercise(maturitydate); BasketOption bo = new BasketOption(payoff2, exercise); bo.setPricingEngine(engine); // Store the option and return its id string id = "OPTION@" + ObjectId; OHRepository.Instance.storeObject(id, bo, callerAddress); id += "#" + (String)DateTime.Now.ToString(@"HH:mm:ss"); return(id); } catch (Exception e) { ExcelUtil.logError(callerAddress, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString(), e.Message); return("#EQ_ERR!"); } }