Gap Fill Statistics

FinzData · study · 80 行 · 点赞 0 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © FinzData
 
//@version=6
// Gap Fill Statistics — open source. Measures how often opening gaps
// close back to the prior session's close, split by gap direction and size.
indicator("Gap Fill Statistics", overlay = true, max_lines_count = 50)
 
b1 = input.float(0.25, "Bucket 1 upper bound (%)", minval = 0.01, step = 0.05)
b2 = input.float(0.50, "Bucket 2 upper bound (%)", minval = 0.01, step = 0.05)
b3 = input.float(1.00, "Bucket 3 upper bound (%)", minval = 0.01, step = 0.05)
b4 = input.float(2.00, "Bucket 4 upper bound (%)", minval = 0.01, step = 0.05)
minGap = input.float(0.05, "Ignore gaps smaller than (%)", minval = 0.0, step = 0.01)
showLine = input.bool(true, "Show today's gap-fill level")
tblPos = input.string("Top right", "Table position", options = ["Top right", "Bottom right", "Top left", "Bottom left"])
 
var upCnt  = array.new_int(5, 0)
var upFill = array.new_int(5, 0)
var dnCnt  = array.new_int(5, 0)
var dnFill = array.new_int(5, 0)
var int firstDay = na
var int days = 0
 
var float refClose = na
var float dOpen = na
var float dHigh = na
var float dLow = na
var line fillLine = na
 
bucketOf(float g) =>
    a = math.abs(g)
    a < b1 ? 0 : a < b2 ? 1 : a < b3 ? 2 : a < b4 ? 3 : 4
 
newDay = timeframe.change("D")
 
if newDay
    // close out the day that just ended
    if not na(dOpen) and not na(refClose) and refClose != 0
        gap = (dOpen - refClose) / refClose * 100
        if math.abs(gap) >= minGap
            i = bucketOf(gap)
            if gap > 0
                array.set(upCnt, i, array.get(upCnt, i) + 1)
                if dLow <= refClose
                    array.set(upFill, i, array.get(upFill, i) + 1)
            else
                array.set(dnCnt, i, array.get(dnCnt, i) + 1)
                if dHigh >= refClose
                    array.set(dnFill, i, array.get(dnFill, i) + 1)
            days += 1
            if na(firstDay)
                firstDay := time[1]
    refClose := close[1]
    dOpen := open
    dHigh := high
    dLow := low
    if showLine and not na(refClose)
        line.delete(fillLine)
        fillLine := line.new(bar_index, refClose, bar_index + 1, refClose, extend = extend.right, color = color.new(color.orange, 20), style = line.style_dashed)
else
    dHigh := math.max(dHigh, high)
    dLow := math.min(dLow, low)
 
pct(int f, int n) => n > 0 ? str.tostring(100.0 * f / n, "#") + "% (" + str.tostring(n) + ")" : "-"
 
pos = tblPos == "Top right" ? position.top_right : tblPos == "Bottom right" ? position.bottom_right : tblPos == "Top left" ? position.top_left : position.bottom_left
var table t = table.new(pos, 3, 7, bgcolor = color.new(color.black, 15), border_width = 1, border_color = color.new(color.gray, 60))
 
if barstate.islast
    labels = array.from("< " + str.tostring(b1) + "%", str.tostring(b1) + "-" + str.tostring(b2) + "%", str.tostring(b2) + "-" + str.tostring(b3) + "%", str.tostring(b3) + "-" + str.tostring(b4) + "%", "> " + str.tostring(b4) + "%")
    table.cell(t, 0, 0, "Gap size", text_color = color.white, text_size = size.small)
    table.cell(t, 1, 0, "Gap up filled", text_color = color.white, text_size = size.small)
    table.cell(t, 2, 0, "Gap down filled", text_color = color.white, text_size = size.small)
    for i = 0 to 4
        table.cell(t, 0, i + 1, array.get(labels, i), text_color = color.silver, text_size = size.small)
        table.cell(t, 1, i + 1, pct(array.get(upFill, i), array.get(upCnt, i)), text_color = color.lime, text_size = size.small)
        table.cell(t, 2, i + 1, pct(array.get(dnFill, i), array.get(dnCnt, i)), text_color = color.red, text_size = size.small)
    since = na(firstDay) ? "-" : str.format_time(firstDay, "yyyy-MM-dd", syminfo.timezone)
    table.cell(t, 0, 6, "Sample: " + str.tostring(days) + " gaps since " + since + " (only bars loaded on this chart)", text_color = color.gray, text_size = size.tiny)
    table.merge_cells(t, 0, 6, 2, 6)

← 返回列表