Skip to content

Commit

Permalink
Small bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
domokane committed Dec 2, 2023
1 parent 56c8b31 commit 4e8a55f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 49 deletions.
2 changes: 1 addition & 1 deletion financepy/products/credit/cds.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ def value_fast_approx(self,
""" Implementation of fast valuation of the CDS contract using an
accurate approximation that avoids curve building. """

if isinstance(value_date, Date):
if isinstance(value_date, Date) is False:
raise FinError("Valuation date must be a Date and not " +
str(value_date))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,13 @@
"if 1==1:\n",
" \n",
" settlementDate = Date(1, 12, 2019)\n",
" issueDate = Date(1, 12, 2018)\n",
" issue_date = Date(1, 12, 2018)\n",
" expiryDate = settlementDate.add_tenor(\"18m\")\n",
" maturityDate = settlementDate.add_tenor(\"10Y\")\n",
" maturity_date = settlementDate.add_tenor(\"10Y\")\n",
" coupon = 0.05\n",
" freqType = FrequencyTypes.SEMI_ANNUAL\n",
" accrualType = DayCountTypes.THIRTY_360_BOND\n",
" bond = Bond(issueDate, maturityDate, coupon, freqType, accrualType)\n",
" freq_type = FrequencyTypes.SEMI_ANNUAL\n",
" accrual_type = DayCountTypes.THIRTY_360_BOND\n",
" bond = Bond(issue_date, maturity_date, coupon, freq_type, accrual_type)\n",
"\n",
" couponTimes = []\n",
" couponFlows = []\n",
Expand Down
38 changes: 21 additions & 17 deletions tests_golden/TestFinBond.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,16 +561,19 @@ def test_BondPaymentDates():
def test_Bond_ror():

test_case_file = 'test_cases_bond_ror.csv'
df = pd.read_csv('./data/' + test_case_file, parse_dates=['buy_date', 'sell_date'])
df = pd.read_csv('./data/' + test_case_file, parse_dates=['buy_date',
'sell_date'])
# A 10-year bond with 1 coupon per year. code: 210215

bond = Bond(
issue_date=Date(13, 9, 2021),
maturity_date=Date(13, 9, 2031),
coupon=0.0312,
freq_type=FrequencyTypes.ANNUAL,
dc_type=DayCountTypes.ACT_ACT_ICMA
)
testCases.header('bond_code', 'buy_date', 'buy_ytm', 'buy_price', 'sell_date', 'sell_ytm', 'sell_price',
dc_type=DayCountTypes.ACT_ACT_ICMA)

testCases.header('bond_code', 'buy_date', 'buy_ytm', 'buy_price',
'sell_date', 'sell_ytm', 'sell_price',
'simple_return', 'irr')

for row in df.itertuples(index=False):
Expand All @@ -581,7 +584,8 @@ def test_Bond_ror():
sell_price = bond.dirty_price_from_ytm(sell_date, row.sell_ytm, YTMCalcType.US_STREET)
simple, irr, pnl = bond.calc_ror(buy_date, sell_date, row.buy_ytm, row.sell_ytm)

testCases.print(row.bond_code, buy_date, row.buy_ytm, buy_price, sell_date, row.sell_ytm, sell_price,
testCases.print(row.bond_code, buy_date, row.buy_ytm, buy_price,
sell_date, row.sell_ytm, sell_price,
simple, irr)


Expand Down Expand Up @@ -663,19 +667,18 @@ def test_key_rate_durations_Bloomberg_example():

krt, krd = bond.key_rate_durations(settle_date,
ytm,
key_rate_tenors = my_tenors,
rates = my_rates)
key_rate_tenors=my_tenors,
rates=my_rates)

# print(key_rate_tenors)
# print(key_rate_durations)

# Differences due to bonds not sitting exactly on these maturity points ? Did BBG interpolate ?
# Differences due to bonds not sitting exactly on these maturity points ?
# Did BBG interpolate ?

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


from financepy.market.curves.discount_curve_flat import DiscountCurveFlat

def test_oas():

issue_date = Date(15, 5, 2010)
Expand All @@ -692,7 +695,8 @@ def test_oas():
liborFlatCurve = DiscountCurveFlat(settle_date, liborFlatRate,
FrequencyTypes.SEMI_ANNUAL)

clean_price = 99.780842 # I specified face to be 100 - if face is 1 then this must be 0.99780842
# I specified face to be 100 - if face is 1 then this must be 0.99780842
clean_price = 99.780842

oas = bond.option_adjusted_spread(settle_date, clean_price,
liborFlatCurve) * 10000
Expand All @@ -705,16 +709,16 @@ def test_oas():

def test_div_dates():

issueDate = Date(15, 5, 2020)
maturityDate = Date(15, 5, 2035)
issue_date = Date(15, 5, 2020)
maturity_date = Date(15, 5, 2035)
coupon = 0.02375
freqType = FrequencyTypes.SEMI_ANNUAL
accrualType = DayCountTypes.ACT_ACT_ICMA
freq_type = FrequencyTypes.SEMI_ANNUAL
accrual_type = DayCountTypes.ACT_ACT_ICMA
face = 125000
ex_div_days = 10

bond = Bond(issueDate, maturityDate, coupon, freqType,
accrualType, ex_div_days)
bond = Bond(issue_date, maturity_date, coupon, freq_type,
accrual_type, ex_div_days)

print(bond)

Expand Down
13 changes: 7 additions & 6 deletions tests_golden/TestFinBondOptionHWModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,16 +470,17 @@ def test_BondOptionDerivaGem():
maturity_date = settle_date.add_tenor("10Y")

coupon = 0.05
freqType = FrequencyTypes.SEMI_ANNUAL
accrualType = DayCountTypes.THIRTY_360_BOND
bond = Bond(issue_date, maturity_date, coupon, freqType, accrualType)
freq_type = FrequencyTypes.SEMI_ANNUAL
accrual_type = DayCountTypes.THIRTY_360_BOND
bond = Bond(issue_date, maturity_date, coupon, freq_type, accrual_type)
strike_price = 100.0
face = 100.0

europeanCallBondOption = BondOption(bond, expiry_date, strike_price,
OptionTypes.EUROPEAN_CALL)
cp = bond.clean_price_from_discount_curve(expiry_date, discount_curve)
fp = bond.dirty_price_from_discount_curve(expiry_date, discount_curve)

# cp = bond.clean_price_from_discount_curve(expiry_date, discount_curve)
# fp = bond.dirty_price_from_discount_curve(expiry_date, discount_curve)
# print("Fixed Income Clean Price: %9.3f"% cp)
# print("Fixed Income Full Price: %9.3f"% fp)

Expand All @@ -488,7 +489,7 @@ def test_BondOptionDerivaGem():
a = 0.1
modelHW = HWTree(sigma, a, num_steps)

ec = europeanCallBondOption.value(settle_date, discount_curve, modelHW)
# ec = europeanCallBondOption.value(settle_date, discount_curve, modelHW)

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

Expand Down
41 changes: 21 additions & 20 deletions tests_golden/runAllTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,60 @@

print("Looking in folder:", dirname(__file__))
modules = sorted(glob.glob(join(dirname(__file__), "Test*.py")))
numModules = len(modules)
num_modules = len(modules)

""" This is the index of the file - change this to start later in the list """
n = 0
m = numModules
N = 0
M = num_modules

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

for moduleFileName in modules[n:m + 1]:
for module_file_name in modules[N:M + 1]:

try:

moduleTextName = basename(moduleFileName[:-3])
print("TEST: %3d out of %3d: MODULE: %-35s " % (n + 1, numModules,
moduleTextName),
module_text_name = basename(module_file_name[:-3])
print("TEST: %3d out of %3d: MODULE: %-35s " % (N + 1, num_modules,
module_text_name),
end="")
moduleName = __import__(moduleTextName)
numErrors = moduleName.testCases._globalNumErrors
numWarnings = moduleName.testCases._globalNumWarnings
module_name = __import__(module_text_name)
num_errors = module_name.testCases._globalNumErrors
num_warnings = module_name.testCases._globalNumWarnings

print("WARNINGS: %3d ERRORS: %3d " % (numWarnings, numErrors), end="")
print("WARNINGS: %3d ERRORS: %3d " % (num_warnings, num_errors),
end="")

if numErrors > 0:
for i in range(0, numErrors):
if num_errors > 0:
for i in range(0, num_errors):
print("*", end="")

print("")
n = n + 1
N = N + 1

# Want testing to continue even if a module has an exception
except FinError as err:
print("FinError:", err._message, "************")
n = n + 1
N = N + 1
pass
except ValueError as err:
print("Value Error:", err.args[0], "************")
n = n + 1
N = N + 1
pass
except NameError as err:
print("Name Error:", err.args[0], "************")
n = n + 1
N = N + 1
pass
except TypeError as err:
print("Type Error:", err.args[0], "************")
n = n + 1
N = N + 1
pass
except BaseException as e:
print("Base error:", e)
n = n + 1
N = N + 1
pass
except Exception:
print("Unexpected error:", sys.exc_info()[0])
n = n + 1
N = N + 1
pass

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

0 comments on commit 4e8a55f

Please sign in to comment.