本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Fantastic Enter", shorttitle = "Fantastic Enter", overlay = true, max_lines_count = 500, max_labels_count = 500)
// This Pine Script(R) code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//
// Credits: the direction engine (six-factor weighted confluence score) is adapted from
// "Quant Confluence Engine [JOAT]" by officialjackofalltrades (MPL 2.0). The EMA 5/13 cross entry
// and the TP/SL drawing idea are inspired by "KN - Smart TP SL Signals". This version combines
// them, adds the point-based targets, the trade tracking and the permanent trade history.
//
// Direction (LONG / SHORT) comes from the confluence score. Entries come from the fast EMA cross
// and are only taken in the direction of the score. Entry, stop and three targets stay on the chart.
// ── SETTINGS ─────────────────────────────────────────────────────────────
gQ = "1) Direction (confluence score)"
emaFastQ = input.int(21, "Trend EMA fast", minval = 2, group = gQ)
emaSlowQ = input.int(55, "Trend EMA slow", minval = 2, group = gQ)
rsiLen = input.int(14, "RSI", minval = 2, group = gQ)
adxLen = input.int(14, "ADX / DI", minval = 2, group = gQ)
adxThr = input.float(18.0, "ADX trend threshold", minval = 5.0, group = gQ)
volLen = input.int(20, "Relative volume window", minval = 2, group = gQ)
locLen = input.int(20, "Location range window", minval = 2, group = gQ)
dirThr = input.float(20.0, "Minimum score to give a direction (0-100)", minval = 5.0, maxval = 100.0, step = 5.0, group = gQ, tooltip = "Higher = a more selective direction and fewer trades.")
minFactors = input.int(3, "Agreeing factors required (of 6)", minval = 1, maxval = 6, group = gQ)
gK = "2) Entry (EMA cross)"
kFast = input.int(5, "Entry EMA fast", minval = 1, group = gK)
kSlow = input.int(13, "Entry EMA slow", minval = 1, group = gK)
lookback = input.int(6, "Bars a cross stays valid while waiting for direction", minval = 1, group = gK, tooltip = "If the cross happens and the score confirms the direction within this many bars, the trade is taken.")
gT = "3) Targets and stop (points)"
tp1Pts = input.float(30.0, "TP1 (points)", minval = 1.0, group = gT)
tp2Pts = input.float(50.0, "TP2 (points)", minval = 1.0, group = gT)
tp3Pts = input.float(60.0, "TP3 (points)", minval = 1.0, group = gT)
atrLen = input.int(14, "ATR", minval = 1, group = gT)
slMult = input.float(1.5, "Stop = ATR x", minval = 0.1, step = 0.1, group = gT)
slMin = input.float(12.0, "Minimum stop (points)", minval = 1.0, group = gT)
slMax = input.float(25.0, "Maximum stop (points)", minval = 1.0, group = gT)
maxBars = input.int(60, "Close the trade after X bars without a result", minval = 5, group = gT)
gS = "4) Other"
cooldown = input.int(3, "Bars to wait between trades", minval = 1, group = gS)
useSess = input.bool(false, "Trade only in the session below (New York time)", group = gS)
sess = input.session("0930-1600", "Session", group = gS)
showEma = input.bool(true, "Show entry EMAs", group = gS)
showBg = input.bool(true, "Tint the background by direction", group = gS)
// ── SCORE: 6 FACTORS ────────────────────────────────────────────────────
eF = ta.ema(close, emaFastQ)
eS = ta.ema(close, emaSlowQ)
slopeUp = eS > eS[2]
vTrend = eF > eS and slopeUp ? 1 : eF < eS and not slopeUp ? -1 : eF > eS ? 1 : eF < eS ? -1 : 0
rsi = ta.rsi(close, rsiLen)
vMom = rsi >= 55 ? 1 : rsi <= 45 ? -1 : rsi > 50 ? 1 : rsi < 50 ? -1 : 0
[macdLine, sigLine, histLine] = ta.macd(close, 12, 26, 9)
vMacd = histLine > 0 and histLine >= histLine[1] ? 1 : histLine < 0 and histLine <= histLine[1] ? -1 : histLine > 0 ? 1 : histLine < 0 ? -1 : 0
upMv = ta.change(high)
downMv = -ta.change(low)
plusDM = na(upMv) ? na : (upMv > downMv and upMv > 0 ? upMv : 0)
minusDM = na(downMv) ? na : (downMv > upMv and downMv > 0 ? downMv : 0)
trur = ta.rma(ta.tr(true), adxLen)
plusDI = 100 * ta.rma(plusDM, adxLen) / trur
minusDI = 100 * ta.rma(minusDM, adxLen) / trur
dx = (plusDI + minusDI) == 0 ? 0 : 100 * math.abs(plusDI - minusDI) / (plusDI + minusDI)
adx = ta.rma(dx, adxLen)
vAdx = adx < adxThr ? 0 : plusDI > minusDI ? 1 : minusDI > plusDI ? -1 : 0
volAvg = ta.sma(volume, volLen)
relVol = volAvg > 0 ? volume / volAvg : 1.0
volDir = close > open ? 1 : close < open ? -1 : 0
vVol = relVol >= 1.2 ? volDir : 0
hh = ta.highest(high, locLen)
ll = ta.lowest(low, locLen)
locPct = (hh - ll) > 0 ? (close - ll) / (hh - ll) : 0.5
vLoc = locPct <= 0.35 ? 1 : locPct >= 0.65 ? -1 : 0
wTrend = 1.5
wMom = 1.0
wMacd = 1.0
wAdx = 1.0
wVol = 0.8
wLoc = 0.7
wSum = wTrend + wMom + wMacd + wAdx + wVol + wLoc
rawScore = vTrend * wTrend + vMom * wMom + vMacd * wMacd + vAdx * wAdx + vVol * wVol + vLoc * wLoc
score = rawScore / wSum * 100.0
scoreSm = ta.ema(score, 2)
bullVotes = (vTrend > 0 ? 1 : 0) + (vMom > 0 ? 1 : 0) + (vMacd > 0 ? 1 : 0) + (vAdx > 0 ? 1 : 0) + (vVol > 0 ? 1 : 0) + (vLoc > 0 ? 1 : 0)
bearVotes = (vTrend < 0 ? 1 : 0) + (vMom < 0 ? 1 : 0) + (vMacd < 0 ? 1 : 0) + (vAdx < 0 ? 1 : 0) + (vVol < 0 ? 1 : 0) + (vLoc < 0 ? 1 : 0)
// direction: 1 = LONG, -1 = SHORT, 0 = no clear direction
bias = scoreSm >= dirThr and bullVotes >= minFactors ? 1 : scoreSm <= -dirThr and bearVotes >= minFactors ? -1 : 0
// ── ENTRY EMAS ────────────────────────────────────────────────────
kF = ta.ema(close, kFast)
kS = ta.ema(close, kSlow)
atrV = ta.atr(atrLen)
var int pendDir = 0
var int pendBar = 0
if ta.crossover(kF, kS)
pendDir := 1
pendBar := bar_index
if ta.crossunder(kF, kS)
pendDir := -1
pendBar := bar_index
inSess = not useSess or not na(time(timeframe.period, sess, "America/New_York"))
// ── TRADE STATE ───────────────────────────────────────────────
var int tDir = 0
var float tEntry = na
var float tSl = na
var float tTp1 = na
var float tTp2 = na
var float tTp3 = na
var float tRisk = na
var int tHits = 0
var int tBar = 0
var int lastSig = -1000
var line lnE = na
var line lnS = na
var line ln1 = na
var line ln2 = na
var line ln3 = na
var label lbE = na
var label lbS = na
var label lb1 = na
var label lb2 = na
var label lb3 = na
f_end(line a, line b, line c, line d, line e, label la, label lb_, label lc, label ld, label le, int x) =>
line.set_x2(a, x)
line.set_x2(b, x)
line.set_x2(c, x)
line.set_x2(d, x)
line.set_x2(e, x)
label.set_x(la, x + 1)
label.set_x(lb_, x + 1)
label.set_x(lc, x + 1)
label.set_x(ld, x + 1)
label.set_x(le, x + 1)
f_follow(line a, line b, line c, line d, line e, label la, label lb_, label lc, label ld, label le, int x) =>
line.set_x2(a, x)
line.set_x2(b, x)
line.set_x2(c, x)
line.set_x2(d, x)
line.set_x2(e, x)
label.set_x(la, x + 1)
label.set_x(lb_, x + 1)
label.set_x(lc, x + 1)
label.set_x(ld, x + 1)
label.set_x(le, x + 1)
bool newBuy = false
bool newSell = false
bool ev1 = false
bool ev2 = false
bool ev3 = false
bool evSl = false
// ── TRADE TRACKING: SL, TP1, TP2, TP3 ───────────────────────────────────────
if tDir != 0 and bar_index > tBar
isL = tDir == 1
if (isL ? low <= tSl : high >= tSl)
evSl := true
label.set_text(lbS, tHits > 0 ? "SL after TP" + str.tostring(tHits) : "SL ✗ -" + str.tostring(tRisk, "#.#") + " pts")
label.set_color(lbS, color.new(color.red, 0))
line.set_width(lnS, 3)
f_end(lnE, lnS, ln1, ln2, ln3, lbE, lbS, lb1, lb2, lb3, bar_index)
tDir := 0
else
if tHits < 1 and (isL ? high >= tTp1 : low <= tTp1)
tHits := 1
ev1 := true
label.set_text(lb1, "TP1 ✓ +" + str.tostring(tp1Pts, "#.#") + " pts " + str.tostring(tTp1, format.mintick))
label.set_color(lb1, color.new(color.green, 0))
line.set_width(ln1, 3)
if tHits < 2 and (isL ? high >= tTp2 : low <= tTp2)
tHits := 2
ev2 := true
label.set_text(lb2, "TP2 ✓ +" + str.tostring(tp2Pts, "#.#") + " pts " + str.tostring(tTp2, format.mintick))
label.set_color(lb2, color.new(color.green, 0))
line.set_width(ln2, 3)
if tHits < 3 and (isL ? high >= tTp3 : low <= tTp3)
tHits := 3
ev3 := true
label.set_text(lb3, "TP3 ✓ +" + str.tostring(tp3Pts, "#.#") + " pts " + str.tostring(tTp3, format.mintick))
label.set_color(lb3, color.new(color.green, 0))
line.set_width(ln3, 3)
f_end(lnE, lnS, ln1, ln2, ln3, lbE, lbS, lb1, lb2, lb3, bar_index)
tDir := 0
// close by time
if tDir != 0 and bar_index - tBar >= maxBars
label.new(bar_index, close, "Closed by time", style = label.style_label_left, color = color.new(color.gray, 0), textcolor = color.white, size = size.small)
f_end(lnE, lnS, ln1, ln2, ln3, lbE, lbS, lb1, lb2, lb3, bar_index)
tDir := 0
// while open, the lines follow price
if tDir != 0
f_follow(lnE, lnS, ln1, ln2, ln3, lbE, lbS, lb1, lb2, lb3, bar_index + 6)
// ── ENTRY: EMA cross + score direction ──────────────────────────────────
crossValid = pendDir != 0 and bar_index - pendBar <= lookback
canEnter = barstate.isconfirmed and tDir == 0 and bias != 0 and crossValid and pendDir == bias and bar_index - lastSig >= cooldown and inSess and not na(atrV)
if canEnter
isL = bias == 1
risk = math.max(slMin, math.min(slMax, atrV * slMult))
tDir := bias
tEntry := close
tRisk := risk
tSl := isL ? close - risk : close + risk
tTp1 := isL ? close + tp1Pts : close - tp1Pts
tTp2 := isL ? close + tp2Pts : close - tp2Pts
tTp3 := isL ? close + tp3Pts : close - tp3Pts
tHits := 0
tBar := bar_index
lastSig := bar_index
pendDir := 0
newBuy := isL
newSell := not isL
x2 = bar_index + 6
lnE := line.new(bar_index, close, x2, close, color = color.white, width = 2)
lnS := line.new(bar_index, tSl, x2, tSl, color = color.red, width = 2, style = line.style_dashed)
ln1 := line.new(bar_index, tTp1, x2, tTp1, color = color.aqua, width = 1, style = line.style_dashed)
ln2 := line.new(bar_index, tTp2, x2, tTp2, color = color.aqua, width = 1, style = line.style_dashed)
ln3 := line.new(bar_index, tTp3, x2, tTp3, color = color.aqua, width = 1, style = line.style_dashed)
lbE := label.new(x2 + 1, close, (isL ? "LONG " : "SHORT ") + str.tostring(close, format.mintick), style = label.style_label_left, color = color.white, textcolor = color.black, size = size.small)
lbS := label.new(x2 + 1, tSl, "SL " + str.tostring(tSl, format.mintick) + " (-" + str.tostring(risk, "#.#") + " pts)", style = label.style_label_left, color = color.new(color.red, 20), textcolor = color.white, size = size.small)
lb1 := label.new(x2 + 1, tTp1, "TP1 " + str.tostring(tTp1, format.mintick) + " (+" + str.tostring(tp1Pts, "#.#") + " pts)", style = label.style_label_left, color = color.new(color.teal, 20), textcolor = color.white, size = size.small)
lb2 := label.new(x2 + 1, tTp2, "TP2 " + str.tostring(tTp2, format.mintick) + " (+" + str.tostring(tp2Pts, "#.#") + " pts)", style = label.style_label_left, color = color.new(color.teal, 20), textcolor = color.white, size = size.small)
lb3 := label.new(x2 + 1, tTp3, "TP3 " + str.tostring(tTp3, format.mintick) + " (+" + str.tostring(tp3Pts, "#.#") + " pts)", style = label.style_label_left, color = color.new(color.teal, 20), textcolor = color.white, size = size.small)
// ── DRAWING ───────────────────────────────────────────────────────────────
plot(showEma ? kF : na, "Entry EMA fast", color = color.new(color.green, 0))
plot(showEma ? kS : na, "Entry EMA slow", color = bias == 1 ? color.new(color.green, 0) : bias == -1 ? color.new(color.red, 0) : color.new(color.gray, 0), linewidth = 2)
bgcolor(showBg ? (bias == 1 ? color.new(color.green, 94) : bias == -1 ? color.new(color.red, 94) : na) : na, title = "Direction")
plotshape(newBuy, "Long", style = shape.labelup, location = location.belowbar, color = color.new(#16C784, 0), textcolor = color.white, text = "BUY", size = size.small)
plotshape(newSell, "Short", style = shape.labeldown, location = location.abovebar, color = color.new(#F0324B, 0), textcolor = color.white, text = "SELL", size = size.small)
plotchar(ev1 ? tTp1 : na, "TP1 hit", char = "✓", location = location.absolute, color = color.lime, size = size.tiny)
plotchar(ev2 ? tTp2 : na, "TP2 hit", char = "✓", location = location.absolute, color = color.lime, size = size.tiny)
plotchar(ev3 ? tTp3 : na, "TP3 hit", char = "✓", location = location.absolute, color = color.lime, size = size.tiny)
plotchar(evSl ? tSl : na, "SL hit", char = "✗", location = location.absolute, color = color.red, size = size.small)
var label dirLbl = na
if barstate.islast
label.delete(dirLbl)
dirLbl := label.new(bar_index, high, "Direction: " + (bias == 1 ? "LONG ▲" : bias == -1 ? "SHORT ▼" : "NO CLEAR DIRECTION •") + " (" + str.tostring(math.round(scoreSm)) + ")", yloc = yloc.abovebar, style = label.style_label_down, color = bias == 1 ? color.new(color.green, 10) : bias == -1 ? color.new(color.red, 10) : color.new(color.gray, 10), textcolor = color.white, size = size.small)
alertcondition(newBuy, "Fantastic Enter Long", "Fantastic Enter: LONG (bullish score + EMA cross)")
alertcondition(newSell, "Fantastic Enter Short", "Fantastic Enter: SHORT (bearish score + EMA cross)")