Skip to content

Commit 9875744

Browse files
authored
corrected libraries
1 parent 3361ea2 commit 9875744

File tree

2 files changed

+66
-29
lines changed

2 files changed

+66
-29
lines changed

libraries/mathlib.py

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -783,35 +783,37 @@ def accumulatedInterest(issue,settle,freq,coup,dayCount,matur) :
783783
aif = 0.0
784784
d1 = st[0]
785785
d2 = st[1]
786-
# print(issue)
787-
# print(settle)
788-
# print(d1)
789-
# print(d2)
790786
ys = d1.year
791787
ye = settle.year
792-
ysEnd = OclDate.newOclDate_String(str(ys) + "/12/31")
793-
yeStart = OclDate.newOclDate_String(str(ye) + "/01/01")
794-
788+
ysEnd = OclDate.newOclDate_YMD(ys, 12, 31)
789+
yeStart = OclDate.newOclDate_YMD(ye, 1, 1)
790+
791+
daysbetween = OclDate.daysBetweenDates(d1,settle)
792+
daystoend = OclDate.daysBetweenDates(d1,ysEnd)
793+
daysfromstart = OclDate.daysBetweenDates(yeStart,settle)
794+
795+
settleIsLeap = settle.isLeapYear()
796+
dayIsLeap = d1.isLeapYear()
797+
795798
if (dayCount == "Actual/365F") :
796-
aif = (OclDate.daysBetweenDates(d1,settle)/365)*coup
799+
aif = (daysbetween/365)*coup
797800
elif (dayCount == "Actual/ActualISDA") :
798-
if (d1.isLeapYear() and settle.isLeapYear()):
799-
aif = (OclDate.daysBetweenDates(d1,settle)/366)*coup
800-
elif (not(d1.isLeapYear()) and not(settle.isLeapYear())) :
801-
aif = (OclDate.daysBetweenDates(d1,settle)/365)*coup
802-
elif (d1.isLeapYear() and not(settle.isLeapYear())) :
803-
aif = (OclDate.daysBetweenDates(d1,ysEnd)/366) * coup +\
804-
(OclDate.daysBetweenDates(yeStart,settle)/365)*coup
801+
if (dayIsLeap and settleIsLeap):
802+
aif = (daysbetween/366)*coup
803+
elif (not(dayIsLeap) and not(settleIsLeap)) :
804+
aif = (daysbetween/365)*coup
805+
elif (dayIsLeap and not(settleIsLeap)) :
806+
aif = (daystoend/366) * coup +\
807+
(daysfromstart/365)*coup
805808
else:
806-
aif = (OclDate.daysBetweenDates(d1,ysEnd)/365)*coup +\
807-
(OclDate.daysBetweenDates(yeStart,settle)/366)*coup
808-
809+
aif = (daystoend/365)*coup +\
810+
(daysfromstart/366)*coup
809811
elif (dayCount == "Actual/364") :
810-
aif = (OclDate.daysBetweenDates(d1,settle)/364)*coup
812+
aif = (daysbetween/364)*coup
811813
elif (dayCount == "Actual/360") :
812-
aif = (OclDate.daysBetweenDates(d1,settle)/360)*coup
814+
aif = (daysbetween/360)*coup
813815
elif (dayCount == "Actual/ActualICMA") :
814-
aif = (OclDate.daysBetweenDates(d1,settle)/(freq*OclDate.daysBetweenDates(d1,d2)))*coup
816+
aif = (daysbetween/(freq*OclDate.daysBetweenDates(d1,d2)))*coup
815817
else :
816818
aif = (FinanceLib.days360(d1,settle,dayCount,matur)/360)*coup
817819
return aif
@@ -927,3 +929,30 @@ def bondPriceClean(Y,I,S,M,c,dcf,f) :
927929

928930
# sq = ['a', 'b', 'c', 'a', 'r', 'a', 'd', 'b', 'r', 'a']
929931
# print(MathLib.frequencyCount(sq))
932+
933+
# sq = [18.4, 18.4, 30.2, 50.3]
934+
# print(MathLib.standardDeviation(sq))
935+
936+
# sq = [334.4, 382.4, 900.8, 1553.6]
937+
# print(MathLib.standardDeviation(sq))
938+
939+
# d1 = OclDate.newOclDate_String("2020/07/31")
940+
# d2 = OclDate.newOclDate_String("2020/08/31")
941+
942+
# print(OclDate.daysBetweenDates(d1, d2))
943+
944+
# print(FinanceLib.accumulatedInterest(d1, d2, 2, 0.02, "Actual/360", "31/07/2024"))
945+
946+
# t1 = time.time()
947+
948+
# for yr in range(1800,2024) :
949+
# for mnt in range(10,13) :
950+
# for dd in range(10,31) :
951+
# dd1 = OclDate.newOclDate_YMD(yr - 1, mnt, 1)
952+
# dd2 = OclDate.newOclDate_YMD(yr, mnt, dd)
953+
# FinanceLib.accumulatedInterest(dd1, dd2,
954+
# 2 , 0.02, "Actual/ActualISDA", "31/07/2024")
955+
956+
# t2 = time.time()
957+
# print(1000*(t2 - t1))
958+

libraries/ocldate.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,23 +306,31 @@ def daysBetweenDates(d1,d2) :
306306
endDay = d2.day
307307
endMonth = d2.month
308308
endYear = d2.year
309-
days = 0
310-
311-
while startYear < endYear or (startYear == endYear and startMonth < endMonth) :
309+
310+
days = 0
311+
312+
if d1.month > d2.month :
313+
days = 365*((endYear - 1) - startYear)
314+
startYear = endYear - 1
315+
else :
316+
days = 365*(endYear - startYear)
317+
startYear = endYear
318+
319+
320+
while startYear < endYear or startMonth < endMonth :
312321
daysinmonth = OclDate.monthDays(startMonth, startYear)
313322
days = days + daysinmonth - startDay + 1
314323
startDay = 1
315324
startMonth = startMonth + 1
316-
if startMonth > 12 :
325+
if startMonth > 12 :
317326
startMonth = 1
318327
startYear = startYear + 1
319-
328+
320329
days = days + endDay - startDay
321330
return days
322331

323-
324-
# d1 = OclDate.newOclDate_YMD(2023, 8, 1)
325-
# d2 = OclDate.newOclDate_YMD(2023, 11, 1)
332+
# d1 = OclDate.newOclDate_YMD(2023, 12, 1)
333+
# d2 = OclDate.newOclDate_YMD(2024, 1, 1)
326334
# print(OclDate.daysBetweenDates(d1,d2))
327335

328336
# print(OclDate.getSystemTime())

0 commit comments

Comments
 (0)