th3r1singking
th3r1singking

Reputation: 131

Pinescript - How to extend boxes to current candle

I have a script that shows FVGs on the chart. Initially it came with only one multi-timeframe FVG and I have added the second time frame.

I want to extend the FVGs until the current candle but I am having trouble figuring it out.

I've looked at the functions for extend but none support what I'm looking for.


//@version=5
int mbp = 500
indicator("FVG/OB MTF w/ Alerts",overlay=true,max_bars_back = mbp,max_lines_count=500, max_labels_count=500, max_boxes_count=500)
max_bars_back(time, mbp),max_bars_back(low, mbp),max_bars_back(close, mbp),max_bars_back(open, mbp),max_bars_back(high, mbp)

isFvg = input.bool(true,'-------------- Fvg ----------------------')
borderstyfvg = switch input.string("dotted","Border Style",options = ["dotted","dashed","Solid"])
    "dotted" => line.style_dotted
    "dashed" => line.style_dashed
    "Solid" => line.style_solid
    

barcount = 0
count = ta.barssince(session.isfirstbar)
if session.islastbar 
    barcount := count

minFvgPoint = 0
extendfvg = input.int(10,"Extend FVG",minval = 1,maxval = 500)
clrFvgBull = input.color(color.new(color.green,90),"fvg Bull",inline="fvgbb")
clrFvgBear = input.color(color.new(color.red,90),"fvg Bear",inline="fvgbb")
clrFvgBulltxt = input.color(color.new(color.green,0),"fvg Text Bull",inline="fvgbbt")
clrFvgBeartxt = input.color(color.new(color.red,0),"fvg Text Bear",inline="fvgbbt")
clrFvgBulltsd = input.color(color.new(color.blue,90),"fvg mitigated",inline="fvgbbtts")
disbalefvgborder = input.bool(false,"Disable Border")

maxfvgBox = input.int(50,"Max FVG Box Count",maxval=150)
showfvgTested = input.bool(false,"Show Tested Box",inline = "mtb")
maxfvgTestedBox = input.int(100," : Max ",maxval = 300,inline = "mtb")

isOThFvg = input.bool(false,"",inline = "tfFVG")
tmfFvg = input.timeframe("","Timeframe",inline = "tfFVG")
clrFvgBull_h = input.color(color.new(color.green,90),"fvg Bull",inline="fvgbb1")
clrFvgBear_h = input.color(color.new(color.red,90),"fvg Bear",inline="fvgbb1")
if tmfFvg == timeframe.period or tmfFvg == ""
    isOThFvg := false
    
isOThFvg2 = input.bool(false,"",inline = "tfFVG2")
tmfFvg2 = input.timeframe("","Timeframe",inline = "tfFVG2")
clrFvgBull_h2 = input.color(color.new(color.green,90),"fvg Bull",inline="fvgbb2")
clrFvgBear_h2 = input.color(color.new(color.red,90),"fvg Bear",inline="fvgbb2")
if tmfFvg2 == timeframe.period or tmfFvg2 == ""
    isOThFvg2 := false

var int toi = na
if na(toi) and  (timeframe.isintraday ? not ta.change(time("D"))  : true)
    toi := (time - time[1])*extendfvg

actual_timeframe() =>
    _tf = str.tostring(timeframe.multiplier)
    _output = str.tostring(timeframe.period)
    if(timeframe.isseconds)
        _output := _tf + "s"
    else if(timeframe.isminutes)
        if(timeframe.multiplier < 59)
            _output := _tf + "M"
        else
            _output := str.tostring(timeframe.multiplier / 60) + "H"
    _output
getFVGMTF()=>
    isGreen = close >=  open       
    int left = na,int righ = na, float top = na, float btm = na, isBull = 0
    if low > high[2]  and close[1] > high[2]  and low[1] < high[2] and (low - math.max(open[1],high[2])) > minFvgPoint 
        left := time[1],righ := time,top :=low,btm := math.max(open[1],high[2]),isBull := 1
    if high < low[2]  and close[1] < low[2]   and high[1] > low[2]  and (math.min(open[1],low[2]) - high) > minFvgPoint
        left := time[1],righ := time,top := high,btm := math.min(open[1],low[2]),isBull := -1
    [left,righ,top,btm,isBull,actual_timeframe(),open]
getFVGMTF2()=>
    isGreen = close >=  open       
    int left = na,int righ = na, float top = na, float btm = na, isBull = 0
    if low > high[2]  and close[1] > high[2]  and low[1] < high[2] and (low - math.max(open[1],high[2])) > minFvgPoint 
        left := time[1],righ := time,top :=low,btm := math.max(open[1],high[2]),isBull := 1
    if high < low[2]  and close[1] < low[2]   and high[1] > low[2]  and (math.min(open[1],low[2]) - high) > minFvgPoint
        left := time[1],righ := time,top := high,btm := math.min(open[1],low[2]),isBull := -1
    [left,righ,top,btm,isBull,actual_timeframe(),open]

[left_Fvg,righ_Fvg,top_Fvg,btm_Fvg,isBull_Fvg,actFvg,open_] = getFVGMTF()
[left_h_Fvg,righ_h_Fvg,top_h_Fvg,btm_h_Fvg,isBull_h_Fvg,act_h_Fvg,open_h] = request.security(syminfo.tickerid,tmfFvg,getFVGMTF())

[left_Fvg2,righ_Fvg2,top_Fvg2,btm_Fvg2,isBull_Fvg2,actFvg2,open_2] = getFVGMTF2()
[left_h_Fvg2,righ_h_Fvg2,top_h_Fvg2,btm_h_Fvg2,isBull_h_Fvg2,act_h_Fvg2,open_h2] = request.security(syminfo.tickerid,tmfFvg2,getFVGMTF2())

if isFvg 
    var fvgBoxBull = array.new_box(0)
    var fvgBoxBear = array.new_box(0)     

    var BoxTested = array.new_box(0)
 
    if array.size(fvgBoxBull) > maxfvgBox
        box.delete(array.pop(fvgBoxBull))
    if array.size(fvgBoxBear) > maxfvgBox
        box.delete(array.pop(fvgBoxBear))
    if array.size(BoxTested) > maxfvgTestedBox
        box.delete(array.pop(BoxTested))
 
    if array.size(fvgBoxBull) > 0
        int i = 0
        while i < array.size(fvgBoxBull) 
            boxfvg = array.get(fvgBoxBull,i)
            top = box.get_top(boxfvg)
            btm = box.get_bottom(boxfvg)
            max = math.max(top,btm)
            min = math.min(top,btm)

            if low <  max and low > min
                box.set_top(boxfvg,low)
                alert("Bull  FVG Touch")
            if low < min 
                if showfvgTested
                    box.set_bgcolor(boxfvg,clrFvgBulltsd)

                    array.insert(BoxTested,0,array.remove(fvgBoxBull,i))
                    alert("Bull  FVG Touch")
                else
                    box.delete(array.remove(fvgBoxBull,i))
            else
                i += 1
    if array.size(fvgBoxBear) > 0
        int i = 0
        while i < array.size(fvgBoxBear) 
            boxfvg = array.get(fvgBoxBear,i)
            top = box.get_top(boxfvg)
            btm = box.get_bottom(boxfvg)
            max = math.max(top,btm)
            min = math.min(top,btm)

            if high > min and high < max
                alert("Bear FVG Touch")
            if high > max
                if showfvgTested
                    box.set_bgcolor(boxfvg,clrFvgBulltsd)

                    array.insert(BoxTested,0,array.remove(fvgBoxBear,i))
                    alert("Bear FVG Touch")
                else
                    box.delete(array.remove(fvgBoxBear,i))
            else
                i += 1

    if barstate.isconfirmed
        // if isBull_Fvg == 1
        //     array.insert(fvgBoxBull,0,box.new(left_Fvg, top_Fvg, righ_Fvg+toi  ,btm_Fvg, border_color =not disbalefvgborder? color.new(clrFvgBull,0) : na, bgcolor = clrFvgBull,border_style=borderstyfvg,xloc=xloc.bar_time,text = "FVG",text_halign = text.align_right,text_size = size.small,text_color = clrFvgBulltxt))
        // if isBull_Fvg == -1
        //     array.insert(fvgBoxBear,0,box.new(left_Fvg, top_Fvg, righ_Fvg+toi  ,btm_Fvg, border_color =  disbalefvgborder? color.new(clrFvgBear,0) : na, bgcolor = clrFvgBear,border_style=borderstyfvg,xloc=xloc.bar_time,text = "FVG",text_halign = text.align_right,text_size = size.small,text_color = clrFvgBeartxt)) 
        if isOThFvg and open_h != open_h[1]
            if isBull_h_Fvg == 1
                array.insert(fvgBoxBull,0,box.new(left_h_Fvg, top_h_Fvg, righ_h_Fvg+toi  ,btm_h_Fvg, border_color = disbalefvgborder? color.new(clrFvgBull_h,0) : na, bgcolor = clrFvgBull_h,border_style=borderstyfvg,xloc=xloc.bar_time,text = act_h_Fvg+" FVG",text_halign = text.align_right,text_size = size.small,text_color = clrFvgBulltxt))
            if isBull_h_Fvg == -1
                array.insert(fvgBoxBear,0,box.new(left_h_Fvg, top_h_Fvg, righ_h_Fvg  +toi,btm_h_Fvg, border_color =  disbalefvgborder? color.new(clrFvgBear_h,0) : na, bgcolor = clrFvgBear_h,border_style=borderstyfvg,xloc=xloc.bar_time,text = act_h_Fvg+" FVG",text_halign = text.align_right,text_size = size.small,text_color = clrFvgBeartxt)) 
        /////////////////////////
        // if isBull_Fvg2 == 1
        //     array.insert(fvgBoxBull,0,box.new(left_Fvg2, top_Fvg2, righ_Fvg2+toi  ,btm_Fvg2, border_color =not disbalefvgborder? color.new(clrFvgBull,0) : na, bgcolor = clrFvgBull,border_style=borderstyfvg,xloc=xloc.bar_time,text = "FVG",text_halign = text.align_right,text_size = size.small,text_color = clrFvgBulltxt))
        // if isBull_Fvg2 == -1
        //     array.insert(fvgBoxBear,0,box.new(left_Fvg2, top_Fvg2, righ_Fvg2+toi  ,btm_Fvg2, border_color =  disbalefvgborder? color.new(clrFvgBear,0) : na, bgcolor = clrFvgBear,border_style=borderstyfvg,xloc=xloc.bar_time,text = "FVG",text_halign = text.align_right,text_size = size.small,text_color = clrFvgBeartxt)) 
        if isOThFvg2 and open_h2 != open_h2[1]
            if isBull_h_Fvg2 == 1
                array.insert(fvgBoxBull,0,box.new(left_h_Fvg2, top_h_Fvg2, righ_h_Fvg2+toi  ,btm_h_Fvg2, border_color = disbalefvgborder? color.new(clrFvgBull_h2,0) : na, bgcolor = clrFvgBull_h2,border_style=borderstyfvg,xloc=xloc.bar_time,text = act_h_Fvg2+" FVG",text_halign = text.align_right,text_size = size.small,text_color = clrFvgBulltxt))
            if isBull_h_Fvg2 == -1
                array.insert(fvgBoxBear,0,box.new(left_h_Fvg2, top_h_Fvg2, righ_h_Fvg2  +toi,btm_h_Fvg2, border_color =  disbalefvgborder? color.new(clrFvgBear_h2,0) : na, bgcolor = clrFvgBear_h2,border_style=borderstyfvg,xloc=xloc.bar_time,text = act_h_Fvg2+" FVG",text_halign = text.align_right,text_size = size.small,text_color = clrFvgBeartxt)) 

Upvotes: 0

Views: 192

Answers (1)

AwadKAB
AwadKAB

Reputation: 111

you need update right point for box to be equal current time:

Try this:

 if array.size(fvgBoxBull) > 0
        int i = 0
        while i < array.size(fvgBoxBull) 
            boxfvg = array.get(fvgBoxBull,i)
            top = box.get_top(boxfvg)
            btm = box.get_bottom(boxfvg)
            max = math.max(top,btm)
            min = math.min(top,btm)

            // ---------------Add this line-------------
            box.set_right(boxfvg, time)
            //-----------------End Edit-----------------

            if low <  max and low > min
                box.set_top(boxfvg,low)
                alert("Bull  FVG Touch")
            if low < min 
                if showfvgTested
                    box.set_bgcolor(boxfvg,clrFvgBulltsd)

                    array.insert(BoxTested,0,array.remove(fvgBoxBull,i))
                    alert("Bull  FVG Touch")
                else
                    box.delete(array.remove(fvgBoxBull,i))
            else
                i += 1
    if array.size(fvgBoxBear) > 0
        int i = 0
        while i < array.size(fvgBoxBear) 
            boxfvg = array.get(fvgBoxBear,i)
            top = box.get_top(boxfvg)
            btm = box.get_bottom(boxfvg)
            max = math.max(top,btm)
            min = math.min(top,btm)

            // ---------------Add this line-------------
            box.set_right(boxfvg, time)
            //-----------------End Edit-----------------
            
            if high > min and high < max
                alert("Bear FVG Touch")
            if high > max
                if showfvgTested
                    box.set_bgcolor(boxfvg,clrFvgBulltsd)

                    array.insert(BoxTested,0,array.remove(fvgBoxBear,i))
                    alert("Bear FVG Touch")
                else
                    box.delete(array.remove(fvgBoxBear,i))
            else
                i += 1

Upvotes: 1

Related Questions