Skip to content

Commit

Permalink
Notebooks fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
domokane committed Dec 9, 2023
1 parent 7ca5216 commit f7945d0
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 246 deletions.
2 changes: 1 addition & 1 deletion financepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cr = "\n"

s = "####################################################################" + cr
s += "# FINANCEPY BETA Version " + str('0.34') + " - This build: 09 Dec 2023 at 18:51 #" + cr
s += "# FINANCEPY BETA Version " + str('0.34') + " - This build: 09 Dec 2023 at 18:59 #" + cr
s += "# This software is distributed FREE AND WITHOUT ANY WARRANTY #" + cr
s += "# Report bugs as issues at https://github.com/domokane/FinancePy #" + cr
s += "####################################################################"
Expand Down
60 changes: 32 additions & 28 deletions financepy/utils/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def calculate_list():

@njit(fastmath=True, cache=True)
def date_index(d, m, y):
''' Calculate the index of a date assuming 31 days in all months '''
idx = (y-g_start_year) * 12 * 31 + (m-1) * 31 + (d-1)
return idx

Expand All @@ -186,8 +187,9 @@ def date_from_index(idx):

@njit(fastmath=True, cache=True)
def weekday(day_count):
weekday = (day_count+5) % 7
return weekday
''' Converts day count to a weekday based on Excel date '''
week_day = (day_count+5) % 7
return week_day

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

Expand Down Expand Up @@ -448,11 +450,11 @@ def eom(self):
leap_year = is_leap_year(y)

if leap_year:
lastDay = month_days_leap_year[m - 1]
return Date(lastDay, m, y)
last_day = month_days_leap_year[m - 1]
return Date(last_day, m, y)
else:
lastDay = month_days_not_leap_year[m - 1]
return Date(lastDay, m, y)
last_day = month_days_not_leap_year[m - 1]
return Date(last_day, m, y)

return False

Expand All @@ -470,11 +472,11 @@ def add_hours(self, hours):
hour = final_hour % 24

# Move forward a specific number of days
dt1 = self.add_days(days)
dt_1 = self.add_days(days)

# On that date we then move to the correct hour
dt2 = Date(dt1._d, dt1._m, dt1._y, hour, dt1._mm, dt1._ss)
return dt2
dt_2 = Date(dt_1.d(), dt_1.m(), dt_1.y(), hour, dt_1._mm, dt_1._ss)
return dt_2

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

Expand Down Expand Up @@ -518,9 +520,10 @@ def add_weekdays(self,
num_days = abs(num_days)

# 5 week days make up a week
oldLogic = False
old_logic = False

if old_logic is True:

if oldLogic is True:
num_weeks = int(num_days / 5)
remaining_days = num_days % 5

Expand All @@ -531,14 +534,16 @@ def add_weekdays(self,
else:
weekend_adjust = 2

if (positive_num_days):
if (self._weekday + remaining_days > self.FRI):
if positive_num_days is True:
if self._weekday + remaining_days > self.FRI:
# add weekend
remaining_days += weekend_adjust

return self.add_days(num_weeks * 7 + remaining_days)

else:
if (self._weekday - remaining_days < self.MON):

if self._weekday - remaining_days < self.MON:
# add weekend
remaining_days += weekend_adjust

Expand All @@ -556,7 +561,7 @@ def add_weekdays(self,
else:
end_dt = end_dt.add_days(-1)

if end_dt._weekday == Date.SAT or end_dt._weekday == Date.SUN:
if end_dt.weekday() == Date.SAT or end_dt.weekday() == Date.SUN:
pass
else:
num_days_left -= 1
Expand Down Expand Up @@ -673,9 +678,9 @@ def next_cds_date(self,

next_dt = self.add_months(mm)

y = next_dt._y
m = next_dt._m
d = next_dt._d
y = next_dt.y()
m = next_dt.m()
d = next_dt.d()

d_cds = 20
y_cds = y
Expand All @@ -699,8 +704,8 @@ def next_cds_date(self,
elif m == 1 or m == 2 or m == 3:
m_cds = 3

cdsDate = Date(d_cds, m_cds, y_cds)
return cdsDate
cds_date = Date(d_cds, m_cds, y_cds)
return cds_date

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

Expand All @@ -719,7 +724,7 @@ def third_wednesday_of_month(self,

for d in range(d_start, d_end+1):
imm_date = Date(d, m, y)
if imm_date._weekday == self.WED:
if imm_date.weekday() == self.WED:
return d

# Should never reach this line but just to be defensive
Expand Down Expand Up @@ -833,9 +838,9 @@ def add_tenor(self,
new_date = new_date.add_months(math.copysign(1, num_periods))

# in case we landed on a 28th Feb and lost the month day we add this logic
y = new_date._y
m = new_date._m
d = min(self._d, new_date.eom()._d)
y = new_date.y()
m = new_date.m()
d = min(self.d(), new_date.eom()._d)
new_date = Date(d, m, y)

elif period_type == YEARS:
Expand All @@ -861,7 +866,7 @@ def datetime(self):
# TODO: Find elegant way to return long and short strings
###########################################################################

def str(self, format):
def str(self, format=None):
""" returns a formatted string of the date """
date_str = ""

Expand Down Expand Up @@ -925,8 +930,7 @@ def __repr__(self):
elif g_date_type_format == DateFormatTypes.US_LONGEST:

sep = " "
date_str = day_name_str + " " + long_month_str + sep + day_str
+ sep + long_year_str
date_str = day_name_str + " " + long_month_str + sep + day_str + sep + long_year_str
return date_str

elif g_date_type_format == DateFormatTypes.US_LONG:
Expand Down Expand Up @@ -1015,7 +1019,7 @@ def daily_working_day_schedule(self,
def datediff(d1: Date,
d2: Date):
""" Calculate the number of days between two Findates. """
dd = (d2._excel_dt - d1._excel_dt)
dd = d2.excel_dt() - d1.excel_dt()
return int(dd)

###############################################################################
Expand Down
8 changes: 4 additions & 4 deletions notebooks/products/bonds/FINANNUITY_Valuation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"output_type": "stream",
"text": [
"####################################################################\n",
"# FINANCEPY BETA Version 0.33 - This build: 11 Nov 2023 at 07:07 #\n",
"# FINANCEPY BETA Version 0.34 - This build: 09 Dec 2023 at 18:59 #\n",
"# This software is distributed FREE AND WITHOUT ANY WARRANTY #\n",
"# Report bugs as issues at https://github.com/domokane/FinancePy #\n",
"####################################################################\n",
Expand Down Expand Up @@ -236,7 +236,7 @@
"\n",
"PAYMENTS VALUATION:\n",
"+---------+-------------+----------+------+----------+--------+----------+-----------+\n",
"| PAY_NUM | PAY_dt | NOTIONAL | RATE | PMNT | DF | PV | CUM_PV |\n",
"| PAY_NUM | PAY_dt | NOTIONAL | RATE | PMNT | DF | PV | CUM_PV |\n",
"+---------+-------------+----------+------+----------+--------+----------+-----------+\n",
"| 1 | 20-DEC-2018 | 1000000 | 5.0 | 25416.67 | 0.9752 | 24786.65 | 24786.65 |\n",
"| 2 | 20-JUN-2019 | 1000000 | 5.0 | 25277.78 | 0.9512 | 24043.46 | 48830.11 |\n",
Expand Down Expand Up @@ -280,7 +280,7 @@
"\n",
"PAYMENTS VALUATION:\n",
"+---------+-------------+----------+--------+----------+--------+----------+-----------+\n",
"| PAY_NUM | PAY_dt | NOTIONAL | IBOR | PMNT | DF | PV | CUM_PV |\n",
"| PAY_NUM | PAY_dt | NOTIONAL | IBOR | PMNT | DF | PV | CUM_PV |\n",
"+---------+-------------+----------+--------+----------+--------+----------+-----------+\n",
"| 1 | 20-SEP-2018 | 1000000 | 5.0794 | 12698.47 | 0.9875 | 12539.24 | 12539.24 |\n",
"| 2 | 20-DEC-2018 | 1000000 | 5.0238 | 12559.58 | 0.9752 | 12248.26 | 24787.49 |\n",
Expand Down Expand Up @@ -380,7 +380,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
14 changes: 7 additions & 7 deletions notebooks/products/bonds/FINBONDFUTURES_ExampleContracts.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"output_type": "stream",
"text": [
"####################################################################\n",
"# FINANCEPY BETA Version 0.33 - This build: 11 Nov 2023 at 07:47 #\n",
"# FINANCEPY BETA Version 0.34 - This build: 09 Dec 2023 at 18:59 #\n",
"# This software is distributed FREE AND WITHOUT ANY WARRANTY #\n",
"# Report bugs as issues at https://github.com/domokane/FinancePy #\n",
"####################################################################\n",
Expand Down Expand Up @@ -174,7 +174,7 @@
"for bond, clean_price in zip(bonds, prices):\n",
" yld = bond.yield_to_maturity(settle_dt, clean_price)\n",
" dt = bond._maturity_dt\n",
" print(\"%18s %9.5f %12.8f\"% (dt, bond._coupon * 100, yld*100))"
" print(\"%18s %9.5f %12.8f\"% (dt, bond._cpn * 100, yld*100))"
]
},
{
Expand Down Expand Up @@ -216,7 +216,7 @@
"for bond in bonds:\n",
" cf = bondFutureContract.conversion_factor(bond)\n",
" dt = bond._maturity_dt\n",
" print(\"%18s %9.5f %12.8f\"% (dt, bond._coupon * 100, cf))"
" print(\"%18s %9.5f %12.8f\"% (dt, bond._cpn * 100, cf))"
]
},
{
Expand Down Expand Up @@ -281,7 +281,7 @@
"\n",
"for bond in bonds:\n",
" pip = bondFutureContract.principal_invoice_price(bond, futures_price)\n",
" print(\"%18s %12.5f %20.4f\"% (str(bond._maturity_dt), bond._coupon*100, pip))"
" print(\"%18s %12.5f %20.4f\"% (str(bond._maturity_dt), bond._cpn*100, pip))"
]
},
{
Expand Down Expand Up @@ -316,7 +316,7 @@
"\n",
"for bond, clean_price in zip(bonds, prices):\n",
" gainloss, payForBond, receiveOnFuture = bondFutureContract.delivery_gain_loss(bond, clean_price, futures_price)\n",
" print(\"%18s %12.5f %20.4f\"% (str(bond._maturity_dt), bond._coupon*100, gainloss))"
" print(\"%18s %12.5f %20.4f\"% (str(bond._maturity_dt), bond._cpn*100, gainloss))"
]
},
{
Expand All @@ -336,7 +336,7 @@
"source": [
"ctd = bondFutureContract.cheapest_to_deliver(bonds, prices, futures_price)\n",
"print(\"CTD MATURITY\", \"CTD COUPON\")\n",
"print(str(ctd._maturity_dt), ctd._coupon*100)"
"print(str(ctd._maturity_dt), ctd._cpn*100)"
]
},
{
Expand All @@ -363,7 +363,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit f7945d0

Please sign in to comment.