NexTrade Strategy 1.1

Daniel_Rios · study · 285 行 · 点赞 7 · TradingView 原页

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

Pine Script

//@version=6
// overlay=false: el indicador vive en un panel inferior (estocastico). Todo lo que va sobre el precio usa force_overlay=true.
indicator("NexTrade Strategy 1.1", overlay=false, max_boxes_count=250, max_labels_count=300)
// Confirmed HTF structure and sweeps; intrabar OB and Bollinger alerts.
gTrend = "Trend and structure"
pivotLen = input.int(5, "Pivot bars", minval=2, maxval=20, group=gTrend)
gZones = "Zones"
showOB = input.bool(true, "Show 1H order blocks", group=gZones)
showFVG = input.bool(false, "Show FVG", group=gZones)
maxZones = input.int(12, "Max zones per type", minval=1, maxval=40, group=gZones)
obAtr = input.float(0.8, "OB displacement ATR", minval=0.1, step=0.1, group=gZones)
gTech = "Technical confirmation"
rsiLen = input.int(5, "RSI length", minval=2, group=gTech)
stochLen = input.int(5, "Stochastic length", minval=2, group=gTech)
stochSmooth = input.int(3, "Stochastic K smoothing", minval=1, group=gTech)
stochD = input.int(3, "Stochastic D smoothing", minval=1, group=gTech)
bbLen = input.int(20, "Bollinger length", minval=2, group=gTech)
bbDev = input.float(2.0, "Bollinger deviation", minval=0.1, group=gTech)
bbPct1 = input.float(25.0, "First breakout % of band width", minval=0.1, group=gTech)
bbPct2 = input.float(30.0, "Second breakout % of band width", minval=0.1, group=gTech)
gAlerts = "Alerts"
aStructure = input.bool(true, "Equal highs-lows alerts", group=gAlerts)
aSweep = input.bool(true, "Confirmed liquidity sweeps", group=gAlerts)
aOBTouch = input.bool(true, "Order block touches (intrabar)", group=gAlerts)
aOBReject = input.bool(false, "Order block rejection (close)", group=gAlerts)
aFVG = input.bool(true, "New FVG", group=gAlerts)
aBB = input.bool(true, "Bollinger breaks (intrabar)", group=gAlerts)
aSignals = input.bool(true, "Trade setup alerts", group=gAlerts)
scoreWith = input.int(2, "Technical confirmations with trend", minval=0, maxval=3, group=gAlerts)
scoreAgainst = input.int(3, "Technical confirmations against trend", minval=0, maxval=3, group=gAlerts)
 
atr = ta.atr(14)
rsi = ta.rsi(close, rsiLen)
k = ta.sma(ta.stoch(close, high, low, stochLen), stochSmooth)
d = ta.sma(k, stochD)
basis = ta.sma(close, bbLen)
dev = bbDev * ta.stdev(close, bbLen)
upper = basis + dev
lower = basis - dev
width = upper - lower
// Bollinger sobre el precio
plot(upper, "BB Upper", color=color.yellow, linewidth=3, force_overlay=true)
plot(basis, "BB Basis", color=color.new(color.yellow, 55), linewidth=2, force_overlay=true)
plot(lower, "BB Lower", color=color.yellow, linewidth=3, force_overlay=true)
// Estocastico 5,3,3 en el panel propio del indicador
plot(k, "Stochastic %K", color=color.blue, linewidth=2)
plot(d, "Stochastic %D", color=color.orange, linewidth=2)
hline(80, "Stoch overbought 80", color=color.new(color.red, 50))
hline(20, "Stoch oversold 20", color=color.new(color.green, 50))
 
// Temporalidad del grafico: una TF pedida con request.security solo es fiable si es >= a la del grafico.
tfSec = timeframe.in_seconds()
use30 = tfSec <= 1800
use60 = tfSec <= 3600
use240 = tfSec <= 14400
 
// Confirmed swing structure; no future data is used in alerts.
f_structure() =>
    ph = ta.pivothigh(high, pivotLen, pivotLen)
    pl = ta.pivotlow(low, pivotLen, pivotLen)
    var float sh = na
    var float sl = na
    var int dir = 0
    if not na(ph)
        sh := ph
    if not na(pl)
        sl := pl
    up = ta.crossover(close, sh)
    dn = ta.crossunder(close, sl)
    if up
        dir := 1
    if dn
        dir := -1
    [dir, up, dn]
[localDir, bosUp, bosDn] = f_structure()
f_trend4() =>
    [dr, bu, bd] = f_structure()
    dr[1]
trend4raw = request.security(syminfo.tickerid, "240", f_trend4(), lookahead=barmerge.lookahead_on)
trend4 = use240 ? trend4raw : localDir[1]
 
// Each sweep is confirmed on the close of its own timeframe.
f_sweep() =>
    ph = ta.pivothigh(high, pivotLen, pivotLen)
    pl = ta.pivotlow(low, pivotLen, pivotLen)
    lastH = ta.valuewhen(not na(ph), ph, 0)
    lastL = ta.valuewhen(not na(pl), pl, 0)
    bull = not na(lastL) and low < lastL and close > lastL
    bear = not na(lastH) and high > lastH and close < lastH
    [bull[1], bear[1]]
[s30b, s30s] = request.security(syminfo.tickerid, "30", f_sweep(), lookahead=barmerge.lookahead_on)
[s60b, s60s] = request.security(syminfo.tickerid, "60", f_sweep(), lookahead=barmerge.lookahead_on)
[s240b, s240s] = request.security(syminfo.tickerid, "240", f_sweep(), lookahead=barmerge.lookahead_on)
// Una TF menor a la del grafico no se puede reconstruir: se ignora.
new30b = use30 and s30b and not s30b[1]
new30s = use30 and s30s and not s30s[1]
new60b = use60 and s60b and not s60b[1]
new60s = use60 and s60s and not s60s[1]
new240b = use240 and s240b and not s240b[1]
new240s = use240 and s240s and not s240s[1]
 
var array<box> obBoxes = array.new<box>()
var array<int> obDirs = array.new<int>()
var array<box> fvgBoxes = array.new<box>()
var array<int> fvgDirs = array.new<int>()
 
// ---------- 1) Primero: invalidacion y toques de zonas EXISTENTES ----------
// Se evalua antes de crear zonas nuevas para que una zona no cuente como tocada en su vela de creacion.
bool touchedBull = false
bool touchedBear = false
bool rejectedBull = false
bool rejectedBear = false
bool activeTouchBull = false
bool activeTouchBear = false
// Primer toque por zona (id = tiempo de la vela OB). varip: no se pierde entre ticks intrabarra.
varip array<int> touchedZoneTimes = array.new<int>()
if array.size(obBoxes) > 0
    for i = array.size(obBoxes) - 1 to 0
        z = array.get(obBoxes, i)
        dr = array.get(obDirs, i)
        top = box.get_top(z)
        bot = box.get_bottom(z)
        invalid = dr == 1 ? close < bot : close > top
        if barstate.isconfirmed and invalid
            box.delete(z)
            array.remove(obBoxes, i)
            array.remove(obDirs, i)
        else
            touching = high >= bot and low <= top
            zoneId = box.get_left(z)
            firstTouch = touching and not array.includes(touchedZoneTimes, zoneId)
            if firstTouch
                array.push(touchedZoneTimes, zoneId)
                if array.size(touchedZoneTimes) > 500
                    array.shift(touchedZoneTimes)
                touchedBull := touchedBull or dr == 1
                touchedBear := touchedBear or dr == -1
            if touching
                activeTouchBull := activeTouchBull or dr == 1
                activeTouchBear := activeTouchBear or dr == -1
                rejectedBull := rejectedBull or (dr == 1 and barstate.isconfirmed and close > top and close > open)
                rejectedBear := rejectedBear or (dr == -1 and barstate.isconfirmed and close < bot and close < open)
if array.size(fvgBoxes) > 0
    for i = array.size(fvgBoxes) - 1 to 0
        z = array.get(fvgBoxes, i)
        dr = array.get(fvgDirs, i)
        filled = dr == 1 ? low <= box.get_bottom(z) : high >= box.get_top(z)
        if barstate.isconfirmed and filled
            box.delete(z)
            array.remove(fvgBoxes, i)
            array.remove(fvgDirs, i)
 
// ---------- 2) Despues: creacion de zonas nuevas ----------
// OB de 1H detectado independientemente de la TF del grafico: vela contraria seguida de desplazamiento ATR.
f_ob1h() =>
    a = ta.atr(14)
    bull = close > open and close[1] < open[1] and close - open >= a * obAtr
    bear = close < open and close[1] > open[1] and open - close >= a * obAtr
    [bull[1], bear[1], high[2], low[2], time[2]]
[ob1Bull, ob1Bear, ob1Top, ob1Bottom, ob1Time] = request.security(syminfo.tickerid, "60", f_ob1h(), lookahead=barmerge.lookahead_on)
// Una vela 1H nueva cerrada; soporta graficos <= 1H.
new1h = tfSec <= 3600 and timeframe.change("60")
if new1h and (ob1Bull or ob1Bear) and not na(ob1Time)
    direction = ob1Bull ? 1 : -1
    zoneColor = direction == 1 ? color.green : color.red
    z = box.new(left=ob1Time, top=ob1Top, right=time, bottom=ob1Bottom, xloc=xloc.bar_time, extend=extend.right, bgcolor=color.new(zoneColor, showOB ? 88 : 100), border_color=color.new(zoneColor, showOB ? 45 : 100), force_overlay=true)
    array.push(obBoxes, z)
    array.push(obDirs, direction)
    if array.size(obBoxes) > maxZones
        box.delete(array.shift(obBoxes))
        array.shift(obDirs)
 
// FVG: desequilibrio de tres velas, tamano minimo 0.15 ATR.
bullFVG = barstate.isconfirmed and low > high[2] and low - high[2] >= atr * 0.15
bearFVG = barstate.isconfirmed and high < low[2] and low[2] - high >= atr * 0.15
if bullFVG or bearFVG
    direction = bullFVG ? 1 : -1
    top = bullFVG ? low : low[2]
    bottom = bullFVG ? high[2] : high
    c = direction == 1 ? color.teal : color.orange
    z = box.new(bar_index - 2, top, bar_index + 1, bottom, bgcolor=color.new(c, showFVG ? 90 : 100), border_color=color.new(c, showFVG ? 65 : 100), extend=extend.right, force_overlay=true)
    array.push(fvgBoxes, z)
    array.push(fvgDirs, direction)
    if array.size(fvgBoxes) > maxZones
        box.delete(array.shift(fvgBoxes))
        array.shift(fvgDirs)
 
// Equal highs and lows based on two confirmed consecutive pivots.
ph = ta.pivothigh(high, pivotLen, pivotLen)
pl = ta.pivotlow(low, pivotLen, pivotLen)
prevPH = ta.valuewhen(not na(ph), ph, 1)
prevPL = ta.valuewhen(not na(pl), pl, 1)
eqh = barstate.isconfirmed and not na(ph) and not na(prevPH) and math.abs(ph - prevPH) <= atr * 0.15
eql = barstate.isconfirmed and not na(pl) and not na(prevPL) and math.abs(pl - prevPL) <= atr * 0.15
// CHoCH: la direccion previa era contraria al BOS actual. Solo uso interno, sin marcador ni alerta.
chochUp = bosUp and localDir[1] == -1
chochDn = bosDn and localDir[1] == 1
plotshape(eqh, "EQH", shape.arrowdown, location.abovebar, color=color.yellow, size=size.small, offset=-pivotLen, force_overlay=true)
plotshape(eql, "EQL", shape.arrowup, location.belowbar, color=color.orange, size=size.small, offset=-pivotLen, force_overlay=true)
 
// Technical confirmations are secondary to a structural zone.
techBull = (rsi > 50 ? 1 : 0) + (k > d and k < 80 ? 1 : 0) + (close > basis ? 1 : 0)
techBear = (rsi < 50 ? 1 : 0) + (k < d and k > 20 ? 1 : 0) + (close < basis ? 1 : 0)
recentBullSweep = ta.barssince(new30b or new60b or new240b) <= 8
recentBearSweep = ta.barssince(new30s or new60s or new240s) <= 8
bullZone = activeTouchBull or recentBullSweep
bearZone = activeTouchBear or recentBearSweep
bullWith = trend4 == 1 and bullZone and techBull >= scoreWith and barstate.isconfirmed
bearWith = trend4 == -1 and bearZone and techBear >= scoreWith and barstate.isconfirmed
bullAgainst = trend4 == -1 and bullZone and chochUp and bullFVG and techBull >= scoreAgainst and barstate.isconfirmed
bearAgainst = trend4 == 1 and bearZone and chochDn and bearFVG and techBear >= scoreAgainst and barstate.isconfirmed
// Alert only on transitions to a new setup.
longWith = bullWith and not bullWith[1]
shortWith = bearWith and not bearWith[1]
longAgainst = bullAgainst and not bullAgainst[1]
shortAgainst = bearAgainst and not bearAgainst[1]
plotshape(longWith or longAgainst, "LONG green arrow", shape.arrowup, location.belowbar, color=color.lime, size=size.small, force_overlay=true)
plotshape(shortWith or shortAgainst, "SHORT red arrow", shape.arrowdown, location.abovebar, color=color.red, size=size.small, force_overlay=true)
 
// Intra-bar alerts: varip flags reset at the start of each real-time bar.
varip bool sent10Up = false
varip bool sent15Up = false
varip bool sent10Dn = false
varip bool sent15Dn = false
if barstate.isnew
    sent10Up := false
    sent15Up := false
    sent10Dn := false
    sent15Dn := false
f_alert(string message) =>
    alert("NexTrade | " + syminfo.ticker + " | " + timeframe.period + " | " + message, alert.freq_all)
if barstate.isrealtime
    if aBB and not sent10Up and close >= upper + width * bbPct1 / 100
        f_alert("Bollinger SUPERIOR +" + str.tostring(bbPct1) + "%")
        sent10Up := true
    if aBB and not sent15Up and close >= upper + width * bbPct2 / 100
        f_alert("Bollinger SUPERIOR +" + str.tostring(bbPct2) + "%")
        sent15Up := true
    if aBB and not sent10Dn and close <= lower - width * bbPct1 / 100
        f_alert("Bollinger INFERIOR -" + str.tostring(bbPct1) + "%")
        sent10Dn := true
    if aBB and not sent15Dn and close <= lower - width * bbPct2 / 100
        f_alert("Bollinger INFERIOR -" + str.tostring(bbPct2) + "%")
        sent15Dn := true
    // touchedBull/Bear solo es true en el primer toque de cada zona (latch por zona en touchedZoneTimes).
    if aOBTouch and touchedBull
        f_alert("Toque Order Block ALCISTA 1H")
    if aOBTouch and touchedBear
        f_alert("Toque Order Block BAJISTA 1H")
if barstate.isconfirmed
    if aStructure
        if eqh
            f_alert("EQH maximos iguales")
        if eql
            f_alert("EQL minimos iguales")
    if aSweep
        if new30b
            f_alert("Barrido ALCISTA 30m confirmado")
        if new30s
            f_alert("Barrido BAJISTA 30m confirmado")
        if new60b
            f_alert("Barrido ALCISTA 1H confirmado")
        if new60s
            f_alert("Barrido BAJISTA 1H confirmado")
        if new240b
            f_alert("Barrido ALCISTA 4H confirmado")
        if new240s
            f_alert("Barrido BAJISTA 4H confirmado")
    if aOBReject and rejectedBull
        f_alert("Rechazo confirmado Order Block ALCISTA")
    if aOBReject and rejectedBear
        f_alert("Rechazo confirmado Order Block BAJISTA")
    if aFVG and bullFVG
        f_alert("Nuevo FVG ALCISTA")
    if aFVG and bearFVG
        f_alert("Nuevo FVG BAJISTA")
    if aSignals
        if longWith
            f_alert("LONG A FAVOR tendencia 4H")
        if shortWith
            f_alert("SHORT A FAVOR tendencia 4H")
        if longAgainst
            f_alert("LONG CONTRA tendencia 4H")
        if shortAgainst
            f_alert("SHORT CONTRA tendencia 4H")

← 返回列表