本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// ═══════════════════════════════════════════════════════════════════════════
// KISS ORB Setup · 15-minute Opening Range Breakout
//
// Rules
// 1. Opening range = HIGH and LOW of the first two 15m candles
// (9:30–10:00 ET). Nothing is traded before 10:00 ET.
// 2. A 15m candle CLOSES above the range → BUY.
// A 15m candle CLOSES below the range → SELL.
// A wick through the range that closes back inside → no trade.
// 3. Stop-loss at the opposite end of the range.
// 4. Take-profit 10 points.
//
// Works on the 15m chart or any lower timeframe (1m, 3m, 5m...).
// On lower timeframes the range is built from the chart bars inside the
// range session, and a breakout only counts on the close of a 15m candle.
// Everything is evaluated on confirmed bars, so signals do not repaint.
// ═══════════════════════════════════════════════════════════════════════════
indicator("KISS ORB Setup", shorttitle = "KISS ORB", overlay = true, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500)
// ─── Inputs ────────────────────────────────────────────────────────────────
const string G_S = "Session"
tz = input.string("America/New_York", "Timezone", group = G_S)
rangeSess = input.session("0930-1000", "Opening range", group = G_S, tooltip = "The first two 15m candles. The range locks when this session ends (10:00 ET).")
entrySess = input.session("1000-1600", "Entry window", group = G_S, tooltip = "New trades are only taken inside this window.")
confTf = input.timeframe("15", "Breakout candle timeframe", group = G_S, tooltip = "A candle of this timeframe must CLOSE outside the range. The chart timeframe must be this or lower.")
const string G_T = "Trade"
tpPts = input.float(10.0, "Take-profit (points)", minval = 0.25, step = 0.25, group = G_T)
slBufTk = input.int(0, "Stop buffer beyond range (ticks)", minval = 0, group = G_T)
maxTrades = input.int(1, "Max trades per day", minval = 1, group = G_T)
maxRisk = input.float(0.0, "Skip trade if stop is wider than (points)", minval = 0.0, step = 0.25, group = G_T, tooltip = "0 = off (take every breakout, as in the original rules). Useful on NQ/MNQ where the 30-min range is often much wider than the 10-pt target.")
flatAtEnd = input.bool(true, "Close open trade when entry window ends", group = G_T)
const string G_V = "Visuals"
rangeCol = input.color(#2962FF, "Range", group = G_V)
longCol = input.color(#089981, "Long / target", group = G_V)
shortCol = input.color(#F23645, "Short / stop", group = G_V)
showWicks = input.bool(true, "Mark wick-throughs (no close = no trade)", group = G_V)
showTable = input.bool(true, "Show stats table", group = G_V)
tablePos = input.string("Top right", "Table position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = G_V)
const string G_A = "Alerts"
jsonAlerts = input.bool(true, "Webhook JSON via alert()", group = G_A, tooltip = "Create the alert with 'Any alert() function call' to get entry and exit messages as JSON. The alertcondition() alerts below work as plain-text alerts.")
// ─── Timeframe handling ────────────────────────────────────────────────────
chartSec = timeframe.in_seconds()
confSec = timeframe.in_seconds(confTf)
if chartSec > confSec
runtime.error("KISS ORB: switch the chart to " + confTf + " minutes or lower.")
lowerTf = chartSec < confSec
tfChange = timeframe.change(confTf)
confCloseT = time_close(confTf)
newBucket = lowerTf ? tfChange : true
// Last chart bar of a 15m candle → its close IS the 15m close.
isConfClose = lowerTf ? time_close >= confCloseT : true
// ─── Sessions ──────────────────────────────────────────────────────────────
inRange = not na(time(timeframe.period, rangeSess, tz))
inWindow = not na(time(timeframe.period, entrySess, tz))
rangeStart = inRange and not inRange[1]
rangeEnd = not inRange and inRange[1]
windowEnd = not inWindow and inWindow[1]
// ─── 15m candle high/low (for wick-through detection on lower TFs) ─────────
var float bHi = na
var float bLo = na
if newBucket or na(bHi)
bHi := high
bLo := low
else
bHi := math.max(bHi, high)
bLo := math.min(bLo, low)
// ─── State ─────────────────────────────────────────────────────────────────
// Opening range
var float rHi = na
var float rLo = na
var int rStartBar = na
var bool rLocked = false
var box rBox = na
var line hiLn = na
var line loLn = na
// Day
var int tradesToday = 0
var bool dayDone = false
// Open trade (dir: 1 long, -1 short, 0 flat)
var int dir = 0
var float ePx = na
var float sPx = na
var float tPx = na
var int eBar = na
var line eLn = na
var box tpBx = na
var box slBx = na
// Stats
var int nTrades = 0
var int nWins = 0
var int nTP = 0
var int nSL = 0
var int nTime = 0
var float netPts = 0.0
// Per-bar events
bool longSig = false
bool shortSig = false
bool tpHit = false
bool slHit = false
bool timeExit = false
float exitPx = na
float exitPts = na
string exitWhy = ""
// ─── Engine ────────────────────────────────────────────────────────────────
if barstate.isconfirmed
// 1. Build the opening range (9:30–10:00 ET)
if rangeStart
rHi := high
rLo := low
rStartBar := bar_index
rLocked := false
tradesToday := 0
dayDone := false
rBox := box.new(bar_index, high, bar_index, low, border_color = color.new(rangeCol, 70), bgcolor = color.new(rangeCol, 90))
else if inRange and not na(rHi)
rHi := math.max(rHi, high)
rLo := math.min(rLo, low)
box.set_top(rBox, rHi)
box.set_bottom(rBox, rLo)
box.set_right(rBox, bar_index)
// 2. Lock the range at 10:00 ET and draw the high / low
if rangeEnd and not na(rHi)
rLocked := true
hiLn := line.new(rStartBar, rHi, bar_index, rHi, color = rangeCol, width = 2)
loLn := line.new(rStartBar, rLo, bar_index, rLo, color = rangeCol, width = 2)
if rLocked and inWindow
line.set_x2(hiLn, bar_index)
line.set_x2(loLn, bar_index)
// 3. Manage an open trade — sit back and let it play out
if dir != 0 and bar_index > eBar
line.set_x2(eLn, bar_index)
box.set_right(tpBx, bar_index)
box.set_right(slBx, bar_index)
if flatAtEnd and windowEnd
exitPx := open
exitWhy := "TIME"
else
bool hitSL = dir == 1 ? low <= sPx : high >= sPx
bool hitTP = dir == 1 ? high >= tPx : low <= tPx
// If stop and target are both touched in one bar, assume the stop came first (conservative).
if hitSL
exitPx := dir == 1 ? math.min(open, sPx) : math.max(open, sPx)
exitWhy := "SL"
else if hitTP
exitPx := dir == 1 ? math.max(open, tPx) : math.min(open, tPx)
exitWhy := "TP"
if not na(exitPx)
exitPts := (exitPx - ePx) * dir
nTrades += 1
netPts += exitPts
if exitPts > 0
nWins += 1
if exitWhy == "TP"
nTP += 1
tpHit := true
else if exitWhy == "SL"
nSL += 1
slHit := true
else
nTime += 1
timeExit := true
color xc = exitPts >= 0 ? longCol : shortCol
label.new(bar_index, exitPx, exitWhy + " " + (exitPts >= 0 ? "+" : "") + str.tostring(exitPts, "#,##0.00"), style = label.style_label_left, color = xc, textcolor = color.white, size = size.small)
dir := 0
// 4. After 10:00 ET: wait for a 15m CLOSE outside the range
if rLocked and inWindow and isConfClose and dir == 0 and not dayDone and tradesToday < maxTrades
bool upBreak = close > rHi
bool dnBreak = close < rLo
if upBreak or dnBreak
int d = upBreak ? 1 : -1
float stop = d == 1 ? rLo - slBufTk * syminfo.mintick : rHi + slBufTk * syminfo.mintick
float risk = math.abs(close - stop)
if maxRisk > 0 and risk > maxRisk
dayDone := true
label.new(bar_index, d == 1 ? high : low, "SKIP\nstop " + str.tostring(risk, "#,##0.00") + " pts", style = d == 1 ? label.style_label_down : label.style_label_up, color = color.new(color.gray, 30), textcolor = color.white, size = size.small)
else
dir := d
ePx := close
sPx := stop
tPx := close + d * tpPts
eBar := bar_index
tradesToday += 1
longSig := d == 1
shortSig := d == -1
color c = d == 1 ? longCol : shortCol
label.new(bar_index, d == 1 ? low : high, (d == 1 ? "BUY " : "SELL ") + str.tostring(ePx, format.mintick), style = d == 1 ? label.style_label_up : label.style_label_down, color = c, textcolor = color.white, size = size.small)
eLn := line.new(bar_index, ePx, bar_index, ePx, color = color.new(color.white, 30), style = line.style_dashed)
tpBx := box.new(bar_index, math.max(ePx, tPx), bar_index, math.min(ePx, tPx), border_color = color.new(longCol, 50), bgcolor = color.new(longCol, 85))
slBx := box.new(bar_index, math.max(ePx, sPx), bar_index, math.min(ePx, sPx), border_color = color.new(shortCol, 50), bgcolor = color.new(shortCol, 85))
else if showWicks
// Wick went through, but the 15m candle closed back inside → do nothing.
if bHi > rHi
label.new(bar_index, bHi, "×", style = label.style_none, textcolor = color.gray, size = size.normal, tooltip = "Wicked above the range but closed inside — no trade")
if bLo < rLo
label.new(bar_index, bLo, "×", style = label.style_none, textcolor = color.gray, size = size.normal, tooltip = "Wicked below the range but closed inside — no trade")
// ─── Alerts ────────────────────────────────────────────────────────────────
alertcondition(longSig, "KISS ORB Buy", "KISS ORB BUY {{ticker}} @ {{close}}")
alertcondition(shortSig, "KISS ORB Sell", "KISS ORB SELL {{ticker}} @ {{close}}")
alertcondition(tpHit, "KISS ORB Target hit", "KISS ORB target hit on {{ticker}}")
alertcondition(slHit, "KISS ORB Stop hit", "KISS ORB stop hit on {{ticker}}")
fmtNum(float x) => str.tostring(math.round_to_mintick(x))
if jsonAlerts
if longSig or shortSig
alert('{"strategy":"KISS ORB","ticker":"' + syminfo.ticker + '","action":"' + (longSig ? "buy" : "sell") + '","entry":' + fmtNum(ePx) + ',"stop":' + fmtNum(sPx) + ',"target":' + fmtNum(tPx) + '}', alert.freq_once_per_bar_close)
if not na(exitPx)
alert('{"strategy":"KISS ORB","ticker":"' + syminfo.ticker + '","action":"exit","reason":"' + exitWhy + '","price":' + fmtNum(exitPx) + ',"points":' + str.tostring(exitPts, "0.00") + '}', alert.freq_once_per_bar_close)
// ─── Stats table ───────────────────────────────────────────────────────────
tPos = switch tablePos
"Top left" => position.top_left
"Bottom right" => position.bottom_right
"Bottom left" => position.bottom_left
=> position.top_right
var table tb = table.new(tPos, 2, 8, bgcolor = color.new(#131722, 5), frame_color = color.new(color.gray, 60), frame_width = 1, border_color = color.new(color.gray, 85), border_width = 1)
row(int r, string k, string v, color vc) =>
table.cell(tb, 0, r, k, text_color = color.gray, text_size = size.small, text_halign = text.align_left)
table.cell(tb, 1, r, v, text_color = vc, text_size = size.small, text_halign = text.align_right)
if showTable and barstate.islast
string status = dir == 1 ? "LONG open" : dir == -1 ? "SHORT open" : inRange ? "Building range" : rLocked and inWindow ? (dayDone or tradesToday >= maxTrades ? "Done for today" : "Watching 15m closes") : "Waiting for next range"
color stCol = dir == 1 ? longCol : dir == -1 ? shortCol : color.white
float usd = netPts * syminfo.pointvalue
row(0, "KISS ORB", status, stCol)
row(1, "Range H / L", na(rHi) ? "—" : str.tostring(rHi, format.mintick) + " / " + str.tostring(rLo, format.mintick), rangeCol)
row(2, "Range size", na(rHi) ? "—" : str.tostring(rHi - rLo, "#,##0.00") + " pts", color.white)
row(3, "Trades", str.tostring(nTrades), color.white)
row(4, "TP / SL / Time", str.tostring(nTP) + " / " + str.tostring(nSL) + " / " + str.tostring(nTime), color.white)
row(5, "Win rate", nTrades > 0 ? str.tostring(nWins * 100.0 / nTrades, "0.0") + "%" : "—", color.white)
row(6, "Net points", (netPts >= 0 ? "+" : "") + str.tostring(netPts, "#,##0.00"), netPts >= 0 ? longCol : shortCol)
row(7, "Net $ / contract", (usd < 0 ? "-$" : "$") + str.tostring(math.abs(usd), "#,##0"), usd >= 0 ? longCol : shortCol)