The Fibonator by KingDonut

KingDonut · study · 555 行 · 点赞 205 · 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/
// © KingDonut
 
//@version=6
// ═══════════════════════════════════════════════════════════════════════
// THE FIBONATOR by KingDonut
// Auto Fibonacci from ATR trend swings, taken from any timeframe.
//
// How it works:
//  - An ATR trend line (period / multiplier) runs on the Fib Timeframe.
//  - When the ATR trend flips, the high and low of the move that just
//    ended become the Fib swing.
//  - Fibs are drawn from that swing and stay until the next flip.
//  - Fibs only appear once the Fib Timeframe candle has CLOSED and the
//    new ATR direction is confirmed. No repainting.
//
// 0% sits at the end of the move, 100% at the start of the move.
// Levels above 100% project beyond the move, negative levels project
// back beyond the 0% point.
// ═══════════════════════════════════════════════════════════════════════
 
indicator("The Fibonator by KingDonut", shorttitle="Fibonator by KD", overlay=true, max_lines_count=500, max_labels_count=500, max_bars_back=1100)
 
// ═══════════════════════════════════════════════════════════════════════
// INPUTS - MAIN
// ═══════════════════════════════════════════════════════════════════════
 
const string G_MAIN = "Main Settings"
const string G_RET  = "Retracement Levels (0 - 100%)"
const string G_EXT  = "Extension Levels (above 100%)"
const string G_NEG  = "Inverted Extensions (beyond 0%)"
const string G_MAN  = "Manual Levels"
 
showLabels  = input.bool(true, "Show Level Labels", group=G_MAIN, tooltip="Turns the level name labels (e.g. 61.8%) on or off.")
labelSize   = input.string("Small", "Label Size", options=["Tiny", "Small", "Normal", "Large"], group=G_MAIN)
labelColor  = input.color(color.white, "Label Colour", group=G_MAIN)
labelOffset = input.int(3, "Label Offset (bars right of last bar)", minval=0, maxval=100, group=G_MAIN)
fibTfInput  = input.string("1H", "Fib Timeframe", options=["15m", "30m", "1H", "2H", "4H", "Daily"], group=G_MAIN, tooltip="Timeframe the swings are taken from. Chart timeframe must be equal or lower. Higher timeframes need more chart history to find a swing.")
atrPeriod   = input.int(5, "ATR Period", minval=1, group=G_MAIN)
atrMult     = input.float(2.62, "ATR Multiplier", minval=0.1, step=0.01, group=G_MAIN)
sameColor   = input.bool(true, "Use One Colour For All Levels", inline="sc", group=G_MAIN, tooltip="On: every line and label uses the colour picked here. Off: each level uses its own colour from its row below.")
sameColorVal = input.color(#4DA6FF, "", inline="sc", group=G_MAIN)
 
 
// ═══════════════════════════════════════════════════════════════════════
// INPUTS - RETRACEMENT LEVELS
// ═══════════════════════════════════════════════════════════════════════
 
r0_on  = input.bool(true, "0%", inline="r0", group=G_RET)
r0_col = input.color(#4DA6FF, "", inline="r0", group=G_RET)
r0_w   = input.int(3, "", minval=1, maxval=5, inline="r0", group=G_RET)
r0_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r0", group=G_RET)
r1_on  = input.bool(false, "11.4%", inline="r1", group=G_RET)
r1_col = input.color(#4DA6FF, "", inline="r1", group=G_RET)
r1_w   = input.int(2, "", minval=1, maxval=5, inline="r1", group=G_RET)
r1_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r1", group=G_RET)
r2_on  = input.bool(false, "14.6%", inline="r2", group=G_RET)
r2_col = input.color(#4DA6FF, "", inline="r2", group=G_RET)
r2_w   = input.int(2, "", minval=1, maxval=5, inline="r2", group=G_RET)
r2_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r2", group=G_RET)
r3_on  = input.bool(true, "23.6%", inline="r3", group=G_RET)
r3_col = input.color(#4DA6FF, "", inline="r3", group=G_RET)
r3_w   = input.int(2, "", minval=1, maxval=5, inline="r3", group=G_RET)
r3_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r3", group=G_RET)
r4_on  = input.bool(false, "25%", inline="r4", group=G_RET)
r4_col = input.color(#4DA6FF, "", inline="r4", group=G_RET)
r4_w   = input.int(2, "", minval=1, maxval=5, inline="r4", group=G_RET)
r4_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r4", group=G_RET)
r5_on  = input.bool(false, "33.3%", inline="r5", group=G_RET)
r5_col = input.color(#4DA6FF, "", inline="r5", group=G_RET)
r5_w   = input.int(2, "", minval=1, maxval=5, inline="r5", group=G_RET)
r5_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r5", group=G_RET)
r6_on  = input.bool(true, "38.2%", inline="r6", group=G_RET)
r6_col = input.color(#4DA6FF, "", inline="r6", group=G_RET)
r6_w   = input.int(2, "", minval=1, maxval=5, inline="r6", group=G_RET)
r6_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r6", group=G_RET)
r7_on  = input.bool(true, "50%", inline="r7", group=G_RET)
r7_col = input.color(#4DA6FF, "", inline="r7", group=G_RET)
r7_w   = input.int(2, "", minval=1, maxval=5, inline="r7", group=G_RET)
r7_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r7", group=G_RET)
r8_on  = input.bool(true, "61.8%", inline="r8", group=G_RET)
r8_col = input.color(#4DA6FF, "", inline="r8", group=G_RET)
r8_w   = input.int(3, "", minval=1, maxval=5, inline="r8", group=G_RET)
r8_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r8", group=G_RET)
r9_on  = input.bool(false, "65%", inline="r9", group=G_RET)
r9_col = input.color(#4DA6FF, "", inline="r9", group=G_RET)
r9_w   = input.int(2, "", minval=1, maxval=5, inline="r9", group=G_RET)
r9_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r9", group=G_RET)
r10_on  = input.bool(false, "66.7%", inline="r10", group=G_RET)
r10_col = input.color(#4DA6FF, "", inline="r10", group=G_RET)
r10_w   = input.int(2, "", minval=1, maxval=5, inline="r10", group=G_RET)
r10_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r10", group=G_RET)
r11_on  = input.bool(false, "70.7%", inline="r11", group=G_RET)
r11_col = input.color(#4DA6FF, "", inline="r11", group=G_RET)
r11_w   = input.int(2, "", minval=1, maxval=5, inline="r11", group=G_RET)
r11_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r11", group=G_RET)
r12_on  = input.bool(false, "75%", inline="r12", group=G_RET)
r12_col = input.color(#4DA6FF, "", inline="r12", group=G_RET)
r12_w   = input.int(2, "", minval=1, maxval=5, inline="r12", group=G_RET)
r12_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r12", group=G_RET)
r13_on  = input.bool(false, "76.4%", inline="r13", group=G_RET)
r13_col = input.color(#4DA6FF, "", inline="r13", group=G_RET)
r13_w   = input.int(2, "", minval=1, maxval=5, inline="r13", group=G_RET)
r13_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r13", group=G_RET)
r14_on  = input.bool(true, "78.6%", inline="r14", group=G_RET)
r14_col = input.color(#4DA6FF, "", inline="r14", group=G_RET)
r14_w   = input.int(2, "", minval=1, maxval=5, inline="r14", group=G_RET)
r14_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r14", group=G_RET)
r15_on  = input.bool(false, "85.4%", inline="r15", group=G_RET)
r15_col = input.color(#4DA6FF, "", inline="r15", group=G_RET)
r15_w   = input.int(2, "", minval=1, maxval=5, inline="r15", group=G_RET)
r15_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r15", group=G_RET)
r16_on  = input.bool(false, "88.6%", inline="r16", group=G_RET)
r16_col = input.color(#4DA6FF, "", inline="r16", group=G_RET)
r16_w   = input.int(2, "", minval=1, maxval=5, inline="r16", group=G_RET)
r16_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r16", group=G_RET)
r17_on  = input.bool(true, "100%", inline="r17", group=G_RET)
r17_col = input.color(#4DA6FF, "", inline="r17", group=G_RET)
r17_w   = input.int(3, "", minval=1, maxval=5, inline="r17", group=G_RET)
r17_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="r17", group=G_RET)
 
// ═══════════════════════════════════════════════════════════════════════
// INPUTS - EXTENSION LEVELS
// ═══════════════════════════════════════════════════════════════════════
 
e0_on  = input.bool(false, "113%", inline="e0", group=G_EXT)
e0_col = input.color(#4DA6FF, "", inline="e0", group=G_EXT)
e0_w   = input.int(2, "", minval=1, maxval=5, inline="e0", group=G_EXT)
e0_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e0", group=G_EXT)
e1_on  = input.bool(false, "123.6%", inline="e1", group=G_EXT)
e1_col = input.color(#4DA6FF, "", inline="e1", group=G_EXT)
e1_w   = input.int(2, "", minval=1, maxval=5, inline="e1", group=G_EXT)
e1_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e1", group=G_EXT)
e2_on  = input.bool(true, "127.2%", inline="e2", group=G_EXT)
e2_col = input.color(#4DA6FF, "", inline="e2", group=G_EXT)
e2_w   = input.int(2, "", minval=1, maxval=5, inline="e2", group=G_EXT)
e2_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e2", group=G_EXT)
e3_on  = input.bool(false, "138.2%", inline="e3", group=G_EXT)
e3_col = input.color(#4DA6FF, "", inline="e3", group=G_EXT)
e3_w   = input.int(2, "", minval=1, maxval=5, inline="e3", group=G_EXT)
e3_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e3", group=G_EXT)
e4_on  = input.bool(false, "141.4%", inline="e4", group=G_EXT)
e4_col = input.color(#4DA6FF, "", inline="e4", group=G_EXT)
e4_w   = input.int(2, "", minval=1, maxval=5, inline="e4", group=G_EXT)
e4_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e4", group=G_EXT)
e5_on  = input.bool(false, "150%", inline="e5", group=G_EXT)
e5_col = input.color(#4DA6FF, "", inline="e5", group=G_EXT)
e5_w   = input.int(2, "", minval=1, maxval=5, inline="e5", group=G_EXT)
e5_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e5", group=G_EXT)
e6_on  = input.bool(true, "161.8%", inline="e6", group=G_EXT)
e6_col = input.color(#4DA6FF, "", inline="e6", group=G_EXT)
e6_w   = input.int(2, "", minval=1, maxval=5, inline="e6", group=G_EXT)
e6_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e6", group=G_EXT)
e7_on  = input.bool(false, "200%", inline="e7", group=G_EXT)
e7_col = input.color(#4DA6FF, "", inline="e7", group=G_EXT)
e7_w   = input.int(2, "", minval=1, maxval=5, inline="e7", group=G_EXT)
e7_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e7", group=G_EXT)
e8_on  = input.bool(false, "224%", inline="e8", group=G_EXT)
e8_col = input.color(#4DA6FF, "", inline="e8", group=G_EXT)
e8_w   = input.int(2, "", minval=1, maxval=5, inline="e8", group=G_EXT)
e8_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e8", group=G_EXT)
e9_on  = input.bool(false, "227.2%", inline="e9", group=G_EXT)
e9_col = input.color(#4DA6FF, "", inline="e9", group=G_EXT)
e9_w   = input.int(2, "", minval=1, maxval=5, inline="e9", group=G_EXT)
e9_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e9", group=G_EXT)
e10_on  = input.bool(false, "241.4%", inline="e10", group=G_EXT)
e10_col = input.color(#4DA6FF, "", inline="e10", group=G_EXT)
e10_w   = input.int(2, "", minval=1, maxval=5, inline="e10", group=G_EXT)
e10_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e10", group=G_EXT)
e11_on  = input.bool(true, "261.8%", inline="e11", group=G_EXT)
e11_col = input.color(#4DA6FF, "", inline="e11", group=G_EXT)
e11_w   = input.int(2, "", minval=1, maxval=5, inline="e11", group=G_EXT)
e11_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e11", group=G_EXT)
e12_on  = input.bool(false, "300%", inline="e12", group=G_EXT)
e12_col = input.color(#4DA6FF, "", inline="e12", group=G_EXT)
e12_w   = input.int(2, "", minval=1, maxval=5, inline="e12", group=G_EXT)
e12_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e12", group=G_EXT)
e13_on  = input.bool(false, "314%", inline="e13", group=G_EXT)
e13_col = input.color(#4DA6FF, "", inline="e13", group=G_EXT)
e13_w   = input.int(2, "", minval=1, maxval=5, inline="e13", group=G_EXT)
e13_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e13", group=G_EXT)
e14_on  = input.bool(false, "327.2%", inline="e14", group=G_EXT)
e14_col = input.color(#4DA6FF, "", inline="e14", group=G_EXT)
e14_w   = input.int(2, "", minval=1, maxval=5, inline="e14", group=G_EXT)
e14_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e14", group=G_EXT)
e15_on  = input.bool(false, "341.4%", inline="e15", group=G_EXT)
e15_col = input.color(#4DA6FF, "", inline="e15", group=G_EXT)
e15_w   = input.int(2, "", minval=1, maxval=5, inline="e15", group=G_EXT)
e15_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e15", group=G_EXT)
e16_on  = input.bool(false, "361.8%", inline="e16", group=G_EXT)
e16_col = input.color(#4DA6FF, "", inline="e16", group=G_EXT)
e16_w   = input.int(2, "", minval=1, maxval=5, inline="e16", group=G_EXT)
e16_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e16", group=G_EXT)
e17_on  = input.bool(false, "400%", inline="e17", group=G_EXT)
e17_col = input.color(#4DA6FF, "", inline="e17", group=G_EXT)
e17_w   = input.int(2, "", minval=1, maxval=5, inline="e17", group=G_EXT)
e17_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e17", group=G_EXT)
e18_on  = input.bool(false, "423.6%", inline="e18", group=G_EXT)
e18_col = input.color(#4DA6FF, "", inline="e18", group=G_EXT)
e18_w   = input.int(2, "", minval=1, maxval=5, inline="e18", group=G_EXT)
e18_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e18", group=G_EXT)
e19_on  = input.bool(false, "461.8%", inline="e19", group=G_EXT)
e19_col = input.color(#4DA6FF, "", inline="e19", group=G_EXT)
e19_w   = input.int(2, "", minval=1, maxval=5, inline="e19", group=G_EXT)
e19_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e19", group=G_EXT)
e20_on  = input.bool(false, "476.4%", inline="e20", group=G_EXT)
e20_col = input.color(#4DA6FF, "", inline="e20", group=G_EXT)
e20_w   = input.int(2, "", minval=1, maxval=5, inline="e20", group=G_EXT)
e20_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="e20", group=G_EXT)
 
// ═══════════════════════════════════════════════════════════════════════
// INPUTS - INVERTED EXTENSIONS
// ═══════════════════════════════════════════════════════════════════════
 
n0_on  = input.bool(false, "-13%", inline="n0", group=G_NEG)
n0_col = input.color(#4DA6FF, "", inline="n0", group=G_NEG)
n0_w   = input.int(2, "", minval=1, maxval=5, inline="n0", group=G_NEG)
n0_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n0", group=G_NEG)
n1_on  = input.bool(false, "-23.6%", inline="n1", group=G_NEG)
n1_col = input.color(#4DA6FF, "", inline="n1", group=G_NEG)
n1_w   = input.int(2, "", minval=1, maxval=5, inline="n1", group=G_NEG)
n1_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n1", group=G_NEG)
n2_on  = input.bool(true, "-27.2%", inline="n2", group=G_NEG)
n2_col = input.color(#4DA6FF, "", inline="n2", group=G_NEG)
n2_w   = input.int(2, "", minval=1, maxval=5, inline="n2", group=G_NEG)
n2_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n2", group=G_NEG)
n3_on  = input.bool(false, "-38.2%", inline="n3", group=G_NEG)
n3_col = input.color(#4DA6FF, "", inline="n3", group=G_NEG)
n3_w   = input.int(2, "", minval=1, maxval=5, inline="n3", group=G_NEG)
n3_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n3", group=G_NEG)
n4_on  = input.bool(false, "-41.4%", inline="n4", group=G_NEG)
n4_col = input.color(#4DA6FF, "", inline="n4", group=G_NEG)
n4_w   = input.int(2, "", minval=1, maxval=5, inline="n4", group=G_NEG)
n4_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n4", group=G_NEG)
n5_on  = input.bool(false, "-50%", inline="n5", group=G_NEG)
n5_col = input.color(#4DA6FF, "", inline="n5", group=G_NEG)
n5_w   = input.int(2, "", minval=1, maxval=5, inline="n5", group=G_NEG)
n5_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n5", group=G_NEG)
n6_on  = input.bool(true, "-61.8%", inline="n6", group=G_NEG)
n6_col = input.color(#4DA6FF, "", inline="n6", group=G_NEG)
n6_w   = input.int(2, "", minval=1, maxval=5, inline="n6", group=G_NEG)
n6_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n6", group=G_NEG)
n7_on  = input.bool(false, "-100%", inline="n7", group=G_NEG)
n7_col = input.color(#4DA6FF, "", inline="n7", group=G_NEG)
n7_w   = input.int(2, "", minval=1, maxval=5, inline="n7", group=G_NEG)
n7_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n7", group=G_NEG)
n8_on  = input.bool(false, "-124%", inline="n8", group=G_NEG)
n8_col = input.color(#4DA6FF, "", inline="n8", group=G_NEG)
n8_w   = input.int(2, "", minval=1, maxval=5, inline="n8", group=G_NEG)
n8_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n8", group=G_NEG)
n9_on  = input.bool(false, "-127.2%", inline="n9", group=G_NEG)
n9_col = input.color(#4DA6FF, "", inline="n9", group=G_NEG)
n9_w   = input.int(2, "", minval=1, maxval=5, inline="n9", group=G_NEG)
n9_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n9", group=G_NEG)
n10_on  = input.bool(false, "-141.4%", inline="n10", group=G_NEG)
n10_col = input.color(#4DA6FF, "", inline="n10", group=G_NEG)
n10_w   = input.int(2, "", minval=1, maxval=5, inline="n10", group=G_NEG)
n10_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n10", group=G_NEG)
n11_on  = input.bool(true, "-161.8%", inline="n11", group=G_NEG)
n11_col = input.color(#4DA6FF, "", inline="n11", group=G_NEG)
n11_w   = input.int(2, "", minval=1, maxval=5, inline="n11", group=G_NEG)
n11_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n11", group=G_NEG)
n12_on  = input.bool(false, "-200%", inline="n12", group=G_NEG)
n12_col = input.color(#4DA6FF, "", inline="n12", group=G_NEG)
n12_w   = input.int(2, "", minval=1, maxval=5, inline="n12", group=G_NEG)
n12_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n12", group=G_NEG)
n13_on  = input.bool(false, "-214%", inline="n13", group=G_NEG)
n13_col = input.color(#4DA6FF, "", inline="n13", group=G_NEG)
n13_w   = input.int(2, "", minval=1, maxval=5, inline="n13", group=G_NEG)
n13_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n13", group=G_NEG)
n14_on  = input.bool(false, "-227.2%", inline="n14", group=G_NEG)
n14_col = input.color(#4DA6FF, "", inline="n14", group=G_NEG)
n14_w   = input.int(2, "", minval=1, maxval=5, inline="n14", group=G_NEG)
n14_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n14", group=G_NEG)
n15_on  = input.bool(false, "-241.4%", inline="n15", group=G_NEG)
n15_col = input.color(#4DA6FF, "", inline="n15", group=G_NEG)
n15_w   = input.int(2, "", minval=1, maxval=5, inline="n15", group=G_NEG)
n15_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n15", group=G_NEG)
n16_on  = input.bool(false, "-261.8%", inline="n16", group=G_NEG)
n16_col = input.color(#4DA6FF, "", inline="n16", group=G_NEG)
n16_w   = input.int(2, "", minval=1, maxval=5, inline="n16", group=G_NEG)
n16_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n16", group=G_NEG)
n17_on  = input.bool(false, "-300%", inline="n17", group=G_NEG)
n17_col = input.color(#4DA6FF, "", inline="n17", group=G_NEG)
n17_w   = input.int(2, "", minval=1, maxval=5, inline="n17", group=G_NEG)
n17_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n17", group=G_NEG)
n18_on  = input.bool(false, "-323.6%", inline="n18", group=G_NEG)
n18_col = input.color(#4DA6FF, "", inline="n18", group=G_NEG)
n18_w   = input.int(2, "", minval=1, maxval=5, inline="n18", group=G_NEG)
n18_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n18", group=G_NEG)
n19_on  = input.bool(false, "-361.8%", inline="n19", group=G_NEG)
n19_col = input.color(#4DA6FF, "", inline="n19", group=G_NEG)
n19_w   = input.int(2, "", minval=1, maxval=5, inline="n19", group=G_NEG)
n19_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n19", group=G_NEG)
n20_on  = input.bool(false, "-376.4%", inline="n20", group=G_NEG)
n20_col = input.color(#4DA6FF, "", inline="n20", group=G_NEG)
n20_w   = input.int(2, "", minval=1, maxval=5, inline="n20", group=G_NEG)
n20_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="n20", group=G_NEG)
 
// ═══════════════════════════════════════════════════════════════════════
// INPUTS - MANUAL LEVELS (enter any % you like, e.g. 41.4 or -50)
// ═══════════════════════════════════════════════════════════════════════
 
m0_on  = input.bool(false, "Manual 1", inline="m0a", group=G_MAN)
m0_pct = input.float(30, "", step=0.1, inline="m0a", group=G_MAN, tooltip="Level in percent. 0 = end of move, 100 = start of move.")
m0_col = input.color(#4DA6FF, "Line", inline="m0b", group=G_MAN)
m0_w   = input.int(2, "", minval=1, maxval=5, inline="m0b", group=G_MAN)
m0_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="m0b", group=G_MAN)
m1_on  = input.bool(false, "Manual 2", inline="m1a", group=G_MAN)
m1_pct = input.float(45, "", step=0.1, inline="m1a", group=G_MAN, tooltip="Level in percent. 0 = end of move, 100 = start of move.")
m1_col = input.color(#4DA6FF, "Line", inline="m1b", group=G_MAN)
m1_w   = input.int(2, "", minval=1, maxval=5, inline="m1b", group=G_MAN)
m1_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="m1b", group=G_MAN)
m2_on  = input.bool(false, "Manual 3", inline="m2a", group=G_MAN)
m2_pct = input.float(55, "", step=0.1, inline="m2a", group=G_MAN, tooltip="Level in percent. 0 = end of move, 100 = start of move.")
m2_col = input.color(#4DA6FF, "Line", inline="m2b", group=G_MAN)
m2_w   = input.int(2, "", minval=1, maxval=5, inline="m2b", group=G_MAN)
m2_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="m2b", group=G_MAN)
m3_on  = input.bool(false, "Manual 4", inline="m3a", group=G_MAN)
m3_pct = input.float(90, "", step=0.1, inline="m3a", group=G_MAN, tooltip="Level in percent. 0 = end of move, 100 = start of move.")
m3_col = input.color(#4DA6FF, "Line", inline="m3b", group=G_MAN)
m3_w   = input.int(2, "", minval=1, maxval=5, inline="m3b", group=G_MAN)
m3_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="m3b", group=G_MAN)
m4_on  = input.bool(false, "Manual 5", inline="m4a", group=G_MAN)
m4_pct = input.float(190, "", step=0.1, inline="m4a", group=G_MAN, tooltip="Level in percent. 0 = end of move, 100 = start of move.")
m4_col = input.color(#4DA6FF, "Line", inline="m4b", group=G_MAN)
m4_w   = input.int(2, "", minval=1, maxval=5, inline="m4b", group=G_MAN)
m4_st  = input.string("Solid", "", options=["Solid", "Dashed", "Dotted"], inline="m4b", group=G_MAN)
 
// ═══════════════════════════════════════════════════════════════════════
// FUNCTIONS
// ═══════════════════════════════════════════════════════════════════════
 
getLineStyle(style) =>
    style == "Solid" ? line.style_solid : style == "Dashed" ? line.style_dashed : line.style_dotted
 
getLabelSize(s) =>
    s == "Tiny" ? size.tiny : s == "Small" ? size.small : s == "Normal" ? size.normal : size.large
 
getFibTf(s) =>
    switch s
        "15m"   => "15"
        "30m"   => "30"
        "1H"    => "60"
        "2H"    => "120"
        "4H"    => "240"
        "Daily" => "D"
        => "60"
 
// ATR trend line (the "SuperTrend" engine, never drawn)
calcAtrTrend(period, multiplier) =>
    atr_val = ta.atr(period)
    hl2_val = (high + low) / 2
    upperBand = hl2_val + (multiplier * atr_val)
    lowerBand = hl2_val - (multiplier * atr_val)
 
    var float finalUpperBand = na
    var float finalLowerBand = na
    var bool trendVar = true
 
    finalUpperBand := na(finalUpperBand[1]) or upperBand < finalUpperBand[1] or close[1] > finalUpperBand[1] ? upperBand : finalUpperBand[1]
    finalLowerBand := na(finalLowerBand[1]) or lowerBand > finalLowerBand[1] or close[1] < finalLowerBand[1] ? lowerBand : finalLowerBand[1]
 
    trendVar := close > finalUpperBand ? true : close < finalLowerBand ? false : trendVar[1]
    atrLine = trendVar ? finalLowerBand : finalUpperBand
 
    [atrLine, trendVar]
 
// High / low of the move that just ended, found on each ATR trend flip
calcSwingPoints(period, multiplier) =>
    [st, tr] = calcAtrTrend(period, multiplier)
    var float swingHigh = na
    var float swingLow = na
    trendChanged_local = ta.change(tr)
    changedToDown_local = trendChanged_local and tr == false
    changedToUp_local = trendChanged_local and tr == true
    if changedToDown_local
        float maxHigh = high
        int lookback = 1
        while lookback < 500
            if tr[lookback] == false
                break
            if high[lookback] > maxHigh
                maxHigh := high[lookback]
            lookback += 1
        swingHigh := maxHigh
    if changedToUp_local
        float minLow = low
        int lookback = 1
        while lookback < 500
            if tr[lookback] == true
                break
            if low[lookback] < minLow
                minLow := low[lookback]
            lookback += 1
        swingLow := minLow
    [swingHigh, swingLow, changedToDown_local, changedToUp_local]
 
// Same values, but from the last CLOSED bar only (offset 1).
// Used with lookahead_on this gives the non-repainting pattern: the flip
// is only reported on the first chart bar after the Fib Timeframe candle closed.
calcConfirmed(period, multiplier) =>
    [sh, sl, cd, cu] = calcSwingPoints(period, multiplier)
    [sh[1], sl[1], cd[1], cu[1]]
 
// ═══════════════════════════════════════════════════════════════════════
// LEVEL TABLE (built once)
// ═══════════════════════════════════════════════════════════════════════
 
var array<float>  lvPct   = array.new<float>()
var array<bool>   lvOn    = array.new<bool>()
var array<color>  lvCol   = array.new<color>()
var array<int>    lvWidth = array.new<int>()
var array<string> lvStyle = array.new<string>()
var array<string> lvText  = array.new<string>()
 
addLevel(pct, on, col, w, st, txt) =>
    array.push(lvPct, pct)
    array.push(lvOn, on)
    array.push(lvCol, col)
    array.push(lvWidth, w)
    array.push(lvStyle, st)
    array.push(lvText, txt)
 
if barstate.isfirst
    addLevel(0.0, r0_on, r0_col, r0_w, r0_st, "0%")
    addLevel(11.4, r1_on, r1_col, r1_w, r1_st, "11.4%")
    addLevel(14.6, r2_on, r2_col, r2_w, r2_st, "14.6%")
    addLevel(23.6, r3_on, r3_col, r3_w, r3_st, "23.6%")
    addLevel(25.0, r4_on, r4_col, r4_w, r4_st, "25%")
    addLevel(33.3, r5_on, r5_col, r5_w, r5_st, "33.3%")
    addLevel(38.2, r6_on, r6_col, r6_w, r6_st, "38.2%")
    addLevel(50.0, r7_on, r7_col, r7_w, r7_st, "50%")
    addLevel(61.8, r8_on, r8_col, r8_w, r8_st, "61.8%")
    addLevel(65.0, r9_on, r9_col, r9_w, r9_st, "65%")
    addLevel(66.7, r10_on, r10_col, r10_w, r10_st, "66.7%")
    addLevel(70.7, r11_on, r11_col, r11_w, r11_st, "70.7%")
    addLevel(75.0, r12_on, r12_col, r12_w, r12_st, "75%")
    addLevel(76.4, r13_on, r13_col, r13_w, r13_st, "76.4%")
    addLevel(78.6, r14_on, r14_col, r14_w, r14_st, "78.6%")
    addLevel(85.4, r15_on, r15_col, r15_w, r15_st, "85.4%")
    addLevel(88.6, r16_on, r16_col, r16_w, r16_st, "88.6%")
    addLevel(100.0, r17_on, r17_col, r17_w, r17_st, "100%")
    addLevel(113.0, e0_on, e0_col, e0_w, e0_st, "113%")
    addLevel(123.6, e1_on, e1_col, e1_w, e1_st, "123.6%")
    addLevel(127.2, e2_on, e2_col, e2_w, e2_st, "127.2%")
    addLevel(138.2, e3_on, e3_col, e3_w, e3_st, "138.2%")
    addLevel(141.4, e4_on, e4_col, e4_w, e4_st, "141.4%")
    addLevel(150.0, e5_on, e5_col, e5_w, e5_st, "150%")
    addLevel(161.8, e6_on, e6_col, e6_w, e6_st, "161.8%")
    addLevel(200.0, e7_on, e7_col, e7_w, e7_st, "200%")
    addLevel(224.0, e8_on, e8_col, e8_w, e8_st, "224%")
    addLevel(227.2, e9_on, e9_col, e9_w, e9_st, "227.2%")
    addLevel(241.4, e10_on, e10_col, e10_w, e10_st, "241.4%")
    addLevel(261.8, e11_on, e11_col, e11_w, e11_st, "261.8%")
    addLevel(300.0, e12_on, e12_col, e12_w, e12_st, "300%")
    addLevel(314.0, e13_on, e13_col, e13_w, e13_st, "314%")
    addLevel(327.2, e14_on, e14_col, e14_w, e14_st, "327.2%")
    addLevel(341.4, e15_on, e15_col, e15_w, e15_st, "341.4%")
    addLevel(361.8, e16_on, e16_col, e16_w, e16_st, "361.8%")
    addLevel(400.0, e17_on, e17_col, e17_w, e17_st, "400%")
    addLevel(423.6, e18_on, e18_col, e18_w, e18_st, "423.6%")
    addLevel(461.8, e19_on, e19_col, e19_w, e19_st, "461.8%")
    addLevel(476.4, e20_on, e20_col, e20_w, e20_st, "476.4%")
    addLevel(-13.0, n0_on, n0_col, n0_w, n0_st, "-13%")
    addLevel(-23.6, n1_on, n1_col, n1_w, n1_st, "-23.6%")
    addLevel(-27.2, n2_on, n2_col, n2_w, n2_st, "-27.2%")
    addLevel(-38.2, n3_on, n3_col, n3_w, n3_st, "-38.2%")
    addLevel(-41.4, n4_on, n4_col, n4_w, n4_st, "-41.4%")
    addLevel(-50.0, n5_on, n5_col, n5_w, n5_st, "-50%")
    addLevel(-61.8, n6_on, n6_col, n6_w, n6_st, "-61.8%")
    addLevel(-100.0, n7_on, n7_col, n7_w, n7_st, "-100%")
    addLevel(-124.0, n8_on, n8_col, n8_w, n8_st, "-124%")
    addLevel(-127.2, n9_on, n9_col, n9_w, n9_st, "-127.2%")
    addLevel(-141.4, n10_on, n10_col, n10_w, n10_st, "-141.4%")
    addLevel(-161.8, n11_on, n11_col, n11_w, n11_st, "-161.8%")
    addLevel(-200.0, n12_on, n12_col, n12_w, n12_st, "-200%")
    addLevel(-214.0, n13_on, n13_col, n13_w, n13_st, "-214%")
    addLevel(-227.2, n14_on, n14_col, n14_w, n14_st, "-227.2%")
    addLevel(-241.4, n15_on, n15_col, n15_w, n15_st, "-241.4%")
    addLevel(-261.8, n16_on, n16_col, n16_w, n16_st, "-261.8%")
    addLevel(-300.0, n17_on, n17_col, n17_w, n17_st, "-300%")
    addLevel(-323.6, n18_on, n18_col, n18_w, n18_st, "-323.6%")
    addLevel(-361.8, n19_on, n19_col, n19_w, n19_st, "-361.8%")
    addLevel(-376.4, n20_on, n20_col, n20_w, n20_st, "-376.4%")
    addLevel(m0_pct, m0_on, m0_col, m0_w, m0_st, str.tostring(m0_pct, "#.###") + "%")
    addLevel(m1_pct, m1_on, m1_col, m1_w, m1_st, str.tostring(m1_pct, "#.###") + "%")
    addLevel(m2_pct, m2_on, m2_col, m2_w, m2_st, str.tostring(m2_pct, "#.###") + "%")
    addLevel(m3_pct, m3_on, m3_col, m3_w, m3_st, str.tostring(m3_pct, "#.###") + "%")
    addLevel(m4_pct, m4_on, m4_col, m4_w, m4_st, str.tostring(m4_pct, "#.###") + "%")
 
// ═══════════════════════════════════════════════════════════════════════
// CALCULATIONS
// ═══════════════════════════════════════════════════════════════════════
 
fibTf = getFibTf(fibTfInput)
 
[swHigh, swLow, flipDown, flipUp] = request.security(syminfo.tickerid, fibTf, calcConfirmed(atrPeriod, atrMult), lookahead=barmerge.lookahead_on)
 
var float lastHigh = na
var float lastLow = na
var bool  lastFlipDown = false
var int   flipBar = na
 
if flipDown and not na(swHigh)
    lastHigh := swHigh
if flipUp and not na(swLow)
    lastLow := swLow
 
flipNow = flipDown or flipUp
if flipNow
    lastFlipDown := flipDown
    if not flipNow[1]
        flipBar := bar_index
 
// ═══════════════════════════════════════════════════════════════════════
// DRAWING (last bar only)
// ═══════════════════════════════════════════════════════════════════════
 
var array<line>  fibLines  = array.new<line>()
var array<label> fibLabels = array.new<label>()
var table warnTable = table.new(position.top_right, 1, 1)
 
tfTooHigh = timeframe.in_seconds(timeframe.period) > timeframe.in_seconds(fibTf)
 
if barstate.islast
    while array.size(fibLines) > 0
        line.delete(array.pop(fibLines))
    while array.size(fibLabels) > 0
        label.delete(array.pop(fibLabels))
 
    bool haveSwing = not na(lastHigh) and not na(lastLow) and not na(flipBar) and lastHigh != lastLow
 
    if haveSwing
        float p0 = lastFlipDown ? lastHigh : lastLow
        float p100 = lastFlipDown ? lastLow : lastHigh
        float rng = p100 - p0
        int x1 = math.max(flipBar, bar_index - 1000)
        lblSize = getLabelSize(labelSize)
 
        for i = 0 to array.size(lvPct) - 1
            if array.get(lvOn, i)
                float levelPrice = p0 + rng * array.get(lvPct, i) / 100.0
                color lc = sameColor ? sameColorVal : array.get(lvCol, i)
                array.push(fibLines, line.new(x1, levelPrice, math.max(bar_index, x1 + 1), levelPrice, xloc=xloc.bar_index, extend=extend.right, color=lc, width=array.get(lvWidth, i), style=getLineStyle(array.get(lvStyle, i))))
                if showLabels
                    array.push(fibLabels, label.new(bar_index + labelOffset, levelPrice, array.get(lvText, i), xloc=xloc.bar_index, style=label.style_label_left, color=color.new(color.black, 100), textcolor=labelColor, size=lblSize))
 
    // Messages
    if tfTooHigh
        table.cell(warnTable, 0, 0, "Fibonator: chart timeframe is higher than the Fib Timeframe. Switch the chart lower or pick a higher Fib Timeframe.", text_color=color.white, bgcolor=color.new(color.red, 20), text_size=size.small)
    else if not haveSwing
        table.cell(warnTable, 0, 0, "Fibonator: no ATR swing found yet on " + fibTfInput + ". Scroll back for more history, load more bars, or try a lower Fib Timeframe.", text_color=color.white, bgcolor=color.new(color.gray, 20), text_size=size.small)
    else
        table.cell(warnTable, 0, 0, "", text_color=color.white, bgcolor=color.new(color.black, 100), text_size=size.tiny)

← 返回列表