OPEN-SOURCE SCRIPT

4-Day Average Initial Balance (RTH)

44
//version=5
indicator("4-Day Average Initial Balance (RTH)", overlay=true, max_labels_count=500, max_lines_count=500)

//===================== Inputs =====================
ibBars = input.int(12, "IB length in bars (5-min = 12 bars)", minval=1)
sessRTH = input.session("0930-1600", "RTH Session (Exchange Time)")
bgColor = input.color(color.new(color.blue, 70), "Background Color")

//===================== Session Logic =====================
inSession = time(timeframe.period, sessRTH) != 0
newSession = inSession and not inSession[1]

//===================== IB Tracking =====================
var float ibHigh = na
var float ibLow = na
var int ibBarCount = 0
var bool ibDone = false

if newSession
ibHigh := na
ibLow := na
ibBarCount := 0
ibDone := false

if inSession and not ibDone
ibHigh := na(ibHigh) ? high : math.max(ibHigh, high)
ibLow := na(ibLow) ? low : math.min(ibLow, low)
ibBarCount += 1
if ibBarCount >= ibBars
ibDone := true

//===================== Store Last 4 IB Ranges =====================
var float ib1 = na
var float ib2 = na
var float ib3 = na
var float ib4 = na

todayIBRange = ibDone ? ibHigh - ibLow : na
justCompletedIB = ibDone and not ibDone[1]

if justCompletedIB and not na(todayIBRange)
ib4 := ib3
ib3 := ib2
ib2 := ib1
ib1 := todayIBRange

//===================== Average of Last 4 =====================
sum = 0.0
count = 0

if not na(ib1)
sum += ib1
count += 1
if not na(ib2)
sum += ib2
count += 1
if not na(ib3)
sum += ib3
count += 1
if not na(ib4)
sum += ib4
count += 1

avgIB = count > 0 ? sum / count : na

//===================== Display Number on Right =====================
var table t = table.new(position.top_right, 1, 1, frame_color=color.new(color.black, 0), frame_width=1)

if barstate.islast
txt = na(avgIB) ? "Avg IB(4d): n/a" : "Avg IB(4d): " + str.tostring(avgIB, "#.00") + " pts"
table.cell(t, 0, 0, txt, text_color=color.white, text_halign=text.align_right, bgcolor=bgColor)

Exención de responsabilidad

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.