本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
strategy("BANKNIFTY Spot 0.09% Strategy | 3 Targets", overlay=true, pyramiding=0, process_orders_on_close=false)
// Settings
entryPct = input.float(0.09, "Entry %")
t1Pct = input.float(0.26, "Target 1 %")
t2Pct = input.float(0.45, "Target 2 %")
t3Pct = input.float(0.90, "Target 3 %")
buffer = input.float(1.0, "Entry Buffer (Points)")
validBars = input.int(3, "Entry Validity (Candles)", minval=1)
// Daily variables
var float firstClose = na
var float entryPrice = na
var float stopPrice = na
var int signalBar = na
var int direction = 0
var bool traded = false
newDay = ta.change(time("D")) != 0
minutes = hour(time, "Asia/Kolkata") * 60 + minute(time, "Asia/Kolkata")
openingBar = minutes == 555
isFiveMin = timeframe.isminutes and timeframe.multiplier == 5
if newDay
strategy.cancel_all()
firstClose := na
entryPrice := na
stopPrice := na
signalBar := na
direction := 0
traded := false
if isFiveMin and openingBar and barstate.isconfirmed
firstClose := close
// All levels from opening candle CLOSE
bullEntry = firstClose * 1.0009
bearEntry = firstClose * 0.9991
bullT1 = firstClose * 1.0026
bullT2 = firstClose * 1.0045
bullT3 = firstClose * 1.0090
bearT1 = firstClose * 0.9974
bearT2 = firstClose * 0.9955
bearT3 = firstClose * 0.9910
// Detect qualifying signal candle
canSignal = isFiveMin and not openingBar and not na(firstClose) and not traded and direction == 0 and strategy.position_size == 0 and minutes < 900
if canSignal and barstate.isconfirmed
if close > bullEntry
direction := 1
signalBar := bar_index
entryPrice := high + buffer
stopPrice := low
else if close < bearEntry
direction := -1
signalBar := bar_index
entryPrice := low - buffer
stopPrice := high
// Entry orders
pending = direction != 0 and not traded and not na(signalBar)
elapsed = pending ? bar_index - signalBar : 0
if pending and elapsed <= validBars and minutes < 900
if direction == 1
strategy.entry("BUY", strategy.long, stop=entryPrice)
else
strategy.entry("SELL", strategy.short, stop=entryPrice)
// Cancel expired signal
if pending and (elapsed > validBars or minutes >= 900)
strategy.cancel("BUY")
strategy.cancel("SELL")
direction := 0
// Profit targets and stop loss
if strategy.position_size > 0
traded := true
strategy.exit("BUY T1", "BUY", qty_percent=40, limit=bullT1, stop=stopPrice)
strategy.exit("BUY T2", "BUY", qty_percent=30, limit=bullT2, stop=stopPrice)
strategy.exit("BUY T3", "BUY", qty_percent=30, limit=bullT3, stop=stopPrice)
if strategy.position_size < 0
traded := true
strategy.exit("SELL T1", "SELL", qty_percent=40, limit=bearT1, stop=stopPrice)
strategy.exit("SELL T2", "SELL", qty_percent=30, limit=bearT2, stop=stopPrice)
strategy.exit("SELL T3", "SELL", qty_percent=30, limit=bearT3, stop=stopPrice)
// Close at 3:15 PM
if minutes >= 915
strategy.cancel_all()
strategy.close_all(comment="3:15 PM Exit")
direction := 0
// Plot reference levels
plot(firstClose, "Opening Close", color=color.gray, style=plot.style_linebr)
plot(bullEntry, "+0.09%", color=color.lime, linewidth=2, style=plot.style_linebr)
plot(bearEntry, "-0.09%", color=color.red, linewidth=2, style=plot.style_linebr)
plot(bullT1, "+0.26%", color=color.aqua, style=plot.style_linebr)
plot(bullT2, "+0.45%", color=color.blue, style=plot.style_linebr)
plot(bullT3, "+0.90%", color=color.purple, style=plot.style_linebr)
plot(bearT1, "-0.26%", color=color.aqua, style=plot.style_linebr)
plot(bearT2, "-0.45%", color=color.blue, style=plot.style_linebr)
plot(bearT3, "-0.90%", color=color.purple, style=plot.style_linebr)