本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Contra Supertrend MTF System by SNIPERMONKEY", overlay=true)
// ============ ENTRY TF (LOWER) SETTINGS ============
grpEntry = "Entry Timeframe (Lower)"
entryTF = input.timeframe("1", "Timeframe", group=grpEntry)
atrLenEntry = input.int(10, "ATR Length", group=grpEntry)
factorEntry = input.float(3.0, "Factor", step=0.1, group=grpEntry)
// ============ CONFIRM TF (HIGHER) SETTINGS ============
grpConfirm = "Confirmation Timeframe (Higher)"
confirmTF = input.timeframe("5", "Timeframe", group=grpConfirm)
atrLenConfirm = input.int(10, "ATR Length", group=grpConfirm)
factorConfirm = input.float(3.0, "Factor", step=0.1, group=grpConfirm)
showConfirmLine = input.bool(true, "Plot Confirmation Supertrend", group=grpConfirm)
grpSession = "Session Settings"
sessStartH = input.int(9, "Entry Start Hour", group=grpSession)
sessStartM = input.int(25, "Entry Start Minute", group=grpSession)
sessEndH = input.int(15, "Entry End Hour", group=grpSession)
sessEndM = input.int(0, "Entry End Minute", group=grpSession)
exitH = input.int(15, "Force Exit Hour", group=grpSession)
exitM = input.int(10, "Force Exit Minute", group=grpSession)
// ============ SESSION TIME GATING (minutes-since-midnight) ============
curMin = hour * 60 + minute
sessStartMn = sessStartH * 60 + sessStartM
sessEndMn = sessEndH * 60 + sessEndM
exitMn = exitH * 60 + exitM
inEntryWindow = curMin >= sessStartMn and curMin < sessEndMn
atForceExit = curMin >= exitMn
// ============ SUPERTREND — ENTRY TF (confirmed-only MTF fetch) ============
get1() =>
[_st, _dir] = ta.supertrend(factorEntry, atrLenEntry)
[_st, _dir]
[st1, dir1] = request.security(syminfo.tickerid, entryTF, get1(), barmerge.gaps_off, barmerge.lookahead_off)
// ============ SUPERTREND — CONFIRM TF (confirmed-only MTF fetch) ============
get5() =>
[_st, _dir] = ta.supertrend(factorConfirm, atrLenConfirm)
[_st, _dir]
[st5, dir5] = request.security(syminfo.tickerid, confirmTF, get5(), barmerge.gaps_off, barmerge.lookahead_off)
bull1 = dir1 < 0
bear1 = dir1 > 0
bull5 = dir5 < 0
bear5 = dir5 > 0
flippedBull1 = bull1 and dir1[1] > 0
flippedBear1 = bear1 and dir1[1] < 0
sessionJustOpened = inEntryWindow and not inEntryWindow[1]
// ============ STATE ============
var int posState = 0 // 0 = flat, 1 = long, -1 = short
canEnterLong = inEntryWindow and posState == 0 and flippedBull1 and bear5
canEnterShort = inEntryWindow and posState == 0 and flippedBear1 and bull5
carryLong = sessionJustOpened and posState == 0 and bull1 and bear5
carryShort = sessionJustOpened and posState == 0 and bear1 and bull5
buySignal = barstate.isconfirmed and (canEnterLong or carryLong)
sellSignal = barstate.isconfirmed and (canEnterShort or carryShort)
buyExitSignal = barstate.isconfirmed and posState == 1 and (flippedBear1 or atForceExit)
sellExitSignal = barstate.isconfirmed and posState == -1 and (flippedBull1 or atForceExit)
if buySignal
posState := 1
if sellSignal
posState := -1
if buyExitSignal
posState := 0
if sellExitSignal
posState := 0
// ============ PLOTS ============
plot(st1, "Supertrend (Entry TF)", color = bull1 ? color.green : color.red)
plot(showConfirmLine ? st5 : na, "Supertrend (Confirm TF)", color = bull5 ? color.new(color.green, 60) : color.new(color.red, 60), style=plot.style_circles)
bgcolor(inEntryWindow ? color.new(color.blue, 92) : na, title="Entry Window")
plotshape(buySignal, "BUY", shape.triangleup, location.belowbar, color.new(color.green, 0), size=size.small, text="BUY")
plotshape(sellSignal, "SELL", shape.triangledown, location.abovebar, color.new(color.red, 0), size=size.small, text="SELL")
plotshape(buyExitSignal, "BUY EXIT", shape.xcross, location.abovebar, color.new(color.orange, 0), size=size.tiny, text="BUY EXIT")
plotshape(sellExitSignal, "SELL EXIT", shape.xcross, location.belowbar, color.new(color.orange, 0), size=size.tiny, text="SELL EXIT")
alertcondition(buySignal, "Contra BUY", "Contra Supertrend BUY")
alertcondition(sellSignal, "Contra SELL", "Contra Supertrend SELL")
alertcondition(buyExitSignal, "BUY EXIT", "Contra Supertrend BUY EXIT")
alertcondition(sellExitSignal, "SELL EXIT", "Contra Supertrend SELL EXIT")