def test_FinEquityChooserOptionDerivicom():
    '''http://derivicom.com/support/finoptionsxl/index.html?complex_chooser.htm '''

    valueDate = FinDate(1, 1, 2007)
    chooseDate = FinDate(1, 2, 2007)
    callExpiryDate = FinDate(1, 4, 2007)
    putExpiryDate = FinDate(1, 5, 2007)
    callStrike = 40.0
    putStrike = 35.0
    stockPrice = 38.0
    volatility = 0.20
    interestRate = 0.08
    dividendYield = 0.0625

    model = FinEquityModelBlackScholes(volatility)
    discountCurve = FinDiscountCurveFlat(valueDate, interestRate)

    chooserOption = FinEquityChooserOption(chooseDate, callExpiryDate,
                                           putExpiryDate, callStrike,
                                           putStrike)

    v = chooserOption.value(valueDate, stockPrice, discountCurve,
                            dividendYield, model)

    v_mc = chooserOption.valueMC(valueDate, stockPrice, discountCurve,
                                 dividendYield, model, 20000)

    v_derivicom = 1.0989
    testCases.header("", "", "", "", "", "")
    testCases.print("FINANCEPY", v, "DERIVICOM", v_derivicom, "MC", v_mc)
def test_FinEquityChooserOptionMatlab():
    '''https://fr.mathworks.com/help/fininst/chooserbybls.html '''

    valueDate = FinDate(1, 6, 2007)
    chooseDate = FinDate(31, 8, 2007)
    callExpiryDate = FinDate(2, 12, 2007)
    putExpiryDate = FinDate(2, 12, 2007)
    callStrike = 60.0
    putStrike = 60.0
    stockPrice = 50.0
    volatility = 0.20
    interestRate = 0.10
    dividendYield = 0.05

    model = FinEquityModelBlackScholes(volatility)
    discountCurve = FinDiscountCurveFlat(valueDate, interestRate)

    chooserOption = FinEquityChooserOption(chooseDate, callExpiryDate,
                                           putExpiryDate, callStrike,
                                           putStrike)

    v = chooserOption.value(valueDate, stockPrice, discountCurve,
                            dividendYield, model)

    v_mc = chooserOption.valueMC(valueDate, stockPrice, discountCurve,
                                 dividendYield, model, 20000)

    v_matlab = 8.9308
    testCases.header("", "", "", "", "", "")
    testCases.print("FINANCEPY", v, "MATLAB", v_matlab, "MC", v_mc)
def test_FinEquityChooserOptionHaug():
    ''' Following example in Haug Page 130 '''

    valueDate = FinDate(1, 1, 2015)
    chooseDate = FinDate(2, 4, 2015)
    callExpiryDate = FinDate(1, 7, 2015)
    putExpiryDate = FinDate(2, 8, 2015)
    callStrike = 55.0
    putStrike = 48.0
    stockPrice = 50.0
    volatility = 0.35
    interestRate = 0.10
    dividendYield = 0.05

    model = FinEquityModelBlackScholes(volatility)
    discountCurve = FinDiscountCurveFlat(valueDate, interestRate)

    chooserOption = FinEquityChooserOption(chooseDate, callExpiryDate,
                                           putExpiryDate, callStrike,
                                           putStrike)

    v = chooserOption.value(valueDate, stockPrice, discountCurve,
                            dividendYield, model)

    v_mc = chooserOption.valueMC(valueDate, stockPrice, discountCurve,
                                 dividendYield, model, 20000)

    v_haug = 6.0508
    testCases.header("", "", "", "", "", "")
    testCases.print("FINANCEPY", v, "HAUG", v_haug, "MC", v_mc)
def test_FinEquityCliquetOptionHaug():
    ''' Following example in Haug Page 130 '''

    startDate = FinDate(1, 1, 2015)
    finalExpiryDate = FinDate(1, 1, 2017)
    frequencyType = FinFrequencyTypes.QUARTERLY
    optionType = FinOptionTypes.EUROPEAN_CALL

    cliquetOption = FinEquityCliquetOption(startDate, finalExpiryDate,
                                           optionType, frequencyType)

    valueDate = FinDate(1, 8, 2016)
    stockPrice = 50.0
    volatility = 0.35
    interestRate = 0.10
    dividendYield = 0.05
    model = FinEquityModelBlackScholes(volatility)
    discountCurve = FinDiscountCurveFlat(valueDate, interestRate)

    v = cliquetOption.value(valueDate, stockPrice, discountCurve,
                            dividendYield, model)

    testCases.print("FINANCEPY", v)
Exemple #5
0
def testConvergence():

    valueDate = FinDate(2014, 1, 1)
    startAveragingDate = FinDate(2014, 6, 1)
    expiryDate = FinDate(2015, 1, 1)
    stockPrice = 100.0
    volatility = 0.20
    interestRate = 0.30
    dividendYield = 0.10
    numObservations = 120  # daily as we have a half year
    accruedAverage = None
    K = 100
    seed = 1976

    model = FinEquityModelBlackScholes(volatility)
    discountCurve = FinDiscountCurveFlat(valueDate, interestRate)

    asianOption = FinEquityAsianOption(startAveragingDate, expiryDate, K,
                                       FinOptionTypes.EUROPEAN_CALL,
                                       numObservations)

    testCases.header("K", "Geometric", "Turnbull_Wakeman", "Curran", "FastMC",
                     "FastMC_CV")

    valuesTurnbull = []
    valuesCurran = []
    valuesGeometric = []
    valuesMC_fast = []
    valuesMC_CV = []

    numPathsList = [5000]

    for numPaths in numPathsList:

        accruedAverage = stockPrice * 1.1

        valueMC_fast = asianOption._valueMC_fast(valueDate, stockPrice,
                                                 discountCurve, dividendYield,
                                                 model, numPaths, seed,
                                                 accruedAverage)

        valueMC_CV = asianOption.valueMC(valueDate, stockPrice, discountCurve,
                                         dividendYield, model, numPaths, seed,
                                         accruedAverage)

        valueGeometric = asianOption.value(
            valueDate, stockPrice, discountCurve, dividendYield, model,
            FinAsianOptionValuationMethods.GEOMETRIC, accruedAverage)

        valueTurnbullWakeman = asianOption.value(
            valueDate, stockPrice, discountCurve, dividendYield, model,
            FinAsianOptionValuationMethods.TURNBULL_WAKEMAN, accruedAverage)

        valueCurran = asianOption.value(valueDate, stockPrice, discountCurve,
                                        dividendYield, model,
                                        FinAsianOptionValuationMethods.CURRAN,
                                        accruedAverage)

        valuesGeometric.append(valueGeometric)
        valuesTurnbull.append(valueTurnbullWakeman)
        valuesCurran.append(valueCurran)
        valuesMC_fast.append(valueMC_fast)
        valuesMC_CV.append(valueMC_CV)

        testCases.print(numPaths, valueGeometric, valueTurnbullWakeman,
                        valueCurran, valueMC_fast, valueMC_CV)
Exemple #6
0
def testMCTimings():

    valueDate = FinDate(2014, 1, 1)
    startAveragingDate = FinDate(2014, 6, 1)
    expiryDate = FinDate(2015, 1, 1)
    stockPrice = 100.0
    volatility = 0.20
    interestRate = 0.30
    dividendYield = 0.10
    numObservations = 120  # daily as we have a half year
    accruedAverage = None
    K = 100
    seed = 1976

    model = FinEquityModelBlackScholes(volatility)
    discountCurve = FinDiscountCurveFlat(valueDate, interestRate)

    asianOption = FinEquityAsianOption(startAveragingDate, expiryDate, K,
                                       FinOptionTypes.EUROPEAN_CALL,
                                       numObservations)

    testCases.header("NUMPATHS", "VALUE", "TIME", "VALUE_MC", "TIME",
                     "VALUE_MC_CV", "TIME")

    valuesMC = []
    valuesMC_fast = []
    valuesMC_fast_CV = []

    tvaluesMC = []
    tvaluesMC_fast = []
    tvaluesMC_fast_CV = []

    numPathsList = [5000]

    for numPaths in numPathsList:

        accruedAverage = stockPrice * 1.1

        start = time.time()
        valueMC = asianOption.valueMC(valueDate, stockPrice, discountCurve,
                                      dividendYield, model, numPaths, seed,
                                      accruedAverage)

        end = time.time()
        t_MC = end - start

        start = time.time()
        valueMC_fast = asianOption._valueMC_fast(valueDate, stockPrice,
                                                 discountCurve, dividendYield,
                                                 model, numPaths, seed,
                                                 accruedAverage)

        end = time.time()
        t_MC_fast = end - start

        start = time.time()
        valueMC_fast_CV = asianOption.valueMC(valueDate, stockPrice,
                                              discountCurve, dividendYield,
                                              model, numPaths, seed,
                                              accruedAverage)

        end = time.time()
        t_MC_fast_CV = end - start

        valuesMC.append(valueMC)
        valuesMC_fast.append(valueMC_fast)
        valuesMC_fast_CV.append(valueMC_fast_CV)

        tvaluesMC.append(t_MC)
        tvaluesMC_fast.append(t_MC_fast)
        tvaluesMC_fast_CV.append(t_MC_fast_CV)

        testCases.print(numPaths, valueMC, t_MC, valueMC_fast, t_MC_fast,
                        valueMC_fast_CV, t_MC_fast_CV)
Exemple #7
0
def testTimeEvolution():

    startAveragingDate = FinDate(2015, 1, 1)
    expiryDate = FinDate(2016, 1, 1)
    stockPrice = 100.0
    volatility = 0.20
    interestRate = 0.30
    dividendYield = 0.10
    numObservations = 100  # weekly as we have a year
    accruedAverage = None
    K = 100
    seed = 1976

    model = FinEquityModelBlackScholes(volatility)

    asianOption = FinEquityAsianOption(startAveragingDate, expiryDate, K,
                                       FinOptionTypes.EUROPEAN_CALL,
                                       numObservations)

    testCases.header("Date", "Geometric", "Turnbull_Wakeman", "Curran",
                     "FastMC", "FastMC_CV")

    valuesTurnbull = []
    valuesCurran = []
    valuesGeometric = []
    valuesMC_fast = []
    valuesMC_CV = []

    valueDates = []
    valueDates.append(FinDate(2014, 4, 1))
    valueDates.append(FinDate(2014, 6, 1))
    valueDates.append(FinDate(2014, 8, 1))
    valueDates.append(FinDate(2015, 2, 1))
    valueDates.append(FinDate(2015, 4, 1))
    valueDates.append(FinDate(2015, 6, 1))
    valueDates.append(FinDate(2015, 8, 1))

    numPaths = 10000

    for valueDate in valueDates:

        accruedAverage = stockPrice * 0.9

        discountCurve = FinDiscountCurveFlat(valueDate, interestRate)

        valueMC_fast = asianOption._valueMC_fast(valueDate, stockPrice,
                                                 discountCurve, dividendYield,
                                                 model, numPaths, seed,
                                                 accruedAverage)

        valueMC_CV = asianOption.valueMC(valueDate, stockPrice, discountCurve,
                                         dividendYield, model, numPaths, seed,
                                         accruedAverage)

        valueGeometric = asianOption.value(
            valueDate, stockPrice, discountCurve, dividendYield, model,
            FinAsianOptionValuationMethods.GEOMETRIC, accruedAverage)

        valueTurnbullWakeman = asianOption.value(
            valueDate, stockPrice, discountCurve, dividendYield, model,
            FinAsianOptionValuationMethods.TURNBULL_WAKEMAN, accruedAverage)

        valueCurran = asianOption.value(valueDate, stockPrice, discountCurve,
                                        dividendYield, model,
                                        FinAsianOptionValuationMethods.CURRAN,
                                        accruedAverage)

        valuesGeometric.append(valueGeometric)
        valuesTurnbull.append(valueTurnbullWakeman)
        valuesCurran.append(valueCurran)
        valuesMC_fast.append(valueMC_fast)
        valuesMC_CV.append(valueMC_CV)

        testCases.print(str(valueDate), valueGeometric, valueTurnbullWakeman,
                        valueCurran, valueMC_fast, valueMC_CV)
Exemple #8
0
def test_FinEquityCompoundOption():

    valueDate = FinDate(1, 1, 2015)
    expiryDate1 = FinDate(1, 1, 2017)
    expiryDate2 = FinDate(1, 1, 2018)
    k1 = 5.0
    k2 = 95.0
    stockPrice = 85.0
    volatility = 0.15
    interestRate = 0.035
    dividendYield = 0.01

    model = FinEquityModelBlackScholes(volatility)
    discountCurve = FinDiscountCurveFlat(valueDate, interestRate)

    optionType1 = FinOptionTypes.EUROPEAN_CALL
    optionType2 = FinOptionTypes.EUROPEAN_PUT

    numStepsList = [
        200, 300, 400, 500, 600, 700, 800, 900, 1000, 2000, 3000, 4000, 5000
    ]

    cmpdOption = FinEquityCompoundOption(expiryDate1, optionType1, k1,
                                         expiryDate2, optionType2, k2)
    stockPrice = 85.0

    testCases.header("TYPE1", "TYPE2", "K1", "K2", "S", "Exact", "TreeSteps",
                     "TreeValue")
    for numSteps in numStepsList:

        value = cmpdOption.value(valueDate, stockPrice, discountCurve,
                                 dividendYield, model)
        values = cmpdOption._valueTree(valueDate, stockPrice, discountCurve,
                                       dividendYield, model, numSteps)
        testCases.print(optionType1, optionType2, k1, k2, stockPrice, value,
                        numSteps, values[0])

    testCases.header("TYPE1", "TYPE2", "K1", "K2", "S", "Exact", "TreeSteps",
                     "TreeValue", "Diff", "DELTA", "GAMMA", "THETA")

    for optionType1 in [
            FinOptionTypes.EUROPEAN_CALL, FinOptionTypes.EUROPEAN_PUT
    ]:
        for optionType2 in [
                FinOptionTypes.EUROPEAN_CALL, FinOptionTypes.EUROPEAN_PUT
        ]:

            cmpdOption = FinEquityCompoundOption(expiryDate1, optionType1, k1,
                                                 expiryDate2, optionType2, k2)
            stockPrices = range(70, 100)

            for stockPrice in stockPrices:
                value = cmpdOption.value(valueDate, stockPrice, discountCurve,
                                         dividendYield, model)
                delta = cmpdOption.delta(valueDate, stockPrice, discountCurve,
                                         dividendYield, model)
                vega = cmpdOption.vega(valueDate, stockPrice, discountCurve,
                                       dividendYield, model)
                theta = cmpdOption.theta(valueDate, stockPrice, discountCurve,
                                         dividendYield, model)

                values = cmpdOption._valueTree(valueDate, stockPrice,
                                               discountCurve, dividendYield,
                                               model)

                diff = value - values[0]

                testCases.print(optionType1, optionType2, k1, k2, stockPrice,
                                value, numSteps, values[0], diff, delta, vega,
                                theta)
Exemple #9
0
def test_FinEquityVanillaOption():

    valueDate = FinDate(2015, 1, 1)
    expiryDate = FinDate(2015, 7, 1)
    stockPrice = 100
    volatility = 0.30
    interestRate = 0.05
    dividendYield = 0.01
    model = FinEquityModelBlackScholes(volatility)
    discountCurve = FinDiscountCurveFlat(valueDate, interestRate)

    numPathsList = [10000, 20000, 40000, 80000, 160000, 320000]

    testCases.header("NUMPATHS", "VALUE_BS", "VALUE_MC", "TIME")

    for numPaths in numPathsList:

        callOption = FinEquityVanillaOption(expiryDate, 100.0,
                                            FinOptionTypes.EUROPEAN_CALL)
        value = callOption.value(valueDate, stockPrice, discountCurve,
                                 dividendYield, model)
        start = time.time()
        valueMC = callOption.valueMC(valueDate, stockPrice, discountCurve,
                                     dividendYield, model, numPaths)
        end = time.time()
        duration = end - start
        testCases.print(numPaths, value, valueMC, duration)

###############################################################################

    stockPrices = range(80, 120, 2)
    numPaths = 100000

    testCases.header("NUMPATHS", "VALUE_BS", "VALUE_MC", "TIME")
    useSobol = True

    for stockPrice in stockPrices:

        callOption = FinEquityVanillaOption(expiryDate, 100.0,
                                            FinOptionTypes.EUROPEAN_CALL)
        value = callOption.value(valueDate, stockPrice, discountCurve,
                                 dividendYield, model)
        start = time.time()

        useSobol = False
        valueMC1 = callOption.valueMC(valueDate, stockPrice, discountCurve,
                                      dividendYield, model, numPaths, useSobol)

        useSobol = True
        valueMC2 = callOption.valueMC(valueDate, stockPrice, discountCurve,
                                      dividendYield, model, numPaths, useSobol)

        end = time.time()
        duration = end - start
        testCases.print(numPaths, value, valueMC, duration)

###############################################################################

    stockPrices = range(80, 120, 2)
    numPaths = 100000

    testCases.header("STOCK PRICE", "VALUE_BS", "VALUE_MC", "TIME")

    for stockPrice in stockPrices:

        putOption = FinEquityVanillaOption(expiryDate, 100.0,
                                           FinOptionTypes.EUROPEAN_PUT)
        value = putOption.value(valueDate, stockPrice, discountCurve,
                                dividendYield, model)
        start = time.time()
        valueMC = putOption.valueMC(valueDate, stockPrice, discountCurve,
                                    dividendYield, model, numPaths)
        end = time.time()
        duration = end - start
        testCases.print(stockPrice, value, valueMC, duration)

###############################################################################

    stockPrices = range(80, 120, 2)

    testCases.header("STOCK PRICE", "VALUE_BS", "DELTA_BS", "VEGA_BS",
                     "THETA_BS", "RHO_BS")

    for stockPrice in stockPrices:
        callOption = FinEquityVanillaOption(expiryDate, 100.0,
                                            FinOptionTypes.EUROPEAN_CALL)
        value = callOption.value(valueDate, stockPrice, discountCurve,
                                 dividendYield, model)
        delta = callOption.delta(valueDate, stockPrice, discountCurve,
                                 dividendYield, model)
        vega = callOption.vega(valueDate, stockPrice, discountCurve,
                               dividendYield, model)
        theta = callOption.theta(valueDate, stockPrice, discountCurve,
                                 dividendYield, model)
        # callOption.rho(valueDate,stockPrice, interestRate, dividendYield, modelType, modelParams)
        rho = 999
        testCases.print(stockPrice, value, delta, vega, theta, rho)

    testCases.header("STOCK PRICE", "VALUE_BS", "DELTA_BS", "VEGA_BS",
                     "THETA_BS", "RHO_BS")

    for stockPrice in stockPrices:
        putOption = FinEquityVanillaOption(expiryDate, 100.0,
                                           FinOptionTypes.EUROPEAN_PUT)
        value = putOption.value(valueDate, stockPrice, discountCurve,
                                dividendYield, model)
        delta = putOption.delta(valueDate, stockPrice, discountCurve,
                                dividendYield, model)
        vega = putOption.vega(valueDate, stockPrice, discountCurve,
                              dividendYield, model)
        theta = putOption.theta(valueDate, stockPrice, discountCurve,
                                dividendYield, model)
        # putOption.rho(valueDate,stockPrice, interestRate, dividendYield,
        # modelType, modelParams)
        rho = 999
        testCases.print(stockPrice, value, delta, vega, theta, rho)

###############################################################################

    testCases.header("STOCK PRICE", "VALUE_BS", "VOL_IN", "IMPLD_VOL")

    stockPrices = range(60, 150, 2)

    for stockPrice in stockPrices:
        callOption = FinEquityVanillaOption(expiryDate, 100.0,
                                            FinOptionTypes.EUROPEAN_CALL)
        value = callOption.value(valueDate, stockPrice, discountCurve,
                                 dividendYield, model)
        impliedVol = callOption.impliedVolatility(valueDate, stockPrice,
                                                  discountCurve, dividendYield,
                                                  value)
        testCases.print(stockPrice, value, volatility, impliedVol)
def test_FinEquityDigitalOption():

    underlyingType = FinDigitalOptionTypes.CASH_OR_NOTHING

    valueDate = FinDate(2015, 1, 1)
    expiryDate = FinDate(2016, 1, 1)
    stockPrice = 100.0
    volatility = 0.30
    interestRate = 0.05
    dividendYield = 0.01
    discountCurve = FinDiscountCurveFlat(valueDate, interestRate)
    model = FinEquityModelBlackScholes(volatility)
    import time

    callOptionValues = []
    callOptionValuesMC = []
    numPathsList = [
        10000, 20000, 40000, 80000, 160000, 320000, 640000, 1280000, 2560000
    ]

    testCases.header("NumLoops", "ValueBS", "ValueMC", "TIME")

    for numPaths in numPathsList:

        callOption = FinEquityDigitalOption(expiryDate, 100.0,
                                            FinOptionTypes.EUROPEAN_CALL,
                                            underlyingType)
        value = callOption.value(valueDate, stockPrice, discountCurve,
                                 dividendYield, model)
        start = time.time()
        valueMC = callOption.valueMC(valueDate, stockPrice, discountCurve,
                                     dividendYield, model, numPaths)
        end = time.time()
        duration = end - start
        testCases.print(numPaths, value, valueMC, duration)

        callOptionValues.append(value)
        callOptionValuesMC.append(valueMC)

#    plt.figure(figsize=(10,8))
#    plt.plot(numPathsList, callOptionValues, color = 'b', label="Call Option")
#    plt.plot(numPathsList, callOptionValuesMC, color = 'r', label = "Call Option MC")
#    plt.xlabel("Num Loops")
#    plt.legend(loc='best')

##########################################################################

    stockPrices = range(50, 150, 50)
    callOptionValues = []
    callOptionDeltas = []
    callOptionVegas = []
    callOptionThetas = []

    for stockPrice in stockPrices:
        callOption = FinEquityDigitalOption(expiryDate, 100.0,
                                            FinOptionTypes.EUROPEAN_CALL,
                                            underlyingType)
        value = callOption.value(valueDate, stockPrice, discountCurve,
                                 dividendYield, model)
        delta = callOption.delta(valueDate, stockPrice, discountCurve,
                                 dividendYield, model)
        vega = callOption.vega(valueDate, stockPrice, discountCurve,
                               dividendYield, model)
        theta = callOption.theta(valueDate, stockPrice, discountCurve,
                                 dividendYield, model)
        callOptionValues.append(value)
        callOptionDeltas.append(delta)
        callOptionVegas.append(vega)
        callOptionThetas.append(theta)

    putOptionValues = []
    putOptionDeltas = []
    putOptionVegas = []
    putOptionThetas = []

    for stockPrice in stockPrices:
        putOption = FinEquityDigitalOption(expiryDate, 100.0,
                                           FinOptionTypes.EUROPEAN_PUT,
                                           underlyingType)
        value = putOption.value(valueDate, stockPrice, discountCurve,
                                dividendYield, model)
        delta = putOption.delta(valueDate, stockPrice, discountCurve,
                                dividendYield, model)
        vega = putOption.vega(valueDate, stockPrice, discountCurve,
                              dividendYield, model)
        theta = putOption.theta(valueDate, stockPrice, discountCurve,
                                dividendYield, model)
        putOptionValues.append(value)
        putOptionDeltas.append(delta)
        putOptionVegas.append(vega)
        putOptionThetas.append(theta)
def test_FinEquityBarrierOption():

    valueDate = FinDate(2015, 1, 1)
    expiryDate = FinDate(2016, 1, 1)
    stockPrice = 100.0
    volatility = 0.20
    interestRate = 0.05
    dividendYield = 0.02
    optionType = FinEquityBarrierTypes.DOWN_AND_OUT_CALL

    drift = interestRate - dividendYield
    scheme = FinGBMNumericalScheme.NORMAL
    processType = FinProcessTypes.GBM
    discountCurve = FinDiscountCurveFlat(valueDate, interestRate)
    model = FinEquityModelBlackScholes(volatility)

    #######################################################################

    import time
    start = time.time()
    numObservationsPerYear = 100

    testCases.header("Type", "K", "B", "S:", "Value:", "ValueMC", "Diff",
                     "TIME")

    for optionType in FinEquityBarrierTypes:
        for stockPrice in range(80, 120, 10):

            B = 110.0
            K = 100.0

            option = FinEquityBarrierOption(expiryDate, K, optionType, B,
                                            numObservationsPerYear)
            value = option.value(valueDate, stockPrice, discountCurve,
                                 dividendYield, model)
            start = time.time()
            modelParams = (stockPrice, drift, volatility, scheme)
            valueMC = option.valueMC(valueDate, stockPrice, discountCurve,
                                     processType, modelParams)

            end = time.time()
            timeElapsed = round(end - start, 3)
            diff = valueMC - value

            testCases.print(optionType, K, B, stockPrice, value, valueMC, diff,
                            timeElapsed)

        for stockPrice in range(80, 120, 10):

            B = 100.0
            K = 110.0

            option = FinEquityBarrierOption(expiryDate, K, optionType, B,
                                            numObservationsPerYear)
            value = option.value(valueDate, stockPrice, discountCurve,
                                 dividendYield, model)
            start = time.time()
            modelParams = (stockPrice, drift, volatility, scheme)
            valueMC = option.valueMC(valueDate, stockPrice, discountCurve,
                                     processType, modelParams)
            end = time.time()
            timeElapsed = round(end - start, 3)
            diff = valueMC - value

            testCases.print(optionType, K, B, stockPrice, value, valueMC, diff,
                            timeElapsed)

        end = time.time()


##########################################################################

    stockPrices = range(50, 150, 50)
    B = 105.0

    testCases.header("Type", "K", "B", "S:", "Value", "Delta", "Vega", "Theta")

    for optionType in FinEquityBarrierTypes:

        for stockPrice in stockPrices:

            barrierOption = FinEquityBarrierOption(expiryDate, 100.0,
                                                   optionType, B,
                                                   numObservationsPerYear)

            value = barrierOption.value(valueDate, stockPrice, discountCurve,
                                        dividendYield, model)
            delta = barrierOption.delta(valueDate, stockPrice, discountCurve,
                                        dividendYield, model)
            vega = barrierOption.vega(valueDate, stockPrice, discountCurve,
                                      dividendYield, model)
            theta = barrierOption.theta(valueDate, stockPrice, discountCurve,
                                        dividendYield, model)

            testCases.print(optionType, K, B, stockPrice, value, delta, vega,
                            theta)
Exemple #12
0
def test_FinBinomialTree():

    stockPrice = 50.0
    riskFreeRate = 0.06
    dividendYield = 0.04
    volatility = 0.40

    valueDate = FinDate(2016, 1, 1)
    expiryDate = FinDate(2017, 1, 1)

    model = FinEquityModelBlackScholes(volatility)
    discountCurve = FinDiscountCurveFlat(valueDate, riskFreeRate)

    numStepsList = [100, 500, 1000, 2000, 5000]

    strikePrice = 50.0

    testCases.banner("================== EUROPEAN PUT =======================")

    putOption = FinEquityVanillaOption(expiryDate, strikePrice,
                                       FinOptionTypes.EUROPEAN_PUT)
    value = putOption.value(valueDate, stockPrice, discountCurve,
                            dividendYield, model)
    delta = putOption.delta(valueDate, stockPrice, discountCurve,
                            dividendYield, model)
    gamma = putOption.gamma(valueDate, stockPrice, discountCurve,
                            dividendYield, model)
    theta = putOption.theta(valueDate, stockPrice, discountCurve,
                            dividendYield, model)
    testCases.header("BS Value", "BS Delta", "BS Gamma", "BS Theta")
    testCases.print(value, delta, gamma, theta)

    payoff = FinEquityTreePayoffTypes.VANILLA_OPTION
    exercise = FinEquityTreeExerciseTypes.EUROPEAN
    params = np.array([-1, strikePrice])

    testCases.header("NumSteps", "Results", "TIME")

    for numSteps in numStepsList:
        start = time.time()
        tree = FinEquityBinomialTree()
        results = tree.value(stockPrice, discountCurve, dividendYield,
                             volatility, numSteps, valueDate, payoff,
                             expiryDate, payoff, exercise, params)
        end = time.time()
        duration = end - start
        testCases.print(numSteps, results, duration)

    testCases.banner("================== AMERICAN PUT =======================")

    payoff = FinEquityTreePayoffTypes.VANILLA_OPTION
    exercise = FinEquityTreeExerciseTypes.AMERICAN
    params = np.array([-1, strikePrice])

    testCases.header("NumSteps", "Results", "TIME")

    for numSteps in numStepsList:
        start = time.time()
        tree = FinEquityBinomialTree()
        results = tree.value(stockPrice, discountCurve, dividendYield,
                             volatility, numSteps, valueDate, payoff,
                             expiryDate, payoff, exercise, params)
        end = time.time()
        duration = end - start
        testCases.print(numSteps, results, duration)

    testCases.banner(
        "================== EUROPEAN CALL =======================")

    callOption = FinEquityVanillaOption(expiryDate, strikePrice,
                                        FinOptionTypes.EUROPEAN_CALL)
    value = callOption.value(valueDate, stockPrice, discountCurve,
                             dividendYield, model)
    delta = callOption.delta(valueDate, stockPrice, discountCurve,
                             dividendYield, model)
    gamma = callOption.gamma(valueDate, stockPrice, discountCurve,
                             dividendYield, model)
    theta = callOption.theta(valueDate, stockPrice, discountCurve,
                             dividendYield, model)
    testCases.header("BS Value", "BS Delta", "BS Gamma", "BS Theta")
    testCases.print(value, delta, gamma, theta)

    payoff = FinEquityTreePayoffTypes.VANILLA_OPTION
    exercise = FinEquityTreeExerciseTypes.EUROPEAN
    params = np.array([1.0, strikePrice])

    testCases.header("NumSteps", "Results", "TIME")
    for numSteps in numStepsList:
        start = time.time()
        tree = FinEquityBinomialTree()

        results = tree.value(stockPrice, discountCurve, dividendYield,
                             volatility, numSteps, valueDate, payoff,
                             expiryDate, payoff, exercise, params)

        end = time.time()
        duration = end - start
        testCases.print(numSteps, results, duration)

    testCases.banner(
        "================== AMERICAN CALL =======================")

    payoff = FinEquityTreePayoffTypes.VANILLA_OPTION
    exercise = FinEquityTreeExerciseTypes.AMERICAN
    params = np.array([1.0, strikePrice])

    testCases.header("NumSteps", "Results", "TIME")
    for numSteps in numStepsList:
        start = time.time()
        tree = FinEquityBinomialTree()

        results = tree.value(stockPrice, discountCurve, dividendYield,
                             volatility, numSteps, valueDate, payoff,
                             expiryDate, payoff, exercise, params)

        end = time.time()
        duration = end - start
        testCases.print(numSteps, results, duration)
Exemple #13
0
def test_FinEquityOneTouchOption():
    # Examples Haug Page 180 Table 4-22
    # Agreement not exact at t is not exactly 0.50

    valueDate = FinDate(1, 1, 2016)
    expiryDate = FinDate(2, 7, 2016)
    interestRate = 0.10
    discountCurve = FinDiscountCurveFlat(valueDate, interestRate)
    volatility = 0.20
    barrierLevel = 100.0  # H
    model = FinEquityModelBlackScholes(volatility)
    dividendYield = 0.03
    numPaths = 10000
    numStepsPerYear = 252

    stockPrice = 105.0
    paymentSize = 15.0

    testCases.header("================================= CASH ONLY")

    downTypes = [
        FinTouchOptionPayoffTypes.DOWN_AND_IN_CASH_AT_HIT,
        FinTouchOptionPayoffTypes.DOWN_AND_IN_CASH_AT_EXPIRY,
        FinTouchOptionPayoffTypes.DOWN_AND_OUT_CASH_OR_NOTHING
    ]

    testCases.header("TYPE", "VALUE", "VALUE_MC")

    for downType in downTypes:

        option = FinEquityOneTouchOption(expiryDate, downType, barrierLevel,
                                         paymentSize)

        v = option.value(valueDate, stockPrice, discountCurve, dividendYield,
                         model)

        v_mc = option.valueMC(valueDate, stockPrice, discountCurve,
                              dividendYield, model, numStepsPerYear, numPaths)

        testCases.print("%60s " % downType, "%9.5f" % v, "%9.5f" % v_mc)

    stockPrice = 95.0
    paymentSize = 15.0

    upTypes = [
        FinTouchOptionPayoffTypes.UP_AND_IN_CASH_AT_HIT,
        FinTouchOptionPayoffTypes.UP_AND_IN_CASH_AT_EXPIRY,
        FinTouchOptionPayoffTypes.UP_AND_OUT_CASH_OR_NOTHING
    ]

    testCases.header("TYPE", "VALUE", "VALUE_MC")

    for upType in upTypes:

        option = FinEquityOneTouchOption(expiryDate, upType, barrierLevel,
                                         paymentSize)

        v = option.value(valueDate, stockPrice, discountCurve, dividendYield,
                         model)

        v_mc = option.valueMC(valueDate, stockPrice, discountCurve,
                              dividendYield, model, numStepsPerYear, numPaths)

        testCases.print("%60s " % upType, "%9.5f" % v, "%9.5f" % v_mc)

    ###########################################################################

    stockPrice = 105.0

    testCases.banner("================= ASSET ONLY")

    downTypes = [
        FinTouchOptionPayoffTypes.DOWN_AND_IN_ASSET_AT_HIT,
        FinTouchOptionPayoffTypes.DOWN_AND_IN_ASSET_AT_EXPIRY,
        FinTouchOptionPayoffTypes.DOWN_AND_OUT_ASSET_OR_NOTHING
    ]

    testCases.header("TYPE", "VALUE", "VALUE_MC")
    for downType in downTypes:

        option = FinEquityOneTouchOption(expiryDate, downType, barrierLevel)

        v = option.value(valueDate, stockPrice, discountCurve, dividendYield,
                         model)

        v_mc = option.valueMC(valueDate, stockPrice, discountCurve,
                              dividendYield, model, numStepsPerYear, numPaths)

        testCases.print("%60s " % downType, "%9.5f" % v, "%9.5f" % v_mc)

    stockPrice = 95.0

    upTypes = [
        FinTouchOptionPayoffTypes.UP_AND_IN_ASSET_AT_HIT,
        FinTouchOptionPayoffTypes.UP_AND_IN_ASSET_AT_EXPIRY,
        FinTouchOptionPayoffTypes.UP_AND_OUT_ASSET_OR_NOTHING
    ]

    for upType in upTypes:

        option = FinEquityOneTouchOption(expiryDate, upType, barrierLevel)

        v = option.value(valueDate, stockPrice, discountCurve, dividendYield,
                         model)

        v_mc = option.valueMC(valueDate, stockPrice, discountCurve,
                              dividendYield, model, numStepsPerYear, numPaths)

        testCases.print("%60s " % upType, "%9.5f" % v, "%9.5f" % v_mc)
Exemple #14
0
def testFinEquityAmericanOption():

    valueDate = FinDate(2016, 1, 1)
    expiryDate = FinDate(2017, 1, 1)
    stockPrice = 50.0
    interestRate = 0.06
    dividendYield = 0.04
    volatility = 0.40
    strikePrice = 50.0

    model = FinEquityModelBlackScholes(volatility)
    discountCurve = FinDiscountCurveFlat(valueDate, interestRate)

    numStepsList = [100, 200, 500, 1000, 2000]

    testCases.banner("================== EUROPEAN PUT =======================")

    putOption = FinEquityAmericanOption(expiryDate, strikePrice,
                                        FinOptionTypes.EUROPEAN_PUT)
    value = putOption.value(valueDate, stockPrice, discountCurve,
                            dividendYield, model)
    delta = putOption.delta(valueDate, stockPrice, discountCurve,
                            dividendYield, model)
    gamma = putOption.gamma(valueDate, stockPrice, discountCurve,
                            dividendYield, model)
    theta = putOption.theta(valueDate, stockPrice, discountCurve,
                            dividendYield, model)

    testCases.header("OPTION_TYPE", "VALUE", "DELTA", "GAMMA", "THETA")
    testCases.print("EUROPEAN_PUT_BS", value, delta, gamma, theta)

    option = FinEquityAmericanOption(expiryDate, strikePrice,
                                     FinOptionTypes.EUROPEAN_PUT)

    testCases.header("OPTION_TYPE", "NUMSTEPS", "VALUE DELTA GAMMA THETA",
                     "TIME")

    for numSteps in numStepsList:

        model = FinEquityModelBlackScholes(volatility, numSteps, True)
        start = time.time()
        results = option.value(valueDate, stockPrice, discountCurve,
                               dividendYield, model)
        end = time.time()
        duration = end - start
        testCases.print("EUROPEAN_PUT_TREE", numSteps, results, duration)

    testCases.banner("================== AMERICAN PUT =======================")

    option = FinEquityAmericanOption(expiryDate, strikePrice,
                                     FinOptionTypes.AMERICAN_PUT)

    testCases.header("OPTION_TYPE", "NUMSTEPS", "VALUE DELTA GAMMA THETA",
                     "TIME")

    for numSteps in numStepsList:

        model = FinEquityModelBlackScholes(volatility, numSteps)
        start = time.time()
        results = option.value(valueDate, stockPrice, discountCurve,
                               dividendYield, model)
        end = time.time()
        duration = end - start
        testCases.print("AMERICAN_PUT", numSteps, results, duration)

    testCases.banner(
        "================== EUROPEAN CALL =======================")

    callOption = FinEquityAmericanOption(expiryDate, strikePrice,
                                         FinOptionTypes.EUROPEAN_CALL)
    value = callOption.value(valueDate, stockPrice, discountCurve,
                             dividendYield, model)
    delta = callOption.delta(valueDate, stockPrice, discountCurve,
                             dividendYield, model)
    gamma = callOption.gamma(valueDate, stockPrice, discountCurve,
                             dividendYield, model)
    theta = callOption.theta(valueDate, stockPrice, discountCurve,
                             dividendYield, model)

    testCases.header("OPTION_TYPE", "VALUE", "DELTA", "GAMMA", "THETA")
    testCases.print("EUROPEAN_CALL_BS", value, delta, gamma, theta)

    option = FinEquityAmericanOption(expiryDate, strikePrice,
                                     FinOptionTypes.EUROPEAN_CALL)

    testCases.header("OPTION_TYPE", "NUMSTEPS", "VALUE DELTA GAMMA THETA",
                     "TIME")

    for numSteps in numStepsList:
        model = FinEquityModelBlackScholes(volatility, numSteps, True)
        start = time.time()
        results = option.value(valueDate, stockPrice, discountCurve,
                               dividendYield, model)
        end = time.time()
        duration = end - start
        testCases.print("EUROPEAN_CALL_TREE", numSteps, results, duration)

    testCases.banner(
        "================== AMERICAN CALL =======================")
    testCases.header("OPTION_TYPE", "NUMSTEPS", "VALUE DELTA GAMMA THETA",
                     "TIME")

    option = FinEquityAmericanOption(expiryDate, strikePrice,
                                     FinOptionTypes.AMERICAN_CALL)

    for numSteps in numStepsList:
        model = FinEquityModelBlackScholes(volatility, numSteps)
        start = time.time()
        results = option.value(valueDate, stockPrice, discountCurve,
                               dividendYield, model)
        end = time.time()
        duration = end - start
        testCases.print("AMERICAN_CALL", numSteps, results, duration)