Balance Point

bashu9 · study · 81 行 · 点赞 10 · 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/
// © bashu9
//@version=6
 
//#region Header Info
// ——————————————————————————————————————————————————————————————————————————
// Indicator:   Balance Point
// Author:      bashu9
// Description: Krausz's Balance Point: the previous period's (H+L+C)/3 across each period, plus the live one projected forward.
// ——————————————————————————————————————————————————————————————————————————
//#endregion
 
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(#00FF00, "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"])
 
// Drawn only while the chart is at or below the timeframe (e.g. D on a daily
// chart: each bar shows the previous bar's BP, and today's BP is projected onto
// the next bar); on a higher chart nothing is drawn.
bool bp_valid = timeframe.in_seconds() <= timeframe.in_seconds(tf)
 
f_style(s) =>
    s == "Solid" ? line.style_solid : s == "Dotted" ? line.style_dotted : line.style_dashed
 
// ─── CALCULATION ─────────────────────────────────────────────────────────────
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: its high, low and close so far,
// collected from the chart bars (no lookahead).
var float per_hi = na
var float per_lo = na
if timeframe.change(tf) or na(per_hi)
    per_hi := high
    per_lo := low
else
    per_hi := math.max(per_hi, high)
    per_lo := math.min(per_lo, low)
float bp_live = bp(per_hi, per_lo, close)
 
// ─── 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 bp_valid and 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 bp_valid and 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 bp_valid and 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)

← 返回列表