Dynamic Fibonacci Channels

bashu9 · study · 71 行 · 点赞 12 · TradingView 原页

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

Pine Script

//@version=6
indicator("Dynamic Fibonacci Channels", shorttitle = "DFC", overlay = true)
 
// Robert Krausz's Dynamic Fibonacci Channels (as in Fibonacci Trader): a
// center line, the Dynamic Balance Point, plus two Fibonacci channels around
// it, over a rolling window of the last N bars of the selected timeframe
// (current bar included, so the live bar updates tick by tick). No forward
// shift. Colors, widths, styles and visibility are set in the Style tab.
 
// ─── INPUTS ──────────────────────────────────────────────────────────────────
string tf     = input.timeframe("D", "Timeframe")
int    len    = input.int(5, "Length", minval = 2)
bool   ch1_on = input.bool(true, "Channel 1", inline = "ch1")
float  ch1_r  = input.float(0.382, "", step = 0.001, inline = "ch1")
bool   ch2_on = input.bool(true, "Channel 2", inline = "ch2")
float  ch2_r  = input.float(0.764, "", step = 0.001, inline = "ch2")
 
if timeframe.in_seconds(tf) < timeframe.in_seconds()
    runtime.error("Dynamic Fibonacci Channels: the timeframe (" + tf + ") must not be lower than the chart's timeframe.")
 
// ─── CALCULATION (reusable) ──────────────────────────────────────────────────
// Dynamic Balance Point: (highest high + lowest low + close) / 3.
dbp(float hh, float ll, float c) =>
    (hh + ll + c) / 3
 
// A channel whose FULL width is rng × ratio, centered on the DBP.
channel(float bp, float rng, float ratio) =>
    [bp + rng * ratio / 2, bp - rng * ratio / 2]
 
// Runs on the selected timeframe: HH/LL of the last `n` bars incl. the current one.
calc(int n) =>
    float hh = ta.highest(high, n)
    float ll = ta.lowest(low, n)
    [dbp(hh, ll, close), hh - ll]
 
[bp, rng] = request.security(syminfo.tickerid, tf, calc(len), lookahead = barmerge.lookahead_off)
 
// Smooth (always; matters on lower chart timeframes): one point per period, on its last chart
// bar, joined by the plot line (drawing only: the bars in between didn't know
// that period's value yet). A period whose closing bar isn't recognised gets
// its point on the next period's first bar. On the chart's last bar the line
// continues to the current period's live value (lookahead on, read only there,
// where it can't reach past existing data; lookahead off would give the last
// closed period on a historical last bar).
[bp_live, rng_live] = request.security(syminfo.tickerid, tf, calc(len), lookahead = barmerge.lookahead_on)
bool per_last = timeframe.in_seconds() >= timeframe.in_seconds(tf) or time_close == time_close(tf)
var int done_key = na  // the last period whose point was placed
bool per_late = timeframe.change(tf) and (na(done_key) or done_key != time(tf)[1])
float s_bp  = na
float s_rng = na
if per_last
    s_bp  := bp
    s_rng := rng
    done_key := time(tf)
else if per_late
    s_bp  := bp[1]
    s_rng := rng[1]
    done_key := time(tf)[1]
else if barstate.islast
    s_bp  := bp_live
    s_rng := rng_live
 
[c1_up, c1_lo] = channel(s_bp, s_rng, ch1_r)
[c2_up, c2_lo] = channel(s_bp, s_rng, ch2_r)
 
// ─── PLOTS (Style tab) ───────────────────────────────────────────────────────
plot(s_bp, "Dynamic BP", color.green, 1, linestyle = plot.linestyle_dashed, display = display.none)
plot(ch1_on ? c1_up : na, "Channel 1 Upper", color.red, 1)
plot(ch1_on ? c1_lo : na, "Channel 1 Lower", color.red, 1)
plot(ch2_on ? c2_up : na, "Channel 2 Upper", color.aqua, 1)
plot(ch2_on ? c2_lo : na, "Channel 2 Lower", color.aqua, 1)

← 返回列表