PINE LIBRARY
Updated key_level_alerts_lib

Library "key_level_alerts_lib"
shouldFireAlertAbove(level, acceptableRange, filterEnabled)
Detects if price crossed above and closed above a level on this bar
Parameters:
level (float): The price level to check for cross
acceptableRange (float): The maximum distance from level for close to be considered valid (used when filterEnabled=true)
filterEnabled (bool): When true, checks that close is within acceptable range above level
Returns: true if price crossed and closed above the level (optionally within acceptable range)
Detection logic:
Condition A: Opened below level AND closed above level (direct cross up)
Condition B: Opened above level, low touched/dipped to level (low <= level), closed above level
shouldFireAlertBelow(level, acceptableRange, filterEnabled)
Detects if price crossed below and closed below a level on this bar
Parameters:
level (float): The price level to check for cross
acceptableRange (float): The maximum distance from level for close to be considered valid (used when filterEnabled=true)
filterEnabled (bool): When true, checks that close is within acceptable range below level
Returns: true if price crossed and closed below the level (optionally within acceptable range)
Detection logic:
Condition A: Opened above level AND closed below level (direct cross down)
Condition B: Opened below level, high touched/spiked to level (high >= level), closed below level
processAlertAbove(level, acceptableRange, currentState, alertEnabled, filterEnabled)
Process "crossed above" alert for a level - fires alert if transitioning from below/unknown to above
Parameters:
level (float): The price level to check
acceptableRange (float): The acceptable range threshold for filtering
currentState (int): Current alert state (-1=below, 0=unknown, 1=above)
alertEnabled (bool): Whether the alert is enabled
filterEnabled (bool): Whether to apply acceptable range filtering
Returns: [newState, shouldFire] tuple where newState is updated alert state and shouldFire indicates if alert should trigger
State behavior:
- Alert fires when: state was -1 or 0 AND cross above detected AND alert enabled
- State updates to 1 (above) on any cross above
- State updates to -1 (below) on any cross below (resets for next above alert)
processAlertBelow(level, acceptableRange, currentState, alertEnabled, filterEnabled)
Process "crossed below" alert for a level - fires alert if transitioning from above/unknown to below
Parameters:
level (float): The price level to check
acceptableRange (float): The acceptable range threshold for filtering
currentState (int): Current alert state (-1=below, 0=unknown, 1=above)
alertEnabled (bool): Whether the alert is enabled
filterEnabled (bool): Whether to apply acceptable range filtering
Returns: [newState, shouldFire] tuple where newState is updated alert state and shouldFire indicates if alert should trigger
State behavior:
- Alert fires when: state was 1 or 0 AND cross below detected AND alert enabled
- State updates to -1 (below) on any cross below
- State updates to 1 (above) on any cross above (resets for next below alert)
calculateAcceptableRange(atrValue, acceptableRangePct)
Calculates the acceptable range threshold for alert filtering based on ATR
Parameters:
atrValue (float): The daily ATR value
acceptableRangePct (float): The acceptable range percentage (e.g., 85 for 85%)
Returns: The acceptable range threshold in price units
Calculation: oneFullLevel = ATR × 0.24 (24% of daily ATR = one standard key level spacing)
acceptableRange = (acceptableRangePct / 100) × oneFullLevel
getAlertLabelTextSize(sizeStr)
Converts a size string to Pine Script size constant for labels
Parameters:
sizeStr (string): The size string ("tiny", "small", "normal", "large", "huge")
Returns: The corresponding Pine Script size constant
shouldFireAlertAbove(level, acceptableRange, filterEnabled)
Detects if price crossed above and closed above a level on this bar
Parameters:
level (float): The price level to check for cross
acceptableRange (float): The maximum distance from level for close to be considered valid (used when filterEnabled=true)
filterEnabled (bool): When true, checks that close is within acceptable range above level
Returns: true if price crossed and closed above the level (optionally within acceptable range)
Detection logic:
Condition A: Opened below level AND closed above level (direct cross up)
Condition B: Opened above level, low touched/dipped to level (low <= level), closed above level
shouldFireAlertBelow(level, acceptableRange, filterEnabled)
Detects if price crossed below and closed below a level on this bar
Parameters:
level (float): The price level to check for cross
acceptableRange (float): The maximum distance from level for close to be considered valid (used when filterEnabled=true)
filterEnabled (bool): When true, checks that close is within acceptable range below level
Returns: true if price crossed and closed below the level (optionally within acceptable range)
Detection logic:
Condition A: Opened above level AND closed below level (direct cross down)
Condition B: Opened below level, high touched/spiked to level (high >= level), closed below level
processAlertAbove(level, acceptableRange, currentState, alertEnabled, filterEnabled)
Process "crossed above" alert for a level - fires alert if transitioning from below/unknown to above
Parameters:
level (float): The price level to check
acceptableRange (float): The acceptable range threshold for filtering
currentState (int): Current alert state (-1=below, 0=unknown, 1=above)
alertEnabled (bool): Whether the alert is enabled
filterEnabled (bool): Whether to apply acceptable range filtering
Returns: [newState, shouldFire] tuple where newState is updated alert state and shouldFire indicates if alert should trigger
State behavior:
- Alert fires when: state was -1 or 0 AND cross above detected AND alert enabled
- State updates to 1 (above) on any cross above
- State updates to -1 (below) on any cross below (resets for next above alert)
processAlertBelow(level, acceptableRange, currentState, alertEnabled, filterEnabled)
Process "crossed below" alert for a level - fires alert if transitioning from above/unknown to below
Parameters:
level (float): The price level to check
acceptableRange (float): The acceptable range threshold for filtering
currentState (int): Current alert state (-1=below, 0=unknown, 1=above)
alertEnabled (bool): Whether the alert is enabled
filterEnabled (bool): Whether to apply acceptable range filtering
Returns: [newState, shouldFire] tuple where newState is updated alert state and shouldFire indicates if alert should trigger
State behavior:
- Alert fires when: state was 1 or 0 AND cross below detected AND alert enabled
- State updates to -1 (below) on any cross below
- State updates to 1 (above) on any cross above (resets for next below alert)
calculateAcceptableRange(atrValue, acceptableRangePct)
Calculates the acceptable range threshold for alert filtering based on ATR
Parameters:
atrValue (float): The daily ATR value
acceptableRangePct (float): The acceptable range percentage (e.g., 85 for 85%)
Returns: The acceptable range threshold in price units
Calculation: oneFullLevel = ATR × 0.24 (24% of daily ATR = one standard key level spacing)
acceptableRange = (acceptableRangePct / 100) × oneFullLevel
getAlertLabelTextSize(sizeStr)
Converts a size string to Pine Script size constant for labels
Parameters:
sizeStr (string): The size string ("tiny", "small", "normal", "large", "huge")
Returns: The corresponding Pine Script size constant
Release Notes
v2Release Notes
v3Pine 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.