本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// This work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
// https://creativecommons.org/licenses/by-nc-sa/4.0/
// © Zeiierman {
indicator("Higher Timeframe Trend Frontier (Zeiierman)",overlay=true,max_lines_count=200,max_labels_count=250,max_boxes_count=350)
//}
// ~~ Tooltips {
const string tt1="Higher timeframe used to create the period boxes and Echo Frontier. Must be higher than the chart timeframe."
const string tt2="Up and down colors for the Echo Frontier, including its fading overlay."
const string tt3="Show the HTF price-range boxes. The two colors represent bullish and bearish HTF periods."
const string tt4="Show high and low price labels at the candles that made the extremes. High and low labels have separate fill colors, shared text color and shared size."
const string tt5="Show the previous completed HTF ATR opening zone, centered on the current HTF open. The multiplier sets each half-width in ATR units; the color applies to its box and dotted midpoint."
const string tt6="Draw dotted vertical dividers at new HTF periods."
const string tt7="How quickly the Echo Frontier follows price, expressed in chart ATR units. This is the main responsiveness control."
const string tt8="Increase the Frontier's speed as the HTF period approaches its close."
const string tt9="HTF session progress percentage at which speed acceleration begins."
const string tt10="Frontier speed multiplier reached near the end of the HTF period."
const string tt11="Acceleration shape. 1.0 is linear; larger values shift more of the acceleration toward the HTF close."
const string tt12="Display separate estimated trapped-buyer and trapped-seller profiles for the newest three HTF boxes, including the developing box. Both sides cover the same complete high-to-low range. Estimates use chart OHLCV, not real exchange bid/ask delta."
const string tt13="Number of price bins across the highest high and lowest low of the newest three HTF boxes. More rows show finer price detail."
const string tt14="Gap from the latest bar to the left edge of the profile, measured in chart bars."
const string tt15="Maximum horizontal width of each profile side, measured in chart bars."
const string tt16="Colors for estimated trapped buyers on the left and trapped sellers on the right. Red HTF boxes increase buyer-trap weighting; green HTF boxes increase seller-trap weighting. Both sides remain visible."
const string tt17="Normalize buyer and seller profiles independently so each side reaches full width at its strongest bin. Disable to compare both sides on one shared scale."
const string tt18="Show or hide the TRAPPED BUYERS and TRAPPED SELLERS text above the profile without affecting the profile bars, peak levels or calculations."
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ Groups {
gMAP="TrendMap"
gAPP="Appearance"
gECH="Echo Memory"
gACC="Frontier Acceleration"
gTRP="Trap Profile"
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ Inputs {
htf=input.timeframe("60","Higher Timeframe",group=gMAP,tooltip=tt1)
upColor=input.color(color.lime,"Frontier Up",inline="trend",group=gMAP)
dnColor=input.color(color.red,"Frontier Down",inline="trend",group=gMAP,tooltip=tt2)
showBoxes=input.bool(true,"Colored HTF Boxes",inline="boxes",group=gAPP)
bullColor=input.color(color.lime,"",inline="boxes",group=gAPP,active=showBoxes)
bearColor=input.color(color.red,"",inline="boxes",group=gAPP,active=showBoxes,tooltip=tt3)
showPrices=input.bool(true,"Price Labels",inline="labels",group=gAPP)
highlCol=input.color(color.lime,"",inline="labels",group=gAPP,active=showPrices)
lowlCol=input.color(color.red,"",inline="labels",group=gAPP,active=showPrices)
txtCol=input.color(color.white,"",inline="labels",group=gAPP,active=showPrices)
Opt=input.string("Small","",options=["Tiny","Small","Normal","Large","Huge"],inline="labels",group=gAPP,active=showPrices,tooltip=tt4)
showOpen=input.bool(true,"HTF ATR Opening Zone",inline="opening",group=gAPP)
openATRMult=input.float(0.05,"",minval=0.01,maxval=1.0,step=0.01,inline="opening",group=gAPP,active=showOpen)
openZoneCol=input.color(color.rgb(30,142,234),"",inline="opening",group=gAPP,active=showOpen,tooltip=tt5)
showLines=input.bool(true,"Vertical Dividers",group=gAPP,tooltip=tt6)
frontSpeed=input.float(0.15,"Frontier Speed (Chart ATR)",minval=0.01,maxval=1.5,step=0.01,group=gECH,tooltip=tt7)
lateAccel=input.bool(true,"Late-HTF Acceleration",group=gACC,tooltip=tt8)
lateStart=input.float(70.0,"Start After HTF Progress (%)",minval=10.0,maxval=95.0,step=5.0,group=gACC,active=lateAccel,tooltip=tt9)
lateFactor=input.float(3.0,"Final Speed Multiplier",minval=1.0,maxval=10.0,step=0.25,group=gACC,active=lateAccel,tooltip=tt10)
lateCurve=input.float(1.5,"Acceleration Curve",minval=0.5,maxval=4.0,step=0.25,group=gACC,active=lateAccel,tooltip=tt11)
showTrap=input.bool(true,"Trapped Buyers / Sellers Profile",group=gTRP,tooltip=tt12)
trapRows=input.int(48,"Price Rows",minval=12,maxval=60,group=gTRP,active=showTrap,tooltip=tt13)
trapOffset=input.int(10,"Offset (Bars)",minval=1,maxval=120,inline="profileSize",group=gTRP,active=showTrap)
trapWidth=input.int(22,"Side Width",minval=5,maxval=80,inline="profileSize",group=gTRP,active=showTrap,tooltip=tt14+"\n\n"+tt15)
trapBuyerCol=input.color(color.rgb(74,84,157),"Trapped Buyers",inline="profileColors",group=gTRP,active=showTrap)
trapSellerCol=input.color(color.rgb(31,141,137),"Trapped Sellers",inline="profileColors",group=gTRP,active=showTrap,tooltip=tt16)
trapSeparate=input.bool(true,"Normalize Each Side",group=gTRP,active=showTrap,tooltip=tt17)
showTrapLabels=input.bool(true,"Show Trap Labels",group=gTRP,active=showTrap,tooltip=tt18)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ Constants {
ZONES=9
HORIZON=3
HALF_LIFE=240
TRAP_CAPACITY=6000
OBJECT_KEEP=95
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ Helpers {
zone_at(float p,float bottom,float width,int count)=>int(math.max(0,math.min(count-1,math.floor((p-bottom)/width))))
label_size(string opt)=>opt=="Small"?size.small:opt=="Normal"?size.normal:opt=="Large"?size.large:opt=="Huge"?size.huge:size.tiny
sync_midline(box zoneBox,line midline)=>
if not na(zoneBox) and not na(midline)
midPrice=(box.get_top(zoneBox)+box.get_bottom(zoneBox))*0.5
box.set_extend(zoneBox,extend.none)
line.set_extend(midline,extend.none)
line.set_xy1(midline,box.get_left(zoneBox),midPrice)
line.set_xy2(midline,box.get_right(zoneBox),midPrice)
0
sync_body(box b,int right,float top,float bottom,color c)=>
if not na(b)
box.set_right(b,right)
box.set_top(b,top)
box.set_bottom(b,bottom)
box.set_bgcolor(b,color.new(c,90))
box.set_border_color(b,color.new(c,52))
0
sync_prices(label hiLabel,label loLabel,int hiBar,float hiPrice,int loBar,float loPrice)=>
if not na(hiLabel) and not na(loLabel)
label.set_xy(hiLabel,hiBar,hiPrice)
label.set_text(hiLabel,str.tostring(hiPrice,format.mintick))
label.set_xy(loLabel,loBar,loPrice)
label.set_text(loLabel,str.tostring(loPrice,format.mintick))
0
trim_boxes(array<box> a,int keep)=>
if a.size()>keep
box.delete(a.shift())
0
trim_lines(array<line> a,int keep)=>
if a.size()>keep
line.delete(a.shift())
0
trim_prices(array<label> highs,array<label> lows,int keep)=>
if highs.size()>keep
label.delete(highs.shift())
label.delete(lows.shift())
0
store_bar(array<int> periods,array<float> os,array<float> hs,array<float> ls,array<float> cs,array<float> vs,int write,int p,float o,float h,float l,float c,float v)=>
if periods.size()<TRAP_CAPACITY
periods.push(p)
os.push(o)
hs.push(h)
ls.push(l)
cs.push(c)
vs.push(v)
else
periods.set(write,p)
os.set(write,o)
hs.set(write,h)
ls.set(write,l)
cs.set(write,c)
vs.set(write,v)
(write+1)%TRAP_CAPACITY
// Scores are estimated from OHLCV, HTF box state, rejection and underwater exposure.
trap_add(array<float> buyers,array<float> sellers,int rows,float rangeLow,float step,float span,float lastPrice,bool greenBox,float o,float h,float l,float c,float v,float ageWeight)=>
candleRange=math.max(h-l,syminfo.mintick)
body=math.max(-1.0,math.min(1.0,(c-o)/candleRange))
closeLocation=math.max(-1.0,math.min(1.0,(2.0*c-h-l)/candleRange))
buyerShare=math.max(0.05,math.min(0.95,0.5+0.35*body+0.15*closeLocation))
sellerShare=1.0-buyerShare
upperWick=math.max(0.0,h-math.max(o,c))/candleRange
lowerWick=math.max(0.0,math.min(o,c)-l)/candleRange
buyerReject=0.70+1.30*upperWick
sellerReject=0.70+1.30*lowerWick
buyerRegime=greenBox?0.30:1.50
sellerRegime=greenBox?1.50:0.30
activity=na(v)?1.0:math.max(0.0,v)
firstRow=int(math.max(0,math.min(rows-1,math.floor((l-rangeLow)/step))))
lastRow=int(math.max(0,math.min(rows-1,math.floor((h-rangeLow)/step))))
for row=firstRow to lastRow
bottom=rangeLow+row*step
top=bottom+step
overlap=h>l?math.max(0.0,math.min(h,top)-math.max(l,bottom))/(h-l):(c>=bottom and c<=top?1.0:0.0)
if overlap>0.0
price=(bottom+top)*0.5
buyerLoss=math.max(0.0,price-lastPrice)
sellerLoss=math.max(0.0,lastPrice-price)
buyerExposure=0.15+0.85*math.min(1.0,buyerLoss/(span*0.25))
sellerExposure=0.15+0.85*math.min(1.0,sellerLoss/(span*0.25))
base=activity*overlap*ageWeight
buyers.set(row,buyers.get(row)+base*buyerShare*buyerRegime*buyerReject*buyerExposure)
sellers.set(row,sellers.get(row)+base*sellerShare*sellerRegime*sellerReject*sellerExposure)
0
render_trap_row(box buyerBox,box sellerBox,int xCenter,float bottom,float top,float buyScore,float sellScore,float buyerScale,float sellerScale)=>
buyRelative=buyerScale>0.0?buyScore/buyerScale:0.0
sellRelative=sellerScale>0.0?sellScore/sellerScale:0.0
buyWidth=buyScore>0.0?math.max(1,int(math.round(trapWidth*buyRelative))):1
sellWidth=sellScore>0.0?math.max(1,int(math.round(trapWidth*sellRelative))):1
buyAlpha=buyScore>0.0?int(72.0-57.0*buyRelative):96
sellAlpha=sellScore>0.0?int(72.0-57.0*sellRelative):96
box.set_lefttop(buyerBox,xCenter-buyWidth,top)
box.set_rightbottom(buyerBox,xCenter,bottom)
box.set_bgcolor(buyerBox,color.new(trapBuyerCol,buyAlpha))
box.set_border_color(buyerBox,color.new(trapBuyerCol,100))
box.set_lefttop(sellerBox,xCenter,top)
box.set_rightbottom(sellerBox,xCenter+sellWidth,bottom)
box.set_bgcolor(sellerBox,color.new(trapSellerCol,sellAlpha))
box.set_border_color(sellerBox,color.new(trapSellerCol,100))
0
render_peak(box b,int xLeft,int xRight,float low,float step,int row,color col,bool hasScore)=>
box.set_lefttop(b,xLeft,low+(row+1)*step)
box.set_rightbottom(b,xRight,low+row*step)
box.set_border_color(b,color.new(col,hasScore?0:100))
0
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ HTF Series {
if barstate.isfirst and timeframe.in_seconds(htf)<=timeframe.in_seconds(timeframe.period)
runtime.error("Higher Timeframe must be greater than the chart timeframe.")
prevHTFHigh=request.security(syminfo.tickerid,htf,high[1],lookahead=barmerge.lookahead_on)
prevHTFLow=request.security(syminfo.tickerid,htf,low[1],lookahead=barmerge.lookahead_on)
prevHTFATR=request.security(syminfo.tickerid,htf,ta.atr(14)[1],lookahead=barmerge.lookahead_on)
htfOpen=request.security(syminfo.tickerid,htf,open,lookahead=barmerge.lookahead_on)
newPeriod=timeframe.change(htf)
[trapPrevOpen,trapPrevClose,trapPrev2Open,trapPrev2Close,trapPrev2High,trapPrev2Low,trapOldStart,trapPrevStart]=request.security(syminfo.tickerid,htf,[open[1],close[1],open[2],close[2],high[2],low[2],time[2],time[1]],lookahead=barmerge.lookahead_on)
htfStartTime=time(htf)
htfEndTime=time_close(htf)
observedTime=nz(time_close,time)
htfProgress=not na(htfStartTime) and not na(htfEndTime) and htfEndTime>htfStartTime?math.max(0.0,math.min(1.0,float(observedTime-htfStartTime)/float(htfEndTime-htfStartTime))):0.0
startFraction=lateStart/100.0
latePhase=math.max(0.0,math.min(1.0,(htfProgress-startFraction)/(1.0-startFraction)))
speedFactor=lateAccel?1.0+(lateFactor-1.0)*math.pow(latePhase,lateCurve):1.0
atr=math.max(nz(ta.atr(14),high-low),syminfo.mintick)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ Echo Memory Series {
validRef=not na(prevHTFHigh) and not na(prevHTFLow)
refSpan=validRef?math.max(prevHTFHigh-prevHTFLow,syminfo.mintick*10):na
refBottom=validRef?prevHTFLow-refSpan*0.25:na
refWidth=validRef?refSpan*1.50/ZONES:na
currentZone=validRef?zone_at(close,refBottom,refWidth,ZONES):na
decay=math.pow(0.5,1.0/HALF_LIFE)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ Objects & State {
var array<float> echoUp=array.new_float(ZONES,0.0)
var array<float> echoDown=array.new_float(ZONES,0.0)
var array<float> echoVisits=array.new_float(ZONES,0.0)
var float echoBias=0.0
var float echoN=0.0
var float frontier=na
var float periodHigh=na
var float periodLow=na
var int periodHighBar=na
var int periodLowBar=na
var int periodStart=na
var int periodAge=0
var box activeBox=na
var box activeOpeningZone=na
var line activeOpenLine=na
var label activeHighLabel=na
var label activeLowLabel=na
var array<label> highLabels=array.new<label>()
var array<label> lowLabels=array.new<label>()
var array<box> bodyBoxes=array.new<box>()
var array<box> openingBoxes=array.new<box>()
var array<line> openLines=array.new<line>()
var array<line> dividers=array.new<line>()
var array<int> trPeriod=array.new_int()
var array<float> trO=array.new_float()
var array<float> trH=array.new_float()
var array<float> trL=array.new_float()
var array<float> trC=array.new_float()
var array<float> trV=array.new_float()
var int trWrite=0
var array<box> trBuyerBoxes=array.new<box>()
var array<box> trSellerBoxes=array.new<box>()
var box trBuyerPeakBox=na
var box trSellerPeakBox=na
var label trBuyerHeader=na
var label trSellerHeader=na
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ Profile Sampling {
if showTrap and barstate.isconfirmed
trWrite:=store_bar(trPeriod,trO,trH,trL,trC,trV,trWrite,htfStartTime,open,high,low,close,volume)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ Echo Memory & Period Update {
if barstate.isconfirmed
for i=0 to ZONES-1
echoUp.set(i,echoUp.get(i)*decay)
echoDown.set(i,echoDown.get(i)*decay)
echoVisits.set(i,echoVisits.get(i)*decay)
oldZone=currentZone[HORIZON]
if not na(oldZone) and not na(atr[HORIZON])
response=math.max(-2.0,math.min(2.0,(close-close[HORIZON])/atr[HORIZON]))
echoUp.set(oldZone,echoUp.get(oldZone)+math.max(response,0))
echoDown.set(oldZone,echoDown.get(oldZone)+math.max(-response,0))
echoVisits.set(oldZone,echoVisits.get(oldZone)+1)
if not na(currentZone)
a=math.max(0,currentZone-1)
b=math.min(ZONES-1,currentZone+1)
c=(echoUp.get(currentZone)-echoDown.get(currentZone))/(2+echoUp.get(currentZone)+echoDown.get(currentZone))
l=(echoUp.get(a)-echoDown.get(a))/(2+echoUp.get(a)+echoDown.get(a))
r=(echoUp.get(b)-echoDown.get(b))/(2+echoUp.get(b)+echoDown.get(b))
echoBias:=0.6*c+0.2*l+0.2*r
echoN:=0.6*echoVisits.get(currentZone)+0.2*echoVisits.get(a)+0.2*echoVisits.get(b)
else
echoBias:=0
echoN:=0
if newPeriod or na(periodStart)
periodStart:=bar_index
periodAge:=0
periodHigh:=high
periodLow:=low
periodHighBar:=bar_index
periodLowBar:=bar_index
frontier:=htfOpen
activeBox:=na
activeOpeningZone:=na
activeOpenLine:=na
activeHighLabel:=na
activeLowLabel:=na
if showPrices
activeHighLabel:=label.new(periodHighBar,periodHigh,str.tostring(periodHigh,format.mintick),xloc=xloc.bar_index,yloc=yloc.price,style=label.style_label_down,color=color.new(highlCol,0),textcolor=txtCol,size=label_size(Opt))
activeLowLabel:=label.new(periodLowBar,periodLow,str.tostring(periodLow,format.mintick),xloc=xloc.bar_index,yloc=yloc.price,style=label.style_label_up,color=color.new(lowlCol,0),textcolor=txtCol,size=label_size(Opt))
highLabels.push(activeHighLabel)
lowLabels.push(activeLowLabel)
trim_prices(highLabels,lowLabels,OBJECT_KEEP)
if showBoxes
activeBox:=box.new(bar_index,high,bar_index,low,extend=extend.none,border_color=color.new(bullColor,65),bgcolor=color.new(bullColor,91))
bodyBoxes.push(activeBox)
trim_boxes(bodyBoxes,OBJECT_KEEP)
if showOpen and not na(prevHTFATR)
band=prevHTFATR*openATRMult
activeOpeningZone:=box.new(time,htfOpen+band,time,htfOpen-band,xloc=xloc.bar_time,extend=extend.none,border_color=color.new(openZoneCol,45),bgcolor=color.new(openZoneCol,83))
activeOpenLine:=line.new(box.get_left(activeOpeningZone),htfOpen,box.get_right(activeOpeningZone),htfOpen,xloc=xloc.bar_time,extend=extend.none,color=color.new(openZoneCol,10),style=line.style_dotted)
openingBoxes.push(activeOpeningZone)
openLines.push(activeOpenLine)
trim_boxes(openingBoxes,OBJECT_KEEP)
trim_lines(openLines,OBJECT_KEEP)
if showLines and bar_index>0
dividerTop=math.max(high,low+syminfo.mintick)
divider=line.new(x1=bar_index,y1=low,x2=bar_index,y2=dividerTop,xloc=xloc.bar_index,extend=extend.both,color=color.new(chart.fg_color,45),style=line.style_dotted)
dividers.push(divider)
trim_lines(dividers,OBJECT_KEEP)
else
periodAge+=1
if high>periodHigh
periodHigh:=high
periodHighBar:=bar_index
if low<periodLow
periodLow:=low
periodLowBar:=bar_index
r=math.min(1.0,echoN/4.0)
q=math.max(0.15,math.min(0.85,0.5+0.3*echoBias*r))
gap=close-frontier
baseVelocity=atr*frontSpeed*(gap>=0?q:1-q)
velocity=baseVelocity*speedFactor
frontier+=math.sign(gap)*math.min(math.abs(gap),velocity)
if showBoxes and not na(activeBox)
c=close>=htfOpen?bullColor:bearColor
sync_body(activeBox,bar_index,periodHigh,periodLow,c)
if showPrices
sync_prices(activeHighLabel,activeLowLabel,periodHighBar,periodHigh,periodLowBar,periodLow)
if showOpen and not na(activeOpeningZone)
box.set_right(activeOpeningZone,time)
sync_midline(activeOpeningZone,activeOpenLine)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ Developing Period Display {
if barstate.islast and not barstate.isconfirmed and not newPeriod and not na(periodStart)
liveHigh=math.max(periodHigh,high)
liveLow=math.min(periodLow,low)
liveHighBar=high>periodHigh?bar_index:periodHighBar
liveLowBar=low<periodLow?bar_index:periodLowBar
if showBoxes and not na(activeBox)
liveColor=close>=htfOpen?bullColor:bearColor
sync_body(activeBox,bar_index,liveHigh,liveLow,liveColor)
if showPrices
sync_prices(activeHighLabel,activeLowLabel,liveHighBar,liveHigh,liveLowBar,liveLow)
if showOpen and not na(activeOpeningZone)
box.set_right(activeOpeningZone,time)
sync_midline(activeOpeningZone,activeOpenLine)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ Historical Midline Bounds {
if barstate.islast and openLines.size()>0
for i=0 to openLines.size()-1
sync_midline(openingBoxes.get(i),openLines.get(i))
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ Two-Sided Trap Profile {
if showTrap and barstate.islast and not na(trapOldStart) and not na(trapPrev2High) and not na(trapPrev2Low)
liveHTFHigh=newPeriod and not barstate.isconfirmed?high:math.max(nz(periodHigh,high),high)
liveHTFLow=newPeriod and not barstate.isconfirmed?low:math.min(nz(periodLow,low),low)
trapHigh=math.max(math.max(trapPrev2High,nz(prevHTFHigh,liveHTFHigh)),liveHTFHigh)
trapLow=math.min(math.min(trapPrev2Low,nz(prevHTFLow,liveHTFLow)),liveHTFLow)
trapSpan=math.max(trapHigh-trapLow,syminfo.mintick*trapRows)
trapStep=trapSpan/trapRows
liveGreen=close>=htfOpen
prevGreen=trapPrevClose>=trapPrevOpen
prev2Green=trapPrev2Close>=trapPrev2Open
array<float> buyerHist=array.new_float(trapRows,0.0)
array<float> sellerHist=array.new_float(trapRows,0.0)
recorded=trPeriod.size()
if recorded>0
for i=0 to recorded-1
idx=(trWrite-1-i+TRAP_CAPACITY)%TRAP_CAPACITY
candlePeriod=trPeriod.get(idx)
if candlePeriod<trapOldStart
break
isGreen=candlePeriod>=htfStartTime?liveGreen:candlePeriod>=trapPrevStart?prevGreen:prev2Green
recency=candlePeriod>=htfStartTime?1.0:candlePeriod>=trapPrevStart?0.85:0.70
trap_add(buyerHist,sellerHist,trapRows,trapLow,trapStep,trapSpan,close,isGreen,trO.get(idx),trH.get(idx),trL.get(idx),trC.get(idx),trV.get(idx),recency)
if not barstate.isconfirmed
trap_add(buyerHist,sellerHist,trapRows,trapLow,trapStep,trapSpan,close,liveGreen,open,high,low,close,volume,1.0)
buyerPeak=0.0
sellerPeak=0.0
buyerPeakRow=0
sellerPeakRow=0
for i=0 to trapRows-1
buyScore=buyerHist.get(i)
sellScore=sellerHist.get(i)
if buyScore>buyerPeak
buyerPeak:=buyScore
buyerPeakRow:=i
if sellScore>sellerPeak
sellerPeak:=sellScore
sellerPeakRow:=i
commonPeak=math.max(buyerPeak,sellerPeak)
buyerScale=trapSeparate?buyerPeak:commonPeak
sellerScale=trapSeparate?sellerPeak:commonPeak
xCenter=bar_index+trapOffset+trapWidth
xLeft=xCenter-trapWidth
xRight=xCenter+trapWidth
if trBuyerBoxes.size()==0
for i=0 to trapRows-1
trBuyerBoxes.push(box.new(xCenter,close,xCenter,close,border_color=color.new(trapBuyerCol,100),bgcolor=color.new(trapBuyerCol,100)))
trSellerBoxes.push(box.new(xCenter,close,xCenter,close,border_color=color.new(trapSellerCol,100),bgcolor=color.new(trapSellerCol,100)))
for i=0 to trapRows-1
bottom=trapLow+i*trapStep
top=bottom+trapStep
render_trap_row(trBuyerBoxes.get(i),trSellerBoxes.get(i),xCenter,bottom,top,buyerHist.get(i),sellerHist.get(i),buyerScale,sellerScale)
if na(trBuyerPeakBox)
trBuyerPeakBox:=box.new(xLeft,trapHigh,xRight,trapHigh,border_color=color.rgb(29,151,255),border_width=2,bgcolor=color.new(color.blue,100))
trSellerPeakBox:=box.new(xLeft,trapLow,xRight,trapLow,border_color=color.rgb(255,145,27),border_width=2,bgcolor=color.new(color.orange,100))
render_peak(trBuyerPeakBox,xLeft,xRight,trapLow,trapStep,buyerPeakRow,color.rgb(29,151,255),buyerPeak>0.0)
render_peak(trSellerPeakBox,xLeft,xRight,trapLow,trapStep,sellerPeakRow,color.rgb(255,145,27),sellerPeak>0.0)
if showTrapLabels
if na(trBuyerHeader)
trBuyerHeader:=label.new(xCenter-int(math.round(trapWidth*0.5)),trapHigh+trapStep,"TRAPPED BUYERS",xloc=xloc.bar_index,yloc=yloc.price,style=label.style_none,textcolor=trapBuyerCol,size=size.tiny)
trSellerHeader:=label.new(xCenter+int(math.round(trapWidth*0.5)),trapHigh+trapStep,"TRAPPED SELLERS",xloc=xloc.bar_index,yloc=yloc.price,style=label.style_none,textcolor=trapSellerCol,size=size.tiny)
else
label.set_xy(trBuyerHeader,xCenter-int(math.round(trapWidth*0.5)),trapHigh+trapStep)
label.set_xy(trSellerHeader,xCenter+int(math.round(trapWidth*0.5)),trapHigh+trapStep)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
// ~~ Plots {
expectedBars=math.max(1.0,timeframe.in_seconds(htf)/timeframe.in_seconds(timeframe.period))
opacity=int(math.min(88.0,periodAge/expectedBars*88.0))
frontierUp=frontier>=nz(frontier[1],frontier)
trendColor=frontierUp?upColor:dnColor
plot(newPeriod?na:frontier,"Echo Frontier",color=trendColor,linewidth=1,style=plot.style_linebr)
plot(newPeriod?na:frontier,"Echo Frontier Fade",color=color.new(trendColor,opacity),linewidth=5,style=plot.style_linebr)
plotshape(newPeriod[1]?frontier:na,"Period Dot",shape.circle,location.absolute,color.new(chart.fg_color,25),size=size.tiny)
plot(htfProgress*100.0,"HTF Session Progress (%)",display=display.data_window)
plot(speedFactor,"Frontier Speed Multiplier",display=display.data_window)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}