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 market return spread economic indicator. All the coding is using pine script and based on the Tradingview platform.
The stock & bond market return spread has a general tendency to signal the market’s expectation of an upcoming recession or general downturn in growth.
To calculate the difference between the stock market return and bond market return, we use the S&P 500 dividend yield and 10-year US Treasury yield as the return of the two markets. The spread will be the difference between the two rates. To get the treasury yield is very easy as you can find the data of 2-year, 5-year, and 10-year treasury yield from major economic databases like FRED. To get the S&P 500 dividend yield at a specific date, we first get the last four dividends distributed, sum them up, and divided by the day’s price. Again, the daily price and historical dividends can be easily found online.
When the economy is good, people are more likely to invest in stocks because they offer a higher return. The mood leads to an increasing S&P 500 dividend yield; on the other hand, the bond market tends to have a higher return when the economy is not performing well since people become pessimistic about the future and want stable returns over time.
Thus, by looking at the difference between the returns of the two markets, we can get a good sense of how our economy is going and make financial decisions based on that. This article will tell you how to check the rate difference at TradingView.
Below is the full version code:
study("SPY Dividend Ratio Versus US10Y ", precision=5,overlay = false) dividendsValue = security('ESD_FACTSET:AMEX;SPY' + ";DIVIDENDS", "D", close, lookahead=true) yr_div = dividendsValue + dividendsValue + dividendsValue + dividendsValue div_ratio = (yr_div / close)*100 trea = security("US10Y","D",close) plot(div_ratio - trea, color = color.green) - plot(0, color = color.red)
Step One: Initialization
study(“SPY Dividend Ratio Versus US10Y “, precision=5, overlay = false)
“study” is different from “strategy”. It contains calculations and may plot information on charts, but cannot be used in back testing.
overlay: No – False
Step Two: Indicator Calculation
dividendsValue = security(‘ESD_FACTSET:AMEX;SPY’ + “;DIVIDENDS”, “D”, close, lookahead=true)
dividendsValue is the set of SPY’s historical dividends. Each day’s dividend is equal to the last dividend paid, unless a new one is paid on the day
//yearly dividend = sum of four dividends in the past 4 quarters
yr_div = dividendsValue + dividendsValue + dividendsValue + dividendsValue
We know that S&P 500 will distribute dividend 4 times on the last day of each quarter. Thus, for any given day we can locate the annual dividend by adding up the 1st, 64th, 127th and 190th dividends since they must represent the dividend amounts over the last year
div_ratio = (yr_div / close)*100 – we calculate the dividend ratio of a given day in percentage
trea = security(“US10Y”,”D”,close) – trea is the day’s yield of 10-year US Treasury bond
Step Three: Plot
plot(div_ratio – trea, color = color.green) – we plot the yield difference in green
plot(0, color = color.red) – we also label the horizon to better see the trend’s move
The spread spikes since March, which is kind of counter intuitive since the difference should be low or negative when the economy is bad. However, the Fed reduced the interest rate by much, which leads the difference to increase!
#algorithmictrading #algotrading #trading #tradingstrategy #bitcoin #sp500 #stock #traderslife #algo #tradingplan #tradingview #pinescript #yieldspread #marketreturnspread #marketreturn #codingtutorial #beginner #economicindex