本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © alexas77442211
//@version=6
indicator("FVG Block Filter (Block Range vs N Range) with Candle Highlighter", overlay=true)
// ====================================================================
// 1. User Inputs & Toggle Switches
// ====================================================================
// FVG Block Filtering Inputs
candlesToCompare = input.int(6, title="Candle Count for Range Comparison (N Range)", minval=2, maxval=20)
bool isFilterConditionGreater = input.bool(true, title="Filter Condition: FVG Block Range > N Range")
// Visualization Toggles
bool showCandleHighlighter = input.bool(true, title="Show FVG Candle Highlighter")
bool showFVGBoxes = input.bool(true, title="Show Filtered FVG Boxes")
// NAUJA: Etiketes išjungiame pagal nutylėjimą
bool showFVGLabels = input.bool(false, title="Show FVG Block Labels")
// HIGHLIGHTER COLORS
color highlightBullColor = input.color(color.new(color.lime, 70), title="Bullish Highlighter Color")
color highlightBearColor = input.color(color.new(color.red, 70), title="Bearish Highlighter Color")
// FVG BOX COLORS
color bullColor = input.color(color.new(color.lime, 80), title="Bullish Block Color")
color bearColor = input.color(color.new(color.red, 80), title="Bearish Block Color")
int transparency = input.int(80, title="Block Transparency (0-100)", minval=0, maxval=100)
// ====================================================================
// 2. FVG Detection & Candle Highlighter
// ====================================================================
// A = i-2, B = i-1, C = i
isBullFVG = low > high[2]
isBearFVG = high < low[2]
// Candle Highlighter Logic (Must be in global scope)
color highlightColor = showCandleHighlighter ? (isBullFVG[0] ? highlightBullColor : isBearFVG[0] ? highlightBearColor : na) : na
barcolor(highlightColor, offset=-1)
// ====================================================================
// 3. Variables for Grouping (Block Status)
// ====================================================================
var bool inBullBlock = false
var bool inBearBlock = false
var float blockHigh = na
var float blockLow = na
var int blockStartBar = na
var float initialBeforeRange = na
// ====================================================================
// N Range Calculation Function
// ====================================================================
getNRange() =>
int offset = 2
float compareMax = high[offset]
float compareMin = low[offset]
for i = offset to offset + candlesToCompare - 1
compareMax := math.max(compareMax, high[i])
compareMin := math.min(compareMin, low[i])
compareMax - compareMin
// ====================================================================
// 4. Bullish FVG Blocks
// ====================================================================
if showFVGBoxes
if isBullFVG
if inBearBlock
inBearBlock := false
if not inBullBlock
inBullBlock := true
blockHigh := high[1]
blockLow := low[1]
blockStartBar := bar_index - 1
initialBeforeRange := getNRange()
else
blockHigh := math.max(blockHigh, high[1])
blockLow := math.min(blockLow, low[1])
else
// BLOCK END and CHECK
if inBullBlock
inBullBlock := false
float blockRange = blockHigh - blockLow
float beforeRange = initialBeforeRange
bool condition = isFilterConditionGreater ? (blockRange > beforeRange) : (blockRange < beforeRange)
if condition
// Block visualization
box.new(
left=blockStartBar,
right=bar_index,
top=blockHigh,
bottom=blockLow,
bgcolor=color.new(bullColor, transparency),
border_color=color.new(bullColor, 0))
// Label
if showFVGLabels // Rodome tik jei įjungta
string labelText = "✅ Bullish FVG Block | R:" + str.tostring(blockRange, "#.####") + " | N R:" + str.tostring(beforeRange, "#.####")
label.new(bar_index, blockHigh, labelText, style=label.style_label_down, textcolor=color.black, color=color.new(color.white, 20), size=size.small)
// --------------------------------------------------------------------
// 5. Bearish FVG Blocks
// --------------------------------------------------------------------
if isBearFVG
if inBullBlock
inBullBlock := false
if not inBearBlock
inBearBlock := true
blockHigh := high[1]
blockLow := low[1]
blockStartBar := bar_index - 1
initialBeforeRange := getNRange()
else
blockHigh := math.max(blockHigh, high[1])
blockLow := math.min(blockLow, low[1])
else
// BLOCK END and CHECK
if inBearBlock
inBearBlock := false
float blockRange = blockHigh - blockLow
float beforeRange = initialBeforeRange
bool condition = isFilterConditionGreater ? (blockRange > beforeRange) : (blockRange < beforeRange)
if condition
// Block visualization
box.new(
left=blockStartBar,
right=bar_index,
top=blockHigh,
bottom=blockLow,
bgcolor=color.new(bearColor, transparency),
border_color=color.new(bearColor, 0))
// Label
if showFVGLabels // Rodome tik jei įjungta
string labelText = "❌ Bearish FVG Block | R:" + str.tostring(blockRange, "#.####") + " | N R:" + str.tostring(beforeRange, "#.####")
label.new(bar_index, blockLow, labelText, style=label.style_label_up, textcolor=color.black, color=color.new(color.white, 20), size=size.small)