iNaka Auto Fibo

Inakatrader · study · 452 行 · 点赞 13 · 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/
// © Inakatrader - iNaka Auto Fibo (AF v1)
//
// A Fibonacci retracement that follows the swing for you. One swing engine drives all three modes;
// the modes differ only in how the first swing is chosen and whether it may move afterwards.
//
// THE SWING
//   A swing has a START (where the move began) and an END (the furthest price the move has reached).
//   Up swing: start = a low, end = the highest high since. Down swing: the mirror.
//   0% sits on the END, 100% on the START. Levels above 100 lie beyond the start; negative levels lie
//   beyond the end (use them as extension targets, e.g. -27.2 and -61.8).
//
// HOW THE SWING MOVES (Auto and Semi-Auto)
//   1. END  - every new extreme in the swing direction moves the end (a new high in an Up swing).
//   2. START - after the end is set, price pulls back. When price makes a new extreme beyond the end,
//      the deepest pullback pivot CONFIRMED by then (a pivot low in an Up swing) becomes the new start.
//      A higher low followed by a higher high = the start steps up to that higher low. A pullback pivot
//      that confirms only after the new extreme (a fast V) moves the start on the bar it confirms, if it
//      lies beyond the current start - so a later, shallower higher low can replace a deeper one.
//      A pivot that is not beyond the current start (a wick under it in Close mode) never moves it.
//   3. FLIP - if price breaks the start (by close or by wick, your choice) the swing reverses: the old
//      end becomes the new start and the extreme of the pullback becomes the new end.
//
// MODES
//   Auto      - the engine starts by itself on the first chart bar and runs the rules above. The
//               first swings therefore depend on how much history is loaded; after a few flips the
//               swing is set by price alone. Judge Auto on the recent part of the chart, not the start.
//   Semi-Auto - you click the start bar and choose Up or Down; from there the rules above take over.
//   Manual    - you set start and end. The end follows the extreme until the End Point bar, then the
//               whole fib is frozen. The start never moves and the swing never flips.
//
// ALERTS
//   Pick the event from the alert dialog's list: one entry per level and direction
//   ("Fibo 61.8% - Close Crosses Above"), plus Fibo Start Moved and Fibo Direction Turns Up / Down.
//   Every level is in the list whether or not it is drawn. A level cross is measured against where
//   the level is NOW, so a level that jumps because the swing moved does not by itself count as a
//   cross. Level alerts are skipped on the bar the swing flips (the flip has its own alert).
//
// NON-REPAINT NOTE
//   A pivot is known only "Pivot Right" bars after it prints, so a start move arrives that late; that
//   lag is what keeps history and realtime the same on closed bars. Inside a forming bar the end, the
//   levels, a start move and a flip can still change with every tick - which is why "Confirmed bars only" is on by default.
//
// v1 (2026-10-12) - first release.
//@version=6
indicator("iNaka Auto Fibo", shorttitle="iNaka AF", overlay=true, max_lines_count=50, max_labels_count=50)
 
// =====================================================================
// === ANCHOR SETTINGS ===
// =====================================================================
ANCHOR_GROUP = "Anchor"
af_mode       = input.string("Semi-Auto", "Anchor Mode", options=["Auto", "Semi-Auto", "Manual"], group=ANCHOR_GROUP, tooltip="Auto: the swing is found and followed with no input from you. Semi-Auto: you pick the start bar, then the swing is followed automatically (the start steps to each new higher low in an Up swing, and the swing flips if the start breaks). Manual: you pick start and end; after the End Point the fib is frozen.")
af_direction  = input.string("Up", "Start Direction", options=["Up", "Down"], group=ANCHOR_GROUP, tooltip="Semi-Auto and Manual only. Up: the start is the LOW of the Start Point bar and the end follows the highs. Down: the start is the HIGH of the Start Point bar and the end follows the lows. Ignored in Auto.")
af_start_time = input.time(timestamp("01 Jan 2026 00:00 +0000"), "Start Point", confirm=true, group=ANCHOR_GROUP, tooltip="Semi-Auto and Manual only. Click the bar the swing starts from (the low of an up move or the high of a down move). You can drag the point on the chart later. Ignored in Auto.")
af_end_time   = input.time(timestamp("01 Jan 2026 00:00 +0000"), "End Point (Manual only)", group=ANCHOR_GROUP, tooltip="Manual only. The end follows the extreme from the Start Point up to and including this bar, then the fib is frozen. Must be later than the Start Point; if it is not, the end keeps following the extreme and the panel says so.")
 
SWING_GROUP = "Swing Engine"
af_left     = input.int(5, "Pivot Left",  minval=1, maxval=500, group=SWING_GROUP, tooltip="Bars to the LEFT of a pullback pivot that must be higher (for a pivot low). Bigger = only deeper pullbacks move the start. Auto and Semi-Auto only.")
af_right    = input.int(5, "Pivot Right", minval=1, maxval=500, group=SWING_GROUP, tooltip="Bars to the RIGHT that must be higher. This is also the confirmation DELAY: a pullback pivot is only known this many bars after it prints. Auto and Semi-Auto only.")
af_break_by = input.string("Close", "Flip When Start Breaks By", options=["Close", "Wick"], group=SWING_GROUP, tooltip="What counts as breaking the start and flipping the swing. Close: a candle must CLOSE beyond the start (a wick through it is ignored). Wick: any touch beyond the start flips. Auto and Semi-Auto only - Manual never flips.")
 
// =====================================================================
// === LEVEL SETTINGS ===
// =====================================================================
LEVEL_GROUP = "Levels"
LEVEL_TIP = "0% = end of the swing, 100% = start. Levels above 100% lie beyond the start. Unticking a level only hides it: its alerts stay available in the alert dialog."
lv0_on    = input.bool(true, "0%",     inline="fix1", group=LEVEL_GROUP, tooltip=LEVEL_TIP)
lv236_on  = input.bool(true, "23.6%",  inline="fix1", group=LEVEL_GROUP)
lv382_on  = input.bool(true, "38.2%",  inline="fix1", group=LEVEL_GROUP)
lv500_on  = input.bool(true, "50%",    inline="fix1", group=LEVEL_GROUP)
lv618_on  = input.bool(true, "61.8%",  inline="fix2", group=LEVEL_GROUP)
lv786_on  = input.bool(true, "78.6%",  inline="fix2", group=LEVEL_GROUP)
lv886_on  = input.bool(true, "88.6%",  inline="fix2", group=LEVEL_GROUP)
lv100_on  = input.bool(true, "100%",   inline="fix2", group=LEVEL_GROUP)
lv1272_on = input.bool(true, "127.2%", inline="fix3", group=LEVEL_GROUP)
lv1618_on = input.bool(true, "161.8%", inline="fix3", group=LEVEL_GROUP)
 
CUSTOM_GROUP = "Custom Levels  (show | percent)"
CUSTOM_TIP = "Four levels with your own percent. Negative values lie beyond the END of the swing (extension targets, e.g. -27.2 or -61.8); values above 100 lie beyond the start. In the alert dialog they appear as Fibo Custom 1-4."
cu1_on = input.bool(false, "Custom 1", inline="cu1", group=CUSTOM_GROUP)
cu1    = input.float(-27.2, "", step=0.1, inline="cu1", group=CUSTOM_GROUP, tooltip=CUSTOM_TIP)
cu2_on = input.bool(false, "Custom 2", inline="cu2", group=CUSTOM_GROUP)
cu2    = input.float(-61.8, "", step=0.1, inline="cu2", group=CUSTOM_GROUP)
cu3_on = input.bool(false, "Custom 3", inline="cu3", group=CUSTOM_GROUP)
cu3    = input.float(200.0, "", step=0.1, inline="cu3", group=CUSTOM_GROUP)
cu4_on = input.bool(false, "Custom 4", inline="cu4", group=CUSTOM_GROUP)
cu4    = input.float(261.8, "", step=0.1, inline="cu4", group=CUSTOM_GROUP)
 
// =====================================================================
// === STYLE SETTINGS ===
// =====================================================================
STYLE_GROUP = "Style"
af_show        = input.bool(true, "Show Fibonacci", group=STYLE_GROUP, tooltip="Hide or show every level line and label. Hiding the drawing does not stop alerts.")
af_show_swing  = input.bool(true, "Show Swing Line", group=STYLE_GROUP, tooltip="A dotted line from the start to the end, so you can see which swing the fib is measuring.")
af_key_color   = input.color(color.rgb(255, 210, 0),   "0 / 100 Color", group=STYLE_GROUP, tooltip="Color of the 0 and 100 levels (drawn thicker).")
af_main_color  = input.color(color.rgb(255, 230, 80),  "Retracement Color", group=STYLE_GROUP, tooltip="Color of the levels between 0 and 100.")
af_ext_color   = input.color(color.rgb(255, 150, 0),   "Extension Color", group=STYLE_GROUP, tooltip="Color of the levels below 0 or above 100 (drawn dashed).")
af_label_side  = input.string("Right", "Label Position", options=["Right", "Left", "None"], group=STYLE_GROUP, tooltip="Right: labels sit past the last bar. Left: labels sit at the start of the swing. None: no labels.")
af_label_off   = input.int(3, "Label Offset Bars", minval=0, maxval=50, group=STYLE_GROUP, tooltip="Right position only: how many bars past the last bar the labels sit.")
af_label_text  = input.color(color.white, "Label Text Color", group=STYLE_GROUP, tooltip="Text color of the level labels.")
 
// =====================================================================
// === ALERT SETTINGS ===
// =====================================================================
ALERT_GROUP = "Alerts"
af_confirmed = input.bool(true, "Confirmed bars only", group=ALERT_GROUP, tooltip="When on, alerts only trigger after the candle closes, whatever frequency you pick in the alert dialog.")
 
PANEL_GROUP = "Panel"
af_show_panel = input.bool(true, "Show Status Panel", group=PANEL_GROUP, tooltip="Show the status panel.")
af_panel_pos  = input.string("Top Right", "Panel Position", options=["Top Left", "Top Right", "Bottom Left", "Bottom Right"], group=PANEL_GROUP, tooltip="Corner of the chart the panel sits in.")
 
// =====================================================================
// === SWING ENGINE ===
// =====================================================================
bool is_auto   = af_mode == "Auto"
bool is_manual = af_mode == "Manual"
 
// Pivots run on every bar (never inside a condition); only their use is gated.
float af_pl      = ta.pivotlow(low, af_left, af_right)
float af_ph      = ta.pivothigh(high, af_left, af_right)
int   af_piv_bar = bar_index - af_right
max_bars_back(time, 501)
int   af_piv_t   = time[af_right]
 
// a is further in direction d than b (higher for d > 0, lower for d < 0).
f_beyond(int d, float a, float b) =>
    d > 0 ? a > b : a < b
 
var int   af_dir       = 0      // 1 = Up swing, -1 = Down swing, 0 = not started
var float af_start     = na
var int   af_start_bar = na
var int   af_start_t   = na
var float af_end       = na
var int   af_end_bar   = na
var int   af_end_t     = na
var float af_pb        = na     // deepest confirmed pullback pivot since the end (start candidate)
var int   af_pb_bar    = na
var int   af_pb_t      = na
var float af_opp       = na     // deepest pullback price since the end (becomes the end on a flip)
var int   af_opp_bar   = na
var int   af_opp_t     = na
var float af_xp        = na     // latest confirmed OPPOSITE pivot since the end (pivot high in an Up swing),
var int   af_xp_bar    = na     // carried into the new swing on a flip so the result does not depend on
var int   af_xp_t      = na     // whether that pivot confirmed just before or just after the flip
var bool  af_frozen    = false  // Manual: End Point reached
var bool  af_clipped   = false  // Start Point lies before the first chart bar
 
bool af_seeded_now = false
bool af_start_moved = false
bool af_flipped = false
 
// A bar "holds" time t when it closes after t: the bar containing t, or the first bar after a gap.
// (A point clicked on a lower timeframe then still lands on the bar that contains it.)
f_holds(int t) =>
    na(time_close) ? time >= t : time_close > t
 
// --- Seed ---
bool af_seed_auto = is_auto and af_dir == 0
bool af_seed_user = not is_auto and af_dir == 0 and f_holds(af_start_time)
if af_seed_auto or af_seed_user
    af_dir       := af_seed_auto ? (close >= open ? 1 : -1) : (af_direction == "Up" ? 1 : -1)
    af_start     := af_dir > 0 ? low : high
    af_start_bar := bar_index
    af_start_t   := time
    af_end       := af_dir > 0 ? high : low
    af_end_bar   := bar_index
    af_end_t     := time
    af_clipped   := af_seed_user and bar_index == 0 and time > af_start_time
    af_seeded_now := true
 
// --- Follow ---
if af_dir != 0 and not af_seeded_now
    float trend_px = af_dir > 0 ? high : low
    float pull_px  = af_dir > 0 ? low : high
    float piv_px   = af_dir > 0 ? af_pl : af_ph
    float xpiv_px  = af_dir > 0 ? af_ph : af_pl
 
    // 1. Pullback pivots (Auto / Semi-Auto). Only a pivot BEYOND the start counts (a higher low in an
    //    Up swing): a wick that poked under the start without closing there never drags the start back.
    if not is_manual and not na(piv_px) and f_beyond(af_dir, piv_px, af_start)
        if af_piv_bar > af_end_bar
            // Formed after the end: a start candidate, used when price makes a new extreme.
            if na(af_pb) or f_beyond(af_dir, af_pb, piv_px)
                af_pb     := piv_px
                af_pb_bar := af_piv_bar
                af_pb_t   := af_piv_t
        else if af_piv_bar < af_end_bar and af_piv_bar > af_start_bar
            // Formed before a newer extreme but confirmed only now (fast V): move the start now.
            af_start     := piv_px
            af_start_bar := af_piv_bar
            af_start_t   := af_piv_t
            af_start_moved := true
 
    // Opposite pivots after the end are kept for a possible flip.
    if not is_manual and not na(xpiv_px) and af_piv_bar > af_end_bar
        af_xp     := xpiv_px
        af_xp_bar := af_piv_bar
        af_xp_t   := af_piv_t
 
    // 2. New extreme moves the end; a waiting pullback pivot becomes the start.
    if not af_frozen and f_beyond(af_dir, trend_px, af_end)
        if not na(af_pb)
            af_start     := af_pb
            af_start_bar := af_pb_bar
            af_start_t   := af_pb_t
            af_start_moved := true
        af_end     := trend_px
        af_end_bar := bar_index
        af_end_t   := time
        af_pb      := na
        af_opp     := na
        af_xp      := na
    else if na(af_opp) or f_beyond(af_dir, af_opp, pull_px)
        af_opp     := pull_px
        af_opp_bar := bar_index
        af_opp_t   := time
 
    // 3. Manual: freeze once the End Point bar is reached.
    if is_manual and not af_frozen and af_end_time > af_start_time and f_holds(af_end_time)
        af_frozen := true
 
    // 4. Start broken: flip (Auto / Semi-Auto).
    float brk_px = af_break_by == "Close" ? close : pull_px
    if not is_manual and f_beyond(af_dir, af_start, brk_px)
        float new_end     = na(af_opp) ? pull_px : af_opp
        int   new_end_bar = na(af_opp) ? bar_index : af_opp_bar
        int   new_end_t   = na(af_opp) ? time : af_opp_t
        af_start     := af_end
        af_start_bar := af_end_bar
        af_start_t   := af_end_t
        af_end       := new_end
        af_end_bar   := new_end_bar
        af_end_t     := new_end_t
        af_dir       := -af_dir
        af_pb        := na
        af_opp       := na
        af_flipped   := true
        // Carry the opposite pivot confirmed before the flip, exactly as if it had confirmed after it:
        // before the new end it moves the start now; after the new end it waits as a start candidate.
        if not na(af_xp) and f_beyond(af_dir, af_xp, af_start)
            if af_xp_bar < af_end_bar and af_xp_bar > af_start_bar
                af_start     := af_xp
                af_start_bar := af_xp_bar
                af_start_t   := af_xp_t
            else if af_xp_bar > af_end_bar
                af_pb     := af_xp
                af_pb_bar := af_xp_bar
                af_pb_t   := af_xp_t
        af_xp := na
 
float af_span = af_end - af_start
bool  af_ok   = af_dir != 0 and not na(af_span) and math.abs(af_span) > syminfo.mintick
 
f_level(float pct) =>
    af_ok ? af_end - af_span * pct / 100.0 : na
 
f_pct_text(float pct) =>
    str.tostring(pct, "#.###") + "%"
 
f_price_text(float v) =>
    na(v) ? "na" : str.tostring(v, format.mintick)
 
var array<float> lv_pct = array.from(0.0, 23.6, 38.2, 50.0, 61.8, 78.6, 88.6, 100.0, 127.2, 161.8, cu1, cu2, cu3, cu4)
var array<bool>  lv_on  = array.from(lv0_on, lv236_on, lv382_on, lv500_on, lv618_on, lv786_on, lv886_on, lv100_on, lv1272_on, lv1618_on, cu1_on, cu2_on, cu3_on, cu4_on)
int LV_N = 14
 
// =====================================================================
// === ALERTS ===
// =====================================================================
float af_prev_close = close[1]
bool  af_gate       = not af_confirmed or barstate.isconfirmed
bool  af_alert_ok   = af_ok and not af_flipped and af_gate
 
// A level cross is measured against the level's CURRENT price, so a level that jumped because the
// swing moved is not a cross by itself. A close sitting exactly ON the level is not a side: look one
// more bar back. No level alerts on a flip bar (the flip has its own alert).
f_cross(float pct) =>
    float lvl  = f_level(pct)
    float side = af_prev_close != lvl ? af_prev_close : close[2]
    bool  ok   = af_alert_ok and not na(lvl) and not na(side)
    [ok and side < lvl and close > lvl, ok and side > lvl and close < lvl]
 
[x0_up, x0_dn] = f_cross(0.0)
[x1_up, x1_dn] = f_cross(23.6)
[x2_up, x2_dn] = f_cross(38.2)
[x3_up, x3_dn] = f_cross(50.0)
[x4_up, x4_dn] = f_cross(61.8)
[x5_up, x5_dn] = f_cross(78.6)
[x6_up, x6_dn] = f_cross(88.6)
[x7_up, x7_dn] = f_cross(100.0)
[x8_up, x8_dn] = f_cross(127.2)
[x9_up, x9_dn] = f_cross(161.8)
[x10_up, x10_dn] = f_cross(cu1)
[x11_up, x11_dn] = f_cross(cu2)
[x12_up, x12_dn] = f_cross(cu3)
[x13_up, x13_dn] = f_cross(cu4)
 
// Hidden plots: level prices in the Data Window and in alert messages via {{plot("...")}}.
plot(f_level(0.0), "Fibo 0%", display=display.data_window, editable=false)
plot(f_level(23.6), "Fibo 23.6%", display=display.data_window, editable=false)
plot(f_level(38.2), "Fibo 38.2%", display=display.data_window, editable=false)
plot(f_level(50.0), "Fibo 50%", display=display.data_window, editable=false)
plot(f_level(61.8), "Fibo 61.8%", display=display.data_window, editable=false)
plot(f_level(78.6), "Fibo 78.6%", display=display.data_window, editable=false)
plot(f_level(88.6), "Fibo 88.6%", display=display.data_window, editable=false)
plot(f_level(100.0), "Fibo 100%", display=display.data_window, editable=false)
plot(f_level(127.2), "Fibo 127.2%", display=display.data_window, editable=false)
plot(f_level(161.8), "Fibo 161.8%", display=display.data_window, editable=false)
plot(f_level(cu1), "Fibo Custom 1", display=display.data_window, editable=false)
plot(f_level(cu2), "Fibo Custom 2", display=display.data_window, editable=false)
plot(f_level(cu3), "Fibo Custom 3", display=display.data_window, editable=false)
plot(f_level(cu4), "Fibo Custom 4", display=display.data_window, editable=false)
plot(af_ok ? af_start : na, "Swing Start", display=display.data_window, editable=false)
plot(af_ok ? af_end : na, "Swing End", display=display.data_window, editable=false)
 
alertcondition(x0_up, title="Fibo 0% - Close Crosses Above", message="Close crossed above Fibo 0% ({{plot(\"Fibo 0%\")}}) on {{ticker}} {{interval}}")
alertcondition(x0_dn, title="Fibo 0% - Close Crosses Below", message="Close crossed below Fibo 0% ({{plot(\"Fibo 0%\")}}) on {{ticker}} {{interval}}")
alertcondition(x1_up, title="Fibo 23.6% - Close Crosses Above", message="Close crossed above Fibo 23.6% ({{plot(\"Fibo 23.6%\")}}) on {{ticker}} {{interval}}")
alertcondition(x1_dn, title="Fibo 23.6% - Close Crosses Below", message="Close crossed below Fibo 23.6% ({{plot(\"Fibo 23.6%\")}}) on {{ticker}} {{interval}}")
alertcondition(x2_up, title="Fibo 38.2% - Close Crosses Above", message="Close crossed above Fibo 38.2% ({{plot(\"Fibo 38.2%\")}}) on {{ticker}} {{interval}}")
alertcondition(x2_dn, title="Fibo 38.2% - Close Crosses Below", message="Close crossed below Fibo 38.2% ({{plot(\"Fibo 38.2%\")}}) on {{ticker}} {{interval}}")
alertcondition(x3_up, title="Fibo 50% - Close Crosses Above", message="Close crossed above Fibo 50% ({{plot(\"Fibo 50%\")}}) on {{ticker}} {{interval}}")
alertcondition(x3_dn, title="Fibo 50% - Close Crosses Below", message="Close crossed below Fibo 50% ({{plot(\"Fibo 50%\")}}) on {{ticker}} {{interval}}")
alertcondition(x4_up, title="Fibo 61.8% - Close Crosses Above", message="Close crossed above Fibo 61.8% ({{plot(\"Fibo 61.8%\")}}) on {{ticker}} {{interval}}")
alertcondition(x4_dn, title="Fibo 61.8% - Close Crosses Below", message="Close crossed below Fibo 61.8% ({{plot(\"Fibo 61.8%\")}}) on {{ticker}} {{interval}}")
alertcondition(x5_up, title="Fibo 78.6% - Close Crosses Above", message="Close crossed above Fibo 78.6% ({{plot(\"Fibo 78.6%\")}}) on {{ticker}} {{interval}}")
alertcondition(x5_dn, title="Fibo 78.6% - Close Crosses Below", message="Close crossed below Fibo 78.6% ({{plot(\"Fibo 78.6%\")}}) on {{ticker}} {{interval}}")
alertcondition(x6_up, title="Fibo 88.6% - Close Crosses Above", message="Close crossed above Fibo 88.6% ({{plot(\"Fibo 88.6%\")}}) on {{ticker}} {{interval}}")
alertcondition(x6_dn, title="Fibo 88.6% - Close Crosses Below", message="Close crossed below Fibo 88.6% ({{plot(\"Fibo 88.6%\")}}) on {{ticker}} {{interval}}")
alertcondition(x7_up, title="Fibo 100% - Close Crosses Above", message="Close crossed above Fibo 100% ({{plot(\"Fibo 100%\")}}) on {{ticker}} {{interval}}")
alertcondition(x7_dn, title="Fibo 100% - Close Crosses Below", message="Close crossed below Fibo 100% ({{plot(\"Fibo 100%\")}}) on {{ticker}} {{interval}}")
alertcondition(x8_up, title="Fibo 127.2% - Close Crosses Above", message="Close crossed above Fibo 127.2% ({{plot(\"Fibo 127.2%\")}}) on {{ticker}} {{interval}}")
alertcondition(x8_dn, title="Fibo 127.2% - Close Crosses Below", message="Close crossed below Fibo 127.2% ({{plot(\"Fibo 127.2%\")}}) on {{ticker}} {{interval}}")
alertcondition(x9_up, title="Fibo 161.8% - Close Crosses Above", message="Close crossed above Fibo 161.8% ({{plot(\"Fibo 161.8%\")}}) on {{ticker}} {{interval}}")
alertcondition(x9_dn, title="Fibo 161.8% - Close Crosses Below", message="Close crossed below Fibo 161.8% ({{plot(\"Fibo 161.8%\")}}) on {{ticker}} {{interval}}")
alertcondition(x10_up, title="Fibo Custom 1 - Close Crosses Above", message="Close crossed above Fibo Custom 1 ({{plot(\"Fibo Custom 1\")}}) on {{ticker}} {{interval}}")
alertcondition(x10_dn, title="Fibo Custom 1 - Close Crosses Below", message="Close crossed below Fibo Custom 1 ({{plot(\"Fibo Custom 1\")}}) on {{ticker}} {{interval}}")
alertcondition(x11_up, title="Fibo Custom 2 - Close Crosses Above", message="Close crossed above Fibo Custom 2 ({{plot(\"Fibo Custom 2\")}}) on {{ticker}} {{interval}}")
alertcondition(x11_dn, title="Fibo Custom 2 - Close Crosses Below", message="Close crossed below Fibo Custom 2 ({{plot(\"Fibo Custom 2\")}}) on {{ticker}} {{interval}}")
alertcondition(x12_up, title="Fibo Custom 3 - Close Crosses Above", message="Close crossed above Fibo Custom 3 ({{plot(\"Fibo Custom 3\")}}) on {{ticker}} {{interval}}")
alertcondition(x12_dn, title="Fibo Custom 3 - Close Crosses Below", message="Close crossed below Fibo Custom 3 ({{plot(\"Fibo Custom 3\")}}) on {{ticker}} {{interval}}")
alertcondition(x13_up, title="Fibo Custom 4 - Close Crosses Above", message="Close crossed above Fibo Custom 4 ({{plot(\"Fibo Custom 4\")}}) on {{ticker}} {{interval}}")
alertcondition(x13_dn, title="Fibo Custom 4 - Close Crosses Below", message="Close crossed below Fibo Custom 4 ({{plot(\"Fibo Custom 4\")}}) on {{ticker}} {{interval}}")
alertcondition(af_gate and af_start_moved and not af_flipped, title="Fibo Start Moved", message="Fibo start moved to {{plot(\"Swing Start\")}} on {{ticker}} {{interval}}")
alertcondition(af_gate and af_flipped and af_dir > 0, title="Fibo Direction Turns Up", message="Fibo swing turned UP (start {{plot(\"Swing Start\")}}, end {{plot(\"Swing End\")}}) on {{ticker}} {{interval}}")
alertcondition(af_gate and af_flipped and af_dir < 0, title="Fibo Direction Turns Down", message="Fibo swing turned DOWN (start {{plot(\"Swing Start\")}}, end {{plot(\"Swing End\")}}) on {{ticker}} {{interval}}")
 
string af_dir_text = af_dir > 0 ? "UP" : af_dir < 0 ? "DOWN" : "NONE"
 
// =====================================================================
// === DRAWING (last bar only) ===
// =====================================================================
var array<line>  lv_lines  = array.new<line>(14)
var array<label> lv_labels = array.new<label>(14)
var line af_swing_line = na
 
if barstate.islast
    for i = 0 to LV_N - 1
        float pct  = array.get(lv_pct, i)
        float lvl  = f_level(pct)
        bool  vis  = af_show and af_ok and array.get(lv_on, i)
        bool  key  = pct == 0.0 or pct == 100.0
        bool  ext  = pct < 0.0 or pct > 100.0
        color col  = key ? af_key_color : ext ? af_ext_color : af_main_color
        line  ln   = array.get(lv_lines, i)
        label lb   = array.get(lv_labels, i)
        if not vis
            line.delete(ln)
            label.delete(lb)
            array.set(lv_lines, i, na)
            array.set(lv_labels, i, na)
        else
            if na(ln)
                ln := line.new(af_start_t, lvl, time, lvl, xloc=xloc.bar_time, extend=extend.right)
                array.set(lv_lines, i, ln)
            line.set_xy1(ln, af_start_t, lvl)
            line.set_xy2(ln, time, lvl)
            line.set_color(ln, col)
            line.set_width(ln, key ? 2 : 1)
            line.set_style(ln, ext ? line.style_dashed : line.style_solid)
            if af_label_side == "None"
                label.delete(lb)
                array.set(lv_labels, i, na)
            else
                bool   right = af_label_side == "Right"
                string txt   = f_pct_text(pct) + "  " + f_price_text(lvl)
                if na(lb)
                    lb := label.new(bar_index, lvl, txt, size=size.small)
                    array.set(lv_labels, i, lb)
                if right
                    label.set_xloc(lb, bar_index + af_label_off, xloc.bar_index)
                else
                    label.set_xloc(lb, af_start_t, xloc.bar_time)
                label.set_y(lb, lvl)
                label.set_text(lb, txt)
                label.set_style(lb, right ? label.style_label_left : label.style_label_right)
                label.set_color(lb, color.new(col, 20))
                label.set_textcolor(lb, af_label_text)
 
    line.delete(af_swing_line)
    af_swing_line := na
    if af_show and af_show_swing and af_ok
        af_swing_line := line.new(af_start_t, af_start, af_end_t, af_end, xloc=xloc.bar_time, color=af_key_color, style=line.style_dotted, width=1)
 
// =====================================================================
// === STATUS PANEL ===
// =====================================================================
f_panel_pos(string p) =>
    switch p
        "Top Left"    => position.top_left
        "Bottom Left" => position.bottom_left
        "Bottom Right" => position.bottom_right
        => position.top_right
 
var table af_panel = table.new(f_panel_pos(af_panel_pos), 2, 7, border_width=1)
 
if barstate.islast
    table.clear(af_panel, 0, 0, 1, 6)
    if af_show_panel
        color hdr = color.rgb(35, 45, 65)
        color txt = color.white
        color bg  = color.new(color.rgb(20, 25, 35), 10)
        string status = af_dir == 0 ? "Waiting for Start Point" : not af_ok ? "Swing too small" : is_manual and af_end_time <= af_start_time ? "End Point not after Start - end not frozen" : is_manual and af_frozen ? "Frozen" : "Following"
        if af_clipped and af_dir != 0
            status += " (start before first bar - anchored there)"
 
        // Nearest drawn level to the close.
        float near_pct = na
        float near_lvl = na
        if af_ok
            for i = 0 to LV_N - 1
                if array.get(lv_on, i)
                    float lvl = f_level(array.get(lv_pct, i))
                    if na(near_lvl) or math.abs(close - lvl) < math.abs(close - near_lvl)
                        near_lvl := lvl
                        near_pct := array.get(lv_pct, i)
        float retrace = af_ok ? (af_end - close) / af_span * 100.0 : na
 
        table.cell(af_panel, 0, 0, "iNaka Auto Fibo", text_color=txt, bgcolor=hdr)
        table.cell(af_panel, 1, 0, af_mode, text_color=txt, bgcolor=hdr)
        table.cell(af_panel, 0, 1, "Status", text_color=txt, bgcolor=bg)
        table.cell(af_panel, 1, 1, status, text_color=txt, bgcolor=bg)
        table.cell(af_panel, 0, 2, "Swing", text_color=txt, bgcolor=bg)
        table.cell(af_panel, 1, 2, af_dir_text, text_color=af_dir > 0 ? color.lime : af_dir < 0 ? color.red : txt, bgcolor=bg)
        table.cell(af_panel, 0, 3, "Start (100%)", text_color=txt, bgcolor=bg)
        table.cell(af_panel, 1, 3, f_price_text(af_start), text_color=txt, bgcolor=bg)
        table.cell(af_panel, 0, 4, "End (0%)", text_color=txt, bgcolor=bg)
        table.cell(af_panel, 1, 4, f_price_text(af_end), text_color=txt, bgcolor=bg)
        table.cell(af_panel, 0, 5, "Retrace", text_color=txt, bgcolor=bg)
        table.cell(af_panel, 1, 5, na(retrace) ? "na" : str.tostring(retrace, "#.#") + "%", text_color=txt, bgcolor=bg)
        table.cell(af_panel, 0, 6, "Nearest level", text_color=txt, bgcolor=bg)
        table.cell(af_panel, 1, 6, na(near_lvl) ? "na" : f_pct_text(near_pct) + "  " + f_price_text(near_lvl), text_color=txt, bgcolor=bg)

← 返回列表