Fractal Break Imbalance / Fair Value Gap (FVG) / Liquidity Void

geneclash · study · 191 行 · 点赞 4,951 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © geneclash
 
//@version=5
indicator("Fractal Break Imbalance / Fair Value Gap (FVG) / Liquidity Void", format=format.price, precision=0, overlay=true)
 
showFractals = input(false,title="Show Fractals?",group="Fractals")
showBrekout = input(false,title="Show Market Structure Breakouts?",group="Fractals")
breakType = input.string("Body",title="Fractal Break Type:",options=["Wick+Body","Body"],group="Fractals")
n = input.int(title="Periods", defval=2, minval=2,group="Fractals") 
 
showImbalance = input(true,title="Show Breakout Imbalances",group="Imbalance")
showOtherImbalance = input(true,title="Show Other Imbalances",group="Imbalance")
imbGreenClr = input.color(color.new(color.green,65),title="Up:",inline="i_1",group="Imbalance")
imbRedClr = input.color(color.new(color.red,65),title="Down:",inline="i_1",group="Imbalance")
imbRestClr = input.color(color.new(color.yellow,65), title="Other:",inline="i_1",group="Imbalance")
 
showBoxes = input(false,title="Show OrderBlocks?",group="OrderBlock")
changeColor = input(false,title="Change OrderBlock Colors?",group="OrderBlock")
transGreenClr = input.color(color.new(color.green,80),title="Bg:",inline="a_1",group="OrderBlock")
greenClr = input.color(color.new(color.green,0),title="Border:",inline="a_1",group="OrderBlock")
transRedClr = input.color(color.new(color.red,80),title="Bg:",inline="b_1",group="OrderBlock")
redClr = input.color(color.new(color.red,0),title="Border:",inline="b_1",group="OrderBlock")
 
//Fractals{
// UpFractal
bool upflagDownFrontier = true
bool upflagUpFrontier0 = true
bool upflagUpFrontier1 = true
bool upflagUpFrontier2 = true
bool upflagUpFrontier3 = true
bool upflagUpFrontier4 = true
 
for i = 1 to n
    upflagDownFrontier := upflagDownFrontier and (high[n-i] < high[n])
    upflagUpFrontier0 := upflagUpFrontier0 and (high[n+i] < high[n])
    upflagUpFrontier1 := upflagUpFrontier1 and (high[n+1] <= high[n] and high[n+i + 1] < high[n])
    upflagUpFrontier2 := upflagUpFrontier2 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+i + 2] < high[n])
    upflagUpFrontier3 := upflagUpFrontier3 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+i + 3] < high[n])
    upflagUpFrontier4 := upflagUpFrontier4 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+4] <= high[n] and high[n+i + 4] < high[n])
flagUpFrontier = upflagUpFrontier0 or upflagUpFrontier1 or upflagUpFrontier2 or upflagUpFrontier3 or upflagUpFrontier4
 
upFractal = (upflagDownFrontier and flagUpFrontier)
 
//DownFractal
bool downflagDownFrontier = true
bool downflagUpFrontier0 = true
bool downflagUpFrontier1 = true
bool downflagUpFrontier2 = true
bool downflagUpFrontier3 = true
bool downflagUpFrontier4 = true
 
for i = 1 to n
    downflagDownFrontier := downflagDownFrontier and (low[n-i] > low[n])
    downflagUpFrontier0 := downflagUpFrontier0 and (low[n+i] > low[n])
    downflagUpFrontier1 := downflagUpFrontier1 and (low[n+1] >= low[n] and low[n+i + 1] > low[n])
    downflagUpFrontier2 := downflagUpFrontier2 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+i + 2] > low[n])
    downflagUpFrontier3 := downflagUpFrontier3 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+i + 3] > low[n])
    downflagUpFrontier4 := downflagUpFrontier4 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+4] >= low[n] and low[n+i + 4] > low[n])
flagDownFrontier = downflagUpFrontier0 or downflagUpFrontier1 or downflagUpFrontier2 or downflagUpFrontier3 or downflagUpFrontier4
 
downFractal = (downflagDownFrontier and flagDownFrontier)
//}
 
var float topValue = na, var float bottomValue = na
var int lastRedIndex = na, var float lastRedLow = na, var float lastRedHigh = na
var int lastGreenIndex = na, var float lastGreenLow = na, var float lastGreenHigh = na
 
var line topLine = na, var line bottomLine = na
var box demandBox = na, var box supplyBox = na
 
var box imbalanceBox = na
var bool checkUpImbalance = false
var bool checkDownImbalance = false
 
var topBreakBlock = false, var bottomBreakBlock = false
var isLongBreak = false, var isShortBreak = false
var arrBoxes = array.new_box(0)
 
topBreakCheckSource = breakType == "Wick+Body" ? high : close
bottomBreakCheckSource = breakType == "Wick+Body" ? low : close
 
//IMBALANCE
//Data
L1 = low
H3 = high[2]
 
H1 = high
L3 = low[2]
 
 
FVGUp = H3 < L1 ? 1 : 0
plotFVGU = FVGUp ? H3 : na
plotFVGUL = FVGUp ? L1 : na
 
FVGDown = L3 > H1 ? 1 : 0
plotFVGD = FVGDown ? L3 : na
plotFVGH = FVGDown ? H1 : na
 
if FVGUp and showOtherImbalance and checkUpImbalance == false
    imbalanceBox := box.new(bar_index-2, plotFVGU,bar_index,plotFVGUL, bgcolor=imbRestClr, border_color=imbRestClr)    
 
if FVGDown and showOtherImbalance and checkDownImbalance == false
    imbalanceBox :=  box.new(bar_index-2, plotFVGH,bar_index,plotFVGD, bgcolor=imbRestClr, border_color=imbRestClr)
 
//Last red check
if close < open
    lastRedIndex := bar_index
    lastRedLow := low
    lastRedHigh := high
    
//Last green check
if close > open
    lastGreenIndex := bar_index
    lastGreenLow := low
    lastGreenHigh := high
 
//Check Imbalance
if checkUpImbalance
    checkUpImbalance := false
    imbTop = low
    imbBottom = high[2]
    if imbTop > imbBottom and showImbalance 
        imbalanceBox := box.new(bar_index-2, imbTop,bar_index,imbBottom, bgcolor=imbGreenClr, border_color=imbGreenClr)
  
if checkDownImbalance
    checkDownImbalance := false
    imbTop = low[2]
    imbBottom = high
    if imbTop > imbBottom and showImbalance
        imbalanceBox := box.new(bar_index-2, imbTop,bar_index,imbBottom, bgcolor=imbRedClr, border_color=imbRedClr)
 
//Top break
if ta.crossover(topBreakCheckSource,topValue) and topBreakBlock == false
    topBreakBlock := true
    isLongBreak := true
    checkUpImbalance := true
    if showBrekout
        line.set_x2(topLine,bar_index)
    if showBoxes
        demandBox := box.new(lastRedIndex-1, lastRedHigh,lastRedIndex+1,lastRedLow, bgcolor=transGreenClr, border_color=greenClr)
        array.push(arrBoxes,demandBox)
 
//Bottom break
if ta.crossunder(bottomBreakCheckSource,bottomValue) and bottomBreakBlock == false
    bottomBreakBlock := true
    isShortBreak := true
    checkDownImbalance := true
    if showBrekout
        line.set_x2(bottomLine,bar_index)
    if showBoxes
        supplyBox := box.new(lastGreenIndex-1, lastGreenHigh,lastGreenIndex+1,lastGreenLow, bgcolor=transRedClr, border_color=redClr)
        array.push(arrBoxes,supplyBox)
 
//New up fractal
if upFractal
    topBreakBlock := false
    isLongBreak := false
    topValue := high[n]
    if showBrekout
        topLine := line.new(bar_index[n],topValue,bar_index,topValue, color=color.teal, style=line.style_dotted, width=2)
        if isLongBreak[1] == false
            line.delete(topLine[1])
    
//New down fractal
if downFractal
    bottomBreakBlock := false
    isShortBreak := false
    bottomValue := low[n]
    if showBrekout
        bottomLine := line.new(bar_index[n],bottomValue,bar_index,bottomValue, color=color.maroon, style=line.style_dotted, width=2)
        if isShortBreak[1] == false
            line.delete(bottomLine[1])
 
//Box state update
//activeBoxes = box.all
activeBoxes = arrBoxes
if array.size(activeBoxes) > 0 and changeColor
	for i = 0 to array.size(activeBoxes) - 1
	    bVal = box.get_bottom(array.get(activeBoxes, i))
	    tVal = box.get_top(array.get(activeBoxes, i))
	    if close < bVal
	        box.set_bgcolor(array.get(activeBoxes, i),transRedClr)
	        box.set_border_color(array.get(activeBoxes, i),redClr)
	    if close > tVal
	        box.set_bgcolor(array.get(activeBoxes, i),transGreenClr)
	        box.set_border_color(array.get(activeBoxes, i),greenClr)
 
//PLOTS
plotshape(showFractals ? downFractal : na,style=shape.triangleup, location=location.belowbar, offset=-n, color=color.new(color.gray,80), size = size.tiny)
plotshape(showFractals ? upFractal : na, style=shape.triangledown,   location=location.abovebar, offset=-n, color=color.new(color.gray,80), size = size.tiny)

← 返回列表