Finance Functions Python
Finance Functions Python
July 7, 2019
'''
#EXAMPLE
price_change = -.05
yield_change = .01
duration(price_change, yield_change)
[1]: 5.0
[2]: def absolute_yield_spread(yield_higher, yield_lower):
'''
Summary: Calculate the absolute yield spread given two bonds.
'''
#EXAMPLE
y_h = .0675
1
y_l = .0650
absolute_yield_spread(y_h, y_l)
[2]: 0.0025000000000000022
[3]: def relative_yield_spread(absolute_yield, yield_benchmark):
'''
Summary: Calculate the relative yield spread of a given bond.
PARA absolute_yield: The the absolute yield spread for two given bonds.
PARA type: float
PARA yield_benchmark: The yield of the bond that we are defining as the␣
,→ benchmark, the one we want to compare.
PARA type: float
'''
#EXAMPLE
y_h = .0675
y_l = .0650
y_a = absolute_yield_spread(y_h, y_l)
relative_yield_spread(y_a, y_l)
[3]: 0.03846153846153849
[4]: def yield_ratio(subject_yield, benchmark_yield):
'''
Summary: Calculate the yield ratio, given two bonds.
PARA benchmark_yield: The yield of the bond that we are defining as the␣
,→ benchmark, the one we want to compare.
PARA type: float
'''
#EXAMPLE
y_h = .0675
y_l = .0650
yield_ratio(y_h, y_l)
[4]: 1.0384615384615385
2
[5]: def after_tax_yield(taxable_yield, marginal_tax_rate):
'''
Summary: Calculate the after-tax yield on a taxable security.
'''
#EXAMPLE
yld_tax = .10
mrg_tax = .40
after_tax_yield(yld_tax, mrg_tax)
[5]: 0.06
[7]: def taxable_equivalent_yield(tax_free_yield, marginal_tax_rate):
'''
Summary: Calculate the taxable-equivalent yield for a given bond.
'''
#EXAMPLE
yld_tax = .045
mrg_tax = .350
taxable_equivalent_yield(yld_tax, mrg_tax)
[7]: 0.06923076923076922
[8]: def effective_duration(bond_dec, bond_inc, bond_int, yld_delta):
'''
Summary: Calculate the effective duration of a bond, given a change in␣
,→yield.
3
PARA type: float
'''
#EXAMPLE
bond_inc = 952.30
bond_dec = 866.80
bond_int = 908.00
delt_yld = .005
effective_duration(bond_dec, bond_inc, bond_int, delt_yld)
[8]: 9.416299559471366
[9]: def pct_change_bond(effective_duration, yld_chg):
'''
Summary: Calculate the percentage change in bond price.
'''
#EXAMPLE
bond_inc = 952.30
bond_dec = 866.80
bond_int = 908.00
delt_yld = .005
ed = effective_duration(bond_dec, bond_inc, bond_int, delt_yld)
pct_yld = .003
pct_change_bond(ed, pct_yld)
4
[9]: -0.028248898678414097
[12]: import numpy as np
PARA durations: A list of durations for each bond in the bonds list
PARA type: float
'''
np_durations = np.array(durations)
#EXAMPLE
bond = [6000, 4000]
dura = [8.5, 4.0]
portfolio_duration(bond, dura)
[12]: 6.699999999999999
[15]: def return_impact(mod_duration, spread, spread_type, convexity):
'''
Summary: Calculate the return impact for a bond, using either a large or␣
,→small spread.
PARA spread_type: Defines the spread as either a small spread change or␣
,→large spread change. Options are 'large' or 'small'
5
PARA type: float
'''
if spread_type == 'small':
else:
#EXAMPLE
duration = 6.4
convexity = 50
spread = -.0075
# small example
spread_type = 'small'
display(return_impact(duration, spread, spread_type, convexity))
# large example
spread_type = 'large'
display(return_impact(duration, spread, spread_type, convexity))
0.048
0.04940625
'''
6
# describe the conditions for a call option
if option_type == 'call':
elif(selling_price == strike_price):
return ('at-the-money', 0)
elif(selling_price == strike_price):
return ('at-the-money', 0)
# EXAMPLE
selling_price = 37.00
strike_price = 40.00
# call example
opt_type = 'call'
display(option_moneyness(opt_type, strike_price, selling_price))
# put example
opt_type = 'put'
display(option_moneyness(opt_type, strike_price, selling_price))
('out-of-the-money', -3.0)
('in-the-money', 3.0)
7
PARA index_price: The index price at expiration.
PARA type: float
'''
# EXAMPLE
i_price = 962.00
e_price = 950.00
cont = 250
exp_date_payoff(e_price, i_price, cont)
[21]: 3000.0
[22]: def option_intrinsic_value(option_type, strike_price, selling_price):
'''
Summary: Returns the intrinsic value of either a put or call option.
'''
# EXAMPLE
selling_price = 55.00
strike_price = 50.00
8
# call example
opt_type = 'call'
display(option_intrinsic_value(opt_type, strike_price, selling_price))
# put example
opt_type = 'put'
display(option_intrinsic_value(opt_type, strike_price, selling_price))
5.0
'''
Summary: Returns the maximum and minimum value for a european or american␣
,→call/put.
PARA time_frame: The time frame of the option, for example a 4 month option␣
,→ would be 4.
PARA type: float
'''
if option_type == 'call':
max_opt_1 = 0
9
max_opt_2 = (selling_price - strike_price / (1 + risk_free_rate)␣
,→ **(time_frame / 12))
else:
if option_style == 'american':
max_opt_1 = 0
max_opt_2 = strike_price - selling_price
else:
max_opt_1 = 0
max_opt_2 = (strike_price / (1 + risk_free_rate) **(time_frame /␣
,→12)) - selling_price
# EXAMPLE - PUTS
selling_price = 63.00
strike_price = 65.00
time_frame = 4
rfr = .05
# put example
opt_type = 'put'
opt_styl = 'american'
display(min_max_option(opt_type, opt_styl, rfr, strike_price, selling_price,␣
,→time_frame))
# put example
opt_type = 'put'
opt_styl = 'european'
10
display(min_max_option(opt_type, opt_styl, rfr, strike_price, selling_price,␣
,→time_frame))
# EXAMPLE - CALLS
selling_price = 68.00
strike_price = 65.00
time_frame = 3
rfr = .05
# call example
opt_type = 'call'
opt_styl = 'american'
display(min_max_option(opt_type, opt_styl, rfr, strike_price, selling_price,␣
,→time_frame))
# call example
opt_type = 'call'
opt_styl = 'european'
display(min_max_option(opt_type, opt_styl, rfr, strike_price, selling_price,␣
,→time_frame))
(2.0, 65.0)
(0.9514295424027992, 63.9514295424028)
(3.788024417500182, 68.0)
(3.788024417500182, 68.0)
'''
Summary: Define the put-call parity formula, which returns the price of a␣
,→call given a put and vice verse.
PARA calc: Calculate either the value of a 'call' given a put or calculate␣
,→a 'put' given a call.
11
PARA exercise_price: The current selling price of the option.
PARA type: float
PARA time_frame: The time frame of the option, for example a 4 month option␣
,→would be 4.
if calc == 'put':
return put
else:
#EXAMPLE
stock = 52.00
rfr = .05
time = 3
exercise = 50.00
put = 1.50
calc_type = 'call'
put_call_parity(calc = calc_type,
put = put,
strike_price = exercise,
stock_price = stock,
risk_free_rate = rfr,
time_frame =time)
[36]: 4.106172628846302
[37]: def fixed_rate_pay(swap_fixed_rate, libor, principal, num_days):
'''
Summary: Define the put-call parity formula, which returns the price of a␣
,→call given a put and vice verse.
12
PARA calc: Calculate either the value of a 'call' given a put or calculate␣
,→ a 'put' given a call.
PARA type: string
#EXAMPLE
swap = .06
libor = .04
principal = 1000000
days = 90
fixed_rate_pay(swap, libor, principal, days)
[37]: 4999.999999999999
13