{"id":2311,"date":"2022-04-26T03:03:49","date_gmt":"2022-04-26T03:03:49","guid":{"rendered":"https:\/\/www.tejwin.com\/en\/insights\/yeld-curve-inversion\/"},"modified":"2026-08-24T18:50:17","modified_gmt":"2026-08-24T18:50:17","slug":"yeld-curve-inversion","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insights\/yeld-curve-inversion\/","title":{"rendered":"Yield curve inversion"},"content":{"rendered":"<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1Zy2CmhNm-tMEc6YRoCemaw.jpg\"\/><figcaption class=\"wp-element-caption\">Photo by\u00a0<a href=\"https:\/\/unsplash.com\/@mcthilda?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"noreferrer noopener\" target=\"_blank\">Mathilda Khoo<\/a>\u00a0on\u00a0<a href=\"https:\/\/unsplash.com\/s\/photos\/upside-down?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" rel=\"noreferrer noopener\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\" id=\"4a01\"><strong>Highlights<\/strong><\/h2>\n<ul class=\"wp-block-list\">\n<li>Difficulty\uff1a\u2605\u2605\u2606\u2606\u2606<\/li>\n<li>Advice: This article adopts TEJ API to get the data, Before reading this, we should familar with data visualization.<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\" id=\"e8d6\"><strong>Preface<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"095f\">when the\u00a0<strong>United States 10Y Government Bond -United States 2Y Government Bond &lt;0 ,<\/strong>we call it<strong>\u00a0<\/strong>yield curve inversion \u3002<\/p>\n<p class=\"wp-block-paragraph\" id=\"e9e3\">Usually longer-term bonds will have higher interest rates, but when the market have doubts about the economy ,yield cruve inversion happen! So in this article , we try to plot the Macroeconomic Index a year before it happened, and two years after it happened<\/p>\n<h2 class=\"wp-block-heading\" id=\"0746\"><strong>The Editing Environment and Modules Required<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"6c71\">Window10 Spyder(anaconda31)<\/p>\n<h2 class=\"wp-block-heading\" id=\"ddf7\"><strong>Database<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"358c\"><a href=\"https:\/\/api.tej.com.tw\/columndoc.html?subId=5\" rel=\"noreferrer noopener\" target=\"_blank\">macroeconomic data\u00a0<\/a>:macroeconomic data database code (GLOBAL\/ANMAR)<\/p>\n<pre class=\"wp-block-preformatted\">#######################3<br\/>from datetime import datetime, timedelta <br\/>import pandas as pd<br\/>import numpy as np<br\/>import matplotlib.pyplot as plt <br\/>#########################3<br\/>import tejapi<br\/>import plotly.graph_objects as go<br\/>import plotly.io as pio<br\/>##################<br\/>pio.renderers.default = 'browser'       ##PLOT IN WEB<\/pre>\n<h1 class=\"wp-block-heading\" id=\"54df\"><strong>Data Processing<\/strong><\/h1>\n<p class=\"wp-block-paragraph\" id=\"ade1\">our bonds data is from Tej pro<\/p>\n<pre class=\"wp-block-preformatted\">df=pd.read_excel(\"C:\\\\Users\/user\/Desktop\/Tej\/\u7f8e\u570b\u516c\u50b5\u6b96\u5229\u7387.xlsx\")<\/pre>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1BuoimnQ_9aTyRcycfBqZww.png\"\/><\/figure>\n<p class=\"wp-block-paragraph\" id=\"7fce\">There is Nan in our data , so we should drop it ,After we reset index<\/p>\n<pre class=\"wp-block-preformatted\">df.dropna(inplace=True)df.reset_index(drop=True,inplace=True)<\/pre>\n<h2 class=\"wp-block-heading\" id=\"fcae\"><strong>step 1\u00a0set the indicator<\/strong><\/h2>\n<pre class=\"wp-block-preformatted\">df[\"dif2Y\"]=df.US10Y-df.US2Y<br\/>df[\"invertkeepday\"]=df[\"dif2Y\"].apply(lambda x : 1 if x &lt; 0 else 0 )df[\"cross\"]=np.nan<br\/>df[\"notcross\"]=np.nan<\/pre>\n<p class=\"wp-block-paragraph\" id=\"de62\">calculate the 10y- 2y<\/p>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1WQV869zXvktE1tu8Z1G2nQ.png\"\/><\/figure>\n<p class=\"wp-block-paragraph\" id=\"1dd3\">find the inverse day and visualize it<\/p>\n<pre class=\"wp-block-preformatted\">startday=[]<br\/>endday  =[]<br\/>keep = 0 <br\/>df=df.copy()<br\/>for i in range(len(df)):<br\/>    if df[\"invertkeepday\"][i]  &gt;0 :<br\/>        if keep == 0:        <br\/>            df[\"cross\"][i]=df[\"US10Y\"][i]<br\/>            startday.append(df[\"\u65e5\u671f\"][i])<br\/>            keep = 1<br\/>    elif  keep == 1 :<br\/>        df[\"notcross\"][i]=df[\"US10Y\"][i]if  df[\"invertkeepday\"][i] &lt; 1 :<br\/>            endday.append(df[\"\u65e5\u671f\"][i])<br\/>            keep = 0<\/pre>\n<p class=\"wp-block-paragraph\" id=\"1a1d\">and we want to see how long did it happen<\/p>\n<pre class=\"wp-block-preformatted\">for start , end  in zip(startday,endday):print(start , end, end-start)<\/pre>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1hEFNvsCyoeeEfscErwpl0w.png\"\/><\/figure>\n<p class=\"wp-block-paragraph\" id=\"d473\">We found that some times were really close ,so we make them in a group<\/p>\n<pre class=\"wp-block-preformatted\">inverted=pd.DataFrame(endday,startday)<br\/>inverted.reset_index(inplace=True)<br\/>inverted[\"year\"]=inverted[\"index\"].dt.year<br\/>datetime=inverted.groupby('year')<br\/>datetime=list(datetime)<br\/>getstart=[]<br\/>getend  =[]<br\/>for i  in datetime :<br\/>    i[1].reset_index(drop=True,inplace=True)<br\/>    start = i[1][\"index\"][0]<br\/>    end = i[1][ 0][len(i[1])-1]<br\/>    getstart.append(start)<br\/>    getend.append(end)<br\/>for start , end  in zip(getstart,getend):<br\/>    print(start , end, end-start)<\/pre>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1osrEF7HxPw4NFE139ZeYPw.png\"\/><\/figure>\n<p class=\"wp-block-paragraph\" id=\"43df\">Now we can see the approximately situation<\/p>\n<p class=\"wp-block-paragraph\" id=\"3857\">Nearly three years we found that there have about 6 times yeild curve inverse<\/p>\n<pre class=\"wp-block-preformatted\">df.set_index(\"\u65e5\u671f\",inplace=True)plt.figure(figsize=(12.2,4.5))<br\/>plt.scatter(df.index,df['cross'],color='red', label='cross!',marker='x',alpha=1)<br\/>plt.scatter(df.index,df['notcross'],color='green', label='Notcross',marker='o',alpha=1)<br\/>plt.plot(df[\"US2Y\"], label='2Y', alpha=0.35)<br\/>plt.plot(df[\"US10Y\"], label='10Y', alpha=0.35)<br\/>plt.plot(df[\"dif2Y\"], label='dif', alpha=0.35)plt.title('Close Price Buy &amp; Sell Signals')<br\/>plt.xticks(rotation=45)<br\/>plt.xlabel('Date')  <br\/>plt.ylabel('Price')<br\/>plt.legend()<br\/>plt.grid()<\/pre>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1_1otUG0WPrypLU6dcNgjV1fg.png\"\/><\/figure>\n<h1 class=\"wp-block-heading\" id=\"5ff3\">Step 2 visualize the stocks<\/h1>\n<p class=\"wp-block-paragraph\" id=\"c579\">we have the time so we can input the time into Tejapi to get the macroecnomic data<\/p>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1UrAHFT8rw5wjTLqBHzPq3Q.png\"\/><figcaption class=\"wp-element-caption\">1990<\/figcaption><\/figure>\n<figure class=\"wp-block-image\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1_1-FCY096vjX1lFlsuMLIK9g.png\"\/><figcaption class=\"wp-element-caption\">1990<\/figcaption><\/figure>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1v8Ln3IEsXlYnCwE929duzA.png\"\/><figcaption class=\"wp-element-caption\">1998<\/figcaption><\/figure>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1_1u_IU2r1RMoqlUZq1M-ustQ.png\"\/><figcaption class=\"wp-element-caption\">1998\u5e74<\/figcaption><\/figure>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1ccx8fwWcqtpwvMLGP7ygrw.png\"\/><figcaption class=\"wp-element-caption\">2000<\/figcaption><\/figure>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1_1r8mWhERn6IV7q88VNzmI5A.png\"\/><figcaption class=\"wp-element-caption\">2000\u5e74<\/figcaption><\/figure>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1-JkWeaKqHQewj-0PFDJHLA.png\"\/><figcaption class=\"wp-element-caption\">2006<\/figcaption><\/figure>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1_1tGzQsihjimSE3T1xGFDlyQ.png\"\/><figcaption class=\"wp-element-caption\">2006\u5e74<\/figcaption><\/figure>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1zPYgq0S50f5mKEXfDDVwYw.png\"\/><figcaption class=\"wp-element-caption\">2019<\/figcaption><\/figure>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1_1u3-J_kf5DIvyV26BA36C5w.png\"\/><figcaption class=\"wp-element-caption\">2019\u5e74<\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\" id=\"39c7\"><strong>Conclusion<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"68a7\">From the perspective of various economic data, it may represent that the prosperity has passed its peak, so there are doubts about the future prosperity, selling short-term bonds and preferring longer-term bonds, which leads to the inversion of yield curves<\/p>\n<p class=\"wp-block-paragraph\" id=\"c785\">Although every time an inversion occurs, it will be accompanied by an recession, but in these five expreience ,there was two times that the U.S. economy has not experienced a recession within two years<\/p>\n<p class=\"wp-block-paragraph\" id=\"8535\">In 2019, the recesion happen because of the COVID-19 , yield curve can\u2019t forecast it !<\/p>\n<p class=\"wp-block-paragraph\" id=\"11cd\">1, In the past 5 inversion ,stocks go up 4 time<\/p>\n<p class=\"wp-block-paragraph\" id=\"ffc8\">2, When three recessions occurred, they were all in a rate-cut cycle, and when two recessions did not occur, the U.S. was in a rate-raising cycle<\/p>\n<p class=\"wp-block-paragraph\" id=\"1c5c\">What about now ?<\/p>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1Gm2XfOhAIq0Jh4pCwtBdMw.png\"\/><\/figure>\n<p class=\"wp-block-paragraph\" id=\"61a7\">At present, although the peak of the US economic data has passed, it is still maintain at a very high level. Because of the strong economic performance, the US Federal Reserve has entered a cycle of raising interest rates. Although each inversion will be followed by a recession, this indicator It is a very leading indicator. The past five inversions between the 2 Y and the 10Y have taken an average of 18 months before a recession occurs. Perhaps the inversion is trying to tell us<\/p>\n<p class=\"wp-block-paragraph\" id=\"4ad4\"><strong>Hey ! it\u2019s time to be aware of the risks<\/strong><\/p>\n<h2 class=\"wp-block-heading\" id=\"c6fd\"><strong>Extended Reading<\/strong><\/h2>\n<ul class=\"wp-block-list\">\n<li><a class=\"ek-link\" href=\"\/en\/insight\/svm-model\/\">SVM Model<\/a><\/li>\n<li><a class=\"ek-link\" href=\"\/en\/insight\/lasso-regression-model\/\">Lasso Regression Model<\/a><\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\" id=\"033d\"><strong>Related Link<\/strong><\/h2>\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/api.tej.com.tw\/index.html\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ API<\/a><\/li>\n<li><a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_intro\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ E-Shop<\/a><\/li>\n<\/ul>\n<h1 class=\"wp-block-heading\" id=\"db05\">Source Code<\/h1>\n<ul class=\"wp-block-list\">\n<li><a class=\"ek-link\" href=\"https:\/\/gist.github.com\/tej87681088\/914d6dbf634e03decdb1827733337444#file-tejapi_medium-18-ipynb\" rel=\"noopener\" target=\"_blank\">Click here to go Github<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Highlights Preface when the\u00a0United States 10Y Government Bond -United States 2Y Government Bond <0 ,we call it\u00a0yield curve inversion \u3002 Usually longer-term bonds will have higher interest rates, but when the market have doubts about the economy ,yield cruve inversion happen! So in this article , we try to plot the Macroeconomic Index a year [\u2026]\n<\/p>\n","protected":false},"featured_media":2296,"template":"","tags":[65,70],"insight_category":[90],"class_list":["post-2311","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-python","tag-tej-api","insight_category-industry-insights"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/2311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight"}],"about":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/types\/insight"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/2296"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=2311"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=2311"},{"taxonomy":"insight_category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight_category?post=2311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}