Balance Point

bashu9 · study · 62 行 · 点赞 6 · TradingView 原页

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

Pine Script

//@version=6
indicator("Balance Point", overlay = true, max_lines_count = 500)
 
// Robert Krausz's Balance Point (as in Fibonacci Trader): the average of the
// higher timeframe bar's high, low and close, projected one period forward.
// Each period shows the previous period's BP as a flat dashed segment; the
// still-forming period's live BP is drawn one period wide into the future.
 
// ─── INPUTS ──────────────────────────────────────────────────────────────────
string tf         = input.timeframe("W", "Timeframe")
color  line_color = input.color(color.green, "Line Color")
int    line_width = input.int(2, "Line Width", minval=1, maxval=4)
string line_style = input.string("Dashed", "Line Style", options=["Solid", "Dashed", "Dotted"])
 
if timeframe.in_seconds(tf) <= timeframe.in_seconds()
    runtime.error("Balance Point: the timeframe (" + tf + ") must be higher than the chart's timeframe.")
 
f_style(s) =>
    s == "Solid" ? line.style_solid : s == "Dotted" ? line.style_dotted : line.style_dashed
 
// ─── CALCULATION ─────────────────────────────────────────────────────────────
// Kept separate so the Dynamic Fibonacci Channel can reuse it.
bp(float h, float l, float c) =>
    (h + l + c) / 3
 
// Closed period's BP: [1] with lookahead on, the standard non-repainting
// pattern — constant for the whole current period, never changes afterwards.
float bp_prev = request.security(syminfo.tickerid, tf, bp(high[1], low[1], close[1]), lookahead = barmerge.lookahead_on)
// Live BP of the current, unfinished period. Read only on the last bar, where
// lookahead can't reach past the data that exists; lookahead off would return
// the last closed period on a historical last bar (e.g. over a weekend).
float bp_live = request.security(syminfo.tickerid, tf, bp(high, low, close), lookahead = barmerge.lookahead_on)
 
// ─── HISTORY: one segment per period ─────────────────────────────────────────
// Bar-time x-coordinates, so the current period's segment can run past the
// last bar: its level is already known (the previous period's BP), and so is
// when the period ends.
var line seg = na
if timeframe.change(tf)
    // The finished segment runs up to this period's first bar (no gap between
    // periods); then a new segment starts at the previous period's BP.
    if not na(seg)
        line.set_x2(seg, time)
    seg := na(bp_prev) ? na : line.new(time, bp_prev, time, bp_prev, xloc = xloc.bar_time, color = line_color, style = f_style(line_style), width = line_width)
else if not na(seg)
    line.set_x2(seg, time)
// On the last bar, extend the current period's segment to the period's end.
if barstate.islast and not na(seg)
    line.set_x2(seg, time_close(tf))
 
// ─── FUTURE: the live BP, one period wide, to the right of the last bar ──────
// Created on the last bar (not on bar 0), so the 500-line pruning, which drops
// the oldest lines first, never removes it.
var line fut = na
if barstate.islast and not na(bp_live)
    int t1 = time_close(tf)                            // the next period's start
    int t2 = t1 + timeframe.in_seconds(tf) * 1000
    if na(fut)
        fut := line.new(t1, bp_live, t2, bp_live, xloc = xloc.bar_time, color = line_color, style = f_style(line_style), width = line_width)
    else
        line.set_xy1(fut, t1, bp_live)
        line.set_xy2(fut, t2, bp_live)

← 返回列表