Skip to main content

A Python library for analysis of fixed income instruments in Brazil

Project description

PyPI version Made with Python License

PYield: Brazilian Fixed Income Toolkit

PYield is a Python library designed for the analysis of Brazilian fixed income instruments. Leveraging the power of popular Python libraries like Pandas and Requests, PYield simplifies the process of obtaining and processing data from key sources such as ANBIMA, BCB, IBGE and B3.

Documentation: https://crdcj.github.io/PYield/

Source Code: https://github.com/crdcj/PYield

Key Features

  • Data Collection: Automated fetching of data from ANBIMA and B3.
  • Data Processing: Efficient processing and normalization of fixed income data.
  • Analysis Tools: Built-in functions for common analysis tasks in fixed income markets.
  • Easy Integration: Seamless integration with pandas data analysis workflows.
  • Type Hints: Full support for static type checking, enhancing development experience and code quality.

Installation

You can install PYield using pip:

pip install pyield

How to use PYield

Important Note on Date Formats

When using date strings in PYield functions, please ensure that the date format is day-first (e.g., "31-05-2024"). This format was chosen to be consistent with the Brazilian date convention.

For production code, it is recommended to parse date strings with pandas.to_datetime using an explicit format to avoid ambiguity and ensure consistency.

For example:

import pandas as pd
# Converting a date string to a pandas Timestamp with a specific format
date = pd.to_datetime("2024/31/05", format="%Y/%d/%m")
date = pd.to_datetime("05-31-2024", format="%m-%d-%Y")

Brazilian Treasury Bonds Tools

>>> from pyield import ntnb, ntnf, ltn

# Calculate the quotation of a NTN-B bond as per ANBIMA's rules
>>> ntnb.quotation("31-05-2024", "15-05-2035", 0.061490)
99.3651
>>> ntnb.quotation("31-05-2024", "15-08-2060", 0.061878)
99.5341

# Calculate the DI Spread of NTN-F bonds in a given date
>>> ntnf.di_spreads("17-07-2024")
2025-01-01   -2.31
2027-01-01   -1.88
2029-01-01   -3.26
2031-01-01    3.61
2033-01-01   -3.12
2035-01-01   -1.00
Name: DISpread, dtype: Float64

# Get ANBIMA's indicative rates for LTN bonds
>>> ltn.anbima_rates("17-07-2024")
2024-10-01    0.104236
2025-01-01    0.105400
2025-04-01    0.107454
2025-07-01    0.108924
2025-10-01    0.110751
2026-01-01    0.111753
2026-04-01    0.112980
2026-07-01    0.113870
2026-10-01    0.114592
2027-07-01    0.116090
2028-01-01    0.117160
2028-07-01    0.118335
2030-01-01    0.120090
Name: IndicativeRate, dtype: Float64

Business Days Tools (Brazilian holidays are automatically considered)

>>> from pyield import bday
# Count the number of business days between two dates
# Start date is included, end date is excluded
>>> bday.count(start='29-12-2023', end='02-01-2024')
1

# Get the next business day after a given date (offset=1)
>>> bday.offset(dates="29-12-2023", offset=1)
Timestamp('2024-01-02 00:00:00')

# Get the next business day if it is not a business day (offset=0)
>>> bday.offset(dates="30-12-2023", offset=0)
Timestamp('2024-01-02 00:00:00')

# Since 29-12-2023 is a business day, it returns the same date (offset=0)
>>> bday.offset(dates="29-12-2023", offset=0)
Timestamp('2023-12-29 00:00:00')

# Generate a pandas series with the business days between two dates
>>> bday.generate(start='29-12-2023', end='03-01-2024')
0   2023-12-29
1   2024-01-02
2   2024-01-03
dtype: datetime64[ns]

Futures Data

>>> import pyield as yd
# Fetch current DI Futures data from B3 (15 minutes delay)
>>> yd.futures(contract_code="DI1")
TradeTime      TickerSymbol ExpirationDate BDaysToExp ... MaxRate LastAskRate LastBidRate CurrentRate
2024-04-21 13:37:39       DI1K24     2024-05-02          7 ... 0.10660     0.10652     0.10660  0.10660
2024-04-21 13:37:39       DI1M24     2024-06-03         28 ... 0.10518     0.10510     0.10516  0.10518
2024-04-21 13:37:39       DI1N24     2024-07-01         48 ... 0.10480     0.10456     0.10462  0.10460
                ...          ...            ...        ... ...     ...         ...         ...      ...
2024-04-21 13:37:39       DI1F37     2037-01-02       3183 ...    <NA>        <NA>     0.11600     <NA>
2024-04-21 13:37:39       DI1F38     2038-01-04       3432 ...    <NA>        <NA>     0.11600     <NA>
2024-04-21 13:37:39       DI1F39     2039-01-03       3683 ...    <NA>        <NA>        <NA>     <NA>

# Fetch historical DI Futures data from B3
>>> yd.futures(contract_code="DI1", reference_date='08-03-2024')
TradeDate  TickerSymbol ExpirationDate BDaysToExp ... LastRate LastAskRate LastBidRate SettlementRate
2024-03-08       DI1J24     2024-04-01         15 ...   10.952      10.952      10.956         10.956
2024-03-08       DI1K24     2024-05-02         37 ...   10.776      10.774      10.780         10.777
2024-03-08       DI1M24     2024-06-03         58 ...   10.604      10.602      10.604         10.608
       ...          ...            ...        ... ...      ...         ...         ...            ...
2024-03-08       DI1F37     2037-01-02       3213 ...     <NA>        <NA>        <NA>         10.859
2024-03-08       DI1F38     2038-01-04       3462 ...     <NA>        <NA>        <NA>         10.859
2024-03-08       DI1F39     2039-01-03       3713 ...     <NA>        <NA>        <NA>         10.85

Indicators Data

>>> import pyield as yd
# Fetch the SELIC target rate from the Central Bank of Brazil
>>> yd.indicator(indicator_code="SELIC", reference_date='12-04-2024')
0.1075  # 10.75%

# Fetch the IPCA monthly inflation rate from IBGE
>>> yd.indicator(indicator_code="IPCA", reference_date='18-03-2024')
0.16  # 0.16%

# If no data is yet available for the indicator, the function returns None
>>> yd.indicator(indicator_code="IPCA", reference_date='10-04-2024')
None

Projections Data

>>> import pyield as yd
# Fetch current month projection for IPCA from IBGE API
>>> ipca = yd.projection(projection_code="IPCA_CM")
>>> print(ipca)
IndicatorProjection(
    last_updated=Timestamp('2024-04-19 18:55:00'),
    reference_month_ts=Timestamp('2024-04-01 00:00:00'),
    reference_month_br='ABR/2024',
    projected_value=0.0035  # 0.35%
)
>>> ipca.projected_value
0.0035  # 0.35%

Interpolation Tools

>>> from pyield import Interpolator
# Interpolate interest rates for specific business days using the Interpolator class.

# Initialize the Interpolator with known business days and interest rates.
>>> known_bdays = [30, 60, 90]
>>> known_rates = [0.045, 0.05, 0.055]
>>> linear_interpolator = Interpolator("linear", known_bdays, known_rates)

# Interpolate the interest rate for a given number of business days.
>>> linear_interpolator(45)
0.0475

# Use the flat forward method for interpolation.
>>> ff_interpolator = Interpolator("flat_forward", known_bdays, known_rates)
>>> ff_interpolator(45)
0.04833068080970859

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyield-0.17.2.tar.gz (41.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyield-0.17.2-py3-none-any.whl (53.5 kB view details)

Uploaded Python 3

File details

Details for the file pyield-0.17.2.tar.gz.

File metadata

  • Download URL: pyield-0.17.2.tar.gz
  • Upload date:
  • Size: 41.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.18.1 CPython/3.12.5 Linux/6.10.6-200.fc40.x86_64

File hashes

Hashes for pyield-0.17.2.tar.gz
Algorithm Hash digest
SHA256 9d1a3c7fc0cc48da7aeb3848d3eb41a0a31eacd36dfcfd9d19e77bfa5a2e9a71
MD5 ac0684022216c70a17fe7ae674608908
BLAKE2b-256 c2d42941e03bf9a75f2a5f11f4d2f57808f549a7211e4d716691ba6e37b9e4f2

See more details on using hashes here.

File details

Details for the file pyield-0.17.2-py3-none-any.whl.

File metadata

  • Download URL: pyield-0.17.2-py3-none-any.whl
  • Upload date:
  • Size: 53.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.18.1 CPython/3.12.5 Linux/6.10.6-200.fc40.x86_64

File hashes

Hashes for pyield-0.17.2-py3-none-any.whl
Algorithm Hash digest
SHA256 330f8c92e5feb074c76be9776c7d97009a6818d5611c67bce904a40a8f3dc1ba
MD5 3e93026aa7e479bf152906d5d25769d8
BLAKE2b-256 2fdd8be7de2d8f61a8887cd34db2a89774031afacf9b6d41235bab1e32402523

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page