本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
strategy("MTF Supertrend Zones + Perfect Entries PRO",
overlay=true,
pyramiding=0,
initial_capital=10000,
process_orders_on_close=true,
calc_on_order_fills=true)
//================ INPUTS =================
atrLength = input.int(10, "ATR Length", minval=1)
stMultiplier = input.float(3.0, "Supertrend Multiplier", step=0.1)
emaLength = input.int(200, "EMA Length", minval=1)
pullbackATRFactor = input.float(0.35, "Pullback ATR Factor", step=0.05)
slATR = input.float(1.0, "Stop Loss ATR Multiplier", step=0.1)
tpATR = input.float(2.0, "Take Profit ATR Multiplier", step=0.1)
use5mFilter = input.bool(true, "Use 5m Filter")
use15mFilter = input.bool(true, "Use 15m Filter")
use30mFilter = input.bool(true, "Use 30m Filter")
use1HFilter = input.bool(true, "Use 1H Filter")
use4HFilter = input.bool(true, "Use 4H Filter")
use8HFilter = input.bool(true, "Use 8H Filter")
useDailyFilter = input.bool(true, "Use Daily Filter")
showBackground = input.bool(true, "Show Zone Background")
showLabels = input.bool(true, "Show Zone Labels")
showDashboard = input.bool(true, "Show Dashboard")
showSignals = input.bool(true, "Show Buy/Sell Signals")
showEMA = input.bool(true, "Show EMA")
showSupertrend = input.bool(true, "Show Supertrend")
//================ INDICATORS =================
[stLine, stDir] = ta.supertrend(stMultiplier, atrLength)
emaValue = ta.ema(close, emaLength)
atrValue = ta.atr(atrLength)
//================ LTF TREND =================
ltfBull = stDir < 0
ltfBear = stDir > 0
priceAboveEMA = close > emaValue
priceBelowEMA = close < emaValue
emaUp = emaValue > emaValue[1]
emaDown = emaValue < emaValue[1]
//================ MTF EMA DATA =================
close5m = request.security(syminfo.tickerid, "5", close)
ema5m = request.security(syminfo.tickerid, "5", ta.ema(close, emaLength))
close15m = request.security(syminfo.tickerid, "15", close)
ema15m = request.security(syminfo.tickerid, "15", ta.ema(close, emaLength))
close30m = request.security(syminfo.tickerid, "30", close)
ema30m = request.security(syminfo.tickerid, "30", ta.ema(close, emaLength))
close1H = request.security(syminfo.tickerid, "60", close)
ema1H = request.security(syminfo.tickerid, "60", ta.ema(close, emaLength))
close4H = request.security(syminfo.tickerid, "240", close)
ema4H = request.security(syminfo.tickerid, "240", ta.ema(close, emaLength))
close8H = request.security(syminfo.tickerid, "480", close)
ema8H = request.security(syminfo.tickerid, "480", ta.ema(close, emaLength))
closeD = request.security(syminfo.tickerid, "D", close)
emaD = request.security(syminfo.tickerid, "D", ta.ema(close, emaLength))
//================ MTF CONDITIONS =================
bull5m = close5m > ema5m
bear5m = close5m < ema5m
bull15m = close15m > ema15m
bear15m = close15m < ema15m
bull30m = close30m > ema30m
bear30m = close30m < ema30m
bull1H = close1H > ema1H
bear1H = close1H < ema1H
bull4H = close4H > ema4H
bear4H = close4H < ema4H
bull8H = close8H > ema8H
bear8H = close8H < ema8H
bullD = closeD > emaD
bearD = closeD < emaD
mtfBull = (not use5mFilter or bull5m) and
(not use15mFilter or bull15m) and
(not use30mFilter or bull30m) and
(not use1HFilter or bull1H) and
(not use4HFilter or bull4H) and
(not use8HFilter or bull8H) and
(not useDailyFilter or bullD)
mtfBear = (not use5mFilter or bear5m) and
(not use15mFilter or bear15m) and
(not use30mFilter or bear30m) and
(not use1HFilter or bear1H) and
(not use4HFilter or bear4H) and
(not use8HFilter or bear8H) and
(not useDailyFilter or bearD)
//================ ZONES =================
buyZone = mtfBull and priceAboveEMA and emaUp
sellZone = mtfBear and priceBelowEMA and emaDown
noTrade = not buyZone and not sellZone
//================ ENTRY LOGIC =================
bullCandle = close > open
bearCandle = close < open
breakHigh = high > high[1]
breakLow = low < low[1]
nearEMA = math.abs(close - emaValue) <= atrValue * pullbackATRFactor
perfectLong = buyZone and ltfBull and nearEMA and bullCandle and breakHigh
perfectShort = sellZone and ltfBear and nearEMA and bearCandle and breakLow
//================ TRADE EXECUTION =================
longStop = close - atrValue * slATR
longTP = close + atrValue * tpATR
shortStop = close + atrValue * slATR
shortTP = close - atrValue * tpATR
if perfectLong and strategy.position_size <= 0
strategy.entry("Long", strategy.long)
strategy.exit("Long Exit", from_entry="Long", stop=longStop, limit=longTP)
if perfectShort and strategy.position_size >= 0
strategy.entry("Short", strategy.short)
strategy.exit("Short Exit", from_entry="Short", stop=shortStop, limit=shortTP)
//================ PLOTS =================
plot(showEMA ? emaValue : na, color=color.blue, linewidth=2, title="EMA 200")
plot(showSupertrend and ltfBull ? stLine : na, color=color.green, linewidth=2, title="Bullish Supertrend")
plot(showSupertrend and ltfBear ? stLine : na, color=color.red, linewidth=2, title="Bearish Supertrend")
//================ BACKGROUND =================
bgcolor(showBackground ?
buyZone ? color.new(color.green, 88) :
sellZone ? color.new(color.red, 88) :
color.new(color.gray, 92)
: na)
//================ LABELS =================
if showLabels and buyZone and not buyZone[1]
label.new(bar_index, low, "BUY ZONE", style=label.style_label_up, color=color.green, textcolor=color.white)
if showLabels and sellZone and not sellZone[1]
label.new(bar_index, high, "SELL ZONE", style=label.style_label_down, color=color.red, textcolor=color.white)
if showLabels and noTrade and not noTrade[1]
label.new(bar_index, close, "NO TRADE", style=label.style_label_left, color=color.gray, textcolor=color.white)
//================ SIGNALS =================
plotshape(showSignals and perfectLong, title="BUY", location=location.belowbar, style=shape.triangleup, color=color.lime, text="BUY", textcolor=color.white, size=size.small)
plotshape(showSignals and perfectShort, title="SELL", location=location.abovebar, style=shape.triangledown, color=color.red, text="SELL", textcolor=color.white, size=size.small)
//================ ALERTS =================
alertcondition(perfectLong, title="Perfect Buy", message="BUY SIGNAL")
alertcondition(perfectShort, title="Perfect Sell", message="SELL SIGNAL")
alertcondition(buyZone and not buyZone[1], title="Buy Zone", message="BUY ZONE")
alertcondition(sellZone and not sellZone[1], title="Sell Zone", message="SELL ZONE")
alertcondition(noTrade and not noTrade[1], title="No Trade", message="NO TRADE")
//================ DASHBOARD =================
f_status(_bull, _bear) =>
_bull ? "BULL" : _bear ? "BEAR" : "NEUTRAL"
f_color(_bull, _bear) =>
_bull ? color.green : _bear ? color.red : color.gray
var table dash = table.new(position.top_right, 2, 10, border_width=1)
if barstate.islast and showDashboard
table.cell(dash, 0, 0, "TF / STATE", text_color=color.white, bgcolor=color.black)
table.cell(dash, 1, 0, "STATUS", text_color=color.white, bgcolor=color.black)
table.cell(dash, 0, 1, "5m")
table.cell(dash, 1, 1, f_status(bull5m, bear5m), text_color=color.white, bgcolor=f_color(bull5m, bear5m))
table.cell(dash, 0, 2, "15m")
table.cell(dash, 1, 2, f_status(bull15m, bear15m), text_color=color.white, bgcolor=f_color(bull15m, bear15m))
table.cell(dash, 0, 3, "30m")
table.cell(dash, 1, 3, f_status(bull30m, bear30m), text_color=color.white, bgcolor=f_color(bull30m, bear30m))
table.cell(dash, 0, 4, "1H")
table.cell(dash, 1, 4, f_status(bull1H, bear1H), text_color=color.white, bgcolor=f_color(bull1H, bear1H))
table.cell(dash, 0, 5, "4H")
table.cell(dash, 1, 5, f_status(bull4H, bear4H), text_color=color.white, bgcolor=f_color(bull4H, bear4H))
table.cell(dash, 0, 6, "8H")
table.cell(dash, 1, 6, f_status(bull8H, bear8H), text_color=color.white, bgcolor=f_color(bull8H, bear8H))
table.cell(dash, 0, 7, "Daily")
table.cell(dash, 1, 7, f_status(bullD, bearD), text_color=color.white, bgcolor=f_color(bullD, bearD))
table.cell(dash, 0, 8, "ZONE")
table.cell(dash, 1, 8, buyZone ? "BUY ZONE" : sellZone ? "SELL ZONE" : "NO TRADE",
text_color=color.white,
bgcolor=buyZone ? color.green : sellZone ? color.red : color.gray)
table.cell(dash, 0, 9, "ENTRY")
table.cell(dash, 1, 9, perfectLong ? "BUY" : perfectShort ? "SELL" : "WAIT",
text_color=color.white,
bgcolor=perfectLong ? color.green : perfectShort ? color.red : color.gray)