OPEN-SOURCE SCRIPT
Crude Oil Macro Regime Indicator

What this indicator is trying to answer
“What macro environment is crude oil (CL) trading in right now?”
It classifies each bar into one of 4 macro regimes by looking at how CL’s returns have been correlating with:
SPY (risk / equities)
DXY (US dollar)
TLT (rates / bonds proxy)
VIX (volatility / fear)
Then it paints:
a thick colored band under price (big visual regime cue) dots above bars (same color cue)
a big panel label on the last bar showing the current regime + the correlation readings
No alerts, just visuals.
Step-by-step: what it calculates
1) Pulls macro “close” data on a chosen timeframe
You pick tf (default 60 minutes). For each bar, it requests closes for:
CL (your chart symbol, using close)
SPY, DXY, TLT, VIX (using request.security)
So all comparisons are made on the same timeframe.
2) Converts prices into returns (log returns)
It computes log returns so the series are comparable:
𝑟=ln
(𝑝𝑟𝑖𝑐𝑒𝑝𝑟𝑖𝑐𝑒[retLen])r=ln(price[retLen]price)
Default retLen=1 means “bar-to-bar” returns.
3) Computes rolling correlations
Over len bars (default 120), it calculates:
cRisk = corr(CL, SPY)
cDollar = corr(CL, DXY)
cRates = corr(CL, TLT)
cVol = corr(CL, VIX)
These are correlations of returns, not prices.
4) Detects volatility expansion (shock filter)
It computes:
atr (14)
atrAvg (100 SMA of ATR)
volSpike = atr / atrAvg
So if current ATR is unusually high vs its normal, that supports a “shock” regime.
How it decides the regime (the “scoring” logic)
Each regime gets a score based on simple rules.
Whichever score is highest becomes the regime.
Regime 1 — GROWTH / RISK-ON (green)
Think: “Oil acting like a growth/risk asset.”
Score increases when:
CL is positively correlated with SPY (above thRiskOn, default 0.25)
CL is not moving with fear (CL~VIX < 0 adds a point)
Dollar isn’t strongly inversely tied (cDollar > -0.10 adds a point)
Regime 2 — INFLATION / COMMODITY (orange)
Think: “Oil behaving like inflation/commodity pressure.”
Score increases when:
SPY link is weak (cRisk < 0.15)
VIX link is muted (cVol < thVolPos)
Dollar relationship is more inverse (cDollar < -0.10)
Rates proxy relationship is positive (cRates > 0)
Regime 3 — LIQUIDITY / DOLLAR (blue)
Think: “Dollar/liquidity is the driver.”
Score increases when:
CL is strongly inverse to DXY (cDollar < thDollarInv, default -0.25)
CL is positively correlated with TLT (cRates > 0.10)
CL is not really acting like SPY (cRisk < 0.10)
Regime 4 — SHOCK / GEOPOLITICAL (red)
Think: “Fear + volatility expansion, oil moving on stress headlines / dislocations.”
Score increases when:
CL is positively correlated with VIX (cVol > thVolPos, default 0.15)
ATR is spiking (volSpike > thVolSpike, default 1.25)
CL is not acting like SPY (cRisk < 0.10)
Tie-break behavior
If two regimes tie for “best”, your selection order is effectively:
Shock → Liquidity → Growth → Inflation
(because of the chained ternary logic).
“No Data”
If correlations or volSpike are na (not enough bars / symbol mismatch / missing history), it sets regime = 0 and shows “NO DATA”.
What you see on the chart
Thick band under price
A wide colored strip plotted below price, with thickness based on ATR (bandHeightATR), colored by regime.
Dots above bars
A small circle above each bar, same color as the current regime, for quick scanning.
Big last-bar panel
On the most recent bar, it prints:
Current regime name
The four correlation numbers
ATR/ATRavg (volSpike)
timeframe + lookback length
How to “read” the panel fast
CL~SPY positive and strong → oil is trading like risk-on (Growth)
CL~DXY strongly negative → dollar/liquidity dominated tape (Liquidity)
CL~VIX positive + ATR spike → stress/shock regime (Shock)
Weak SPY link + inverse dollar + supportive rates → inflation/commodity regime (Inflation)
“What macro environment is crude oil (CL) trading in right now?”
It classifies each bar into one of 4 macro regimes by looking at how CL’s returns have been correlating with:
SPY (risk / equities)
DXY (US dollar)
TLT (rates / bonds proxy)
VIX (volatility / fear)
Then it paints:
a thick colored band under price (big visual regime cue) dots above bars (same color cue)
a big panel label on the last bar showing the current regime + the correlation readings
No alerts, just visuals.
Step-by-step: what it calculates
1) Pulls macro “close” data on a chosen timeframe
You pick tf (default 60 minutes). For each bar, it requests closes for:
CL (your chart symbol, using close)
SPY, DXY, TLT, VIX (using request.security)
So all comparisons are made on the same timeframe.
2) Converts prices into returns (log returns)
It computes log returns so the series are comparable:
𝑟=ln
(𝑝𝑟𝑖𝑐𝑒𝑝𝑟𝑖𝑐𝑒[retLen])r=ln(price[retLen]price)
Default retLen=1 means “bar-to-bar” returns.
3) Computes rolling correlations
Over len bars (default 120), it calculates:
cRisk = corr(CL, SPY)
cDollar = corr(CL, DXY)
cRates = corr(CL, TLT)
cVol = corr(CL, VIX)
These are correlations of returns, not prices.
4) Detects volatility expansion (shock filter)
It computes:
atr (14)
atrAvg (100 SMA of ATR)
volSpike = atr / atrAvg
So if current ATR is unusually high vs its normal, that supports a “shock” regime.
How it decides the regime (the “scoring” logic)
Each regime gets a score based on simple rules.
Whichever score is highest becomes the regime.
Regime 1 — GROWTH / RISK-ON (green)
Think: “Oil acting like a growth/risk asset.”
Score increases when:
CL is positively correlated with SPY (above thRiskOn, default 0.25)
CL is not moving with fear (CL~VIX < 0 adds a point)
Dollar isn’t strongly inversely tied (cDollar > -0.10 adds a point)
Regime 2 — INFLATION / COMMODITY (orange)
Think: “Oil behaving like inflation/commodity pressure.”
Score increases when:
SPY link is weak (cRisk < 0.15)
VIX link is muted (cVol < thVolPos)
Dollar relationship is more inverse (cDollar < -0.10)
Rates proxy relationship is positive (cRates > 0)
Regime 3 — LIQUIDITY / DOLLAR (blue)
Think: “Dollar/liquidity is the driver.”
Score increases when:
CL is strongly inverse to DXY (cDollar < thDollarInv, default -0.25)
CL is positively correlated with TLT (cRates > 0.10)
CL is not really acting like SPY (cRisk < 0.10)
Regime 4 — SHOCK / GEOPOLITICAL (red)
Think: “Fear + volatility expansion, oil moving on stress headlines / dislocations.”
Score increases when:
CL is positively correlated with VIX (cVol > thVolPos, default 0.15)
ATR is spiking (volSpike > thVolSpike, default 1.25)
CL is not acting like SPY (cRisk < 0.10)
Tie-break behavior
If two regimes tie for “best”, your selection order is effectively:
Shock → Liquidity → Growth → Inflation
(because of the chained ternary logic).
“No Data”
If correlations or volSpike are na (not enough bars / symbol mismatch / missing history), it sets regime = 0 and shows “NO DATA”.
What you see on the chart
Thick band under price
A wide colored strip plotted below price, with thickness based on ATR (bandHeightATR), colored by regime.
Dots above bars
A small circle above each bar, same color as the current regime, for quick scanning.
Big last-bar panel
On the most recent bar, it prints:
Current regime name
The four correlation numbers
ATR/ATRavg (volSpike)
timeframe + lookback length
How to “read” the panel fast
CL~SPY positive and strong → oil is trading like risk-on (Growth)
CL~DXY strongly negative → dollar/liquidity dominated tape (Liquidity)
CL~VIX positive + ATR spike → stress/shock regime (Shock)
Weak SPY link + inverse dollar + supportive rates → inflation/commodity regime (Inflation)
סקריפט קוד פתוח
ברוח האמיתית של TradingView, יוצר הסקריפט הזה הפך אותו לקוד פתוח, כך שסוחרים יוכלו לעיין בו ולאמת את פעולתו. כל הכבוד למחבר! אמנם ניתן להשתמש בו בחינם, אך זכור כי פרסום חוזר של הקוד כפוף ל־כללי הבית שלנו.
כתב ויתור
המידע והפרסומים אינם מיועדים להיות, ואינם מהווים, ייעוץ או המלצה פיננסית, השקעתית, מסחרית או מכל סוג אחר המסופקת או מאושרת על ידי TradingView. קרא עוד ב־תנאי השימוש.
סקריפט קוד פתוח
ברוח האמיתית של TradingView, יוצר הסקריפט הזה הפך אותו לקוד פתוח, כך שסוחרים יוכלו לעיין בו ולאמת את פעולתו. כל הכבוד למחבר! אמנם ניתן להשתמש בו בחינם, אך זכור כי פרסום חוזר של הקוד כפוף ל־כללי הבית שלנו.
כתב ויתור
המידע והפרסומים אינם מיועדים להיות, ואינם מהווים, ייעוץ או המלצה פיננסית, השקעתית, מסחרית או מכל סוג אחר המסופקת או מאושרת על ידי TradingView. קרא עוד ב־תנאי השימוש.