Multi-Period Fibo (Expanding)

DimmDim · study · 139 行 · 点赞 11 · TradingView 原页

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

Pine Script

//@version=6
indicator("Multi-Period Fibo (Expanding)", overlay=true, max_lines_count=500, max_labels_count=500)
 
string periodsGroup = "Periods"
string styleGroup = "Style"
 
fwdBars = input.int(40, "Forward bars", minval=1, group=styleGroup)
lblGap  = input.int(25, "Label spacing (bars)", minval=2, group=styleGroup)
keyLabelsOnly = input.bool(true, "Label 0/50/100% only", group=styleGroup)
fibTxt  = input.string("small", "Font size", options=["tiny","small","normal","large","huge"], group=styleGroup)
txtColor = input.color(color.white, "Text color", group=styleGroup)
 
en12 = input.bool(true, "Year (12M)", group=periodsGroup)
c12  = input.color(#e91e63, "Year color", group=periodsGroup)
en6  = input.bool(true, "6M", group=periodsGroup)
c6   = input.color(#9c27b0, "6M color", group=periodsGroup)
en3  = input.bool(true, "3M", group=periodsGroup)
c3   = input.color(#3f51b5, "3M color", group=periodsGroup)
enM  = input.bool(true, "Month", group=periodsGroup)
cM   = input.color(#ff9800, "Month color", group=periodsGroup)
enW  = input.bool(true, "Week", group=periodsGroup)
cW   = input.color(#00bcd4, "Week color", group=periodsGroup)
enD  = input.bool(true, "Day", group=periodsGroup)
cD   = input.color(#8bc34a, "Day color", group=periodsGroup)
 
f_size(string s) =>
    switch s
        "tiny" => size.tiny
        "small" => size.small
        "large" => size.large
        "huge" => size.huge
        => size.normal
 
var array<line>  gLines = array.new_line()
var array<label> gLbls  = array.new_label()
var array<float> RATIOS  = array.from(0.0, 0.236, 0.382, 0.5, 0.618, 0.786, 1.0, 1.618, 2.0)
var array<int>   WIDTHS  = array.from(4, 3, 3, 4, 3, 3, 4, 2, 2)
var array<bool>  ENABLED = array.from(true, true, true, true, true, true, true, false, false)
var table t = table.new(position.top_left, 5, 7)
 
[y12HiP, y12LoP, y12TP, y12HiC, y12LoC] = request.security(syminfo.tickerid, "12M", [high[1], low[1], time[1], high, low], barmerge.gaps_off, barmerge.lookahead_off)
[y6HiP,  y6LoP,  y6TP,  y6HiC,  y6LoC]  = request.security(syminfo.tickerid, "6M",  [high[1], low[1], time[1], high, low], barmerge.gaps_off, barmerge.lookahead_off)
[y3HiP,  y3LoP,  y3TP,  y3HiC,  y3LoC]  = request.security(syminfo.tickerid, "3M",  [high[1], low[1], time[1], high, low], barmerge.gaps_off, barmerge.lookahead_off)
[mHiP,   mLoP,   mTP,   mHiC,   mLoC]   = request.security(syminfo.tickerid, "M",   [high[1], low[1], time[1], high, low], barmerge.gaps_off, barmerge.lookahead_off)
[wHiP,   wLoP,   wTP,   wHiC,   wLoC]   = request.security(syminfo.tickerid, "W",   [high[1], low[1], time[1], high, low], barmerge.gaps_off, barmerge.lookahead_off)
[dHiP,   dLoP,   dTP,   dHiC,   dLoC]   = request.security(syminfo.tickerid, "D",   [high[1], low[1], time[1], high, low], barmerge.gaps_off, barmerge.lookahead_off)
 
f_hi(float hp, float hc) => na(hp) ? hc : na(hc) ? hp : math.max(hp, hc)
f_lo(float lp, float lc) => na(lp) ? lc : na(lc) ? lp : math.min(lp, lc)
 
rHi12 = f_hi(y12HiP, y12HiC)
rLo12 = f_lo(y12LoP, y12LoC)
rHi6  = f_hi(y6HiP,  y6HiC)
rLo6  = f_lo(y6LoP,  y6LoC)
rHi3  = f_hi(y3HiP,  y3HiC)
rLo3  = f_lo(y3LoP,  y3LoC)
rHiM  = f_hi(mHiP,   mHiC)
rLoM  = f_lo(mLoP,   mLoC)
rHiW  = f_hi(wHiP,   wHiC)
rLoW  = f_lo(wLoP,   wLoC)
rHiD  = f_hi(dHiP,   dHiC)
rLoD  = f_lo(dLoP,   dLoC)
 
f_brk(float hp, float lp, float hc, float lc) =>
    bool bh = not na(hp) and not na(hc) and hc > hp
    bool bl = not na(lp) and not na(lc) and lc < lp
    bh and bl ? "hi+lo" : bh ? "hi" : bl ? "lo" : "-"
 
f_draw(string pname, float lo, float hi, int st, color c, bool en, int slot) =>
    if en and not na(lo) and not na(hi) and not na(st) and hi > lo
        int tfMs = int(timeframe.in_seconds(timeframe.period) * 1000)
        int rT = time + fwdBars * tfMs
        int lT = rT + slot * lblGap * tfMs
        for i = 0 to array.size(RATIOS) - 1
            if array.get(ENABLED, i)
                float r = array.get(RATIOS, i)
                float p = lo + (hi - lo) * r
                line ln = line.new(st, p, rT, p, xloc=xloc.bar_time, extend=extend.none, color=c, style=line.style_solid, width=array.get(WIDTHS, i))
                array.push(gLines, ln)
                bool isKey = r == 0.0 or r == 0.5 or r == 1.0
                if not keyLabelsOnly or isKey
                    string s = pname + " " + str.tostring(r * 100.0, "#.#") + "%  " + str.tostring(p, format.mintick)
                    label lb = label.new(lT, p, s, xloc=xloc.bar_time, yloc=yloc.price, style=label.style_label_left, color=color.new(color.black, 100), textcolor=txtColor, size=f_size(fibTxt))
                    array.push(gLbls, lb)
 
if barstate.islast
    while array.size(gLines) > 0
        line.delete(array.pop(gLines))
    while array.size(gLbls) > 0
        label.delete(array.pop(gLbls))
 
    f_draw("Y",  rLo12, rHi12, y12TP, c12, en12, 0)
    f_draw("6M", rLo6,  rHi6,  y6TP,  c6,  en6,  1)
    f_draw("3M", rLo3,  rHi3,  y3TP,  c3,  en3,  2)
    f_draw("M",  rLoM,  rHiM,  mTP,   cM,  enM,  3)
    f_draw("W",  rLoW,  rHiW,  wTP,   cW,  enW,  4)
    f_draw("D",  rLoD,  rHiD,  dTP,   cD,  enD,  5)
 
    table.cell(t, 0, 0, "Period", text_color=txtColor, bgcolor=color.new(color.black, 0))
    table.cell(t, 1, 0, "Low",    text_color=txtColor, bgcolor=color.new(color.black, 0))
    table.cell(t, 2, 0, "High",   text_color=txtColor, bgcolor=color.new(color.black, 0))
    table.cell(t, 3, 0, "Break",  text_color=txtColor, bgcolor=color.new(color.black, 0))
    table.cell(t, 4, 0, "Range",  text_color=txtColor, bgcolor=color.new(color.black, 0))
 
    table.cell(t, 0, 1, "Y",  text_color=c12)
    table.cell(t, 1, 1, str.tostring(rLo12, format.mintick), text_color=txtColor)
    table.cell(t, 2, 1, str.tostring(rHi12, format.mintick), text_color=txtColor)
    table.cell(t, 3, 1, f_brk(y12HiP, y12LoP, y12HiC, y12LoC), text_color=txtColor)
    table.cell(t, 4, 1, str.tostring(rHi12 - rLo12, format.mintick), text_color=txtColor)
 
    table.cell(t, 0, 2, "6M", text_color=c6)
    table.cell(t, 1, 2, str.tostring(rLo6, format.mintick), text_color=txtColor)
    table.cell(t, 2, 2, str.tostring(rHi6, format.mintick), text_color=txtColor)
    table.cell(t, 3, 2, f_brk(y6HiP, y6LoP, y6HiC, y6LoC), text_color=txtColor)
    table.cell(t, 4, 2, str.tostring(rHi6 - rLo6, format.mintick), text_color=txtColor)
 
    table.cell(t, 0, 3, "3M", text_color=c3)
    table.cell(t, 1, 3, str.tostring(rLo3, format.mintick), text_color=txtColor)
    table.cell(t, 2, 3, str.tostring(rHi3, format.mintick), text_color=txtColor)
    table.cell(t, 3, 3, f_brk(y3HiP, y3LoP, y3HiC, y3LoC), text_color=txtColor)
    table.cell(t, 4, 3, str.tostring(rHi3 - rLo3, format.mintick), text_color=txtColor)
 
    table.cell(t, 0, 4, "M", text_color=cM)
    table.cell(t, 1, 4, str.tostring(rLoM, format.mintick), text_color=txtColor)
    table.cell(t, 2, 4, str.tostring(rHiM, format.mintick), text_color=txtColor)
    table.cell(t, 3, 4, f_brk(mHiP, mLoP, mHiC, mLoC), text_color=txtColor)
    table.cell(t, 4, 4, str.tostring(rHiM - rLoM, format.mintick), text_color=txtColor)
 
    table.cell(t, 0, 5, "W", text_color=cW)
    table.cell(t, 1, 5, str.tostring(rLoW, format.mintick), text_color=txtColor)
    table.cell(t, 2, 5, str.tostring(rHiW, format.mintick), text_color=txtColor)
    table.cell(t, 3, 5, f_brk(wHiP, wLoP, wHiC, wLoC), text_color=txtColor)
    table.cell(t, 4, 5, str.tostring(rHiW - rLoW, format.mintick), text_color=txtColor)
 
    table.cell(t, 0, 6, "D", text_color=cD)
    table.cell(t, 1, 6, str.tostring(rLoD, format.mintick), text_color=txtColor)
    table.cell(t, 2, 6, str.tostring(rHiD, format.mintick), text_color=txtColor)
    table.cell(t, 3, 6, f_brk(dHiP, dLoP, dHiC, dLoC), text_color=txtColor)
    table.cell(t, 4, 6, str.tostring(rHiD - rLoD, format.mintick), text_color=txtColor)

← 返回列表