Hi,
I do a research on bond market. Searching on the net, I could not find any stata cammand which calculate YTM (for each day) based on price, time to maturity, coupon rate, and par value of some bonds. I appreciate if there is a stata command to do so or any other suggestion for obtaining YTM for such a big (daily) data set.
Announcement
Collapse
No announcement yet.
X

calculating yield to maturity for coupon bonds
Tags: None

Suppose that I want to calculate irr based on montly periods so I should calculate aformantioend time intervals by dividing them by 30 (transforming daily data to monthly). As a consequence aformantioend time variables will not bear an integer value, hence I could not use this code (https://ideas.repec.org/c/boc/bocode/s457597.html).
let me provide an example
t=0 cf0=100 (purchasing day)
t=39(days later) cf1=10 (first coupon)
t=54(days later) cf1=10 (second coupon)
t=63(days later) cf1=100 (maturity day)Last edited by Andrew Musau; 06 Mar 2019, 15:07.

Ali Bahador  I will look at this for you, but I have a lot of teaching this semester, so it may take a while.
Leave a comment:

Thank you Andrew,
I read the code you offered me (https://ideas.repec.org/c/boc/bocode/s457597.html), it has a simple and interesting logic "make required discount factor for each element of cashflow using some educated guess of IRR and calculate NPV of cash flow, if NPV is close enough to zero then you have desirable IRR".
Unfortunately in this code the time period between each cash flow should be an interger number, thus it is not exactly what I am searching for. So I tried to customize it for my use, but my code has some errors and I am looking for more guidance.
As I have said before I have about 7000 daily observations containing price of some (20) different coupon bonds (panel data). Additionally, I have issuing date, maturity date, date of first coupon payment, time period between coupons, nominal rate of each coupon, and par value for each bond in a separated data set which I have merged it with the former. So I have current price, par value, time to maturity or time to receiving par value (T), coupon value (cf_{t}), and time to receiving each coupon (t_{i}= t_{1}, t_{2},…) [or time to first coupon, number of remained coupon, and time interval between coupons which as will be shown in following paragraphs I think are more convenient information].
Problem:
Suppose that I want to calculate irr based on montly periods so I should calculate aformantioend time intervals by dividing them by 30 (transforming daily data to monthly). As a consequence aformantioend time variables will not bear an integer value, hence I could not use this code (https://ideas.repec.org/c/boc/bocode/s457597.html).
let me provide an example
t=0 cf0=100 (purchasing day)
t=39(days later) cf1=10 (first coupon)
t=54(days later) cf1=10 (second coupon)
t=63(days later) cf1=100 (maturity day)
transforming daily data to monthly:
t=0 cf0=100 (purchasing day)
t=1.3(month later) cf1=10 (first coupon)
t=1.8(month later) cf1=10 (second coupon)
t=2.1(month later) cf1=100 (maturity day)
To overcome this problem I have added 3 new arguments (time to first coupon, time interval between coupon payments, and time from last coupon to maturity date) to the iir ado file.
in aformentioned example
time to first coupon: a=1.3 (ttfrip in modified code)
time interval between coupon payments: b=0.5 (cp in modified code)
time from last coupon to maturity date: c=0.3 (tflctm in modified code)
By doing so I can form required equation as follows:
\[ 100 + \frac{10}{(1+r)^a} + \frac{10}{(1+r)^{a+b}} + {\frac{100}{(1+r)^{a+b+c}}}=0 \]
I have attached original code (irr) (https://ideas.repec.org/c/boc/bocode/s457597.html) and modified one (myirr). would you mind helping me?Attached FilesLast edited by Ali Bahador; 02 Mar 2019, 08:01.
Leave a comment:

You can think of the yield to maturity (YTM) as the bond's internal rate of return (IRR). While it is possible to write a program that can calculate this for a fixed number of periods (see here, for example), even the most seasoned programmers might struggle to come up with a routine that handles arbitrary number of periods and varying coupons. Stata is not built for this kind of optimization  you are better off computing the YTM in Excel or elsewhere and exporting the result to Stata. However, there is a formula which approximates the YTM that you can easily implement in Stata.
Searching a bit further, there seems to be such a program in existence. Have not downloaded it or used it, so I can't vouch for it.
https://ideas.repec.org/c/boc/bocode/s457597.htmlLast edited by Andrew Musau; 15 Feb 2019, 07:09.
Leave a comment:

Thank you Phil;
I will try to say what I am searching for more exactly.
I have about 7000 daily observations containing price of some (20) different coupon bonds (panel data). Additionally, I have issuing date, maturity date, date of first coupon payment, time period between coupons, nominal rate of each coupon, and par value for each bond in a separated data set which I have merged it with the former. So I have current price, par value, time to maturity or time to receiving par value (T), coupon value (cf_{t}), and time to receiving each coupon (t_{i}= t_{1}, t_{2},…) [or time to first coupon, number of remained coupon, and time interval between coupons which as will be shown in following paragraphs I think are more convenient information].
I want to calculate yield to maturity for each observation which is the R solving following equation.
\[ Current Price= \frac{cf_{t_{1}}}{{(1+R)}^{ t_{1}}}+\frac{cf_{ t_{2}}}{{(1+R)}^{ t_{2}}}+...+\frac{par value}{{(1+R)}^{T}} =\sum_{t_{i}= t_{1}, t_{2},…}\frac{cf_{t_{i}}}{{(1+R)}^{ t_{i}}}+\frac{par value}{{(1+R)}^{T}} \]
At first sight I thought it will be easy to define a user defined ado file which receives aforementioned information and uses solvenl() to calculate yield to maturity; However, there is a problem. With this in mind that some coupons will be paid after some specific days, each bond has different remained coupon bond in different days. Thus ado file should make and solve a different equation with respect to remained coupons. I have no idea how I can write such ado file.
Would you mind helping me?
Leave a comment:

I found https://fintechprofessor.com/complet...couponbonds/
almost instantly. However, this website charges.
https://www.researchgate.net/publica...nance_Students
might also be helpful.
If this is not what you want, then you may be able to start with this code and then modify it to fit your problem. Alternatively, write out what you want to calculate, try to program it, and then ask Statalist for help. Don't assume we know the formula you want to program  tell us.
 1 like
Leave a comment:
Leave a comment: