-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMarket Health Monitor
296 lines (259 loc) · 18.8 KB
/
Market Health Monitor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//@version=5
indicator('Market Health Monitor', shorttitle = 'MHM', overlay = false, precision = 2)
// Libraries
import gotbeatz26107/ma_/2 as ma
// Return On Investment
roi(metric, period) =>
// Calculate returns
var float prev_close = na
var float returns = na
prev_close := metric[period]
returns := (metric / prev_close - 1) * 100
// Profiler
profiler(float src, float percentile, int lookback) =>
poc_val = ta.percentile_linear_interpolation(src, lookback, 50)
lower_boundary_val = ta.percentile_linear_interpolation(src, lookback, 100 - percentile)
upper_boundary_val = ta.percentile_linear_interpolation(src, lookback, percentile)
[poc_val, lower_boundary_val, upper_boundary_val]
//
eval_down(indicator, roi_period, percentile, ma_period, include_indicator) =>
indicator_roi = roi(indicator, roi_period)
[poc, lower, upper] = profiler(indicator_roi, percentile, ma_period)
score = include_indicator ? (indicator_roi < poc ? 50 : 0) + (indicator_roi < lower ? 25 : 0) + (indicator_roi < upper ? 25 : 0) : 0
count = include_indicator? na(indicator) ? 0 : 1 : 0
[score, count]
//
eval_up(indicator, roi_period, percentile, ma_period, include_indicator) =>
indicator_roi = roi(indicator, roi_period)
[poc, lower, upper] = profiler(indicator_roi, percentile, ma_period)
score = include_indicator ? (indicator_roi > poc ? 50 : 0) + (indicator_roi > lower ? 25 : 0) + (indicator_roi > upper ? 25 : 0) : 0
count = include_indicator? na(indicator) ? 0 : 1 : 0
[score, count]
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// General Parameters //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// General Settings
timeframe = input.string('1M', 'Indicator Timeframe', ['1D', '3D', '1W', '2W', '1M'], group = 'General Settings')
average_type = input.string("VWMA", options = ['SMA', 'EMA', 'ALMA', 'DEMA', 'HMA'
, 'JMA', 'KAMA', 'SMMA', 'TMA', 'TSF'
, 'VAR', 'VMA', 'VAMA', 'VWMA', 'WMA'
,'WWMA', 'ZLEMA'], title = "MA Type", group = 'General Settings')
roi_period = input(12, 'ROI Period', group = 'General Settings')
percentile = input(80, 'Percentile', group = 'General Settings')
res_ma = input(1, 'Result Moving Average', group = 'General Settings')
// Moving averages
ma_1 = input(3, '1 - ', group = 'Moving Average Lengths')
ma_2 = input(12, '2 - ', group = 'Moving Average Lengths')
ma_3 = input(36, '3 - ', group = 'Moving Average Lengths')
ma_4 = input(120, '4 - ', group = 'Moving Average Lengths')
// Oscillator Thresholds
recession_threshold = input(50, title = 'Recession Threshold', group = 'Oscillator Thresholds')
growth_threshold = input(25, title = 'Expansion Thresholds', group = 'Oscillator Thresholds')
// Indicators to Include
include_claims = input(true, 'Continued Jobless Claims', group = 'Labor Market')
include_un_rate = input(true, 'Unemployment Rate', group = 'Labor Market')
include_infl_rate = input(true, 'Inflation Rate', group = 'Price Stability')
include_funds_rate = input(true, 'FED Funds Rate', group = 'Interest Rates and Monetary Policy')
include_cci = input(true, 'Consumer Confidence Index', group = 'Consumer Confidence and Spending')
include_gpdi = input(true, 'Real Gross Private Domestic Investment', group = 'Consumer Confidence and Spending')
include_housing_index = input(true, 'Housing Index', group = 'Housing Market')
include_crude_oil = input(true, 'Crude Oil Price', group = 'Sector-Specific Indicators')
include_iptx = input(true, 'Industrial Production Total Index', group = 'Sector-Specific Indicators')
include_bank_tight = input(true, 'Bank Credit Tightening', group = 'Financial Conditions')
include_stocks_growth = input(true, 'Stocks Growth Rate', group = 'Financial Conditions')
include_fed_reserve = input(true, 'GDP Rate', group = 'National Income and Fiscal Health')
include_gdi = input(true, 'Gross Domestic Income', group = 'National Income and Fiscal Health')
include_fctr = input(true, 'FED Current Tax Receipts', group = 'National Income and Fiscal Health')
include_bsti = input(true, 'Total Business Sales to Inventories Ratio', group = 'Business Activity and Inventories')
include_tbs = input(true, 'Total Business Sales', group = 'Business Activity and Inventories')
include_impexp = input(true, 'Import / Export of Goods and Services', group = 'Trade and External Sector')
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Economic Indicators //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Labor Market Indicators //
// These indicators are important for assessing the health of the labor market,
// a key factor in monetary policy decisions due to its implications for inflation and economic stability.
// Contunued Jobless Claims
// Measures the number of people receiving unemployment benefits after the initial claim.
// Reflects the health of the job market.
continued_claims = request.security('USCJC', timeframe, close)
[claims_score, claims_count] = eval_up(continued_claims, roi_period, percentile, ma_4, include_claims)
// Unemployment Rate
// The percentage of the labor force that is jobless and actively looking for employment.
// A key indicator of labor market health.
un_rate = request.security('UNRATE', timeframe, close)
un_rate_1 = ma.selector(un_rate, ma_1, average_type)
un_rate_2 = ma.selector(un_rate, ma_2, average_type)
un_rate_3 = ma.selector(un_rate, ma_3, average_type)
un_score = include_un_rate ? ((un_rate > un_rate_1 ? 50 : 0) + (un_rate_1 > un_rate_2 ? 25 : 0) + (un_rate_2 > un_rate_3 ? 25 : 0)) : 0
un_count = include_un_rate ? na(un_rate) ? 0 : 1 : 0
// Price Stability Indicators //
// Price stability is a primary mandate for many central banks, including the Federal Reserve.
// These indicators help gauge inflationary pressures.
// Inflation rate
// Measures the rate at which the general level of prices for goods and services is rising,
// and, subsequently, purchasing power is falling.
// Central banks attempt to limit inflation.
infl_rate = request.security('USIRYY', timeframe, close)
infl_count = include_infl_rate ? na(infl_rate) ? 0 : 1 : 0
infl_threshold = 1.0
infl_poc = ta.percentile_linear_interpolation(infl_rate, ma_4, 50)
infl_delta = math.abs(infl_rate - infl_poc)
infl_score = include_infl_rate ? infl_delta > infl_threshold ? 100 : 0 : 0
// Interest Rates and Monetary Policy //
// The Federal Reserve sets this rate to influence economic activity, inflation, and employment levels.
// FED Funds Rate
// The interest rate at which depository institutions trade federal funds with each other overnight.
// It's an important benchmark in financial markets.
funds_rate = request.security('FEDFUNDS', timeframe, close)
funds_rate_1 = ma.selector(funds_rate, ma_1, average_type)
funds_rate_2 = ma.selector(funds_rate, ma_2, average_type)
funds_rate_3 = ma.selector(funds_rate, ma_3, average_type)
funds_score = include_funds_rate ? (funds_rate <= funds_rate[1] ? 25 : 0) + (funds_rate_1 <= funds_rate_1[1] ? 25 : 0)
+ (funds_rate_2 <= funds_rate_2[1] ? 25 : 0) + (funds_rate_3 <= funds_rate_3[1] ? 25 : 0) : 0
funds_count = include_funds_rate ? na(funds_rate) ? 0 : 1 : 0
// Consumer Confidence and Spending //
// Consumer confidence and spending are leading indicators of economic activity,
// influencing demand-driven growth and investment trends.
// Consumer Confidence Index
// Reflects the degree of optimism that consumers feel about the overall state of the economy
// and their personal financial situation.
cci_ind = request.security('USCCI', timeframe, close)
cci_1 = ma.selector(cci_ind, ma_1, average_type)
cci_2 = ma.selector(cci_ind, ma_2, average_type)
cci_3 = ma.selector(cci_ind, ma_3, average_type)
cci_score = include_cci ? (cci_ind < cci_1 ? 50 : 0) + (cci_1 < cci_2 ? 25 : 0) + (cci_2 < cci_3 ? 25 : 0) : 0
cci_count = include_cci ? na(cci_ind) ? 0 : 1 : 0
// Real Gross Private Domestic Investment
// Represents the value of all goods and services produced in a country for investment purposes.
// It includes business investments in equipment and structures, residential construction,
// and changes in business inventories.
gpdi = request.security("GPDIC1", timeframe, close)
[gpdi_score, gpdi_count] = eval_down(gpdi, roi_period, percentile, ma_3, include_gpdi)
// Housing Market //
// The housing market is closely watched as it impacts consumer wealth, spending, and overall economic momentum.
// Housing Market Index
// An indicator that reflects the strength of the housing market,
// which can include measures of home prices, construction starts, or sales.
house_index = request.security('USHMI', timeframe, close)
house_poc = ta.percentile_linear_interpolation(house_index, ma_4, 50)
house_score = include_housing_index ? house_index < house_poc ? 100 : 0 : 0
house_count = include_housing_index ? na(house_index) ? 0 : 1 : 0
// Sector-Specific Indicators //
// Industrial Production Total Index
// Measures the output of the industrial sector, including manufacturing, mining, and utilities.
iptx = request.security("INDPRO", timeframe, close)
[iptx_score, iptx_count] = eval_down(iptx, roi_period, percentile, ma_4, include_iptx)
// Crude Oil
// Oil is one of the main commodities needed for transportation and economical operations.
// The price of crude oil can influence inflation and economic growth, as it is a critical input for most economies.
crude_oil = request.security('UKOIL', timeframe, close)
crude_1 = ma.selector(crude_oil, ma_1, average_type)
crude_2 = ma.selector(crude_oil, ma_2, average_type)
crude_3 = ma.selector(crude_oil, ma_3, average_type)
oil_score = include_crude_oil ? (crude_oil < crude_1 ? 50 : 0) + (crude_oil < crude_2 ? 25 : 0) + (crude_oil < crude_3 ? 25 : 0) : 0
oil_count = include_crude_oil ? (na(crude_oil) or na(crude_1) or na(crude_2) or na(crude_3)) ? 0 : 1 : 0
// Financial Conditions //
// These indicators provide insights into the financial health of the economy,
// credit market conditions, and investor sentiment.
// Net Percentage Of Domestic Banks Tightening Credit Standarts
// Reflects changes in the lending practices of banks.
// Tightening credit conditions can signal a cautious or pessimistic outlook by banks.
loans_threshold = input(20, 'Net % of Banks Started Loans Tightening', group = 'Financial Conditions')
consumer_loans = request.security('DRTSCLCC', timeframe, close)
small_biz_loans = request.security('DRTSCIS', timeframe, close)
big_biz_loans = request.security('DRTSCILM', timeframe, close)
consumer_score = consumer_loans >= loans_threshold ? 100.0 : 0
small_biz_score = small_biz_loans >= loans_threshold ? 100.0 : 0
big_biz_score = big_biz_loans >= loans_threshold ? 100.0 : 0
loans_score = include_bank_tight ? (consumer_score + small_biz_score + big_biz_score) / 3 : 0
loans_count = include_bank_tight ? na(loans_score) ? 0 : 1 : 0
// Stock Market Growth Rate
// Indicates the overall health of the stock market and investor sentiment toward the economy.
instrument = input.string('SPX', title = 'Stock Market Index', options = ['SPX', 'NDX', 'RTY'], group = 'Financial Conditions')
index = request.security(instrument, timeframe, close)
index_ma = ma.selector(index, ma_2, average_type)
delta = index_ma - index_ma[1]
growthrate = (delta / index_ma[1]) * 100
growth_count = include_stocks_growth ? na(growthrate) ? 0 : 1 : 0
growth_score = include_stocks_growth ? growthrate <= 0 ? 100 : 0 : 0
// National Income and Fiscal Health //
// These metrics offer a view of the economy's overall income, output, and the government's fiscal position.
// Federal Reserve GDP/Growth
// The growth rate of the Gross Domestic Product, which measures the economic output of a country.
gdp_growth = request.security("GDPC1", timeframe, close)
gdp_count = include_fed_reserve ? na(gdp_growth) ? 0 : 1 : 0
var gdp_score = 0
if gdp_growth>gdp_growth[1]
gdp_score := 0
if gdp_growth<gdp_growth[1]
gdp_score := 100
gdp_score := include_fed_reserve ? gdp_score : 0
// Gross Domestic Income
// Measures the total income earned by all entities in the economy, including wages, profits, and taxes minus subsidies.
gdi = request.security("GDI", timeframe, close)
[gdi_score, gdi_count] = eval_down(gdi, roi_period, percentile, ma_3, include_gdi)
// FED Current Tax Receipts
// Reflects government income from taxes, which can indicate the level of economic activity and profitability.
fctr = request.security("W006RC1Q027SBEA", timeframe, close)
[fctr_score, fctr_count] = eval_down(fctr, roi_period, percentile, ma_4, include_fctr)
// Business Activity and Inventories //
// Business activity indicators provide insights into production, demand, and potential future economic activity.
// Total Business Sales to Inventories Ratio
// A ratio that compares the amount of inventory a business has on hand to the number of sales it makes,
// which can indicate future production needs.
bsti = request.security("ISRATIO", timeframe, close)
[bsti_score, bsti_count] = eval_up(bsti, roi_period, percentile, ma_4, include_bsti)
// Total Business Sales
// The sum of sales made by businesses, reflecting overall business activity.
tbs = request.security("TOTBUSSMSA", timeframe, close)
[tbs_score, tbs_count] = eval_down(tbs, roi_period, percentile, ma_4, include_tbs)
// Trade and External Sector //
// Trade balances and flows are essential for understanding an economy's external position
// and the impact of global economic conditions.
// Imports / Exports of Goods and Services
// Measures the total value of goods and services imported and exported by a country,
// indicating the level of international trade.
impgs = request.security("IMPGSC1", timeframe, close)
impgs_roi = roi(impgs, roi_period)
[poc_impgs, lower_impgs, upper_impgs] = profiler(impgs_roi, percentile, ma_3)
impgs_score = (impgs_roi < poc_impgs ? 50 : 0) + (impgs_roi < lower_impgs ? 25 : 0) + (impgs_roi < upper_impgs ? 25 : 0)
expgs = request.security("EXPGSC1", timeframe, close)
expgs_roi = roi(expgs, roi_period)
[poc_expgs, lower_expgs, upper_expgs] = profiler(expgs_roi, percentile, ma_3)
expgs_score = (expgs_roi < poc_expgs ? 50 : 0) + (expgs_roi < lower_expgs ? 25 : 0) + (expgs_roi < upper_expgs ? 25 : 0)
impexp_score = not include_impexp or na(impgs) or na(expgs) ? 0 : (impgs_score + expgs_score) / 2
impexp_count = not include_impexp or na(impgs) or na(expgs) ? 0 : 1
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Score Calculations //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
indicator_count = un_count + infl_count + funds_count + cci_count + house_count + oil_count
+ growth_count + gdp_count + loans_count + claims_count + gdi_count + impexp_count
+ gpdi_count + fctr_count + iptx_count + bsti_count + tbs_count
score = (un_score + infl_score + funds_score + cci_score + house_score + oil_score + growth_score + gdp_score
+ loans_score + claims_score + gdi_score + impexp_score + gpdi_score + fctr_score
+ iptx_score + bsti_score + tbs_score)
/ indicator_count
score_ma = ta.sma(score, res_ma)
// Indicator states
econ_downtrend = score_ma > recession_threshold
econ_uptrend = score_ma < growth_threshold
recessions = request.security("USREC", timeframe, close) // Actual Recessions
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Plotting //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Colors
white = color.white
aqua = color.aqua
green_signal = color.rgb(0, 137, 123, 50)
red_signal = color.rgb(255, 82, 82, 50)
red_recession = color.new(#ff5252, 60)
bgcolor(econ_downtrend ? red_signal : na, title = 'Downtrend')
bgcolor(econ_uptrend ? green_signal : na, title = 'Uptrend')
bgcolor(recessions ? red_recession : na, title = 'Recession')
// Plots
plot(score_ma, color = aqua, title = 'Oscillator MA', linewidth = 2)
plot(score, color = white, title = 'Oscillator Score', linewidth = 2)
hline(recession_threshold, 'Recession Threshold')
hline(growth_threshold, title = 'Expansion Threshold')