PINE LIBRARY
emaStackCore

Library "emaStackCore"
emaCalc(source, length)
Calculate EMA
Parameters:
source (float): Price source (close, hl2, etc.)
length (simple int): EMA period
Returns: EMA value
emaSlope(emaValue, lookback)
Calculate EMA slope (% change over lookback period)
Parameters:
emaValue (float): Current EMA value
lookback (int): Number of bars to look back
Returns: Slope in percentage
emaDist(ema1, ema2, mode, atrVal, price)
Calculate distance between 2 EMAs (normalized)
Parameters:
ema1 (float): First EMA value
ema2 (float): Second EMA value
mode (string): Normalization mode ("ATR" or "%")
atrVal (float): ATR value for ATR mode
price (float): Reference price for % mode
Returns: Normalized distance
slopeClass(slope, thresholdFlat)
Classify EMA slope
Parameters:
slope (float): EMA slope in %
thresholdFlat (float): Threshold to consider slope as flat
Returns: +1 (rising), 0 (flat), -1 (falling)
slopeScore5(slope10, slope20, slope50, slope100, slope200, threshold)
Calculate combined slope score for 5 EMAs
Parameters:
slope10 (float): EMA10 slope
slope20 (float): EMA20 slope
slope50 (float): EMA50 slope
slope100 (float): EMA100 slope
slope200 (float): EMA200 slope
threshold (float): Flat threshold
Returns: Sum of slope classes (-5 to +5)
emaStack5Score(e10, e20, e50, e100, e200, tolEq)
Calculate EMA stack score (5 EMAs)
Parameters:
e10 (float): EMA 10 value
e20 (float): EMA 20 value
e50 (float): EMA 50 value
e100 (float): EMA 100 value
e200 (float): EMA 200 value
tolEq (float): Tolerance for equality (%)
Returns: Stack score (-4 to +4)
emaStack5ScoreWithSlope(e10, e20, e50, e100, e200, slope10, slope20, slope50, slope100, slope200, tolEq, slopeThreshold)
Enhanced EMA stack with slope
Parameters:
e10 (float)
e20 (float)
e50 (float)
e100 (float)
e200 (float)
slope10 (float)
slope20 (float)
slope50 (float)
slope100 (float)
slope200 (float)
tolEq (float)
slopeThreshold (float)
Returns: [score, slopeScore]
emaRegimeChange(currentScore, previousScore, threshold)
Detect regime change in EMA stack
Parameters:
currentScore (int): Current stack score
previousScore (int): Previous stack score
threshold (int): Minimum change to consider significant
Returns: [changed, direction, delta]
emaCompressed(e10, e20, e50, e100, e200, threshold)
Detect EMA compression (squeeze)
Parameters:
e10 (float): EMA 10 value
e20 (float): EMA 20 value
e50 (float): EMA 50 value
e100 (float): EMA 100 value
e200 (float): EMA 200 value
threshold (float): Max spread % to consider compressed
Returns: [compressed, spread]
emaConfluence9(price, e10_tf1, e20_tf1, e50_tf1, e10_tf2, e20_tf2, e50_tf2, e10_tf3, e20_tf3, e50_tf3, threshold)
Calculate confluence score (how many EMAs are near price)
Parameters:
price (float): Current price
e10_tf1 (float): EMA10 on timeframe 1
e20_tf1 (float): EMA20 on timeframe 1
e50_tf1 (float): EMA50 on timeframe 1
e10_tf2 (float): EMA10 on timeframe 2
e20_tf2 (float): EMA20 on timeframe 2
e50_tf2 (float): EMA50 on timeframe 2
e10_tf3 (float): EMA10 on timeframe 3
e20_tf3 (float): EMA20 on timeframe 3
e50_tf3 (float): EMA50 on timeframe 3
threshold (float): Max distance % to consider "near"
Returns: Confluence count (0-9)
emaConfluence3(price, e10, e20, e50, threshold)
Simplified confluence for 3 EMAs on single TF
Parameters:
price (float)
e10 (float)
e20 (float)
e50 (float)
threshold (float)
Returns: Confluence count (0-3)
emaLeadLag(score15m, score1H, score4H, lookback)
Determine which timeframe is leading the move
Parameters:
score15m (int): Stack score on 15m
score1H (int): Stack score on 1H
score4H (int): Stack score on 4H
lookback (int): Bars to look back for change detection
Returns: Leader timeframe ("15m", "1H", "4H", "ALIGNED", "MIXED")
emaPriceDivergence(price, emaValue, lookback)
Detect divergence between price and EMA direction
Parameters:
price (float): Current price
emaValue (float): EMA value (typically EMA20)
lookback (int): Bars to compare
Returns: [divergence, priceChange%, emaChange%]
emaOrderStrength(e10, e20, e50, e100, e200)
Calculate EMA order strength (weighted by distance)
Parameters:
e10 (float): EMA 10
e20 (float): EMA 20
e50 (float): EMA 50
e100 (float): EMA 100
e200 (float): EMA 200
Returns: Strength score (higher = stronger trend)
emaPriceZone(price, e10, e20, e50, e100, e200)
Determine which EMA zone price is in
Parameters:
price (float): Current price
e10 (float): EMA 10
e20 (float): EMA 20
e50 (float): EMA 50
e100 (float): EMA 100
e200 (float): EMA 200
Returns: Zone code (5=above all, 0=below all, -1=mixed)
emaCalc(source, length)
Calculate EMA
Parameters:
source (float): Price source (close, hl2, etc.)
length (simple int): EMA period
Returns: EMA value
emaSlope(emaValue, lookback)
Calculate EMA slope (% change over lookback period)
Parameters:
emaValue (float): Current EMA value
lookback (int): Number of bars to look back
Returns: Slope in percentage
emaDist(ema1, ema2, mode, atrVal, price)
Calculate distance between 2 EMAs (normalized)
Parameters:
ema1 (float): First EMA value
ema2 (float): Second EMA value
mode (string): Normalization mode ("ATR" or "%")
atrVal (float): ATR value for ATR mode
price (float): Reference price for % mode
Returns: Normalized distance
slopeClass(slope, thresholdFlat)
Classify EMA slope
Parameters:
slope (float): EMA slope in %
thresholdFlat (float): Threshold to consider slope as flat
Returns: +1 (rising), 0 (flat), -1 (falling)
slopeScore5(slope10, slope20, slope50, slope100, slope200, threshold)
Calculate combined slope score for 5 EMAs
Parameters:
slope10 (float): EMA10 slope
slope20 (float): EMA20 slope
slope50 (float): EMA50 slope
slope100 (float): EMA100 slope
slope200 (float): EMA200 slope
threshold (float): Flat threshold
Returns: Sum of slope classes (-5 to +5)
emaStack5Score(e10, e20, e50, e100, e200, tolEq)
Calculate EMA stack score (5 EMAs)
Parameters:
e10 (float): EMA 10 value
e20 (float): EMA 20 value
e50 (float): EMA 50 value
e100 (float): EMA 100 value
e200 (float): EMA 200 value
tolEq (float): Tolerance for equality (%)
Returns: Stack score (-4 to +4)
emaStack5ScoreWithSlope(e10, e20, e50, e100, e200, slope10, slope20, slope50, slope100, slope200, tolEq, slopeThreshold)
Enhanced EMA stack with slope
Parameters:
e10 (float)
e20 (float)
e50 (float)
e100 (float)
e200 (float)
slope10 (float)
slope20 (float)
slope50 (float)
slope100 (float)
slope200 (float)
tolEq (float)
slopeThreshold (float)
Returns: [score, slopeScore]
emaRegimeChange(currentScore, previousScore, threshold)
Detect regime change in EMA stack
Parameters:
currentScore (int): Current stack score
previousScore (int): Previous stack score
threshold (int): Minimum change to consider significant
Returns: [changed, direction, delta]
emaCompressed(e10, e20, e50, e100, e200, threshold)
Detect EMA compression (squeeze)
Parameters:
e10 (float): EMA 10 value
e20 (float): EMA 20 value
e50 (float): EMA 50 value
e100 (float): EMA 100 value
e200 (float): EMA 200 value
threshold (float): Max spread % to consider compressed
Returns: [compressed, spread]
emaConfluence9(price, e10_tf1, e20_tf1, e50_tf1, e10_tf2, e20_tf2, e50_tf2, e10_tf3, e20_tf3, e50_tf3, threshold)
Calculate confluence score (how many EMAs are near price)
Parameters:
price (float): Current price
e10_tf1 (float): EMA10 on timeframe 1
e20_tf1 (float): EMA20 on timeframe 1
e50_tf1 (float): EMA50 on timeframe 1
e10_tf2 (float): EMA10 on timeframe 2
e20_tf2 (float): EMA20 on timeframe 2
e50_tf2 (float): EMA50 on timeframe 2
e10_tf3 (float): EMA10 on timeframe 3
e20_tf3 (float): EMA20 on timeframe 3
e50_tf3 (float): EMA50 on timeframe 3
threshold (float): Max distance % to consider "near"
Returns: Confluence count (0-9)
emaConfluence3(price, e10, e20, e50, threshold)
Simplified confluence for 3 EMAs on single TF
Parameters:
price (float)
e10 (float)
e20 (float)
e50 (float)
threshold (float)
Returns: Confluence count (0-3)
emaLeadLag(score15m, score1H, score4H, lookback)
Determine which timeframe is leading the move
Parameters:
score15m (int): Stack score on 15m
score1H (int): Stack score on 1H
score4H (int): Stack score on 4H
lookback (int): Bars to look back for change detection
Returns: Leader timeframe ("15m", "1H", "4H", "ALIGNED", "MIXED")
emaPriceDivergence(price, emaValue, lookback)
Detect divergence between price and EMA direction
Parameters:
price (float): Current price
emaValue (float): EMA value (typically EMA20)
lookback (int): Bars to compare
Returns: [divergence, priceChange%, emaChange%]
emaOrderStrength(e10, e20, e50, e100, e200)
Calculate EMA order strength (weighted by distance)
Parameters:
e10 (float): EMA 10
e20 (float): EMA 20
e50 (float): EMA 50
e100 (float): EMA 100
e200 (float): EMA 200
Returns: Strength score (higher = stronger trend)
emaPriceZone(price, e10, e20, e50, e100, e200)
Determine which EMA zone price is in
Parameters:
price (float): Current price
e10 (float): EMA 10
e20 (float): EMA 20
e50 (float): EMA 50
e100 (float): EMA 100
e200 (float): EMA 200
Returns: Zone code (5=above all, 0=below all, -1=mixed)
Pine library
In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in publications is governed by House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Pine library
In true TradingView spirit, the author has published this Pine code as an open-source library so that other Pine programmers from our community can reuse it. Cheers to the author! You may use this library privately or in other open-source publications, but reuse of this code in publications is governed by House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.