Opening ATM Breakout Strategy | NIFTY & Bank NIFTY

alamcet · study · 254 行 · 点赞 32 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Opening ATM Breakout Strategy | NIFTY & Bank NIFTY", overlay=true, max_labels_count=100)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// INPUTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
string G1 = "Index & Buffer"
string G2 = "Display Settings"
 
indexMode = input.string("NIFTY", "Select Index",
     options=["NIFTY", "BANK NIFTY"], group=G1)
 
niftyBuffer = input.int(10, "NIFTY Buffer (5–15)",
     minval=5, maxval=15, group=G1)
 
bankBuffer = input.int(15, "Bank NIFTY Buffer (10–20)",
     minval=10, maxval=20, group=G1)
 
showLabels = input.bool(true, "Show Level Labels", group=G2)
showSignals = input.bool(true, "Show Entry Signals", group=G2)
showBuffer = input.bool(true, "Show Buffer Line", group=G2)
showSL = input.bool(true, "Show ATM Stop-Loss Line", group=G2)
 
bool isNifty = indexMode == "NIFTY"
float strikeInterval = isNifty ? 50.0 : 100.0
float bandDistance = isNifty ? 54.0 : 108.0
float bufferPoints = isNifty ? niftyBuffer : bankBuffer
 
bool isFiveMinute = timeframe.isminutes and timeframe.multiplier == 5
bool newDay = timeframe.change("D")
 
labelSizeInput = input.string("Normal", "Level Label Text Size",
     options=["Tiny", "Small", "Normal", "Large", "Huge"],
     group=G2)
 
labelSize = switch labelSizeInput
    "Tiny"   => size.tiny
    "Small"  => size.small
    "Normal" => size.normal
    "Large"  => size.large
    "Huge"   => size.huge
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// DAILY STATE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var float atm = na
var float upperBand = na
var float lowerBand = na
var float bufferLevel = na
 
var int setupBar = na
var int breakoutBar = na
var int breakoutDirection = 0
 
var bool entryTriggered = false
var bool bufferActive = false
 
if newDay
    atm := na
    upperBand := na
    lowerBand := na
    bufferLevel := na
    setupBar := na
    breakoutBar := na
    breakoutDirection := 0
    entryTriggered := false
    bufferActive := false
 
// Establish levels from the first completed 5-minute candle.
if isFiveMinute and barstate.isconfirmed and na(atm)
    atm := math.round(close / strikeInterval) * strikeInterval
    upperBand := atm + bandDistance
    lowerBand := atm - bandDistance
    setupBar := bar_index
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// VALID BREAKOUT DETECTION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
bool ready = isFiveMinute and not na(atm) and bar_index > setupBar
bool noSetup = breakoutDirection == 0
 
// The breakout candle must cross the band and stay clear of ATM.
 
bool bullishBreakout = ready and noSetup and
     high >= upperBand and low > atm
 
bool bearishBreakout = ready and noSetup and
     low <= lowerBand and high < atm
 
 
if barstate.isconfirmed and bullishBreakout
    breakoutDirection := 1
    breakoutBar := bar_index
    bufferLevel := high + bufferPoints
    bufferActive := true
    entryTriggered := false
 
if barstate.isconfirmed and bearishBreakout
    breakoutDirection := -1
    breakoutBar := bar_index
    bufferLevel := low - bufferPoints
    bufferActive := true
    entryTriggered := false
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BUFFER ENTRY & ATM RE-ARMING
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
// Only candles after the breakout candle can trigger an entry.
bool afterBreakout = not na(breakoutBar) and bar_index > breakoutBar
 
bool referenceTouched = isFiveMinute and barstate.isconfirmed and
     bufferActive and afterBreakout and low <= atm and high >= atm
 
// Do not issue an entry if the same candle also touches ATM.
bool validEntryBar = isFiveMinute and barstate.isconfirmed and
     bufferActive and afterBreakout and not referenceTouched
 
bool bullishEntry = validEntryBar and breakoutDirection == 1 and
     low <= bufferLevel and high >= bufferLevel
 
bool bearishEntry = validEntryBar and breakoutDirection == -1 and
     low <= bufferLevel and high >= bufferLevel
 
if bullishEntry or bearishEntry
    entryTriggered := true
 
// ATM touch ends the current setup.
// The next candle can establish a new valid breakout.
if referenceTouched
    bufferActive := false
    bufferLevel := na
    breakoutDirection := 0
    breakoutBar := na
    entryTriggered := false
 
 
 //━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// LEVEL PLOTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
plot(atm, "ATM Reference", color=color.black,
     linewidth=3, style=plot.style_stepline)
 
plot(upperBand, "Upper Band", color=color.lime,
     linewidth=3, style=plot.style_stepline)
 
plot(lowerBand, "Lower Band", color=color.red,
     linewidth=3, style=plot.style_stepline)
 
plot(showBuffer and bufferActive ? bufferLevel : na,
     "Active Buffer Line",
     color=breakoutDirection == 1 ? color.aqua : color.orange,
     linewidth=3, style=plot.style_stepline)
 
plot(showSL ? atm : na, "ATM Stop Loss",
     color=color.black, linewidth=3,
     style=plot.style_linebr)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// BREAKOUT & ENTRY SIGNALS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
plotshape(showSignals and bullishBreakout and barstate.isconfirmed,
     title="Bullish Breakout", style=shape.diamond,
     location=location.belowbar, color=color.lime,
     size=size.tiny, text="BO")
 
plotshape(showSignals and bearishBreakout and barstate.isconfirmed,
     title="Bearish Breakout", style=shape.diamond,
     location=location.abovebar, color=color.red,
     size=size.tiny, text="BO")
 
plotshape(showSignals and bullishEntry,
     title="Bullish Entry", style=shape.labelup,
     location=location.belowbar, color=color.green,
     textcolor=color.white, size=size.small, text="BUY")
 
plotshape(showSignals and bearishEntry,
     title="Bearish Entry", style=shape.labeldown,
     location=location.abovebar, color=color.maroon,
     textcolor=color.white, size=size.small, text="SELL")
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// RIGHT-SIDE LABELS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var label atmLabel = na
var label upperLabel = na
var label lowerLabel = na
var label bufferLabel = na
 
if barstate.islast and showLabels and not na(atm)
    if na(atmLabel)
        atmLabel := label.new(bar_index, atm, "",
             style=label.style_label_left,
             color=color.gray, textcolor=color.white)
 
        upperLabel := label.new(bar_index, upperBand, "",
             style=label.style_label_left,
             color=color.green, textcolor=color.white)
 
        lowerLabel := label.new(bar_index, lowerBand, "",
             style=label.style_label_left,
             color=color.red, textcolor=color.white)
 
        bufferLabel := label.new(bar_index, atm, "",
             style=label.style_label_left,
             color=color.blue, textcolor=color.white)
 
    label.set_xy(atmLabel, bar_index, atm)
    label.set_text(atmLabel, "ATM / SL: " + str.tostring(atm, format.mintick))
 
    label.set_xy(upperLabel, bar_index, upperBand)
    label.set_text(upperLabel, "Upper: " + str.tostring(upperBand, format.mintick))
 
    label.set_xy(lowerLabel, bar_index, lowerBand)
    label.set_text(lowerLabel, "Lower: " + str.tostring(lowerBand, format.mintick))
 
    if showBuffer and bufferActive
        label.set_xy(bufferLabel, bar_index, bufferLevel)
        label.set_text(bufferLabel, "Buffer: " + str.tostring(bufferLevel, format.mintick))
        label.set_color(bufferLabel,
             breakoutDirection == 1 ? color.teal : color.orange)
    else
        label.set_text(bufferLabel, "")
 
label.set_size(atmLabel, labelSize)
label.set_size(upperLabel, labelSize)
label.set_size(lowerLabel, labelSize)
label.set_size(bufferLabel, labelSize)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ALERTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
alertcondition(bullishBreakout and barstate.isconfirmed,
     title="Bullish Breakout Detected",
     message="Bullish breakout detected on {{ticker}}.")
 
alertcondition(bearishBreakout and barstate.isconfirmed,
     title="Bearish Breakout Detected",
     message="Bearish breakout detected on {{ticker}}.")
 
alertcondition(bullishEntry,
     title="Bullish Entry Signal",
     message="Bullish buffer-line entry on {{ticker}}.")
 
alertcondition(bearishEntry,
     title="Bearish Entry Signal",
     message="Bearish buffer-line entry on {{ticker}}.")

← 返回列表