{"id":16859,"date":"2022-04-26T03:03:49","date_gmt":"2022-04-25T19:03:49","guid":{"rendered":"https:\/\/www.tejwin.com\/?post_type=insight&#038;p=16859"},"modified":"2023-08-17T13:45:59","modified_gmt":"2023-08-17T05:45:59","slug":"yeld-curve-inversion","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insight\/yeld-curve-inversion\/","title":{"rendered":"Yield curve inversion"},"content":{"rendered":"\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1Zy2CmhNm-tMEc6YRoCemaw.jpg\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Photo by&nbsp;<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>&nbsp;on&nbsp;<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\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a05bffa7e879\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"ez-toc-cssicon\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a05bffa7e879\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.tejwin.com\/en\/insight\/yeld-curve-inversion\/#Highlights\" >Highlights<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.tejwin.com\/en\/insight\/yeld-curve-inversion\/#Preface\" >Preface<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.tejwin.com\/en\/insight\/yeld-curve-inversion\/#The_Editing_Environment_and_Modules_Required\" >The Editing Environment and Modules Required<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.tejwin.com\/en\/insight\/yeld-curve-inversion\/#Database\" >Database<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.tejwin.com\/en\/insight\/yeld-curve-inversion\/#step_1_set_the_indicator\" >step 1\u00a0set the indicator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.tejwin.com\/en\/insight\/yeld-curve-inversion\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.tejwin.com\/en\/insight\/yeld-curve-inversion\/#Extended_Reading\" >Extended Reading<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.tejwin.com\/en\/insight\/yeld-curve-inversion\/#Related_Link\" >Related Link<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"4a01\"><span class=\"ez-toc-section\" id=\"Highlights\"><\/span><strong>Highlights<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Difficulty\uff1a\u2605\u2605\u2606\u2606\u2606<\/li>\n\n\n\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\n\n\n<h2 class=\"wp-block-heading\" id=\"e8d6\"><span class=\"ez-toc-section\" id=\"Preface\"><\/span><strong>Preface<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"095f\">when the&nbsp;<strong>United States 10Y Government Bond -United States 2Y Government Bond &lt;0 ,<\/strong>we call it<strong>&nbsp;<\/strong>yield curve inversion \u3002<\/p>\n\n\n\n<p 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\n\n\n<h2 class=\"wp-block-heading\" id=\"0746\"><span class=\"ez-toc-section\" id=\"The_Editing_Environment_and_Modules_Required\"><\/span><strong>The Editing Environment and Modules Required<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"6c71\">Window10 Spyder(anaconda31)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ddf7\"><span class=\"ez-toc-section\" id=\"Database\"><\/span><strong>Database<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"358c\"><a href=\"https:\/\/api.tej.com.tw\/columndoc.html?subId=5\" rel=\"noreferrer noopener\" target=\"_blank\">macroeconomic data&nbsp;<\/a>:macroeconomic data database code (GLOBAL\/ANMAR)<\/p>\n\n\n\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\n\n\n<h1 class=\"wp-block-heading\" id=\"54df\"><strong>Data Processing<\/strong><\/h1>\n\n\n\n<p id=\"ade1\">our bonds data is from Tej pro<\/p>\n\n\n\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\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1BuoimnQ_9aTyRcycfBqZww.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"7fce\">There is Nan in our data , so we should drop it ,After we reset index<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">df.dropna(inplace=True)df.reset_index(drop=True,inplace=True)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"fcae\"><span class=\"ez-toc-section\" id=\"step_1_set_the_indicator\"><\/span><strong>step 1\u00a0set the indicator<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\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\n\n\n<p id=\"de62\">calculate the 10y- 2y<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1WQV869zXvktE1tu8Z1G2nQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"1dd3\">find the inverse day and visualize it<\/p>\n\n\n\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\n\n\n<p id=\"1a1d\">and we want to see how long did it happen<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for start , end  in zip(startday,endday):print(start , end, end-start)<\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1hEFNvsCyoeeEfscErwpl0w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"d473\">We found that some times were really close ,so we make them in a group<\/p>\n\n\n\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\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1osrEF7HxPw4NFE139ZeYPw.png\" alt=\"\"\/><\/figure>\n\n\n\n<p id=\"43df\">Now we can see the approximately situation<\/p>\n\n\n\n<p id=\"3857\">Nearly three years we found that there have about 6 times yeild curve inverse<\/p>\n\n\n\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\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_1otUG0WPrypLU6dcNgjV1fg.png\" alt=\"\"\/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"5ff3\">Step 2 visualize the stocks<\/h1>\n\n\n\n<p id=\"c579\">we have the time so we can input the time into Tejapi to get the macroecnomic data<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1UrAHFT8rw5wjTLqBHzPq3Q.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">1990<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_1-FCY096vjX1lFlsuMLIK9g.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">1990<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1v8Ln3IEsXlYnCwE929duzA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">1998<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_1u_IU2r1RMoqlUZq1M-ustQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">1998\u5e74<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1ccx8fwWcqtpwvMLGP7ygrw.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">2000<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_1r8mWhERn6IV7q88VNzmI5A.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">2000\u5e74<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1-JkWeaKqHQewj-0PFDJHLA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">2006<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_1tGzQsihjimSE3T1xGFDlyQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">2006\u5e74<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1zPYgq0S50f5mKEXfDDVwYw.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">2019<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_1u3-J_kf5DIvyV26BA36C5w.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">2019\u5e74<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"39c7\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p 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\n\n\n<p 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\n\n\n<p id=\"8535\">In 2019, the recesion happen because of the COVID-19 , yield curve can\u2019t forecast it !<\/p>\n\n\n\n<p id=\"11cd\">1, In the past 5 inversion ,stocks go up 4 time<\/p>\n\n\n\n<p 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\n\n\n<p id=\"1c5c\">What about now ?<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1Gm2XfOhAIq0Jh4pCwtBdMw.png\" alt=\"\"\/><\/figure>\n\n\n\n<p 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\n\n\n<p id=\"4ad4\"><strong>Hey ! it\u2019s time to be aware of the risks<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c6fd\"><span class=\"ez-toc-section\" id=\"Extended_Reading\"><\/span><strong>Extended Reading<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.tejwin.com\/en\/insight\/svm-model\/\" class=\"ek-link\">SVM Model<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.tejwin.com\/en\/insight\/lasso-regression-model\/\" class=\"ek-link\">Lasso Regression Model<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"033d\"><span class=\"ez-toc-section\" id=\"Related_Link\"><\/span><strong>Related Link<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\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\n\n\n<li><a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_intro\" target=\"_blank\" rel=\"noreferrer noopener\">TEJ E-Shop<\/a><\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"db05\">Source Code<\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/gist.github.com\/tej87681088\/914d6dbf634e03decdb1827733337444#file-tejapi_medium-18-ipynb\" class=\"ek-link\" target=\"_blank\" rel=\"noopener\">Click here to go Github<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Highlights Preface when the&nbsp;United States 10Y Government Bond -United States 2Y Government Bond &lt;0 ,we call it&nbsp;yield 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 [&hellip;]<\/p>\n","protected":false},"featured_media":16860,"template":"","tags":[2371,3008,2541],"insight-category":[690,50],"class_list":["post-16859","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-python","tag-tejapi-quant","tag-2541","insight-category-data-analysis","insight-category-fintech"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/16859","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"}],"version-history":[{"count":1,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/16859\/revisions"}],"predecessor-version":[{"id":16894,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/16859\/revisions\/16894"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/16860"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=16859"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=16859"},{"taxonomy":"insight-category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight-category?post=16859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}