本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
strategy("NAS100 3-Min Breakout + Reverse After SL", overlay=true, initial_capital=1000000, default_qty_type=strategy.fixed, default_qty_value=1, process_orders_on_close=true, pyramiding=0, max_labels_count=500)
//==================================================
// SETTINGS
//==================================================
string TZ = "America/New_York"
string rangeSession = input.session("0400-0700", "Lowest Volume Window")
int swingLength = input.int(3, "Swing Pivot Length", minval=1)
float riskReward = input.float(2.0, "Risk/Reward", minval=0.1, step=0.1)
color lowVolColor = input.color(color.yellow, "Lowest Volume Candle")
color decisionColor = input.color(color.aqua, "09:00–09:15 Window")
color highColor = input.color(color.lime, "Range High")
color lowColor = input.color(color.red, "Range Low")
//==================================================
// TIMEFRAME CHECK
//==================================================
if barstate.isfirst and (not timeframe.isminutes or timeframe.multiplier != 3)
runtime.error("Please set the chart timeframe to 3 minutes.")
//==================================================
// NEW YORK DATE
//==================================================
int localDate = year(time, TZ) * 10000 + month(time, TZ) * 100 + dayofmonth(time, TZ)
bool newDay = na(localDate[1]) or localDate != localDate[1]
bool inRange = not na(time(timeframe.period, rangeSession, TZ))
bool inDecisionWindow = hour(time, TZ) == 9 and minute(time, TZ) >= 0 and minute(time, TZ) < 15
bool decisionBar = hour(time_close, TZ) == 9 and minute(time_close, TZ) == 15
//==================================================
// DAILY STATE
//==================================================
var float lowestVolume = na
var float rangeHigh = na
var float rangeLow = na
var int lowestVolumeBar = na
var float previousSwingHigh = na
var float previousSwingLow = na
var int tradesToday = 0
var bool dayDone = false
var bool reversePending = false
var bool firstTradeWasLong = false
var int lastClosedTrades = 0
if newDay
lowestVolume := na
rangeHigh := na
rangeLow := na
lowestVolumeBar := na
previousSwingHigh := na
previousSwingLow := na
tradesToday := 0
dayDone := false
reversePending := false
firstTradeWasLong := false
lastClosedTrades := strategy.closedtrades
//==================================================
// LOWEST-VOLUME CANDLE: 04:00–07:00
//==================================================
if inRange and barstate.isconfirmed and not na(volume)
if na(lowestVolume) or volume < lowestVolume
lowestVolume := volume
rangeHigh := high
rangeLow := low
lowestVolumeBar := bar_index
//==================================================
// CONFIRMED SWINGS
//==================================================
float pivotHigh = ta.pivothigh(high, swingLength, swingLength)
float pivotLow = ta.pivotlow(low, swingLength, swingLength)
if not na(pivotHigh)
previousSwingHigh := pivotHigh
if not na(pivotLow)
previousSwingLow := pivotLow
//==================================================
// CHART DISPLAY
//==================================================
color candleColor = bar_index == lowestVolumeBar ? lowVolColor : inDecisionWindow ? decisionColor : na
barcolor(candleColor, title="Session Highlights")
plotshape(bar_index == lowestVolumeBar, title="Lowest Volume Candle", style=shape.diamond, location=location.abovebar, color=color.yellow, size=size.small)
plot(rangeHigh, title="Range High", color=highColor, linewidth=2, style=plot.style_linebr)
plot(rangeLow, title="Range Low", color=lowColor, linewidth=2, style=plot.style_linebr)
//==================================================
// DETECT CLOSED TRADE AND CHECK EXIT REASON
//==================================================
if strategy.closedtrades > lastClosedTrades
int closedIndex = strategy.closedtrades - 1
string exitReason = strategy.closedtrades.exit_comment(closedIndex)
if tradesToday == 1
if exitReason == "SL"
reversePending := true
else
dayDone := true
else
dayDone := true
lastClosedTrades := strategy.closedtrades
//==================================================
// FIRST TRADE: 09:15 BREAKOUT
//==================================================
bool canStart = decisionBar and barstate.isconfirmed and not dayDone and not reversePending and tradesToday == 0 and strategy.position_size == 0 and not na(rangeHigh) and not na(rangeLow)
bool buySignal = canStart and close > rangeHigh and not na(previousSwingLow) and previousSwingLow < close
bool sellSignal = canStart and close < rangeLow and not na(previousSwingHigh) and previousSwingHigh > close
if buySignal
float entryPrice = close
float stopLoss = previousSwingLow
float risk = entryPrice - stopLoss
float takeProfit = entryPrice + risk * riskReward
if risk > 0
firstTradeWasLong := true
strategy.entry("BUY", strategy.long)
strategy.exit("BUY Exit", from_entry="BUY", stop=stopLoss, limit=takeProfit, comment_loss="SL", comment_profit="TP")
tradesToday := 1
if sellSignal
float entryPrice = close
float stopLoss = previousSwingHigh
float risk = stopLoss - entryPrice
float takeProfit = entryPrice - risk * riskReward
if risk > 0
firstTradeWasLong := false
strategy.entry("SELL", strategy.short)
strategy.exit("SELL Exit", from_entry="SELL", stop=stopLoss, limit=takeProfit, comment_loss="SL", comment_profit="TP")
tradesToday := 1
//==================================================
// REVERSE TRADE AFTER FIRST TRADE HITS STOP-LOSS
//==================================================
bool canReverse = reversePending and not dayDone and tradesToday == 1 and strategy.position_size == 0 and barstate.isconfirmed
// First trade was BUY and stopped out: reverse to SELL using swing-high SL.
if canReverse and firstTradeWasLong and not na(previousSwingHigh)
float entryPrice = close
float stopLoss = previousSwingHigh
float risk = stopLoss - entryPrice
float takeProfit = entryPrice - risk * riskReward
if risk > 0
strategy.entry("Reverse SELL", strategy.short)
strategy.exit("Reverse SELL Exit", from_entry="Reverse SELL", stop=stopLoss, limit=takeProfit, comment_loss="SL", comment_profit="TP")
tradesToday := 2
reversePending := false
// First trade was SELL and stopped out: reverse to BUY using swing-low SL.
if canReverse and not firstTradeWasLong and not na(previousSwingLow)
float entryPrice = close
float stopLoss = previousSwingLow
float risk = entryPrice - stopLoss
float takeProfit = entryPrice + risk * riskReward
if risk > 0
strategy.entry("Reverse BUY", strategy.long)
strategy.exit("Reverse BUY Exit", from_entry="Reverse BUY", stop=stopLoss, limit=takeProfit, comment_loss="SL", comment_profit="TP")
tradesToday := 2
reversePending := false
//==================================================
// DAILY STOP: NO MORE TRADES AFTER SECOND TRADE
//==================================================
if tradesToday >= 2 and strategy.position_size == 0
dayDone := true