示例#1
0
 def __call__(self, barrierType, barrier, rebate):
     '''计算PayOff与Exercise,定义Option为QuantLib标准BarrierOption对象'''
     payoff, exercise, process = self.PreWork(
     )  # Vanilla Payoff, European/American, BSM process
     option = ql.BarrierOption(barrierType, barrier, rebate, payoff,
                               exercise)
     return option, process
示例#2
0
 def __init__(self, strike, maturitydt, optionType, barrier, barrierType):
     self.strike = strike
     self.maturitydt = maturitydt
     self.optionType = optionType
     self.barrier = barrier
     self.barrierType = barrierType
     exercise = ql.EuropeanExercise(maturitydt)
     self.exercise = exercise
     payoff = ql.PlainVanillaPayoff(optionType, strike)
     self.payoff = payoff
     barrieroption = ql.BarrierOption(barrierType, barrier, 0.0, payoff,
                                      exercise)
     self.option_ql = barrieroption
示例#3
0
    def __init__(self, strike, maturitydt, optionType, barrier, barrierType):
        self.strike = strike
        self.maturitydt = maturitydt
        self.optionType = optionType
        self.barrier = barrier
        self.barrierType = barrierType
        exercise = ql.EuropeanExercise(maturitydt)
        self.exercise = exercise
        payoff = ql.PlainVanillaPayoff(optionType, strike)
        self.payoff = payoff
        barrieroption = ql.BarrierOption(barrierType, barrier, 0.0, payoff,
                                         exercise)
        self.option_ql = barrieroption


# class OptionPlainAsian:
#     def __init__(self, strike,effectivedt, maturitydt, optionType):
#         self.strike = strike
#         self.maturitydt = maturitydt
#         self.optionType = optionType
#         exercise = ql.EuropeanExercise(maturitydt)
#         payoff = ql.PlainVanillaPayoff(optionType, strike)
#         option = ql.DiscreteAveragingAsianOption(payoff, exercise,)
#         self.option_ql = option
示例#4
0
def BarrierOptionCalc(options):
    print(options)
    spot = ql.SimpleQuote(options["spot"])
    strike = options["strike"]
    barrier = options["barrier"]
    barrierType = options["barrierType"]
    volatility = ql.SimpleQuote(options["volatility"])
    maturity = options["maturity"]
    rf = ql.SimpleQuote(options["rf"])
    optionType = options["optionType"]
    pricingEngine = options["pricingEngine"]
    optionExercise = options["optionExercise"]

    if not hasattr(ql.Barrier, barrierType):
        raise Exception("barrier type not understood")
    barrierType = getattr(ql.Barrier, barrierType)

    if not hasattr(ql.Option, optionType):
        raise Exception("option type not understood")
    optionType = getattr(ql.Option, optionType)

    today = ql.Settings.instance().evaluationDate
    maturity_date = today + int(maturity)

    divYield = 0.
    rebate = 0.

    # process = ql.BlackScholesMertonProcess(
    #     ql.QuoteHandle(spot),
    #     ql.YieldTermStructureHandle(ql.FlatForward(today, divYield, day_count)),
    #     ql.YieldTermStructureHandle(ql.FlatForward(today, ql.QuoteHandle(rf), day_count)),
    #     ql.BlackVolTermStructureHandle(ql.BlackConstantVol(today, calendar, ql.QuoteHandle(volatility), day_count)))

    process = ql.BlackScholesProcess(
        ql.QuoteHandle(spot),
        ql.YieldTermStructureHandle(
            ql.FlatForward(today, ql.QuoteHandle(rf), day_count)),
        ql.BlackVolTermStructureHandle(
            ql.BlackConstantVol(today, calendar, ql.QuoteHandle(volatility),
                                day_count)))

    if (optionExercise == "European"):
        optionExercise = ql.EuropeanExercise(maturity_date)
    elif (optionExercise == "American"):
        optionExercise = ql.AmericanExercise(today + 1, maturity_date)
    else:
        raise Exception("optionExercise not understood")

    timeSteps = 1000
    gridPoints = 1000

    if (pricingEngine == "Analytical"):
        pricingEngine = ql.AnalyticBarrierEngine(process)
    elif (pricingEngine == "AnalyticalBinary"):
        pricingEngine = ql.AnalyticBinaryBarrierEngine(process)
    elif (pricingEngine == "FD"):
        pricingEngine = ql.FdBlackScholesBarrierEngine(process, timeSteps,
                                                       gridPoints)
    elif (pricingEngine == "MC"):
        pricingEngine = ql.MCBarrierEngine(process,
                                           'pseudorandom',
                                           timeSteps=1,
                                           requiredTolerance=0.02,
                                           seed=42)
    elif (pricingEngine == "Binomial"):
        pricingEngine = ql.BinomialBarrierEngine(process, 'jr', timeSteps)
    else:
        raise Exception("pricingEngine not understood")

    option = ql.BarrierOption(barrierType, barrier, rebate,
                              ql.PlainVanillaPayoff(optionType, strike),
                              optionExercise)

    option.setPricingEngine(pricingEngine)

    results = {}
    for t in ["NPV", "delta", "vega", "theta", "rho", "gamma"]:
        try:
            results[t] = getattr(option, t)()
        except RuntimeError as e:
            print(t, e)
            results[t] = float('nan')

    if math.isnan(results["NPV"]):
        return results
    computegreeks(results, option, spot, volatility, rf, today)
    return results
h1 = ql.QuoteHandle(riskFreeRate)
h2 = ql.QuoteHandle(volatility)
flatRate = ql.YieldTermStructureHandle(
    ql.FlatForward(0, ql.NullCalendar(), h1, dayCounter))
flatVol = ql.BlackVolTermStructureHandle(
    ql.BlackConstantVol(0, ql.NullCalendar(), h2, dayCounter))

# instantiate the option
exercise = ql.EuropeanExercise(maturity)
payoff = ql.PlainVanillaPayoff(type, strike)
bsProcess = ql.BlackScholesProcess(ql.QuoteHandle(underlying), flatRate,
                                   flatVol)
barrierEngine = ql.AnalyticBarrierEngine(bsProcess)
europeanEngine = ql.AnalyticEuropeanEngine(bsProcess)

referenceOption = ql.BarrierOption(barrierType, barrier, rebate, payoff,
                                   exercise)

referenceOption.setPricingEngine(barrierEngine)

referenceValue = referenceOption.NPV()

tab1.add_row(["Original barrier option", referenceValue, 'N/A'])

# Replicating portfolios
portfolio1 = ql.CompositeInstrument()
portfolio2 = ql.CompositeInstrument()
portfolio3 = ql.CompositeInstrument()

# Final payoff first (the same for all portfolios):
# as shown in Joshi, a put struck at K...
put1 = ql.EuropeanOption(payoff, exercise)
示例#6
0
maturity = 0.25
divYield = 0.0


V=[]
delta=[]

for s in S:
            
    underlying=s    
        
#    Grids = (5 , 10 , 25 , 50 , 100 , 1000 , 5000)
#    maxG = Grids [ -1]
#    
    today = ql.Settings.instance().evaluationDate
    maturity_date = today + int ( maturity * 12)
    process = ql.BlackScholesMertonProcess(ql.QuoteHandle(ql.SimpleQuote(underlying)) ,
    ql.YieldTermStructureHandle(ql.FlatForward(today , divYield , ql.Thirty360())) ,
    ql.YieldTermStructureHandle(ql.FlatForward(today , rf , ql.Thirty360())) ,
    ql.BlackVolTermStructureHandle(ql.BlackConstantVol(today,ql.NullCalendar() , sigma , ql.Thirty360 ())))
    option = ql.BarrierOption( barrierType , barrier , rebate , ql.PlainVanillaPayoff( optionType , strike), ql.EuropeanExercise(maturity_date))
    option.setPricingEngine(ql.AnalyticBarrierEngine(process))
    trueValue = option.NPV()
    #trueDelta=option.delta()w
    V.append(trueValue)
    #delta.append(trueDelta)

plt.plot(V)

#plt.plot(delta)
# print(black_var_surface.blackVol(ttm,2.495))
underlying = ql.SimpleQuote(daily_close)
# process = evaluation.get_bsmprocess(daycounter, underlying, black_var_surface)
vol = estimated_vols.get(to_dt_date(evalDate))
# vol = 0.12
process = evaluation.get_bsmprocess_cnstvol(daycounter, calendar, underlying,
                                            vol)
print(daily_close)
# barrierstrike = daily_close # 2.475
strike = 2.495
barrier = 2.35

exercise = ql.EuropeanExercise(maturitydt)
payoff = ql.PlainVanillaPayoff(optionType, strike)
barrieroption = ql.BarrierOption(barrierType, barrier, 0.0, payoff, exercise)
# barrieroption.setPricingEngine(ql.AnalyticBarrierEngine(process))
barrier_option = OptionBarrierEuropean(strike, maturitydt, optionType, barrier,
                                       barrierType)
# staticHedge = StaticHedgePortfolio(barrier_option)
# staticHedge.set_static_portfolio(evaluation,spot,black_var_surface)

strike_barrier = ((barrier)**2) / strike
strike_barrierforward = ((barrier * np.exp(rf * ttm))**2) / strike
strike_strikeforward = ((daily_close)**2) / strike

print(maturitydt)
print('init spot', daily_close)
print('underlying : ', daily_close)
print('barrier : ', barrier)
print('strike : ', strike)
示例#8
0
    opt_locvol_surface = ql.LocalVolSurface(
        ql.BlackVolTermStructureHandle(opt_var_surface), ts_rd_flat,
        ts_rq_flat, spot)
    opt_locvol_surface.enableExtrapolation()
    print("black vol: {0}\nloc vol: {1}".format(
        opt_var_surface.blackVol(0.9, 2.83),
        opt_locvol_surface.localVol(0.9, 2.83)))

    # to price a double no touch option value
    # 所有的upout downin的期权都是美式观察欧式交割的,行权方式选择European
    dnt = ql.DoubleBarrierOption(
        ql.DoubleBarrier.KnockOut, 2.7, 2.93, 0.0,
        ql.CashOrNothingPayoff(ql.Option.Call, 0.0, 1.0),
        ql.EuropeanExercise(ql.Date(18, 10, 2019)))
    upnt = ql.BarrierOption(ql.Barrier.UpOut, 2.93, 0.0,
                            ql.CashOrNothingPayoff(ql.Option.Call, 0.0, 1.0),
                            ql.EuropeanExercise(ql.Date(18, 10, 2019)))
    downnt = ql.BarrierOption(ql.Barrier.DownOut, 2.7, 0.0,
                              ql.CashOrNothingPayoff(ql.Option.Call, 0.0, 1.0),
                              ql.EuropeanExercise(ql.Date(18, 10, 2019)))
    european_option = ql.VanillaOption(
        ql.PlainVanillaPayoff(ql.Option.Call, 2.8),
        ql.EuropeanExercise(ql.Date(18, 10, 2019)))
    spot_handle = ql.QuoteHandle(ql.SimpleQuote(spot))
    bsm_process = ql.BlackScholesMertonProcess(
        spot_handle, ts_rq_flat, ts_rd_flat,
        ql.BlackVolTermStructureHandle(opt_var_surface))
    #    dnt.setPricingEngine(ql.BinomialBarrierEngine(bsm_process,'crr',400))
    #    upnt.setPricingEngine(ql.BinomialBarrierEngine(bsm_process,'crr',400))
    #    downnt.setPricingEngine(ql.BinomialBarrierEngine(bsm_process,'crr',400))