本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Balance Step", overlay = true)
// Robert Krausz's Balance Step (as in Fibonacci Trader): a simple moving
// average of the last 5 Balance Points ((high + low + close) / 3) of the
// selected timeframe, in step formation, projected one period forward. Each
// period shows the value known at the previous period's close; the latest value
// is drawn one period into the future.
// ─── INPUTS ──────────────────────────────────────────────────────────────────
string tf = input.timeframe("D", "Timeframe")
int len = input.int(5, "Length", minval = 1)
color line_color = input.color(color.orange, "Line Color")
int line_width = input.int(2, "Line Width", minval = 1, maxval = 4)
string line_style = input.string("Solid", "Line Style", options = ["Solid", "Dashed", "Dotted"])
// Drawn only while the chart is at or below the timeframe; on a higher chart
// nothing is drawn (no error), as in the other indicators.
bool bs_valid = timeframe.in_seconds() <= timeframe.in_seconds(tf)
// ─── CALCULATION ─────────────────────────────────────────────────────────────
bp(float h, float l, float c) =>
(h + l + c) / 3
bstep(int n) =>
ta.sma(bp(high, low, close), n)
// One step-line plot. On a chart at the timeframe: each bar's own value
// (including that bar) drawn one bar = one period forward (offset 1), so every
// bar shows the value known at the previous close and the latest value sits
// one period into the future. Below the timeframe: each chart bar shows the
// value in force for its period (the previous period's, [1] with lookahead on,
// no repaint); a plot's offset counts chart bars, so there's no future step.
bool same = timeframe.in_seconds() >= timeframe.in_seconds(tf)
float bs_now = request.security(syminfo.tickerid, tf, bstep(len), lookahead = barmerge.lookahead_off)
float bs_prev = request.security(syminfo.tickerid, tf, bstep(len)[1], lookahead = barmerge.lookahead_on)
plot_ls = line_style == "Dashed" ? plot.linestyle_dashed : line_style == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid
plot(bs_valid ? (same ? bs_now : bs_prev) : na, "Balance Step", line_color, line_width, plot.style_stepline, offset = same ? 1 : 0, linestyle = plot_ls, editable = false)
// Below the timeframe the plot ends on the last chart bar, mid-period. The rest
// is drawn as one polyline from the plot's last point (the last bar) in the same
// look (Inputs): the current step to the period's end, a riser to the live
// value, and that value one period into the future.
float bs_live = request.security(syminfo.tickerid, tf, bstep(len), lookahead = barmerge.lookahead_on)
var polyline tail = na
if barstate.islast
polyline.delete(tail)
tail := na
if bs_valid and not same and not na(bs_prev)
int t1 = time_close(tf) // the next period's start
array<chart.point> tp = array.from(chart.point.from_time(time, bs_prev), chart.point.from_time(t1, bs_prev))
if not na(bs_live)
tp.push(chart.point.from_time(t1, bs_live))
tp.push(chart.point.from_time(t1 + timeframe.in_seconds(tf) * 1000, bs_live))
tail := polyline.new(tp, xloc = xloc.bar_time, line_color = line_color, line_width = line_width,
line_style = line_style == "Dashed" ? line.style_dashed : line_style == "Dotted" ? line.style_dotted : line.style_solid)