Strong Magnet Zones PRO [Thomas]

Thomas-Sterling · study · 360 行 · 点赞 457 · 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/
// © Thomas-Sterling
 
 
 // This Pine Script® code is subject to the terms of the Mozilla Public License 2.0
 // © Thomas-Sterling
 
//@version=6
indicator("Strong Magnet Zones PRO [Thomas]", overlay=true, max_boxes_count=100, max_labels_count=100)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 1. SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
groupZone = "Magnet Zone Settings"
 
pivotLen = input.int(8, "Pivot Strength", minval=2, maxval=30, group=groupZone)
atrLen = input.int(14, "ATR Length", minval=1, group=groupZone)
zoneATR = input.float(0.50, "Zone Width (ATR)", minval=0.1, step=0.1, group=groupZone)
maxZones = input.int(8, "Maximum Zones", minval=1, maxval=30, group=groupZone)
extendBars = input.int(30, "Zone Extension", minval=5, maxval=200, group=groupZone)
 
groupFeatures = "Premium Features"
 
showBuySell = input.bool(true, "Show Buy / Sell Signals", group=groupFeatures)
showZoneLabels = input.bool(true, "Show Zone Labels", group=groupFeatures)
showMagnetPanel = input.bool(true, "Right-Side Magnet Panel", group=groupFeatures)
showDashboard = input.bool(true, "Market Dashboard", group=groupFeatures)
showStrength = input.bool(true, "Show Strength Score", group=groupFeatures)
showGreyZones = input.bool(true, "Show Grey Reaction Zones", group=groupFeatures)
 
groupMTF = "Multi-Timeframe Confirmation"
 
useMTF = input.bool(true, "Enable MTF Confirmation", group=groupMTF)
confirmTF = input.timeframe("240", "Confirmation Timeframe", group=groupMTF)
cooldown = input.int(8, "Signal Cooldown Bars", minval=0, group=groupMTF)
 
// Colors
buyColor = color.rgb(0, 220, 145)
sellColor = color.rgb(210, 0, 255)
greyColor = color.rgb(110, 110, 110)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 2. CALCULATIONS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
atr = ta.atr(atrLen)
 
pivotHigh = ta.pivothigh(high, pivotLen, pivotLen)
pivotLow = ta.pivotlow(low, pivotLen, pivotLen)
 
volAverage = ta.sma(volume, 20)
 
// MTF trend
mtfEMA50 = request.security(syminfo.tickerid, confirmTF, ta.ema(close, 50))
mtfEMA200 = request.security(syminfo.tickerid, confirmTF, ta.ema(close, 200))
 
mtfBull = mtfEMA50 > mtfEMA200
mtfBear = mtfEMA50 < mtfEMA200
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 3. ZONE STORAGE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var box[] zoneBoxes = array.new_box()
var int[] zoneTypes = array.new_int()
var bool[] zoneTested = array.new_bool()
var float[] zoneStrengths = array.new_float()
 
var float latestBuyTop = na
var float latestBuyBottom = na
var float latestSellTop = na
var float latestSellBottom = na
 
var float latestBuyStrength = na
var float latestSellStrength = na
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 4. STRENGTH SCORE CALCULATION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
getStrength(int offset, bool isHigh) =>
    candleVolume = volume[offset]
    averageVolume = volAverage[offset]
 
    volumeRatio = na(candleVolume) or na(averageVolume) or averageVolume == 0 ? 1.0 : candleVolume / averageVolume
 
    candleBody = math.abs(close[offset] - open[offset])
    candleWick = isHigh ? high[offset] - math.max(open[offset], close[offset]) : math.min(open[offset], close[offset]) - low[offset]
 
    wickRatio = candleWick / math.max(candleBody, syminfo.mintick)
 
    score = 50.0 + (volumeRatio - 1.0) * 12.0 + math.min(wickRatio, 3.0) * 8.0
 
    math.max(1.0, math.min(100.0, score))
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 5. CREATE SELL MAGNET ZONES
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
if not na(pivotHigh)
    zoneWidth = atr[pivotLen] * zoneATR
 
    latestSellTop := pivotHigh + zoneWidth
    latestSellBottom := pivotHigh - zoneWidth
 
    latestSellStrength := getStrength(pivotLen, true)
 
    sellBox = box.new(
         left=bar_index - pivotLen,
         top=latestSellTop,
         right=bar_index + extendBars,
         bottom=latestSellBottom,
         xloc=xloc.bar_index,
         bgcolor=color.new(sellColor, 88),
         border_color=color.new(sellColor, 35))
 
    array.push(zoneBoxes, sellBox)
    array.push(zoneTypes, -1)
    array.push(zoneTested, false)
    array.push(zoneStrengths, latestSellStrength)
 
    if showZoneLabels
        box.set_text(sellBox, "STRONG MAGNET | SELL")
        box.set_text_color(sellBox, color.white)
        box.set_text_size(sellBox, size.small)
 
    if array.size(zoneBoxes) > maxZones
        oldBox = array.shift(zoneBoxes)
        box.delete(oldBox)
        array.shift(zoneTypes)
        array.shift(zoneTested)
        array.shift(zoneStrengths)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 6. CREATE BUY MAGNET ZONES
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
if not na(pivotLow)
    zoneWidth = atr[pivotLen] * zoneATR
 
    latestBuyTop := pivotLow + zoneWidth
    latestBuyBottom := pivotLow - zoneWidth
 
    latestBuyStrength := getStrength(pivotLen, false)
 
    buyBox = box.new(
         left=bar_index - pivotLen,
         top=latestBuyTop,
         right=bar_index + extendBars,
         bottom=latestBuyBottom,
         xloc=xloc.bar_index,
         bgcolor=color.new(buyColor, 88),
         border_color=color.new(buyColor, 35))
 
    array.push(zoneBoxes, buyBox)
    array.push(zoneTypes, 1)
    array.push(zoneTested, false)
    array.push(zoneStrengths, latestBuyStrength)
 
    if showZoneLabels
        box.set_text(buyBox, "STRONG MAGNET | BUY")
        box.set_text_color(buyBox, color.white)
        box.set_text_size(buyBox, size.small)
 
    if array.size(zoneBoxes) > maxZones
        oldBox = array.shift(zoneBoxes)
        box.delete(oldBox)
        array.shift(zoneTypes)
        array.shift(zoneTested)
        array.shift(zoneStrengths)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 7. ZONE FRESHNESS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
if array.size(zoneBoxes) > 0
    for i = 0 to array.size(zoneBoxes) - 1
        bx = array.get(zoneBoxes, i)
        zoneType = array.get(zoneTypes, i)
        tested = array.get(zoneTested, i)
        strength = array.get(zoneStrengths, i)
 
        box.set_right(bx, bar_index + extendBars)
 
        zoneTop = box.get_top(bx)
        zoneBottom = box.get_bottom(bx)
 
        touched = high >= zoneBottom and low <= zoneTop
 
        if touched and not tested
            array.set(zoneTested, i, true)
 
            box.set_bgcolor(bx, color.new(zoneType == 1 ? buyColor : zoneType == -1 ? sellColor : greyColor, 93))
 
        freshness = array.get(zoneTested, i) ? "TESTED" : "FRESH"
 
        zoneName = zoneType == 1 ? "BUY" : zoneType == -1 ? "SELL" : "REACTION"
 
        strengthText = showStrength ? " | " + str.tostring(strength, "#") + "%" : ""
 
        if showZoneLabels
            box.set_text(bx, "MAGNET " + zoneName + strengthText + " | " + freshness)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 8. GREY REACTION ZONES
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
if showGreyZones and not na(pivotHigh) and latestSellStrength < 55
    greyTop = math.max(open[pivotLen], close[pivotLen])
    greyBottom = math.min(open[pivotLen], close[pivotLen])
 
    if greyTop > greyBottom
        greyBox = box.new(
             left=bar_index - pivotLen,
             top=greyTop,
             right=bar_index + extendBars,
             bottom=greyBottom,
             xloc=xloc.bar_index,
             bgcolor=color.new(greyColor, 90),
             border_color=color.new(greyColor, 65))
 
        array.push(zoneBoxes, greyBox)
        array.push(zoneTypes, 0)
        array.push(zoneTested, false)
        array.push(zoneStrengths, latestSellStrength)
 
        if array.size(zoneBoxes) > maxZones
            oldBox = array.shift(zoneBoxes)
            box.delete(oldBox)
            array.shift(zoneTypes)
            array.shift(zoneTested)
            array.shift(zoneStrengths)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 9. BUY / SELL SIGNALS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
buyTouch = not na(latestBuyTop) and low <= latestBuyTop and high >= latestBuyBottom
 
sellTouch = not na(latestSellTop) and high >= latestSellBottom and low <= latestSellTop
 
buyMTFAllowed = not useMTF or mtfBull
sellMTFAllowed = not useMTF or mtfBear
 
var int lastBuyBar = na
var int lastSellBar = na
 
buyCooldownOK = na(lastBuyBar) or bar_index - lastBuyBar > cooldown
sellCooldownOK = na(lastSellBar) or bar_index - lastSellBar > cooldown
 
buySignal = buyTouch and close > latestBuyTop and close > open and buyMTFAllowed and buyCooldownOK
 
sellSignal = sellTouch and close < latestSellBottom and close < open and sellMTFAllowed and sellCooldownOK
 
if buySignal
    lastBuyBar := bar_index
 
if sellSignal
    lastSellBar := bar_index
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 10. SIGNAL DISPLAY
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
plotshape(
     showBuySell and buySignal,
     title="Magnet Buy",
     style=shape.labelup,
     location=location.belowbar,
     color=buyColor,
     text="BUY",
     textcolor=color.white,
     size=size.small)
 
plotshape(
     showBuySell and sellSignal,
     title="Magnet Sell",
     style=shape.labeldown,
     location=location.abovebar,
     color=sellColor,
     text="SELL",
     textcolor=color.white,
     size=size.small)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 11. RIGHT-SIDE MAGNET PANEL
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var label buyMagLabel = na
var label sellMagLabel = na
 
if barstate.islast and showMagnetPanel
    if not na(latestBuyTop)
        buyText = "MAGNET BUY | " + str.tostring(latestBuyBottom, format.mintick) + " | " + str.tostring(latestBuyStrength, "#") + "%"
 
        if na(buyMagLabel)
            buyMagLabel := label.new(
                 bar_index + extendBars,
                 latestBuyBottom,
                 buyText,
                 style=label.style_label_left,
                 color=buyColor,
                 textcolor=color.white,
                 size=size.small)
        else
            label.set_xy(buyMagLabel, bar_index + extendBars, latestBuyBottom)
            label.set_text(buyMagLabel, buyText)
 
    if not na(latestSellTop)
        sellText = "MAGNET SELL | " + str.tostring(latestSellTop, format.mintick) + " | " + str.tostring(latestSellStrength, "#") + "%"
 
        if na(sellMagLabel)
            sellMagLabel := label.new(
                 bar_index + extendBars,
                 latestSellTop,
                 sellText,
                 style=label.style_label_left,
                 color=sellColor,
                 textcolor=color.white,
                 size=size.small)
        else
            label.set_xy(sellMagLabel, bar_index + extendBars, latestSellTop)
            label.set_text(sellMagLabel, sellText)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 12. MARKET DASHBOARD
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var table dashboard = table.new(position.top_right, 2, 5, border_width=1)
 
if barstate.islast and showDashboard
    marketTrend = mtfBull ? "BULLISH" : mtfBear ? "BEARISH" : "NEUTRAL"
 
    trendColor = mtfBull ? buyColor : mtfBear ? sellColor : greyColor
 
    marketStatus = buySignal ? "BUY SIGNAL" : sellSignal ? "SELL SIGNAL" : "WAITING"
 
    table.cell(dashboard, 0, 0, "THOMAS", bgcolor=color.black, text_color=color.white)
    table.cell(dashboard, 1, 0, "MAGNET PRO", bgcolor=color.black, text_color=color.yellow)
 
    table.cell(dashboard, 0, 1, "Market Trend", bgcolor=color.black, text_color=color.white)
    table.cell(dashboard, 1, 1, marketTrend, bgcolor=trendColor, text_color=color.white)
 
    table.cell(dashboard, 0, 2, "Buy Strength", bgcolor=color.black, text_color=color.white)
    table.cell(dashboard, 1, 2, na(latestBuyStrength) ? "N/A" : str.tostring(latestBuyStrength, "#") + "%", bgcolor=color.new(buyColor, 30), text_color=color.white)
 
    table.cell(dashboard, 0, 3, "Sell Strength", bgcolor=color.black, text_color=color.white)
    table.cell(dashboard, 1, 3, na(latestSellStrength) ? "N/A" : str.tostring(latestSellStrength, "#") + "%", bgcolor=color.new(sellColor, 30), text_color=color.white)
 
    table.cell(dashboard, 0, 4, "Signal Status", bgcolor=color.black, text_color=color.white)
    table.cell(dashboard, 1, 4, marketStatus, bgcolor=color.new(trendColor, 20), text_color=color.white)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 13. ALERTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
alertcondition(buySignal, title="Strong Magnet Buy", message="Thomas Strong Magnet Zones: BUY signal")
 
alertcondition(sellSignal, title="Strong Magnet Sell", message="Thomas Strong Magnet Zones: SELL signal")

← 返回列表