# [Step 1] Set up the environment
import lseg.data as ld
from lseg.data.content import search
import datetime
import pandas as pd
ld.open_session()
<lseg.data.session.Definition object at 0x277524adca0 {name='workspace'}>
# [Step 2] Using search function to retrieve the data
df = ld.discovery.search(
view = ld.discovery.Views.GOV_CORP_INSTRUMENTS,
top = 10000,
filter = "((DbType eq 'GOVT' or DbType eq 'CORP' or DbType eq 'AGNC' or DbType eq 'OMUN' or DbType eq 'OTHR') and IsActive eq true and ((RCSRiskOrganisationCountry xeq 'G:53' or RCSRiskOrganisationCountry xeq 'G:3H' or RCSRiskOrganisationCountry xeq 'G:7U' or RCSRiskOrganisationCountry xeq 'G:3I') and RCSCurrency in ('C:6') and RCSCountryGenealogy ne 'M:DH\G:K\G:S\G:6\G:53'))",
select = "RIC,RCSTRBC2012Leaf,IssueDate,EOMAmountOutstanding,PricingRedemDate,IssuerLegalName,PricingClosingYield, CurrentYield, FaceIssuedTotal,EOMPriceChange,RCSBondGradeLeaf,EOMPriceReturn,RCSRiskOrganisationCountry",
)
display(df)
C:\Users\U6086063\Miniconda3\envs\ldlib-24\lib\site-packages\lseg\data\content\search\_data_provider.py:298:UserWarning: Search result for None not full. Result is maxed at 10000 while the total is 11864 rows. Requested - 10000, skipped - 0 rows. C:\Users\U6086063\Miniconda3\envs\ldlib-24\lib\site-packages\pandas\core\dtypes\cast.py:1057:RuntimeWarning: invalid value encountered in cast C:\Users\U6086063\Miniconda3\envs\ldlib-24\lib\site-packages\pandas\core\dtypes\cast.py:1081:RuntimeWarning: invalid value encountered in cast
RIC | RCSTRBC2012Leaf | IssueDate | EOMAmountOutstanding | PricingRedemDate | IssuerLegalName | PricingClosingYield | CurrentYield | FaceIssuedTotal | EOMPriceChange | RCSBondGradeLeaf | EOMPriceReturn | RCSRiskOrganisationCountry | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | VG226919449= | Highway & Bridge Construction | 2021-04-01 | 500000000 | NaT | Powerchina Roadbridge Group (British Virgin Is... | 8.353371 | 3.158974 | 500000000.0 | 0.625 | Investment Grade | 0.0 | G:53 |
1 | CN207889792= | Auto & Truck Manufacturers (NEC) | 2019-12-09 | 500000000 | NaT | GEELY AUTOMOBILE HOLDINGS LIMITED | 8.960789 | 4.020101 | 500000000.0 | 0.125 | High Yield | 0.124602 | G:3H |
2 | KY230704619= | Multiline Utilities | 2021-06-02 | 300000000 | 2026-06-02 | Cheung Kong Infrastructure Finance (BVI) Limited | 20.931142 | 5.42811 | 300000000.0 | 0.625 | <NA> | 0.803101 | G:3H |
3 | CH278532615= | Real Estate Rental, Development & Operations (... | 2024-03-20 | 1571186629 | NaT | CHINA AOYUAN GROUP LIMITED | <NA> | 0.0 | 1571186629.0 | -0.25 | <NA> | -33.333333 | G:53 |
4 | CN223856128= | Banks (NEC) | 2020-11-18 | 2800000000 | 2025-11-18 | Jiao Tong Yin Hang Gu Fen You Xian Gong Si | 5.041834 | 3.851729 | 2800000000.0 | 0.56 | High Yield | 0.55382 | G:53 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
9995 | <NA> | Business Support Services (NEC) | 2024-06-25 | <NA> | NaT | BOCOM INTERNATIONAL PRODUCT AND SOLUTION LIMITED | <NA> | <NA> | 10000000.0 | <NA> | <NA> | <NA> | G:3H |
9996 | <NA> | Business Support Services (NEC) | 2022-07-27 | <NA> | NaT | BOCOM INTERNATIONAL PRODUCT AND SOLUTION LIMITED | <NA> | <NA> | 2400000.0 | <NA> | <NA> | <NA> | G:3H |
9997 | <NA> | Banks (NEC) | 2022-04-29 | <NA> | NaT | Bank of China Ltd (Macau Branch) | <NA> | <NA> | 6500000.0 | <NA> | <NA> | <NA> | G:53 |
9998 | <NA> | Corporate Financial Services (NEC) | 2024-09-18 | <NA> | NaT | CSI Financial Products Limited | <NA> | <NA> | 2000000.0 | <NA> | <NA> | <NA> | G:3H |
9999 | <NA> | Corporate Financial Services (NEC) | 2024-09-19 | <NA> | NaT | CSI Financial Products Limited | <NA> | <NA> | 300000.0 | <NA> | <NA> | <NA> | G:3H |
10000 rows × 13 columns
# [Step 2] Using search function to retrieve the data
df = ld.discovery.search(
view = ld.discovery.Views.GOV_CORP_INSTRUMENTS,
top = 10000,
filter = "((DbType eq 'GOVT' or DbType eq 'CORP' or DbType eq 'AGNC' or DbType eq 'OMUN' or DbType eq 'OTHR') and IsActive eq true and ((RCSRiskOrganisationCountry xeq 'G:53' or RCSRiskOrganisationCountry xeq 'G:3H' or RCSRiskOrganisationCountry xeq 'G:7U' or RCSRiskOrganisationCountry xeq 'G:3I') and RCSCurrency in ('C:6') and RCSCountryGenealogy ne 'M:DH\G:K\G:S\G:6\G:53'))",
select = "RIC,RCSTRBC2012Leaf,IssueDate,EOMAmountOutstanding,PricingRedemDate,IssuerLegalName,PricingClosingYield, CurrentYield, FaceIssuedTotal,EOMPriceChange,RCSBondGradeLeaf,EOMPriceReturn",
)
display(df)
C:\Users\U6086063\Miniconda3\envs\ldlib-24\lib\site-packages\lseg\data\content\search\_data_provider.py:298:UserWarning: Search result for None not full. Result is maxed at 10000 while the total is 11864 rows. Requested - 10000, skipped - 0 rows. C:\Users\U6086063\Miniconda3\envs\ldlib-24\lib\site-packages\pandas\core\dtypes\cast.py:1057:RuntimeWarning: invalid value encountered in cast C:\Users\U6086063\Miniconda3\envs\ldlib-24\lib\site-packages\pandas\core\dtypes\cast.py:1081:RuntimeWarning: invalid value encountered in cast
RIC | RCSTRBC2012Leaf | IssueDate | EOMAmountOutstanding | PricingRedemDate | IssuerLegalName | PricingClosingYield | CurrentYield | FaceIssuedTotal | EOMPriceChange | RCSBondGradeLeaf | EOMPriceReturn | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | VG226919449= | Highway & Bridge Construction | 2021-04-01 | 500000000 | NaT | Powerchina Roadbridge Group (British Virgin Is... | 8.353371 | 3.158974 | 500000000.0 | 0.625 | Investment Grade | 0.0 |
1 | CN207889792= | Auto & Truck Manufacturers (NEC) | 2019-12-09 | 500000000 | NaT | GEELY AUTOMOBILE HOLDINGS LIMITED | 8.960789 | 4.020101 | 500000000.0 | 0.125 | High Yield | 0.124602 |
2 | KY230704619= | Multiline Utilities | 2021-06-02 | 300000000 | 2026-06-02 | Cheung Kong Infrastructure Finance (BVI) Limited | 20.931142 | 5.42811 | 300000000.0 | 0.625 | <NA> | 0.803101 |
3 | CH278532615= | Real Estate Rental, Development & Operations (... | 2024-03-20 | 1571186629 | NaT | CHINA AOYUAN GROUP LIMITED | <NA> | 0.0 | 1571186629.0 | -0.25 | <NA> | -33.333333 |
4 | CN223856128= | Banks (NEC) | 2020-11-18 | 2800000000 | 2025-11-18 | Jiao Tong Yin Hang Gu Fen You Xian Gong Si | 5.041834 | 3.851729 | 2800000000.0 | 0.56 | High Yield | 0.55382 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
9995 | <NA> | Business Support Services (NEC) | 2024-06-25 | <NA> | NaT | BOCOM INTERNATIONAL PRODUCT AND SOLUTION LIMITED | <NA> | <NA> | 10000000.0 | <NA> | <NA> | <NA> |
9996 | <NA> | Business Support Services (NEC) | 2022-07-27 | <NA> | NaT | BOCOM INTERNATIONAL PRODUCT AND SOLUTION LIMITED | <NA> | <NA> | 2400000.0 | <NA> | <NA> | <NA> |
9997 | <NA> | Banks (NEC) | 2022-04-29 | <NA> | NaT | Bank of China Ltd (Macau Branch) | <NA> | <NA> | 6500000.0 | <NA> | <NA> | <NA> |
9998 | <NA> | Corporate Financial Services (NEC) | 2024-09-18 | <NA> | NaT | CSI Financial Products Limited | <NA> | <NA> | 2000000.0 | <NA> | <NA> | <NA> |
9999 | <NA> | Corporate Financial Services (NEC) | 2024-09-19 | <NA> | NaT | CSI Financial Products Limited | <NA> | <NA> | 300000.0 | <NA> | <NA> | <NA> |
10000 rows × 12 columns
response = search.Definition(
view = search.Views.GOV_CORP_INSTRUMENTS,
filter = "((DbType eq 'GOVT' or DbType eq 'CORP' or DbType eq 'AGNC' or DbType eq 'OMUN' or DbType eq 'OTHR') and IsActive eq true and ((RCSRiskOrganisationCountry xeq 'G:53' or RCSRiskOrganisationCountry xeq 'G:3H' or RCSRiskOrganisationCountry xeq 'G:7U' or RCSRiskOrganisationCountry xeq 'G:3I') and RCSCurrency in ('C:6') and RCSCountryGenealogy ne 'M:DH\G:K\G:S\G:6\G:53'))",
top = 0,
navigators = "RCSRiskOrganisationCountry"
).get_data()
response.data.raw["Navigators"]["RCSRiskOrganisationCountry"]["Buckets"]
[{'Label': 'G:3H', 'Count': 6898}, {'Label': 'G:53', 'Count': 4694}, {'Label': 'G:7U', 'Count': 158}, {'Label': 'G:3I', 'Count': 114}]
filter1 = "(RCSRiskOrganisationCountry xeq 'G:53' or RCSRiskOrganisationCountry xeq 'G:3I')"
filter2 = "(RCSRiskOrganisationCountry xeq 'G:3H' or RCSRiskOrganisationCountry xeq 'G:7U')"
def bond_search(filter):
response = search.Definition(
view = search.Views.GOV_CORP_INSTRUMENTS,
filter = f"((DbType eq 'GOVT' or DbType eq 'CORP' or DbType eq 'AGNC' or DbType eq 'OMUN' or DbType eq 'OTHR') and IsActive eq true and ({filter} and RCSCurrency in ('C:6') and RCSCountryGenealogy ne 'M:DH\G:K\G:S\G:6\G:53'))",
top = 10000,
select = "RIC,RCSTRBC2012Leaf,IssueDate,EOMAmountOutstanding,PricingRedemDate,IssuerLegalName,PricingClosingYield, CurrentYield, FaceIssuedTotal,EOMPriceChange,RCSBondGradeLeaf,EOMPriceReturn"
).get_data()
df = response.data.df
return df
df1 = bond_search(filter1)
df2 = bond_search(filter2)
df = pd.concat([df1, df2], ignore_index=True)
df
RIC | RCSTRBC2012Leaf | IssueDate | EOMAmountOutstanding | PricingRedemDate | IssuerLegalName | PricingClosingYield | CurrentYield | FaceIssuedTotal | EOMPriceChange | RCSBondGradeLeaf | EOMPriceReturn | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | VG226919449= | Highway & Bridge Construction | 2021-04-01 | 500000000 | NaT | Powerchina Roadbridge Group (British Virgin Is... | 8.353371 | 3.158974 | 500000000.0 | 0.625 | Investment Grade | 0.0 |
1 | CH278532615= | Real Estate Rental, Development & Operations (... | 2024-03-20 | 1571186629 | NaT | CHINA AOYUAN GROUP LIMITED | <NA> | 0.0 | 1571186629.0 | -0.25 | <NA> | -33.333333 |
2 | CN223856128= | Banks (NEC) | 2020-11-18 | 2800000000 | 2025-11-18 | Jiao Tong Yin Hang Gu Fen You Xian Gong Si | 5.041834 | 3.851729 | 2800000000.0 | 0.56 | High Yield | 0.55382 |
3 | CN245541155= | Construction & Engineering (NEC) | 2022-04-14 | 500000000 | 2027-04-14 | DIANJIAN HAIYU LIMITED | 5.099941 | 4.336735 | 500000000.0 | 0.375 | Investment Grade | 0.377728 |
4 | CN209223643= | Banks (NEC) | 2019-12-13 | <NA> | NaT | China Construction Bank Corp (Hong Kong Branch) | <NA> | <NA> | 500000000.0 | <NA> | <NA> | <NA> |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
11859 | CN254312533= | Residential Real Estate Rental & Development | 2022-10-20 | <NA> | NaT | Landsea Green Management Limited | <NA> | <NA> | 118994400.0 | <NA> | <NA> | <NA> |
11860 | HK208588311= | Real Estate Rental, Development & Operations (... | 2019-12-12 | 288840000 | NaT | Hua Nan Cheng Kong Gu You Xian Gong Si | <NA> | <NA> | 350000000.0 | 0.0 | <NA> | <NA> |
11861 | XS2369300681=CDCM | Corporate Financial Services (NEC) | 2021-07-28 | <NA> | NaT | CSI Financial Products Limited | <NA> | <NA> | 7700000.0 | <NA> | <NA> | <NA> |
11862 | VG233315486= | Real Estate Rental, Development & Operations (... | 2021-05-17 | 230000000 | NaT | JIAYUAN INTERNATIONAL GROUP LIMITED | <NA> | <NA> | 230000000.0 | -0.5 | <NA> | <NA> |
11863 | <NA> | Investment Holding Companies (NEC) | 2005-03-16 | <NA> | NaT | NEW ASAT (FINANCE) LIMITED | <NA> | <NA> | 150000000.0 | <NA> | <NA> | <NA> |
11864 rows × 12 columns
# [Step 3] Visualize the Kungfu bonds
# 3.1 ) TRBC Pie
import plotly.express as px
rt = df.groupby("RCSTRBC2012Leaf",as_index=False).agg('count')
rt = rt.sort_values('RIC', ascending = False).head(10)
fig = px.pie(rt, values='RIC', names='RCSTRBC2012Leaf', title='TRBC Pie',color_discrete_sequence=px.colors.sequential.RdBu)
fig.show()
# 3.2 ) Bond Grade Pie
df['RCSBondGradeLeaf'].fillna("No Grade", inplace=True)
grad = df.groupby("RCSBondGradeLeaf",as_index=False).agg('count')
fig = px.pie(grad, values='RIC', names='RCSBondGradeLeaf', title='Bond Grade Pie',color_discrete_sequence=px.colors.sequential.RdBu)
fig.show()
# 3.3 ) Issued amount last 12 months
from datetime import date
from dateutil.relativedelta import relativedelta
decb = date.today() + relativedelta(months=-12)
start = datetime.datetime(decb.year, decb.month, 1).strftime("%Y-%m-%d %H:%M:%S")[0:10]
end = date.today().strftime("%Y-%m-%d %H:%M:%S")[0:10]
cols = ['RIC','IssueDate','FaceIssuedTotal']
c = df[cols].copy()
c.loc[:,"IssueDate"] = pd.to_datetime(c["IssueDate"])
c = c.set_index("IssueDate").sort_index(ascending=False)
c_month = c.resample('BM').sum()
c_monthb = c_month[start:end]
c_monthb.index.name = None
import plotly.express as px
fig = px.bar(c_monthb, y='FaceIssuedTotal', title = 'Issued amount last 12 months', color_discrete_sequence=px.colors.sequential.RdBu)
fig.show()
# 3.4 ) Redem amount in 10 years
yearsf = date.today() + relativedelta(years=+10)
end = datetime.datetime(yearsf.year, yearsf.month, 1).strftime("%Y-%m-%d %H:%M:%S")[0:4]
start = date.today().strftime("%Y-%m-%d %H:%M:%S")[0:4]
cols = ['RIC','PricingRedemDate','FaceIssuedTotal']
d = df[cols].copy()
d.loc[:,"PricingRedemDate"] = pd.to_datetime(d["PricingRedemDate"], errors = 'coerce')
d = d.set_index("PricingRedemDate").sort_index(ascending=True)
d_month = d.resample('BY').sum()
d_monthf = d_month[start:end]
d_monthf.index.name = None
import plotly.express as px
fig = px.bar(d_monthf, y='FaceIssuedTotal',title = 'Redem amount in 10 years', color_discrete_sequence=px.colors.sequential.RdBu)
fig.show()
# 3.5 ) Newly issued bonds last 3 months
monb3 = date.today() + relativedelta(months=-3)
start = datetime.datetime(monb3.year, monb3.month, 1).strftime("%Y-%m-%d %H:%M:%S")[0:10]
end = date.today().strftime("%Y-%m-%d %H:%M:%S")[0:10]
print(start, end)
cols = ['RIC','IssueDate','FaceIssuedTotal','RCSTRBC2012Leaf']
f = df[cols].copy()
f.loc[:,"IssueDate"] = pd.to_datetime(f["IssueDate"])
f = f.set_index("IssueDate").sort_index(ascending=True)
f_3m = f[start:end]
rt = f_3m.groupby("RCSTRBC2012Leaf",as_index=False).agg('count')
rt = rt.sort_values('RIC', ascending = False).head(10)
fig = px.pie(rt, values='RIC', names='RCSTRBC2012Leaf', title='newly issued bonds last 3 months',color_discrete_sequence=px.colors.sequential.RdBu)
fig.show()
2024-06-01 2024-09-30
# 3.6 ) Price top 10
cols = ['RIC','IssuerLegalName','EOMPriceReturn','EOMPriceChange']
price = df[cols].copy()
price_top10 = price.sort_values('EOMPriceReturn', ascending = False).head(10)
price_top10
RIC | IssuerLegalName | EOMPriceReturn | EOMPriceChange | |
---|---|---|---|---|
4746 | VG229375067= | ZHENRO PROPERTIES GROUP LIMITED | 300.0 | 0.375 |
4516 | VG222689821= | ZHENRO PROPERTIES GROUP LIMITED | 300.0 | 0.375 |
11843 | KY224431511= | REDSUN PROPERTIES GROUP LIMITED | 50.0 | 0.25 |
3956 | 62914VAG1= | NIO INC. | 36.8125 | 24.4052 |
4010 | 62914VAH9= | NIO INC. | 32.333021 | 20.0063 |
40 | USG11056AA91= | BEST INC. | 25.87146 | 20.499 |
41 | 08653CAA4= | BEST INC. | 25.869873 | 20.4995 |
39 | 08653CAB2= | BEST INC. | 25.869873 | 20.4995 |
3944 | CN285974623= | Zhong Guo Ping An Bao Xian (Ji Tuan) Gu Fen Yo... | 20.639041 | 21.237 |
3252 | VG226484086= | CHINA HONGQIAO GROUP LIMITED | 17.61138 | 30.125 |
# 3.7 ) Yield
cols = ['RIC','IssuerLegalName','CurrentYield','PricingClosingYield']
y = df[cols]
y = y[y['CurrentYield']>0]
# 3.7.1 ) Yield top 10
yield_top10 = y.sort_values('CurrentYield', ascending = False).head(10)
yield_top10
RIC | IssuerLegalName | CurrentYield | PricingClosingYield | |
---|---|---|---|---|
4694 | CN224803265= | Yang Guang Cheng Jia Shi Guo Ji You Xian Gong Si | 6000.0 | 1390889.198562 |
4749 | VG201351260= | ZHENRO PROPERTIES GROUP LIMITED | 2944.8 | <NA> |
4722 | VG229030884= | RONSHINE CHINA HOLDINGS LIMITED | 2840.0 | 126000.0 |
4516 | VG222689821= | ZHENRO PROPERTIES GROUP LIMITED | 1470.0 | 57774.193548 |
4746 | VG229375067= | ZHENRO PROPERTIES GROUP LIMITED | 1340.0 | 642.837833 |
11843 | KY224431511= | REDSUN PROPERTIES GROUP LIMITED | 973.333333 | 46705.882353 |
4644 | VG200347161= | AGILE GROUP HOLDINGS LIMITED | 827.946667 | <NA> |
4077 | CH278530264=RRPS | CHINA AOYUAN GROUP LIMITED | 733.333333 | 84.258514 |
4076 | CH278530299= | CHINA AOYUAN GROUP LIMITED | 733.333333 | 84.258514 |
4607 | VG178542273= | AGILE GROUP HOLDINGS LIMITED | 598.933333 | <NA> |
# 3.7.2 ) Yield last 10
yield_last10 = y.sort_values('CurrentYield', ascending = True).head(10)
yield_last10
RIC | IssuerLegalName | CurrentYield | PricingClosingYield | |
---|---|---|---|---|
2457 | BCMKFC24013=RRPS | Jiao Tong Yin Hang Gu Fen You Xian Gong Si | 0.051486 | 4.700459 |
3874 | VG233295710= | Li Auto Inc. | 0.207793 | -4.865266 |
3873 | 50202MAB8= | Li Auto Inc. | 0.210542 | -4.506101 |
3875 | 50202MAA0= | Li Auto Inc. | 0.210542 | -4.506101 |
3938 | G8208BAA9= | JD.COM, INC. | 0.220932 | -2.396061 |
3937 | 47215PAH9= | JD.COM, INC. | 0.220932 | -2.396061 |
3924 | USGDS0329= | GDS HOLDINGS LIMITED | 0.25 | 0.249998 |
4042 | G01719AJ5= | ALIBABA GROUP HOLDING LIMITED | 0.411184 | -2.460243 |
4043 | 01609WBF8= | ALIBABA GROUP HOLDING LIMITED | 0.411184 | -2.460243 |
3538 | 090040AE6= | BILIBILI INC. | 0.480377 | -1.349106 |
ld.close_session()