First Half-Hour Bias [Badr Edge]

kh_badrr · study · 121 行 · 点赞 2 · TradingView 原页

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

Pine Script

//@version=6
// First Half-Hour Bias [Badr Edge] · v1.0.0
// Educational chart-marking tool. Markers show what happened on a bar. They are not trade signals.
indicator("First Half-Hour Bias [Badr Edge]", shorttitle = "BE First 30", overlay = true, max_boxes_count = 200, max_labels_count = 200)
const color GOLD  = #D9B46C
const color TEAL  = #4FB8A0
const color ROSE  = #E07A6C
const color GREY  = #8F939B
const color PANEL = #0F1013
const string TZ = "America/New_York"
const string G_S = "Session"
sess     = input.session("0930-1600", "Session (New York time)", group = G_S)
winMin   = input.int(30, "Window length (minutes)", options = [15, 30, 60], group = G_S, tooltip = "Length of both the first and the last window of the session.")
fromPrev = input.bool(true, "Measure the first window from the prior close", group = G_S, tooltip = "On: the first-window move runs from the prior session's close to the end of the first window, so it includes the overnight gap. Off: it runs from today's open.")
const string G_V = "Visuals"
showBoxes = input.bool(true, "Window boxes", group = G_V)
showMarks = input.bool(true, "Bias marker at the last window", group = G_V, tooltip = "A small triangle at the start of the last window, pointing the way the first window moved. A marker shows what happened earlier in the day. It is not a trade signal.")
showStats = input.bool(true, "Tally table", group = G_V, tooltip = "Counts, on the bars loaded on this chart, how often the last window moved the same way as the first window.")
const string G_Y = "Style"
showWm = input.bool(true, "Watermark", group = G_Y)
const string G_A = "Alerts"
useAlerts = input.bool(true, "Alert at the start of the last window", group = G_A, tooltip = "Create the alert with \"Any alert() function call\". Fires once, after the bar closes.")
 
hhmm(string s) => int(str.tonumber(str.substring(s, 0, 2))) * 60 + int(str.tonumber(str.substring(s, 2, 4)))
sStart  = hhmm(str.substring(sess, 0, 4))
sEnd    = hhmm(str.substring(sess, 5, 9))
sessMs  = ((sEnd - sStart + 1440) % 1440) * 60000
winMs   = winMin * 60000
tfOK    = timeframe.isintraday and timeframe.in_seconds() <= winMin * 60
inS     = tfOK and not na(time(timeframe.period, sess, TZ))
start   = inS and not inS[1]
 
var int   t0 = na
var float prevC = na
var float lastC = na
var float base  = na
var float hi = na
var float lo = na
var int   bias = 0
var float firstRet = na
var bool  firstDone = false
var bool  lastOn = false
var float lastBase = na
var int   lastBias = 0
var int   nSess = 0
var int   nSame = 0
var box   b1 = na
var box   b2 = na
bool mUp = false
bool mDn = false
 
// close out the previous session's tally when a new one starts
if start
    if lastOn and lastBias != 0 and not na(lastBase) and lastC != lastBase
        nSess += 1
        if (lastC > lastBase) == (lastBias == 1)
            nSame += 1
    prevC := lastC
    t0    := time
    base  := fromPrev and not na(prevC) ? prevC : open
    hi    := high
    lo    := low
    bias  := 0
    firstDone := false
    lastOn    := false
    if showBoxes
        b1 := box.new(bar_index, hi, bar_index, lo, border_color = color.new(GREY, 55), bgcolor = color.new(GREY, 92), text_size = size.tiny, text_color = GREY, text_halign = text.align_left, text_valign = text.align_top)
else if inS and not na(t0)
    if not firstDone
        if time < t0 + winMs
            hi := math.max(hi, high)
            lo := math.min(lo, low)
            if showBoxes
                b1.set_top(hi)
                b1.set_bottom(lo)
                b1.set_right(bar_index)
        else
            firstDone := true
            firstRet  := close[1] / base - 1
            bias      := firstRet > 0 ? 1 : firstRet < 0 ? -1 : 0
            if showBoxes
                color c = bias == 1 ? TEAL : bias == -1 ? ROSE : GREY
                b1.set_border_color(color.new(c, 45))
                b1.set_bgcolor(color.new(c, 91))
                b1.set_text_color(color.new(c, 15))
                b1.set_text("FIRST " + str.tostring(winMin) + "  " + (firstRet > 0 ? "+" : "") + str.tostring(firstRet * 100, "0.00") + "%")
    if firstDone and not lastOn and time >= t0 + sessMs - winMs
        lastOn   := true
        lastBase := close[1]
        lastBias := bias
        mUp := bias == 1
        mDn := bias == -1
        if showBoxes
            b2 := box.new(bar_index, high, bar_index, low, border_color = color.new(GOLD, 55), bgcolor = color.new(GOLD, 93), text = "LAST " + str.tostring(winMin), text_size = size.tiny, text_color = color.new(GOLD, 20), text_halign = text.align_left, text_valign = text.align_top)
    else if lastOn and showBoxes
        b2.set_top(math.max(b2.get_top(), high))
        b2.set_bottom(math.min(b2.get_bottom(), low))
        b2.set_right(bar_index)
if inS
    lastC := close
 
plotshape(showMarks and mUp, "First window was up", shape.triangleup, location.belowbar, color.new(TEAL, 0), size = size.tiny)
plotshape(showMarks and mDn, "First window was down", shape.triangledown, location.abovebar, color.new(ROSE, 0), size = size.tiny)
plot(inS ? firstRet * 100 : na, "First window move (%)", display = display.data_window)
plot(nSess, "Sessions counted", display = display.data_window)
plot(nSame, "Sessions where the last window matched", display = display.data_window)
 
if useAlerts and (mUp or mDn)
    alert(syminfo.ticker + ": last window started. The first window moved " + (mUp ? "up " : "down ") + str.tostring(firstRet * 100, "0.00") + "%", alert.freq_once_per_bar_close)
 
var table tally = table.new(position.top_right, 1, 2, frame_color = color.new(GREY, 75), frame_width = 1)
if barstate.islast and showStats and tfOK
    tally.cell(0, 0, "FIRST VS LAST " + str.tostring(winMin) + " MIN", text_color = GOLD, text_size = size.small, text_halign = text.align_left, bgcolor = color.new(PANEL, 10))
    tally.cell(0, 1, nSess == 0 ? "No completed sessions loaded yet" : "Same direction in " + str.tostring(nSame) + " of " + str.tostring(nSess) + " sessions (" + str.tostring(100.0 * nSame / nSess, "0") + "%)", text_color = GREY, text_size = size.small, text_halign = text.align_left, bgcolor = color.new(PANEL, 10))
 
var table wm = table.new(position.bottom_right, 1, 1)
if barstate.islast and showWm
    wm.cell(0, 0, "BADR EDGE", text_color = color.new(GREY, 72), text_size = size.small)
var table note = table.new(position.middle_right, 1, 1)
if barstate.islast and not tfOK
    note.cell(0, 0, "Use a chart timeframe of " + str.tostring(winMin) + " minutes or less.", text_color = GOLD, bgcolor = color.new(color.black, 20), text_size = size.small)

← 返回列表