7AM Fib V7 - FIB Testing Strat

Mshayes773 · study · 443 行 · 点赞 1 · TradingView 原页

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

Pine Script

//@version=6
indicator("Kaysee 7AM Fib V7 - Styled Fib", overlay=true, max_labels_count=300)
 
//====================================================
// 1. SETTINGS
//====================================================
gFib = "1 | Fibonacci"
fibMode = input.string("Original PDH to PDL", "Fib anchor mode", options=["Original PDH to PDL", "Downtrend PDL to older low", "Auto"], group=gFib)
searchDays = input.int(30, "Search older lows (days)", minval=3, maxval=100, group=gFib)
nearPct = input.float(25.0, "Auto mode: near yesterday LOW (%)", minval=1, maxval=50, step=1, group=gFib)
showFib = input.bool(true, "Show styled Fibonacci", group=gFib)
showDeep = input.bool(false, "Show 0.02 and 0.03 lines", group=gFib)
showPrices = input.bool(true, "Show left-side price labels", group=gFib)
leftOffset = input.int(18, "Left label offset (bars)", minval=5, maxval=100, group=gFib)
 
gTrend = "2 | Trend"
fastLen = input.int(20, "Fast EMA", minval=1, group=gTrend)
slowLen = input.int(50, "Slow EMA", minval=2, group=gTrend)
needStrong = input.bool(true, "Require Daily + 4H + 1H alignment for 40% entry", group=gTrend)
needMicro = input.bool(true, "Require 1M + 5M continuation agreement", group=gTrend)
 
gSignal = "3 | Signals"
tz = input.string("America/Chicago", "7AM time zone", group=gSignal)
wickRatio = input.float(0.5, "Wick / body ratio", minval=0.1, step=0.1, group=gSignal)
showDashboard = input.bool(true, "Show dashboard", group=gSignal)
showTargets = input.bool(true, "Show target reference prices on scale", group=gSignal)
 
gDeep = "4 | Deep Levels"
level02 = input.float(0.02, "0.02 level", minval=0.0, maxval=0.40, step=0.01, group=gDeep)
level03 = input.float(0.03, "0.03 level", minval=0.0, maxval=0.40, step=0.01, group=gDeep)
 
//====================================================
// 2. HELPER FUNCTIONS
//====================================================
f_trend(float c, float f, float s) =>
    c > f and f > s ? 1 : c < f and f < s ? -1 : 0
 
f_trendText(int d) =>
    d == 1 ? "UP" : d == -1 ? "DOWN" : "MIXED"
 
f_nextOlderLow(int daysBack) =>
    float candidate = na
    for i = 2 to daysBack + 1
        if not na(low[i]) and low[i] < low[1]
            candidate := na(candidate) ? low[i] : math.max(candidate, low[i])
    candidate
 
f_makeText(string lvl, float price) =>
    lvl + " (" + str.tostring(price, format.mintick) + ")"
 
f_updateLabel(label oldLbl, float y, string txt, color txtColor, int xOffset) =>
    label out = oldLbl
    if na(y) or not showPrices
        if not na(out)
            label.delete(out)
        out := na
    else
        int xPos = bar_index > xOffset ? bar_index - xOffset : 0
        if na(out)
            out := label.new(x=xPos, y=y, text=txt, xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, textcolor=txtColor, size=size.large)
        else
            label.set_x(out, xPos)
            label.set_y(out, y)
            label.set_text(out, txt)
            label.set_textcolor(out, txtColor)
    out
 
//====================================================
// 3. PREVIOUS DAY DATA + NEXT OLDER LOW
//====================================================
[pdh, pdl, nextOlderLow] = request.security(
     syminfo.tickerid,
     "D",
     [high[1], low[1], f_nextOlderLow(searchDays)],
     lookahead=barmerge.lookahead_on)
 
validPrev = not na(pdh) and not na(pdl) and pdh > pdl
hasLower = not na(nextOlderLow) and nextOlderLow < pdl
 
//====================================================
// 4. HIGHER TIMEFRAME TREND DATA
//====================================================
[dC, dF, dS] = request.security(syminfo.tickerid, "D", [close[1], ta.ema(close, fastLen)[1], ta.ema(close, slowLen)[1]], lookahead=barmerge.lookahead_on)
[h4C, h4F, h4S] = request.security(syminfo.tickerid, "240", [close[1], ta.ema(close, fastLen)[1], ta.ema(close, slowLen)[1]], lookahead=barmerge.lookahead_on)
[h1C, h1F, h1S] = request.security(syminfo.tickerid, "60", [close[1], ta.ema(close, fastLen)[1], ta.ema(close, slowLen)[1]], lookahead=barmerge.lookahead_on)
 
trendD = f_trend(dC, dF, dS)
trend4H = f_trend(h4C, h4F, h4S)
trend1H = f_trend(h1C, h1F, h1S)
 
strongBuy = trendD == 1 and trend4H == 1 and trend1H == 1
strongSell = trendD == -1 and trend4H == -1 and trend1H == -1
 
trend1M = request.security(syminfo.tickerid, "1", f_trend(close, ta.ema(close, fastLen), ta.ema(close, slowLen)), lookahead=barmerge.lookahead_off)
trend5M = request.security(syminfo.tickerid, "5", f_trend(close, ta.ema(close, fastLen), ta.ema(close, slowLen)), lookahead=barmerge.lookahead_off)
 
microBuy = trend1M == 1 and trend5M == 1
microSell = trend1M == -1 and trend5M == -1
 
//====================================================
// 5. FIB MODE
//====================================================
autoDown = validPrev and hasLower and trendD == -1 and close <= pdl + (pdh - pdl) * nearPct / 100.0
 
downMode = fibMode == "Downtrend PDL to older low" or (fibMode == "Auto" and autoDown and hasLower)
 
fibTop = downMode ? pdl : pdh
fibBottom = downMode ? nextOlderLow : pdl
fibSpan = fibTop - fibBottom
validFib = not na(fibTop) and not na(fibBottom) and fibTop > fibBottom
 
//====================================================
// 6. FIB LEVELS FOR RULES
//====================================================
fib50 = validFib ? fibTop - fibSpan * 0.50 : na
 
buy40 = validFib ? fibBottom + fibSpan * 0.40 : na
sell40 = validFib ? fibTop - fibSpan * 0.40 : na
 
buy618 = validFib ? fibBottom + fibSpan * 0.618 : na
sell618 = validFib ? fibTop - fibSpan * 0.618 : na
 
buy02 = validFib ? fibBottom + fibSpan * level02 : na
sell02 = validFib ? fibTop - fibSpan * level02 : na
 
buy03 = validFib ? fibBottom + fibSpan * level03 : na
sell03 = validFib ? fibTop - fibSpan * level03 : na
 
buyTarget = fibTop
sellTarget = fibBottom
 
//====================================================
// 7. STYLED FIB LEVELS FOR DISPLAY
//====================================================
fib0v   = validFib ? fibTop - fibSpan * 0.000 : na
fib01v  = validFib ? fibTop - fibSpan * 0.100 : na
fib236v = validFib ? fibTop - fibSpan * 0.236 : na
fib382v = validFib ? fibTop - fibSpan * 0.382 : na
fib50v  = validFib ? fibTop - fibSpan * 0.500 : na
fib618v = validFib ? fibTop - fibSpan * 0.618 : na
fib787v = validFib ? fibTop - fibSpan * 0.787 : na
fib886v = validFib ? fibTop - fibSpan * 0.886 : na
 
//====================================================
// 8. 7AM SESSION WATCH
//====================================================
is15 = timeframe.isminutes and timeframe.multiplier == 15
is7am = is15 and hour(time, tz) == 7 and minute(time, tz) == 0
 
dayKey = year(time, tz) * 10000 + month(time, tz) * 100 + dayofmonth(time, tz)
newDay = na(dayKey[1]) or dayKey != dayKey[1]
 
var bool started = false
var bool longArmed = false
var bool shortArmed = false
var bool buyUsed = false
var bool sellUsed = false
var int activeTrade = 0
var bool reachedBuyTarget = false
var bool reachedSellTarget = false
var int lastBuy618Bar = na
var int lastSell618Bar = na
 
if newDay
    started := false
    longArmed := false
    shortArmed := false
    buyUsed := false
    sellUsed := false
    activeTrade := 0
    reachedBuyTarget := false
    reachedSellTarget := false
    lastBuy618Bar := na
    lastSell618Bar := na
 
if is7am and barstate.isconfirmed
    started := true
 
ready = is15 and validFib and started
confirmed = ready and barstate.isconfirmed
 
//====================================================
// 9. 50% BREAK
//====================================================
up50 = confirmed and close > fib50 and close[1] <= fib50
down50 = confirmed and close < fib50 and close[1] >= fib50
 
if up50 and activeTrade == 0
    longArmed := true
    shortArmed := false
 
if down50 and activeTrade == 0
    shortArmed := true
    longArmed := false
 
//====================================================
// 10. 61.8 TEST / REJECTION
//====================================================
buy618Test = confirmed and longArmed and high >= buy618
sell618Test = confirmed and shortArmed and low <= sell618
 
buy618Pass = buy618Test and close >= buy618
sell618Pass = sell618Test and close <= sell618
 
buy618Reject = buy618Test and close < buy618
sell618Reject = sell618Test and close > sell618
 
if buy618Test
    lastBuy618Bar := bar_index
 
if sell618Test
    lastSell618Bar := bar_index
 
nextUp = confirmed and ((not na(lastBuy618Bar) and bar_index == lastBuy618Bar + 1) or (not na(lastSell618Bar) and bar_index == lastSell618Bar + 1)) and close > close[1]
nextDown = confirmed and ((not na(lastBuy618Bar) and bar_index == lastBuy618Bar + 1) or (not na(lastSell618Bar) and bar_index == lastSell618Bar + 1)) and close < close[1]
 
//====================================================
// 11. 40% WATCH / SIGNALS
//====================================================
possibleBuy40 = confirmed and longArmed and not buyUsed and activeTrade == 0 and low <= buy40 and high < buyTarget
possibleSell40 = confirmed and shortArmed and not sellUsed and activeTrade == 0 and high >= sell40 and low > sellTarget
 
buySignal = possibleBuy40 and close > open and close >= buy40 and (not needStrong or strongBuy)
sellSignal = possibleSell40 and close < open and close <= sell40 and (not needStrong or strongSell)
 
weakBuy = possibleBuy40 and close > open and close >= buy40 and not strongBuy
weakSell = possibleSell40 and close < open and close <= sell40 and not strongSell
 
reverseBuy = buySignal and reachedSellTarget
reverseSell = sellSignal and reachedBuyTarget
 
if buySignal
    activeTrade := 1
    buyUsed := true
    longArmed := false
    shortArmed := false
 
if sellSignal
    activeTrade := -1
    sellUsed := true
    shortArmed := false
    longArmed := false
 
//====================================================
// 12. TARGET TOUCH
//====================================================
exitBuy = confirmed and activeTrade == 1 and high >= buyTarget
exitSell = confirmed and activeTrade == -1 and low <= sellTarget
 
if exitBuy
    activeTrade := 0
    reachedBuyTarget := true
 
if exitSell
    activeTrade := 0
    reachedSellTarget := true
 
nextLowTouch = confirmed and downMode and hasLower and low <= nextOlderLow and (na(low[1]) or low[1] > nextOlderLow)
 
//====================================================
// 13. WICK WARNINGS
//====================================================
body = math.max(math.abs(close - open), syminfo.mintick)
upperWick = high - math.max(open, close)
lowerWick = math.min(open, close) - low
 
nearBuy618 = validFib and high >= buy618 - fibSpan * 0.08 and low <= buy618 + fibSpan * 0.08
nearSell618 = validFib and high >= sell618 - fibSpan * 0.08 and low <= sell618 + fibSpan * 0.08
 
wickBear = confirmed and nearBuy618 and upperWick >= body * wickRatio
wickBull = confirmed and nearSell618 and lowerWick >= body * wickRatio
 
//====================================================
// 14. OPTIONAL 0.02 / 0.03 TOUCHES
//====================================================
buy02Touch = confirmed and low <= buy02
sell02Touch = confirmed and high >= sell02
buy03Touch = confirmed and low <= buy03
sell03Touch = confirmed and high >= sell03
 
continueDown = confirmed and buy03Touch and close < open and close < buy03 and (not needMicro or microSell)
continueUp = confirmed and sell03Touch and close > open and close > sell03 and (not needMicro or microBuy)
 
//====================================================
// 15. COLORS
//====================================================
c0    = color.new(color.gray, 0)
c01   = color.new(color.blue, 0)
c236  = color.new(color.red, 0)
c382  = color.new(color.lime, 0)
c50   = color.new(color.green, 0)
c618  = color.new(color.aqua, 0)
c787  = color.new(color.rgb(61, 159, 255), 0)
c886  = color.new(color.gray, 0)
 
//====================================================
// 16. STYLED FIB PLOTS
//====================================================
p0    = plot(showFib ? fib0v   : na, title="0",     color=c0,   linewidth=2)
p01   = plot(showFib ? fib01v  : na, title="0.1",   color=c01,  linewidth=2)
p236  = plot(showFib ? fib236v : na, title="0.236", color=c236, linewidth=2)
p382  = plot(showFib ? fib382v : na, title="0.382", color=c382, linewidth=2)
p50   = plot(showFib ? fib50v  : na, title="0.5",   color=c50,  linewidth=2)
p618  = plot(showFib ? fib618v : na, title="0.618", color=c618, linewidth=2)
p787  = plot(showFib ? fib787v : na, title="0.787", color=c787, linewidth=2)
p886  = plot(showFib ? fib886v : na, title="0.886", color=c886, linewidth=2)
 
fill(p0,   p01,  color.new(color.purple, 82))
fill(p01,  p236, color.new(color.red, 86))
fill(p236, p382, color.new(color.orange, 88))
fill(p382, p50,  color.new(color.green, 88))
fill(p50,  p618, color.new(color.teal, 88))
fill(p618, p787, color.new(color.blue, 86))
fill(p787, p886, color.new(color.gray, 84))
 
//====================================================
// 17. RIGHT-SIDE PRICE SCALE VALUES
//====================================================
plot(showTargets ? pdh : na, title="PDH PRICE", color=color.lime, display=display.price_scale)
plot(showTargets ? pdl : na, title="PDL PRICE", color=downMode ? color.teal : color.red, display=display.price_scale)
plot(showTargets and hasLower ? nextOlderLow : na, title="NEXT OLDER LOW PRICE", color=color.fuchsia, display=display.price_scale)
 
plot(showFib ? fib0v   : na, title="0 PRICE",     color=c0,   display=display.price_scale)
plot(showFib ? fib01v  : na, title="0.1 PRICE",   color=c01,  display=display.price_scale)
plot(showFib ? fib236v : na, title="0.236 PRICE", color=c236, display=display.price_scale)
plot(showFib ? fib382v : na, title="0.382 PRICE", color=c382, display=display.price_scale)
plot(showFib ? fib50v  : na, title="0.5 PRICE",   color=c50,  display=display.price_scale)
plot(showFib ? fib618v : na, title="0.618 PRICE", color=c618, display=display.price_scale)
plot(showFib ? fib787v : na, title="0.787 PRICE", color=c787, display=display.price_scale)
plot(showFib ? fib886v : na, title="0.886 PRICE", color=c886, display=display.price_scale)
 
plot(showFib and showDeep ? buy02 : na, title="BUY 0.02", color=color.new(color.aqua, 35), style=plot.style_linebr)
plot(showFib and showDeep ? sell02 : na, title="SELL 0.02", color=color.new(color.orange, 35), style=plot.style_linebr)
plot(showFib and showDeep ? buy03 : na, title="BUY 0.03", color=color.new(color.aqua, 65), style=plot.style_linebr)
plot(showFib and showDeep ? sell03 : na, title="SELL 0.03", color=color.new(color.orange, 65), style=plot.style_linebr)
 
//====================================================
// 18. LEFT-SIDE TEXT LABELS
//====================================================
var label lbl0 = na
var label lbl01 = na
var label lbl236 = na
var label lbl382 = na
var label lbl50 = na
var label lbl618 = na
var label lbl787 = na
var label lbl886 = na
 
if barstate.islast
    lbl0   := f_updateLabel(lbl0,   showFib ? fib0v   : na, f_makeText("0", fib0v),       c0,   leftOffset)
    lbl01  := f_updateLabel(lbl01,  showFib ? fib01v  : na, f_makeText("0.1", fib01v),    c01,  leftOffset)
    lbl236 := f_updateLabel(lbl236, showFib ? fib236v : na, f_makeText("0.236", fib236v), c236, leftOffset)
    lbl382 := f_updateLabel(lbl382, showFib ? fib382v : na, f_makeText("0.382", fib382v), c382, leftOffset)
    lbl50  := f_updateLabel(lbl50,  showFib ? fib50v  : na, f_makeText("0.5", fib50v),    c50,  leftOffset)
    lbl618 := f_updateLabel(lbl618, showFib ? fib618v : na, f_makeText("0.618", fib618v), c618, leftOffset)
    lbl787 := f_updateLabel(lbl787, showFib ? fib787v : na, f_makeText("0.787", fib787v), c787, leftOffset)
    lbl886 := f_updateLabel(lbl886, showFib ? fib886v : na, f_makeText("0.886", fib886v), c886, leftOffset)
 
//====================================================
// 19. SIGNAL MARKERS
//====================================================
plotshape(is7am and barstate.isconfirmed, title="7AM START", style=shape.circle, location=location.belowbar, color=color.yellow, size=size.tiny, text="7AM")
 
plotshape(up50, title="50 UP", style=shape.triangleup, location=location.belowbar, color=color.aqua, size=size.tiny, text="50 UP")
plotshape(down50, title="50 DOWN", style=shape.triangledown, location=location.abovebar, color=color.orange, size=size.tiny, text="50 DN")
 
plotshape(buy618Pass or sell618Pass, title="618 PASS", style=shape.circle, location=location.abovebar, color=color.teal, size=size.tiny, text="618 OK")
plotshape(buy618Reject or sell618Reject, title="618 REJECT", style=shape.diamond, location=location.abovebar, color=color.yellow, size=size.tiny, text="618 REJ")
 
plotshape(wickBear or wickBull, title="WICK WARNING", style=shape.diamond, location=location.abovebar, color=color.yellow, size=size.tiny, text="WICK")
 
plotshape(possibleBuy40 and not buySignal, title="BUY 40 WATCH", style=shape.circle, location=location.belowbar, color=color.aqua, size=size.tiny, text="40?")
plotshape(possibleSell40 and not sellSignal, title="SELL 40 WATCH", style=shape.circle, location=location.abovebar, color=color.orange, size=size.tiny, text="40?")
 
plotshape(weakBuy or weakSell, title="WEAK TREND", style=shape.circle, location=location.abovebar, color=color.yellow, size=size.tiny, text="WEAK")
 
plotshape(buySignal, title="BUY 40", style=shape.labelup, location=location.belowbar, color=color.lime, text="BUY 40", textcolor=color.black)
plotshape(sellSignal, title="SELL 40", style=shape.labeldown, location=location.abovebar, color=color.red, text="SELL 40", textcolor=color.white)
 
plotshape(exitBuy or exitSell, title="EXIT TARGET", style=shape.xcross, location=location.abovebar, color=color.yellow, text="EXIT")
plotshape(nextLowTouch, title="NEXT LOWER LOW", style=shape.diamond, location=location.belowbar, color=color.fuchsia, size=size.tiny, text="NEXT LOW")
 
plotshape(continueUp, title="CONTINUATION UP", style=shape.labelup, location=location.belowbar, color=color.green, text="CONT UP?", textcolor=color.white)
plotshape(continueDown, title="CONTINUATION DOWN", style=shape.labeldown, location=location.abovebar, color=color.red, text="CONT DN?", textcolor=color.white)
 
//====================================================
// 20. DASHBOARD
//====================================================
var table dash = table.new(position.top_right, 2, 11, border_width=1)
 
if barstate.islast and showDashboard
    table.cell(dash, 0, 0, "KAYSEE FIB", bgcolor=color.black, text_color=color.white)
    table.cell(dash, 1, 0, "STATUS", bgcolor=color.black, text_color=color.white)
 
    table.cell(dash, 0, 1, "Fib mode")
    table.cell(dash, 1, 1, downMode ? "DOWN MODE" : "NORMAL MODE")
 
    table.cell(dash, 0, 2, "Fib top")
    table.cell(dash, 1, 2, str.tostring(fibTop, format.mintick))
 
    table.cell(dash, 0, 3, "Fib bottom")
    table.cell(dash, 1, 3, str.tostring(fibBottom, format.mintick))
 
    table.cell(dash, 0, 4, "Daily / 4H / 1H")
    table.cell(dash, 1, 4, f_trendText(trendD) + " / " + f_trendText(trend4H) + " / " + f_trendText(trend1H))
 
    table.cell(dash, 0, 5, "1M / 5M")
    table.cell(dash, 1, 5, f_trendText(trend1M) + " / " + f_trendText(trend5M))
 
    table.cell(dash, 0, 6, "Strong")
    table.cell(dash, 1, 6, strongBuy ? "STRONG BUY" : strongSell ? "STRONG SELL" : "MIXED")
 
    table.cell(dash, 0, 7, "7AM watch")
    table.cell(dash, 1, 7, not is15 ? "USE 15M" : started ? "ACTIVE" : "WAIT 7AM")
 
    table.cell(dash, 0, 8, "50%")
    table.cell(dash, 1, 8, str.tostring(fib50, format.mintick))
 
    table.cell(dash, 0, 9, "PDH / PDL")
    table.cell(dash, 1, 9, str.tostring(pdh, format.mintick) + " / " + str.tostring(pdl, format.mintick))
 
    table.cell(dash, 0, 10, "Next older low")
    table.cell(dash, 1, 10, hasLower ? str.tostring(nextOlderLow, format.mintick) : "NOT FOUND")
 
//====================================================
// 21. ALERTS
//====================================================
alertcondition(up50, "50 UP", "Price closed above the 50% line after 7AM.")
alertcondition(down50, "50 DOWN", "Price closed below the 50% line after 7AM.")
 
alertcondition(buy618Pass or sell618Pass, "618 PASS", "Price tested and passed 61.8%.")
alertcondition(buy618Reject or sell618Reject, "618 REJECT", "Price tested and rejected 61.8%.")
 
alertcondition(possibleBuy40 or possibleSell40, "40 WATCH", "Price reached the 40% area.")
alertcondition(buySignal, "BUY 40", "Possible BUY 40 setup.")
alertcondition(sellSignal, "SELL 40", "Possible SELL 40 setup.")
 
alertcondition(exitBuy or exitSell, "TARGET TOUCH", "Target price was touched.")
alertcondition(nextLowTouch, "NEXT LOWER LOW TOUCHED", "Price touched the next older lower daily low.")
 
alertcondition(wickBear or wickBull, "WICK WARNING", "Possible rejection wick near 61.8%.")
alertcondition(continueUp, "CONTINUATION UP", "Possible upward continuation.")
alertcondition(continueDown, "CONTINUATION DOWN", "Possible downward continuation.")

← 返回列表