PINE LIBRARY
ที่อัปเดต: OHLCVRangeX

The OHLCVRange library provides modular range-building utilities for Pine Script v6 based on custom conditions like time, price, volatility, volume, and pattern detection. Each function updates a persistent range (OHLCVRange) passed in from the calling script, based on live streaming candles.
This library is designed to support dynamic windowing over incoming OHLCV bars, with all persistent state handled externally (in the indicator or strategy). The library merely acts as a filter and updater, appending or clearing candles according to custom logic.
📦
export type OHLCVRange
OHLCV.OHLCV[] candles // Sliding window of candles
The OHLCVRange is a simple container holding an array of OHLCV.OHLCV structures.
This structure should be declared in the indicator using var to ensure persistence across candles.
🧩 Range Updater Functions
Each function follows this pattern:
export updateXxxRange(OHLCVRange r, OHLCV.OHLCV current, ...)
r is the range to update.
current is the latest OHLCV candle (typically from your indicator).
Additional parameters control the behavior of the range filter.
🔁 Function List
1. Fixed Lookback Range
export updateFixedRange(OHLCVRange r, OHLCV.OHLCV current, int barsBack)
Keeps only the last barsBack candles.
Sliding window based purely on number of bars.
2. Session Time Range
export updateSessionRange(OHLCVRange r, OHLCV.OHLCV current, int minuteStart, int minuteEnd)
Keeps candles within the [minuteStart, minuteEnd) intraday session.
Clears the range once out of session bounds.
3. Price Zone Range
export updatePriceZoneRange(OHLCVRange r, OHLCV.OHLCV current, float minP, float maxP)
Retains candles within the vertical price zone [minP, maxP].
Clears when a candle exits the zone.
4. Consolidation Range
export updateConsolidationRange(OHLCVRange r, OHLCV.OHLCV current, float thresh)
Stores candles as long as the candle range (high - low) is less than or equal to thresh.
Clears on volatility breakout.
5. Volume Spike Range
export updateVolumeSpikeRange(OHLCVRange r, OHLCV.OHLCV current, float avgVol, float mult, int surround)
Triggers a new range when a volume spike ≥ avgVol * mult occurs.
Adds candles around the spike (total surround * 2 + 1).
Can be used to zoom in around anomalies.
6. Engulfing Pattern Range
export updateEngulfingRange(OHLCVRange r, OHLCV.OHLCV current, int windowAround)
Detects bullish or bearish engulfing candles.
Stores 2 * windowAround + 1 candles centered around the pattern.
Clears if no valid engulfing pattern is found.
7. HTF-Aligned Range
export updateHTFAlignedRange(OHLCVRange r, OHLCV.OHLCV current, OHLCV.OHLCV prevHtf)
Used when aligning lower timeframe candles to higher timeframe bars.
Clears and restarts the range on HTF bar transition (compare prevHtf.bar_index with current).
Requires external management of HTF candle state.
💡 Usage Notes
All OHLCVRange instances should be declared as var in the indicator to preserve state:
var OHLCVRange sessionRange = OHLCVRange.new()
sessionRange := OHLCVRange.updateSessionRange(sessionRange, current, 540, 900)
All OHLCV data should come from the OHLCVData library (v15 or later):
import userId/OHLCVData/15 as OHLCV
OHLCV.OHLCV current = OHLCV.getCurrentChartOHLCV()
This library does not use var internally to enforce clean separation of logic and persistence.
📅 Planned Enhancements
Fib zone ranges: capture candles within custom Fibonacci levels.
Custom event ranges: combine multiple filters (e.g., pattern + volume spike).
Trend-based ranges: windowing based on moving average or trend breaks.
This library is designed to support dynamic windowing over incoming OHLCV bars, with all persistent state handled externally (in the indicator or strategy). The library merely acts as a filter and updater, appending or clearing candles according to custom logic.
📦
export type OHLCVRange
OHLCV.OHLCV[] candles // Sliding window of candles
The OHLCVRange is a simple container holding an array of OHLCV.OHLCV structures.
This structure should be declared in the indicator using var to ensure persistence across candles.
🧩 Range Updater Functions
Each function follows this pattern:
export updateXxxRange(OHLCVRange r, OHLCV.OHLCV current, ...)
r is the range to update.
current is the latest OHLCV candle (typically from your indicator).
Additional parameters control the behavior of the range filter.
🔁 Function List
1. Fixed Lookback Range
export updateFixedRange(OHLCVRange r, OHLCV.OHLCV current, int barsBack)
Keeps only the last barsBack candles.
Sliding window based purely on number of bars.
2. Session Time Range
export updateSessionRange(OHLCVRange r, OHLCV.OHLCV current, int minuteStart, int minuteEnd)
Keeps candles within the [minuteStart, minuteEnd) intraday session.
Clears the range once out of session bounds.
3. Price Zone Range
export updatePriceZoneRange(OHLCVRange r, OHLCV.OHLCV current, float minP, float maxP)
Retains candles within the vertical price zone [minP, maxP].
Clears when a candle exits the zone.
4. Consolidation Range
export updateConsolidationRange(OHLCVRange r, OHLCV.OHLCV current, float thresh)
Stores candles as long as the candle range (high - low) is less than or equal to thresh.
Clears on volatility breakout.
5. Volume Spike Range
export updateVolumeSpikeRange(OHLCVRange r, OHLCV.OHLCV current, float avgVol, float mult, int surround)
Triggers a new range when a volume spike ≥ avgVol * mult occurs.
Adds candles around the spike (total surround * 2 + 1).
Can be used to zoom in around anomalies.
6. Engulfing Pattern Range
export updateEngulfingRange(OHLCVRange r, OHLCV.OHLCV current, int windowAround)
Detects bullish or bearish engulfing candles.
Stores 2 * windowAround + 1 candles centered around the pattern.
Clears if no valid engulfing pattern is found.
7. HTF-Aligned Range
export updateHTFAlignedRange(OHLCVRange r, OHLCV.OHLCV current, OHLCV.OHLCV prevHtf)
Used when aligning lower timeframe candles to higher timeframe bars.
Clears and restarts the range on HTF bar transition (compare prevHtf.bar_index with current).
Requires external management of HTF candle state.
💡 Usage Notes
All OHLCVRange instances should be declared as var in the indicator to preserve state:
var OHLCVRange sessionRange = OHLCVRange.new()
sessionRange := OHLCVRange.updateSessionRange(sessionRange, current, 540, 900)
All OHLCV data should come from the OHLCVData library (v15 or later):
import userId/OHLCVData/15 as OHLCV
OHLCV.OHLCV current = OHLCV.getCurrentChartOHLCV()
This library does not use var internally to enforce clean separation of logic and persistence.
📅 Planned Enhancements
Fib zone ranges: capture candles within custom Fibonacci levels.
Custom event ranges: combine multiple filters (e.g., pattern + volume spike).
Trend-based ranges: windowing based on moving average or trend breaks.
เอกสารเผยแพร่
v2Added:
fastClear(r)
Parameters:
r (OHLCVRange)
getRangeHigh(r)
Parameters:
r (OHLCVRange)
getRangeLow(r)
Parameters:
r (OHLCVRange)
getRangeVolume(r)
Parameters:
r (OHLCVRange)
getRangeAvg(r)
Parameters:
r (OHLCVRange)
mergeRanges(r1, r2)
Parameters:
r1 (OHLCVRange)
r2 (OHLCVRange)
Updated:
updatePriceZoneRange(r, current, minP, maxP, bufferPct)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
minP (float)
maxP (float)
bufferPct (float)
updateVolumeSpikeRange(r, current, avgVol, mult, surround, confirmBars)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
avgVol (float)
mult (float)
surround (int)
confirmBars (int)
updateHTFAlignedRange(r, current, tf, prevHtf)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
tf (string)
prevHtf (OHLCV type from viorel8/OHLCVData/15)
OHLCVRange
Fields:
id (series string)
start_bar_index (series int)
end_bar_index (series int)
start_time (series int)
end_time (series int)
candles (array<OHLCV> type from viorel8/OHLCVData/15)
duration (series int)
isValid (series bool)
เอกสารเผยแพร่
v3 - fixing the range validation functionsm and adding some debugging capabilities Added:
updateRangeCommon(r, current, isValid, clear)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
isValid (bool)
clear (bool)
เอกสารเผยแพร่
v4Updated:
updateVolumeSpikeRange(r, current, avgVol, mult, surround, confirmBars, spikeConfirmed)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
avgVol (float)
mult (float)
surround (int)
confirmBars (int)
spikeConfirmed (int)
เอกสารเผยแพร่
v5Added ATR Delta to compute automaticlly the consolidation range using volatility treshhold
Updated:
updateConsolidationRange(r, current, atr, atrMultiplier, minCandles)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
atr (float)
atrMultiplier (float)
minCandles (int)
เอกสารเผยแพร่
v6fixing Update Consolidation Range
เอกสารเผยแพร่
v7 - fix some issues,testing all the ranges in order to check edge cases เอกสารเผยแพร่
v8 - fix issuesเอกสารเผยแพร่
v9 - update the trashhold computation, for assets with high volatility we need to have tighter ATR treshholdเอกสารเผยแพร่
v10* adding a volume filter to identify breakouts, nad setup a grace limit for the range
Updated:
updateConsolidationRange(r, current, atr, atrMultiplier, minCandles, useVolumeFilter, volMultiplier, gracePeriod)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
atr (float)
atrMultiplier (float)
minCandles (int)
useVolumeFilter (bool)
volMultiplier (float)
gracePeriod (int)
เอกสารเผยแพร่
v11 - fix trashhold issues เอกสารเผยแพร่
v12We’ll modify updateConsolidationRange to:
Calculate isVolatile using the range including the current candle for inConsol.
Ensure breakout detection uses the range excluding the current candle.
เอกสารเผยแพร่
v13 dinamically updateing ht erange as new candles are added and reset it after a brakout
Updated:
OHLCVRange
Fields:
id (series string)
start_bar_index (series int)
end_bar_index (series int)
start_time (series int)
end_time (series int)
candles (array<OHLCV> type from viorel8/OHLCVData/15)
duration (series int)
isValid (series bool)
count (series int)
เอกสารเผยแพร่
v14 fix the array null issue, adding some validationsเอกสารเผยแพร่
v15fixing logic
เอกสารเผยแพร่
v16Summary
The library is clearing the range too early, which breaks downstream usage.
✅ Fix: Set clear = false inside the library.
📦 Caller should handle finalization and cleanup.
🧠 In the future, you can add a caching mechanism (or even events or callback functions if TradingView eventually supports them).
Updated:
OHLCVRange
Fields:
id (series string)
start_bar_index (series int)
end_bar_index (series int)
start_time (series int)
end_time (series int)
candles (array<OHLCV> type from viorel8/OHLCVData/15)
duration (series int)
isValid (series bool)
count (series int)
finalized (series bool)
เอกสารเผยแพร่
v17the clear responsability will be in the indicator
เอกสารเผยแพร่
v18Updated:
updateConsolidationRange(r, current, atr, atrMultiplier, minCandles, useVolumeFilter, volMultiplier, gracePeriod, confirmBars)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
atr (float)
atrMultiplier (float)
minCandles (int)
useVolumeFilter (bool)
volMultiplier (float)
gracePeriod (int)
confirmBars (int)
OHLCVRange
Fields:
id (series string)
start_bar_index (series int)
end_bar_index (series int)
start_time (series int)
end_time (series int)
candles (array<OHLCV> type from viorel8/OHLCVData/15)
duration (series int)
isValid (series bool)
count (series int)
breakoutConfirmCandles (series int)
finalized (series bool)
เอกสารเผยแพร่
v19* move some computations form indicator into the library
เอกสารเผยแพร่
v20* code revire and comments
Updated:
updateVolumeSpikeRange(r, current, avgVol, mult, surround, confirmBars)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
avgVol (float)
mult (float)
surround (int)
confirmBars (int)
เอกสารเผยแพร่
v21cleanup
Updated:
OHLCVRange
Fields:
id (series string)
start_bar_index (series int)
end_bar_index (series int)
start_time (series int)
end_time (series int)
candles (array<OHLCV> type from viorel8/OHLCVData/15)
duration (series int)
isValid (series bool)
count (series int)
breakoutConfirmCandles (series int)
finalized (series bool)
inConsolidation (series bool)
เอกสารเผยแพร่
v22* refactor for better states management, now we have the samepattern for finalize and isValid cross all ranges, so in the indicator we will use the funcitons with more eaze
Added:
getRangeOpen(r)
Parameters:
r (OHLCVRange)
getRangeClose(r)
Parameters:
r (OHLCVRange)
updateAccumulationRange(r, current, atr, atrMultiplier, minCandles, useVolumeFilter, volMultiplier, gracePeriod, confirmBars)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
atr (float)
atrMultiplier (float)
minCandles (int)
useVolumeFilter (bool)
volMultiplier (float)
gracePeriod (int)
confirmBars (int)
updateManipulationRange(r, current, atr, minCandles, gracePeriod, avgVolumeLookback, lowVolMultiplier, highVolAtrMultiplier, priceDeltaAtrMultiplier)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
atr (float)
minCandles (int)
gracePeriod (int)
avgVolumeLookback (int)
lowVolMultiplier (float)
highVolAtrMultiplier (float)
priceDeltaAtrMultiplier (float)
updateDistributionRange(r, current, atr, minCandles, gracePeriod, avgVolumeLookback, highVolMultiplier, highVolAtrMultiplier, priceDeltaAtrMultiplier)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
atr (float)
minCandles (int)
gracePeriod (int)
avgVolumeLookback (int)
highVolMultiplier (float)
highVolAtrMultiplier (float)
priceDeltaAtrMultiplier (float)
updateOpeningRange(r, current, orbDurationMinutes, sessionStartHour, sessionStartMinute)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
orbDurationMinutes (int)
sessionStartHour (int)
sessionStartMinute (int)
updateCompressionRange(r, current, isInCompressionCondition, minCompressionBars, gracePeriodExit)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
isInCompressionCondition (bool)
minCompressionBars (int)
gracePeriodExit (int)
updateInsideBarRange(r, current, prev1)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
prev1 (OHLCV type from viorel8/OHLCVData/15)
Updated:
updateRangeCommon(r, current, shouldBeValidAfterAdd)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
shouldBeValidAfterAdd (bool)
updateHTFAlignedRange(r, current, tfStr, prevHtfOhlcv)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
tfStr (string)
prevHtfOhlcv (OHLCV type from viorel8/OHLCVData/15)
OHLCVRange
Fields:
id (series string)
start_bar_index (series int)
end_bar_index (series int)
start_time (series int)
end_time (series int)
candles (array<OHLCV> type from viorel8/OHLCVData/15)
duration (series int)
isValid (series bool)
count (series int)
breakoutConfirmCandles (series int)
finalized (series bool)
Removed:
updateConsolidationRange(r, current, atr, atrMultiplier, minCandles, useVolumeFilter, volMultiplier, gracePeriod, confirmBars)
เอกสารเผยแพร่
v23Updated:
updateInsideBarRange(r, current)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
เอกสารเผยแพร่
v24* fixing the AMD ranges
Added:
updateFVGRange(r, current)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
Updated:
updateAccumulationRange(r, current, atr, atrMultiplier, minCandles, useVolumeFilter, volMultiplier, accAvgVolumeLookback, gracePeriod, confirmBars)
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
atr (float)
atrMultiplier (float)
minCandles (int)
useVolumeFilter (bool)
volMultiplier (float)
accAvgVolumeLookback (int)
gracePeriod (int)
confirmBars (int)
เอกสารเผยแพร่
v25* add Delta ATR Adaptive range, in order to help user to configure the indicaotr correctly be statistically tracking hte past voaltility
* in this version the computations are made in the test indicator, but in the next version we will mode the computational functions in th elibrary
เอกสารเผยแพร่
v26Updated:
* now we are using smart grace period, meaning that we do nto have a static fixed number of candles that we wait till a range expires
เอกสารเผยแพร่
v27 - clear the publish screen เอกสารเผยแพร่
v28Added:
updateLowVolumeRange(r, current, isLowVolume, isSpike, minBarsToStart, graceOnNormalVol)
updateLowVolumeRange
description NEW: Handles the specific state logic for the Low Volume Phase (exit on spike).
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
isLowVolume (bool)
isSpike (bool)
minBarsToStart (int)
graceOnNormalVol (int)
เอกสารเผยแพร่
v29Updated:
updateAccumulationRange(r, current, atr, atrMultiplier, minCandles, useVolumeFilter, volMultiplier, accAvgVolumeLookback, gracePeriod, confirmBars)
updateAccumulationRange
description CORRECTED: Identifies periods of low volatility, now with robust state management and breakout confirmation logic.
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
atr (float)
atrMultiplier (float)
minCandles (int)
useVolumeFilter (bool)
volMultiplier (float)
accAvgVolumeLookback (int)
gracePeriod (int)
confirmBars (int)
updateCompressionRange(r, current, isConditionMet, minBarsToStart, consecutiveFailsForExit, magnitudeMultiplierForExit)
updateCompressionRange
description Identifies a sustained phase using the "Smart Grace Period" logic.
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
isConditionMet (bool)
minBarsToStart (int)
consecutiveFailsForExit (int)
magnitudeMultiplierForExit (float)
updateLowVolumeRange(r, current, isLowVolume, isSpike, minBarsToStart, graceOnNormalVol)
updateLowVolumeRange
description Handles the specific state logic for the Low Volume Phase (exit on spike).
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
isLowVolume (bool)
isSpike (bool)
minBarsToStart (int)
graceOnNormalVol (int)
Removed:
updateVolumeSpikeRange(r, current, avgVol, mult, surround, confirmBars)
updateVolumeSpikeRange
description Detects a volume spike and creates a range of the bars immediately surrounding it.
updateEngulfingRange(r, current, windowAround)
updateEngulfingRange
description Detects a bullish or bearish engulfing candlestick pattern.
updateHTFAlignedRange(r, current, tfStr, prevHtfOhlcv)
updateHTFAlignedRange
description Creates ranges that align with the bars of a higher timeframe.
updateOpeningRange(r, current, orbDurationMinutes, sessionStartHour, sessionStartMinute)
updateOpeningRange
description Defines the high-low range of the initial period of a trading session.
updateInsideBarRange(r, current)
updateInsideBarRange
description Detects an Inside Bar pattern, storing the Mother Bar as the range.
updateFVGRange(r, current)
updateFVGRange
description Identifies Fair Value Gaps (FVGs) or price imbalances.
เอกสารเผยแพร่
v30Updated:
updateAccumulationRange(r, current, atr, atrMultiplier, minCandles, useVolumeFilter, volMultiplier, accAvgVolumeLookback, gracePeriod, confirmBars)
updateAccumulationRange
description REWRITTEN: Correctly identifies low volatility periods with robust state management.
Parameters:
r (OHLCVRange)
current (OHLCV type from viorel8/OHLCVData/15)
atr (float)
atrMultiplier (float)
minCandles (int)
useVolumeFilter (bool)
volMultiplier (float)
accAvgVolumeLookback (int)
gracePeriod (int)
confirmBars (int)
ไลบรารีไพน์
ด้วยเจตนารมณ์หลักของ TradingView ผู้เขียนได้เผยแพร่ Pine code นี้เป็นโอเพนซอร์สไลบรารีเพื่อให้ Pine โปรแกรมเมอร์คนอื่นในชุมชนของเราสามารถนำไปใช้ซ้ำได้ ต้องขอบคุณผู้เขียน! คุณสามารถใช้ไลบรารีนี้ในแบบส่วนตัวหรือในการเผยแพร่แบบโอเพนซอร์สอื่น ๆ แต่การนำโค้ดนี้ไปใช้ในการเผยแพร่ซ้ำจะต้องอยู่ภายใต้ กฎระเบียบการใช้งาน
คำจำกัดสิทธิ์ความรับผิดชอบ
ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมที่ ข้อกำหนดการใช้งาน
ไลบรารีไพน์
ด้วยเจตนารมณ์หลักของ TradingView ผู้เขียนได้เผยแพร่ Pine code นี้เป็นโอเพนซอร์สไลบรารีเพื่อให้ Pine โปรแกรมเมอร์คนอื่นในชุมชนของเราสามารถนำไปใช้ซ้ำได้ ต้องขอบคุณผู้เขียน! คุณสามารถใช้ไลบรารีนี้ในแบบส่วนตัวหรือในการเผยแพร่แบบโอเพนซอร์สอื่น ๆ แต่การนำโค้ดนี้ไปใช้ในการเผยแพร่ซ้ำจะต้องอยู่ภายใต้ กฎระเบียบการใช้งาน
คำจำกัดสิทธิ์ความรับผิดชอบ
ข้อมูลและบทความไม่ได้มีวัตถุประสงค์เพื่อก่อให้เกิดกิจกรรมทางการเงิน, การลงทุน, การซื้อขาย, ข้อเสนอแนะ หรือคำแนะนำประเภทอื่น ๆ ที่ให้หรือรับรองโดย TradingView อ่านเพิ่มเติมที่ ข้อกำหนดการใช้งาน