Example #1
0
def test_value_mc():
    #   Example from Hull 4th edition page 284
    valuation_date = Date(1, 1, 2015)
    expiry_date = valuation_date.add_months(4)
    spot_fx_rate = 1.60
    volatility = 0.1411
    dom_interest_rate = 0.08
    forInterestRate = 0.11
    model = BlackScholes(volatility)
    dom_discount_curve = DiscountCurveFlat(valuation_date, dom_interest_rate)
    for_discount_curve = DiscountCurveFlat(valuation_date, forInterestRate)
    num_paths = 100000

    strike_fx_rate = 1.6

    call_option = FXVanillaOption(expiry_date, strike_fx_rate, "EURUSD",
                                  OptionTypes.EUROPEAN_CALL, 1000000, "USD")

    value_mc = call_option.value_mc(valuation_date, spot_fx_rate,
                                    dom_discount_curve, for_discount_curve,
                                    model, num_paths)

    assert round(value_mc, 4) == 0.0429

    put_option = FXVanillaOption(expiry_date, strike_fx_rate, "EURUSD",
                                 OptionTypes.EUROPEAN_PUT, 1000000, "USD")

    value_mc = put_option.value_mc(valuation_date, spot_fx_rate,
                                   dom_discount_curve, for_discount_curve,
                                   model, num_paths)

    assert round(value_mc, 4) == 0.0582
def test_FinFXVanillaOptionHullExample():

    #   Example from Hull 4th edition page 284
    valuation_date = Date(1, 1, 2015)
    expiry_date = valuation_date.add_months(4)
    spot_fx_rate = 1.60
    volatility = 0.1411
    dom_interest_rate = 0.08
    forInterestRate = 0.11
    model = BlackScholes(volatility)
    dom_discount_curve = DiscountCurveFlat(valuation_date, dom_interest_rate)
    for_discount_curve = DiscountCurveFlat(valuation_date, forInterestRate)

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

    testCases.header("NUMPATHS", "VALUE_BS", "VALUE_MC")
    strike_fx_rate = 1.60

    for num_paths in num_paths_list:

        call_option = FXVanillaOption(expiry_date, strike_fx_rate, "EURUSD",
                                      OptionTypes.EUROPEAN_CALL, 1000000,
                                      "USD")

        value = call_option.value(valuation_date, spot_fx_rate,
                                  dom_discount_curve, for_discount_curve,
                                  model)

        start = time.time()

        value_mc = call_option.value_mc(valuation_date, spot_fx_rate,
                                        dom_discount_curve, for_discount_curve,
                                        model, num_paths)

        end = time.time()
        duration = end - start
        testCases.print(num_paths, value, value_mc)

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

    spot_fx_rates = np.arange(100, 200, 10)
    spot_fx_rates = spot_fx_rates / 100.0
    num_paths = 100000

    testCases.header("NUMPATHS", "CALL_VALUE_BS", "CALL_VALUE_MC")

    for spot_fx_rate in spot_fx_rates:

        call_option = FXVanillaOption(expiry_date, strike_fx_rate, "EURUSD",
                                      OptionTypes.EUROPEAN_CALL, 1000000,
                                      "USD")

        value = call_option.value(valuation_date, spot_fx_rate,
                                  dom_discount_curve, for_discount_curve,
                                  model)
        start = time.time()
        value_mc = call_option.value_mc(valuation_date, spot_fx_rate,
                                        dom_discount_curve, for_discount_curve,
                                        model, num_paths)
        end = time.time()
        duration = end - start
        testCases.print(num_paths, value, value_mc)

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

    spot_fx_rates = np.arange(100, 200, 10) / 100.0
    num_paths = 100000

    testCases.header("SPOT FX RATE", "PUT_VALUE_BS", "PUT_VALUE_MC")

    for spot_fx_rate in spot_fx_rates:

        put_option = FXVanillaOption(expiry_date, strike_fx_rate, "EURUSD",
                                     OptionTypes.EUROPEAN_PUT, 1000000, "USD")

        value = put_option.value(valuation_date, spot_fx_rate,
                                 dom_discount_curve, for_discount_curve, model)
        start = time.time()
        value_mc = put_option.value_mc(valuation_date, spot_fx_rate,
                                       dom_discount_curve, for_discount_curve,
                                       model, num_paths)
        end = time.time()
        duration = end - start
        testCases.print(spot_fx_rate, value, value_mc)

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

    spot_fx_rates = np.arange(100, 200, 10) / 100.0

    testCases.header("SPOT FX RATE", "CALL_VALUE_BS", "DELTA_BS", "VEGA_BS",
                     "THETA_BS", "RHO_BS")

    for spot_fx_rate in spot_fx_rates:
        call_option = FXVanillaOption(expiry_date, strike_fx_rate, "EURUSD",
                                      OptionTypes.EUROPEAN_CALL, 1000000,
                                      "USD")
        value = call_option.value(valuation_date, spot_fx_rate,
                                  dom_discount_curve, for_discount_curve,
                                  model)
        delta = call_option.delta(valuation_date, spot_fx_rate,
                                  dom_discount_curve, for_discount_curve,
                                  model)
        vega = call_option.vega(valuation_date, spot_fx_rate,
                                dom_discount_curve, for_discount_curve, model)
        theta = call_option.theta(valuation_date, spot_fx_rate,
                                  dom_discount_curve, for_discount_curve,
                                  model)
        #  call_option.rho(valuation_date,stock_price, interest_rate,
        #  dividend_yield, modelType, model_params)
        rho = 999
        testCases.print(spot_fx_rate, value, delta, vega, theta, rho)

    testCases.header("SPOT FX RATE", "PUT_VALUE_BS", "DELTA_BS", "VEGA_BS",
                     "THETA_BS", "RHO_BS")

    for spot_fx_rate in spot_fx_rates:
        put_option = FXVanillaOption(expiry_date, strike_fx_rate, "EURUSD",
                                     OptionTypes.EUROPEAN_PUT, 1000000, "USD")

        value = put_option.value(valuation_date, spot_fx_rate,
                                 dom_discount_curve, for_discount_curve, model)
        delta = put_option.delta(valuation_date, spot_fx_rate,
                                 dom_discount_curve, for_discount_curve, model)
        vega = put_option.vega(valuation_date, spot_fx_rate,
                               dom_discount_curve, for_discount_curve, model)
        theta = put_option.theta(valuation_date, spot_fx_rate,
                                 dom_discount_curve, for_discount_curve, model)
        # put_option.rho(valuation_date,stock_price, interest_rate, dividend_yield,
        # modelType, model_params)
        rho = 999
        testCases.print(spot_fx_rate, value, delta, vega, theta, rho)

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

    testCases.header("SPOT FX RATE", "VALUE_BS", "VOL_IN", "IMPLD_VOL")

    spot_fx_rates = np.arange(100, 200, 10) / 100.0

    for spot_fx_rate in spot_fx_rates:
        call_option = FXVanillaOption(expiry_date, strike_fx_rate, "EURUSD",
                                      OptionTypes.EUROPEAN_CALL, 1000000,
                                      "USD")

        value = call_option.value(valuation_date, spot_fx_rate,
                                  dom_discount_curve, for_discount_curve,
                                  model)['v']

        impliedVol = call_option.implied_volatility(valuation_date,
                                                    spot_fx_rate,
                                                    dom_discount_curve,
                                                    for_discount_curve, value)

        testCases.print(spot_fx_rate, value, volatility, impliedVol)