Algo trading is not as difficult as you think. In this tutorial article, there are three simple steps to lead the beginner to understand how to code your own best algorithm trading strategy for treasury spread economic indicator. All the coding is using pine script and based on the Tradingview platform.
The 10-year/2-year US Treasury Yield Spread has a general tendency to signal the market’s expectation of an upcoming recession or general downturn in growth.
The 10-year/2-year US Treasury Yield Spread = 10-Year Treasury Constant Maturity Minus 2-Year Treasury Constant Maturity.
Long-term Treasury yields are often used to examine the country’s economic inflation, while short-term ones are often used to predict the country’s interest rate. When the spread rises, the US economic risk increases, which is suitable for investing in bonds. When the spread declines, the US economic risk decreases, which is suitable for investing in stocks. When the spread turns negative (referred to as the Inverted yield curve), the US economy will reach its peak, revealing that in the next 1 to 2 years, US stocks may have a significant decline (1990, 2000, and 2008).
The width of the yield spread between these two securities helps to support predictions on whether the economy will experience a recession or a recovery over the course of the next 12 months. The spread between the yields on the two- and 10-year U.S. Treasury notes, for example, is an important gauge regarding the current “shape” of the yield curve. When the market foresees an environment of stronger growth, higher inflation, and/or interest-rate increases by the Federal Reserve, the yield curves steepen, meaning long-term yield rises more than short-term yield.
Conversely, when investors expect weaker growth, lower inflation, and easier Fed policy, the yield curves often flatten. In this case, the yields on longer-term bonds fall more than the yields on short-term issues. One of the most popular ways to measure these changes is to measure the difference between the yields
Below is the full version code:
study("US 10 Year & 2 Year Treasury Spread", overlay = false)
us2y=security("US02Y", "D", close)
us10y = security("US10Y","D",close)
diff = us10y-us2y
plot(diff,color = color.red)
Step One: Initialization
study(“US 10 Year & 2 Year Treasury Spread”, overlay = false)
“study” is different from “strategy”. It contains calculations and may plot information on charts, but cannot be used in back testing.
overlay: Yes – True; No – False
Step Two: Input
us2y=security(“US02Y”, “D”, close)
us10y = security(“US10Y”,”D”,close)
us2y – US 2-Year Treasury Yield
us10y – US 10-Year Treasury Yield
security – the function that enables to extract data from the symbol
“D”- we set the interval as each day
“close”- we use the closing price
Index Calculation: diff = us10y-us2y
Diff-The 10-year/2-year US Treasury Spread
Step Three: Plot
plot(diff,color = color.red) -color for the index is set as red