本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
strategy("Pivot reversal strategy", overlay=true)
leftBars = input(4, "Pivot Lookback Left")
rightBars = input(2, "Pivot Lookback Right")
tradeWindow = not na(time(timeframe.period, "0915-1130:1234567", "America/New_York"))
slPoints = 50.0
// ONE ACTION PER CANDLE
var int lastActionBar = na
// Detect executions that occurred since the previous calculation
positionChanged = strategy.position_size != nz(strategy.position_size[1], 0)
tradeClosed = strategy.closedtrades > nz(strategy.closedtrades[1], 0)
if positionChanged or tradeClosed
lastActionBar := bar_index
canAct = na(lastActionBar) or bar_index > lastActionBar
// DAILY RULES
dailyProfit = 0.0
if strategy.closedtrades > 0
for i = 0 to strategy.closedtrades - 1
exitTime = strategy.closedtrades.exit_time(i)
sameDay = year(exitTime, "America/New_York") == year(time, "America/New_York") and month(exitTime, "America/New_York") == month(time, "America/New_York") and dayofmonth(exitTime, "America/New_York") == dayofmonth(time, "America/New_York")
if sameDay
dailyProfit += strategy.closedtrades.profit(i) - strategy.closedtrades.commission(i)
// Daily profit and loss limits
dailyLimitReached = dailyProfit <= -300 or dailyProfit >= 500
canTrade = tradeWindow and not dailyLimitReached
// Original pivot calculations
swh = ta.pivothigh(leftBars, rightBars)
swl = ta.pivotlow(leftBars, rightBars)
swh_cond = not na(swh)
hprice = 0.0
hprice := swh_cond ? swh : hprice[1]
le = swh_cond
if le and canTrade and canAct
strategy.entry("Buy", strategy.long, comment="Buy", stop=hprice + syminfo.mintick)
lastActionBar := bar_index
canAct := false
swl_cond = not na(swl)
lprice = 0.0
lprice := swl_cond ? swl : lprice[1]
se = swl_cond
if se and canTrade and canAct
strategy.entry("Sell", strategy.short, comment="Sell", stop=lprice - syminfo.mintick)
lastActionBar := bar_index
canAct := false
// Cancel pending entries outside trading hours or after daily limit
if not canTrade
strategy.cancel("Buy")
strategy.cancel("Sell")
// 50-point stop losses
longSL = strategy.position_avg_price - slPoints
shortSL = strategy.position_avg_price + slPoints
if strategy.position_size > 0
strategy.exit("Buy SL", from_entry="Buy", stop=longSL)
if strategy.position_size < 0
strategy.exit("Sell SL", from_entry="Sell", stop=shortSL)
// Stop loss plot
plot(
strategy.position_size > 0 ? longSL : strategy.position_size < 0 ? shortSL : na,
title="STOP LOSS - 50 POINTS",
color=color.red,
linewidth=3,
style=plot.style_stepline
)
// Entry price plot
plot(
strategy.position_size != 0 ? strategy.position_avg_price : na,
title="ENTRY PRICE",
color=color.yellow,
linewidth=2,
style=plot.style_stepline
)
// Close any remaining position at 11:30 AM
sessionJustEnded = not tradeWindow and tradeWindow[1]
if sessionJustEnded and strategy.position_size != 0
strategy.close_all(comment="End of Session Exit", immediately=true)