Exemplo n.º 1
0
def payment_dates(settle, maturity):
    """
    Generates the payment dates between settle and maturity in brazilian markets

    :param settle:
    :param maturity:
    :return:



    """
    date_s = dt.date_dmy(settle)
    date_m = dt.date_dmy(maturity)

    ys, _, _ = dt.date2ymd(date_s)
    ym, _, _ = dt.date2ymd(date_m)
    dates = []

    for year in range(ys, ym + 1):
        # payment at 01/01/xx
        dsemester1 = dt.ymd2date(year, 1, 1)
        # payment at 01/07/xx
        dsemester2 = dt.ymd2date(year, 7, 1)
        dates.extend([dsemester1, dsemester2])

    dates = [d for d in dates if date_s < d <= date_m]

    dates[0] = dt.daysadd(dates[0], -1)

    return dates
Exemplo n.º 2
0
    def date_range(self, start_date, end_date):

        if self.serie_type == "rserie":
            return None

        start_date = dtime.date_dmy(start_date)
        end_date = dtime.date_dmy(end_date)

        table = [self.time]
        list(map(table.append, self.data))
        table = list(zip(*table))

        table2 = [d for d in table if start_date <= d[0] <= end_date]

        table2 = list(zip(*table2))

        return Tserie(time=table2[0], data=table2[1:])
Exemplo n.º 3
0
def bond_price(ytm,
               settle,
               maturity,
               couponRate=0.0,
               facevalue=1000.0,
               coupondates=[]):
    """
    Pre-Fixed Brazilian bonds calculations

    :param ytm:          Yield to maturity rate
    :param settle:   settle date
    :param maturity:     Maturity date
    :param couponRate:   Coupon interest compounded twice per year  [default = 0.0]
    :param facevalue:    Face Value [default: 1000.0 ]
    :return:             price

    Where:
        price : bond price
    """
    # ytm = abs(ytm)

    couponRate = (1 + couponRate)**0.5 - 1

    date0 = dt.date_dmy(settle)
    daten = dt.date_dmy(maturity)

    if couponRate and not coupondates:
        coupondates = payment_dates(settle, maturity)

    PV = 0

    for date in coupondates:
        # Number of business days between settle and
        # coupon payment
        N = dt.daysbus(date0, date)
        f = N / 252.0

        #print dict(N=N, f=f, date=dt.date2str_dmy(date))

        PV += round(1 / (1 + ytm)**f, 8)

    Nm = dt.daysbus(date0, daten)
    fm = Nm / 252.0
    PV = facevalue * (couponRate * PV + round(1 / (1 + ytm)**fm, 8))
    return round(PV, 2)
Exemplo n.º 4
0
    pprint(zip(dates, values)[0:10])

    s.scrap(link)
    link = get_nextlink()

    print("Next = ", link)

    if not link:
        break

    print("----------")

_dates = column(data, 0)
values = column(data, 1)

dates = [dt.date_dmy(d, '/') for d in _dates]
values = [float(s.replace(',', '.')) for s in values]

usd2brl = Tserie(
    dates,
    [values],
    headers=["rate"],
    name="usd2brl",
    description="Dollar to BRL exchange Rate",
    dataprovider="https://www.debit.com.br",
    url=
    "https://www.debit.com.br/consulta20.php?indexador=12&imes=01&iano=2000&fmes=12&fano=2014",
)

thisdir = utils.this_dir()
datasets = utils.resource_path("datasets")
Exemplo n.º 5
0
def bond_yield(price, couponRate, settle, maturity, facevalue=1000.0):
    """
    Calculates the Bond Yield or Internal rate of return.

    :param price:        Spot Price of the bond
    :param couponRate:   Coupon interest rate per year
    :param settle:       Settlement date
    :param maturity:     Maturity date
    :return:
    """
    couponRate = (1 + couponRate)**0.5 - 1

    from m2py.numerical import roots

    date0 = dt.date_dmy(settle)
    daten = dt.date_dmy(maturity)

    if couponRate:
        coupondates = payment_dates(settle, maturity)
    else:
        coupondates = []

    expoents = []
    coefficients = []
    k = couponRate

    for date in coupondates:
        # Number of business days between settle and
        # coupon payment
        N = dt.daysbus(date0, date)
        c = N / 252.0

        #print dict(c=c, N=N)

        expoents.append(c)
        coefficients.append(k)

    # Assemble Equation
    # PV: Price, FV: Face Value
    #
    # f(x) = a1.X^c1 + a2.X^c2 + .... + an.X^cn + a0
    #  f(x) = kX^c1 + kX^c2 + k.X^c3 + ... + (k+1).X^Cn - PV/FV
    #
    #  a1 = k, a2 = k, a3 = k, .... an= k+1,  a0 = -PV/FV
    #  c0 = 0
    #
    #  x = 1/(1+YMT)
    #
    coefficients[-1] = coefficients[-1] + 1
    coefficients.append(-price / facevalue)
    expoents.append(0)

    #from pprint import pprint
    #pprint(zip(coefficients, expoents))

    equation = make_expsum_function(coefficients, expoents)

    #print "result = ", equation(1 / (0.1652 + 1))

    #print stefessen(equation, 0.46, 1e-3, 200)

    #result = roots.regualfalsi(equation, 0, 1, 1e-6, 400)
    result = roots.steffenssen(equation, 1, 1e-6, 400)
    print(result)
    x = result[0]

    #print "x = ", x
    YTM = 1 / x - 1
    return YTM